NIFI-5881: Enable to export the template for non-ascii name

This closes #3210.

Signed-off-by: Koji Kawamura <ijokarumawak@apache.org>
This commit is contained in:
Kemix Koo 2018-12-07 16:31:36 +08:00 committed by Koji Kawamura
parent ecbed6a782
commit e76c54234d
1 changed files with 14 additions and 2 deletions

View File

@ -43,9 +43,13 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.util.Set; import java.util.Set;
/** /**
@ -142,14 +146,22 @@ public class TemplateResource extends ApplicationResource {
attachmentName = attachmentName.replaceAll("\\s", "_"); attachmentName = attachmentName.replaceAll("\\s", "_");
} }
final Charset utf8 = StandardCharsets.UTF_8;
try {
attachmentName = URLEncoder.encode(attachmentName, utf8.name());
} catch (UnsupportedEncodingException e) {
//
}
// generate the response // generate the response
/* /*
* Here instead of relying on default JAXB marshalling we are simply * Here instead of relying on default JAXB marshalling we are simply
* serializing template to String (formatted, indented etc) and sending * serializing template to String (formatted, indented etc) and sending
* it as part of the response. * it as part of the response.
*/ */
String serializedTemplate = new String(TemplateSerializer.serialize(template), StandardCharsets.UTF_8); String serializedTemplate = new String(TemplateSerializer.serialize(template), utf8);
return generateOkResponse(serializedTemplate).header("Content-Disposition", String.format("attachment; filename=\"%s.xml\"", attachmentName)).build(); String filename = attachmentName + ".xml";
return generateOkResponse(serializedTemplate).encoding(utf8.name()).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename* = " + utf8.name() + "''" + filename).build();
} }
/** /**