You will need to write your own filter to unzip the body of gzipped requests. Sine, you will read the entire input stream from the request, you also need to override the parameter parameterization method. This is the filter that I use in my code. It only supports gzip POST requests, but you can update it to use other types of requests if necessary. Also be careful to analyze the parameters that I use in the guava library, you can capture here: http://central.maven.org/maven2/com/google/guava/guava/
public class GzipBodyDecompressFilter extends Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public final void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; boolean isGzipped = request.getHeader(HttpHeaders.CONTENT_ENCODING) != null && request.getHeader(HttpHeaders.CONTENT_ENCODING).contains("gzip"); boolean requestTypeSupported = HttpMethods.POST.equals(request.getMethod()); if (isGzipped && !requestTypeSupported) { throw new IllegalStateException(request.getMethod() + " is not supports gzipped body of parameters." + " Only POST requests are currently supported."); } if (isGzipped && requestTypeSupported) { request = new GzippedInputStreamWrapper((HttpServletRequest) servletRequest); } chain.doFilter(request, response); } @Override public final void destroy() { } final class GzippedInputStreamWrapper extends HttpServletRequestWrapper { public static final String DEFAULT_ENCODING = "ISO-8859-1"; private byte[] bytes; public GzippedInputStreamWrapper(final HttpServletRequest request) throws IOException { super(request); try { final InputStream in = new GZIPInputStream(request.getInputStream()); bytes = ByteStreams.toByteArray(in); } catch (EOFException e) { bytes = new byte[0]; } } @Override public ServletInputStream getInputStream() throws IOException { final ByteArrayInputStream sourceStream = new ByteArrayInputStream(bytes); return new ServletInputStream() { public int read() throws IOException { return sourceStream.read(); } public void close() throws IOException { super.close(); sourceStream.close(); } }; } @Override public Map getParameterMap() { String contentEncodingHeader = getHeader(HttpHeaders.CONTENT_TYPE); if (!Strings.isNullOrEmpty(contentEncodingHeader) && contentEncodingHeader.contains("application/x-www-form-urlencoded")) { Map params = new HashMap(super.getParameterMap()); try { params.putAll(parseParams(new String(bytes))); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return params; } else { return super.getParameterMap(); } } private Map<String, String[]> parseParams(final String body) throws UnsupportedEncodingException { String characterEncoding = getCharacterEncoding(); if (null == characterEncoding) { characterEncoding = DEFAULT_ENCODING; } final Multimap<String, String> parameters = ArrayListMultimap.create(); for (String pair : body.split("&")) { if (Strings.isNullOrEmpty(pair)) { continue; } int idx = pair.indexOf("="); String key = null; if (idx > 0) { key = URLDecoder.decode(pair.substring(0, idx), characterEncoding); } else { key = pair; } String value = null; if (idx > 0 && pair.length() > idx + 1) { value = URLDecoder.decode(pair.substring(idx + 1), characterEncoding); } else { value = null; } parameters.put(key, value); } return Maps.transformValues(parameters.asMap(), new Function<Collection<String>, String[]>() { @Nullable @Override public String[] apply(final Collection<String> input) { return Iterables.toArray(input, String.class); } }); } }
}
ioanbsu
source share