I\'m trying to ensure that all JSON responses from a Jersey based java application have a UTF-8 character encoding parameter appended to their ContentType header.
So
Had success using a ContainerResponseFilter
:
public class ContentTypeEncodingFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
String contentType = responseContext.getHeaderString(HttpHeaders.CONTENT_TYPE);
if (contentType == null) {
return;
}
ContentType parsedType = ContentType.parse(contentType);
if (parsedType.getCharset() != null) {
return;
}
ContentType encodedType = parsedType.withCharset(StandardCharsets.UTF_8);
responseContext.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, encodedType.toString());
}
}
This is not going to work in this way.
When you call chain.doFilter(request, response);
your headers are already flushed and you can't reset them later on.
What you can do is actually a quick and dirty trick:
public void doFilter(...) {
HttpServletResponse resp = new HttpServletResponseWrapper(response) {
public void setContentType(String ct) {
if(ct!=null && ct.toLowerCase().startsWith("application/json")) {
super.setContentType("application/json;charset=UTF-8");
} else {
super.setContentType(ct);
}
}
}
// Set content type manually to override any potential defaults,
// See if you need it at all
response.setContentType("application/json;charset=UTF-8");
chain.doFilter(request, resp); // Inject our response!
}
EDIT: ct.toUpperCase().startsWith("application/json")
changed to ct.toLowerCase().startsWith("application/json")
.
Thanks to the other answers on this page, I found a way to do it.... Very close to what they were suggesting, but it turned out the only way I could get it to work was to override "getOutputStream" and look at the contentType at that point. I've put this filter as the first filter in the chain, and it seems to work fine.
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.ws.rs.core.MediaType;
public class EnsureJsonIsUtf8ResponseFilter implements Filter
{
final String APPLICATION_JSON_WITH_UTF8_CHARSET = MediaType.APPLICATION_JSON + ";charset=" + java.nio.charset.StandardCharsets.UTF_8;
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
HttpServletResponse r = (HttpServletResponse) response;
HttpServletResponse wrappedResponse = new HttpServletResponseWrapper(r)
{
@Override
public ServletOutputStream getOutputStream() throws java.io.IOException
{
ServletResponse response = this.getResponse();
String ct = (response != null) ? response.getContentType() : null;
if (ct != null && ct.toLowerCase().startsWith(MediaType.APPLICATION_JSON))
{
response.setContentType(APPLICATION_JSON_WITH_UTF8_CHARSET);
}
return super.getOutputStream();
}
};
chain.doFilter(request, wrappedResponse);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException
{
// This method intentionally left blank
}
@Override
public void destroy()
{
// This method intentionally left blank
}
}
Using this answer as reference the solution to your question is to re-encode the JSON text as shown here:
public void doFilter(...) {
final CharResponseWrapper wrappedResponse =
new CharResponseWrapper((HttpServletResponse) response);
chain.doFilter(request, wrappedResponse);
final String content = wrappedResponse.toString();
final String type = wrappedResponse.getContentType();
if (type != null && type.contains(MediaType.APPLICATION_JSON)) {
// Re-encode the JSON response as UTF-8.
response.setCharacterEncoding("UTF-8");
final OutputStream out = response.getOutputStream();
out.write(content.getBytes("UTF-8"));
out.close();
}
else {
// Otherwise just write it as-is.
final PrintWriter out = response.getWriter();
out.write(content);
out.close();
}
}
This also may be doable using a ClientFilter
, which I've just come across a StackOverflow post for a similar purpose:
https://stackoverflow.com/a/7464585/26510
Not 100% sure I got what you're trying to achieve. Do you want to set the header charset after calling
chain.doFilter(request, response)
?
If that is the case I'm afraid you cannot because very likely at that point, after that chain.doFilter(request, response) has returned and the request processed, the content charset has already been sent to the client and therefore you cannot alter it anymore.