All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yestech.lib.web.ResponseUtils Maven / Gradle / Ivy

Go to download

A collection of classes that can be used across yestech artifacts/components, but must not be dependant on any yestech component. Most of the code is utility type code. When more than a few classes are found to be in a package or the package start to handle more that a few reposibilities then a new independant component is created and the existing code in yeslib is ported to the new component.

There is a newer version: 1.2.0
Show newest version
/*
 * Copyright LGPL3
 * YES Technology Association
 * http://yestech.org
 *
 * http://www.opensource.org/licenses/lgpl-3.0.html
 */
package org.yestech.lib.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * A collection of response processing utilities
 *
 * @author Artie Copeland
 * @version $Revision: $
 */
public final class ResponseUtils {

    final private static Logger logger = LoggerFactory.getLogger(ResponseUtils.class);
    
    /**
     * Gzipping an empty file or stream always results in a 20 byte output
     * This is in java or elsewhere.
     * 

* On a unix system to reproduce do gzip -n empty_file. -n tells gzip to not * include the file name. The resulting file size is 20 bytes. *

* Therefore 20 bytes can be used indicate that the gzip byte[] will be empty when ungzipped. */ private static final int EMPTY_GZIPPED_CONTENT_SIZE = 20; /** * Utility class. No public constructor. */ private ResponseUtils() { //noop } /** * Checks whether a gzipped body is actually empty and should just be zero. * When the compressedBytes is {@link #EMPTY_GZIPPED_CONTENT_SIZE} it should be zero. * * @param compressedBytes the gzipped response body * @param request the client HTTP request * @return true if the response should be 0, even if it is isn't. */ public static boolean shouldGzippedBodyBeZero(byte[] compressedBytes, HttpServletRequest request) { //Check for 0 length body if (compressedBytes.length == EMPTY_GZIPPED_CONTENT_SIZE) { if (logger.isDebugEnabled()) { logger.debug(request.getRequestURL() + " resulted in an empty response."); } return true; } else { return false; } } /** * Performs a number of checks to ensure response saneness according to the rules of RFC2616: *

    *
  1. 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 *
  2. 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 *
* * @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 (logger.isDebugEnabled()) { logger.debug(request.getRequestURL() + " resulted in a " + HttpServletResponse.SC_NO_CONTENT + " response. Removing message body in accordance with RFC2616."); } return true; } //Check for NOT_MODIFIED if (responseStatus == HttpServletResponse.SC_NOT_MODIFIED) { if (logger.isDebugEnabled()) { logger.debug(request.getRequestURL() + " resulted in a " + HttpServletResponse.SC_NOT_MODIFIED + " response. Removing message body in accordance with RFC2616."); } return true; } return false; } /** * Adds the gzip HTTP header to the response. This is need when a gzipped body is returned so that browsers can properly decompress it. *

* @param response the response which will have a header added to it. I.e this method changes its parameter * @throws ResponseHeadersNotModifiableException Either the response is committed or we were called using the include method * from a {@link javax.servlet.RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)} * method and the set set header is ignored. */ public static void addGzipHeader(final HttpServletResponse response) throws ResponseHeadersNotModifiableException { response.setHeader("Content-Encoding", "gzip"); boolean containsEncoding = response.containsHeader("Content-Encoding"); if (!containsEncoding) { throw new ResponseHeadersNotModifiableException("Failure when attempting to set " + "Content-Encoding: gzip"); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy