当前位置: 首页>>代码示例>>Java>>正文


Java HttpServletResponse.SC_NO_CONTENT属性代码示例

本文整理汇总了Java中javax.servlet.http.HttpServletResponse.SC_NO_CONTENT属性的典型用法代码示例。如果您正苦于以下问题:Java HttpServletResponse.SC_NO_CONTENT属性的具体用法?Java HttpServletResponse.SC_NO_CONTENT怎么用?Java HttpServletResponse.SC_NO_CONTENT使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在javax.servlet.http.HttpServletResponse的用法示例。


在下文中一共展示了HttpServletResponse.SC_NO_CONTENT属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: shouldBodyBeZero

/**
 * Performs a number of checks to ensure response saneness according to the rules of RFC2616:
 * <ol>
 * <li>If the response code is {@link javax.servlet.http.HttpServletResponse#SC_NO_CONTENT} then it is illegal for the body
 * to contain anything. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5
 * <li>If the response code is {@link javax.servlet.http.HttpServletResponse#SC_NOT_MODIFIED} then it is illegal for the body
 * to contain anything. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
 * </ol>
 *
 * @param request        the client HTTP request
 * @param responseStatus the responseStatus
 * @return true if the response should be 0, even if it is isn't.
 */
public static boolean shouldBodyBeZero(HttpServletRequest request, int responseStatus) {

    //Check for NO_CONTENT
    if (responseStatus == HttpServletResponse.SC_NO_CONTENT) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("{} resulted in a {} response. Removing message body in accordance with RFC2616.",
                    request.getRequestURL(), HttpServletResponse.SC_NO_CONTENT);
        }
        return true;
    }

    //Check for NOT_MODIFIED
    if (responseStatus == HttpServletResponse.SC_NOT_MODIFIED) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("{} resulted in a {} response. Removing message body in accordance with RFC2616.",
                    request.getRequestURL(), HttpServletResponse.SC_NOT_MODIFIED);
        }
        return true;
    }
    return false;
}
 
开发者ID:ugouku,项目名称:shoucang,代码行数:34,代码来源:GZipResponseUtil.java

示例2: doFilter

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

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;

    if (!isIncluded(httpRequest) && acceptsGZipEncoding(httpRequest) && !response.isCommitted()) {
        // Client accepts zipped content
        if (log.isTraceEnabled()) {
            log.trace("{} Written with gzip compression", httpRequest.getRequestURL());
        }

        // Create a gzip stream
        final ByteArrayOutputStream compressed = new ByteArrayOutputStream();
        final GZIPOutputStream gzout = new GZIPOutputStream(compressed);

        // Handle the request
        final GZipServletResponseWrapper wrapper = new GZipServletResponseWrapper(httpResponse, gzout);
        wrapper.setDisableFlushBuffer(true);
        chain.doFilter(request, wrapper);
        wrapper.flush();

        gzout.close();

        // double check one more time before writing out
        // repsonse might have been committed due to error
        if (response.isCommitted()) {
            return;
        }

        // return on these special cases when content is empty or unchanged
        switch (wrapper.getStatus()) {
            case HttpServletResponse.SC_NO_CONTENT:
            case HttpServletResponse.SC_RESET_CONTENT:
            case HttpServletResponse.SC_NOT_MODIFIED:
                return;
            default:
        }

        // Saneness checks
        byte[] compressedBytes = compressed.toByteArray();
        boolean shouldGzippedBodyBeZero = GZipResponseUtil.shouldGzippedBodyBeZero(compressedBytes, httpRequest);
        boolean shouldBodyBeZero = GZipResponseUtil.shouldBodyBeZero(httpRequest, wrapper.getStatus());
        if (shouldGzippedBodyBeZero || shouldBodyBeZero) {
            // No reason to add GZIP headers or write body if no content was written or status code specifies no
            // content
            response.setContentLength(0);
            return;
        }

        // Write the zipped body
        GZipResponseUtil.addGzipHeader(httpResponse);

        response.setContentLength(compressedBytes.length);

        response.getOutputStream().write(compressedBytes);

    } else {
        // Client does not accept zipped content - don't bother zipping
        if (log.isTraceEnabled()) {
            log.trace("{} Written without gzip compression because the request does not accept gzip", httpRequest.getRequestURL());
        }
        chain.doFilter(request, response);
    }
}
 
开发者ID:GastonMauroDiaz,项目名称:buenojo,代码行数:65,代码来源:GZipServletFilter.java


注:本文中的javax.servlet.http.HttpServletResponse.SC_NO_CONTENT属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。