Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ContentType or CharacterEncoding in Java Filter ONLY IF ContentType === JSON

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 if it's a JSON response, I would like the response header for the Content-Type to be

Content-Type: application/json;charset=UTF-8

EDIT: I know I can do this on a case by case basis, but I'd like to do it globally, so it affects all content responses that have a content type of "application/json".

If I just try and set the character encoding in my filter regardless of the content type, it works fine. But I only want to set the character encoding if the ContentType is "application/json". I find that the response.getContentType() method always returns null unless I call chain.doFilter first. But if I try and change the Character Encoding after this, it seems to always get overwritten.

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.ws.rs.core.MediaType;

public class EnsureJsonResponseIsUtf8Filter implements Filter
{
    private class SimpleWrapper extends HttpServletResponseWrapper
    {
        public SimpleWrapper(HttpServletResponse response)
        {
            super(response);
        }

        @Override
        public String getCharacterEncoding()
        {
            return "UTF-8";
        }
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {
        chain.doFilter(request, response);

        if (response.getContentType() != null && response.getContentType().contains(MediaType.APPLICATION_JSON))
        {
            response.setCharacterEncoding("UTF-8");
            chain.doFilter(request, new SimpleWrapper((HttpServletResponse) response));
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {
    }

    @Override
    public void destroy()
    {
    }
}

I've seen other similar questions, but none them seem to have this issue. I've tried registering my filter as the first, and last filter with no luck.

like image 415
Brad Parks Avatar asked Apr 15 '14 12:04

Brad Parks


3 Answers

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

like image 57
Brad Parks Avatar answered Oct 18 '22 18:10

Brad Parks


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").

like image 23
boky Avatar answered Oct 18 '22 16:10

boky


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();
    }
}
like image 1
Cebence Avatar answered Oct 18 '22 16:10

Cebence