如何在Spring MVC中解码Gzip压缩请求体

时间:2013-05-19 19:00:50

标签: java spring http rest spring-mvc

我有一个使用

发送数据的客户端
CONTENT-ENCODING deflate

我有这样的代码

@RequestMapping(value = "/connect", method = RequestMethod.POST)
@ResponseBody
public Map onConnect(@RequestBody String body){}

目前'body'打印出乱码的压缩数据。 有没有办法让Spring MVC自动解压缩它?

3 个答案:

答案 0 :(得分:17)

您需要编写自己的过滤器来解压缩gzip压缩请求。 正确的是,您将从请求中读取整个输入流,您还需要覆盖参数parcing方法。 这是我在我的代码中使用的过滤器。仅支持gzip压缩POST请求,但如果需要,您可以更新它以使用其他类型的请求。 还要注意使用guava库解析参数,你可以从这里抓住你的参数:http://central.maven.org/maven2/com/google/guava/guava/

public class GzipBodyDecompressFilter extends Filter {

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

}
/**
 * Analyzes servlet request for possible gzipped body.
 * When Content-Encoding header has "gzip" value and request method is POST we read all the
 * gzipped stream and is it haz any data unzip it. In case when gzip Content-Encoding header
 * specified but body is not actually in gzip format we will throw ZipException.
 *
 * @param servletRequest  servlet request
 * @param servletResponse servlet response
 * @param chain           filter chain
 * @throws IOException      throws when fails
 * @throws ServletException thrown when fails
 */
@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);

}

/**
 * @inheritDoc
 */
@Override
public final void destroy() {
}

/**
 * Wrapper class that detects if the request is gzipped and ungzipps it.
 */
final class GzippedInputStreamWrapper extends HttpServletRequestWrapper {
    /**
     * Default encoding that is used when post parameters are parsed.
     */
    public static final String DEFAULT_ENCODING = "ISO-8859-1";

    /**
     * Serialized bytes array that is a result of unzipping gzipped body.
     */
    private byte[] bytes;

    /**
     * Constructs a request object wrapping the given request.
     * In case if Content-Encoding contains "gzip" we wrap the input stream into byte array
     * to original input stream has nothing in it but hew wrapped input stream always returns
     * reproducible ungzipped input stream.
     *
     * @param request request which input stream will be wrapped.
     * @throws java.io.IOException when input stream reqtieval failed.
     */
    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];
        }
    }


    /**
     * @return reproduceable input stream that is either equal to initial servlet input
     * stream(if it was not zipped) or returns unzipped input stream.
     * @throws IOException if fails.
     */
    @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();
            }
        };
    }

    /**
     * Need to override getParametersMap because we initially read the whole input stream and
     * servlet container won't have access to the input stream data.
     *
     * @return parsed parameters list. Parameters get parsed only when Content-Type
     * "application/x-www-form-urlencoded" is set.
     */
    @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();
        }
    }

    /**
     * parses params from the byte input stream.
     *
     * @param body request body serialized to string.
     * @return parsed parameters map.
     * @throws UnsupportedEncodingException if encoding provided is not supported.
     */
    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);
                    }
                });
    }
}

}

答案 1 :(得分:6)

这应该由服务器处理,而不是应用程序。

据我所知,Tomcat不支持它,尽管你可能会写一个过滤器。

处理此问题的常用方法是将Tomcat(或您正在使用的任何Java容器)放在配置为处理压缩请求主体的Apache服务器后面。

答案 2 :(得分:3)

你在Spring中没有处理它。相反,您使用过滤器,以便数据到达Spring已经放气。

希望这两个链接可以帮助您入门。