Java Code Examples for javax.servlet.http.HttpServletResponse#SC_RESET_CONTENT

The following examples show how to use javax.servlet.http.HttpServletResponse#SC_RESET_CONTENT . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@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);
    }
}
 
Example 2
Source Project: ServiceCutter   File: GZipServletFilter.java    License: Apache License 2.0 4 votes vote down vote up
@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);
    }
}
 
Example 3
@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);
    }
}
 
Example 4
Source Project: para   File: GZipServletFilter.java    License: Apache License 2.0 4 votes vote down vote up
@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);

		// Only write out header Vary as needed
		GZipResponseUtil.addVaryAcceptEncoding(wrapper);

		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);
	}
}