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 . 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
Source File: GZipServletFilter.java    From angularjs-springboot-bookstore with MIT License 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 2
Source File: GZipServletFilter.java    From ServiceCutter with 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
Source File: GZipServletFilter.java    From expper with GNU General Public License v3.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 4
Source File: GZipServletFilter.java    From para with 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);
	}
}