當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。