Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.helger.webbasics.servlet;
 
 import java.util.List;
 import java.util.Map;
 
 
 
This filter is used, to determine if some crucial information is missing in some responses. Checked things are status code, character encoding, content type and some headers. This is mainly for debugging purposes.

Author(s):
Philip Helger
 
 public class CheckResponseFilter implements Filter
 {
   private static final Logger s_aLogger = LoggerFactory.getLogger (CheckResponseFilter.class);
 
   public void init (@Nonnull final FilterConfig filterConfigthrows ServletException
   {}

  

Parameters:
sRequestURL
nStatusCode
 
   protected void checkStatusCode (@Nonnull final String sRequestURLfinal int nStatusCode)
   {
     if (nStatusCode != . &&
         nStatusCode < . &&
         nStatusCode >= .)
       .warn ("Status code " + nStatusCode + " in response to '" + sRequestURL + "'");
   }

  

Parameters:
sRequestURL
sCharacterEncoding
nStatusCode
 
   protected void checkCharacterEncoding (@Nonnull final String sRequestURL,
                                          @Nullable final String sCharacterEncoding,
                                          final int nStatusCode)
   {
     if (StringHelper.hasNoText (sCharacterEncoding) && !ResponseHelper.isEmptyStatusCode (nStatusCode))
       .warn ("No character encoding on " + nStatusCode + " response to '" + sRequestURL + "'");
   }

  

Parameters:
sRequestURL
sContentType
nStatusCode
 
   protected void checkContentType (@Nonnull final String sRequestURL,
                                    @Nullable final String sContentType,
                                    final int nStatusCode)
   {
     if (StringHelper.hasNoText (sContentType) && !ResponseHelper.isEmptyStatusCode (nStatusCode))
       .warn ("No content type on " + nStatusCode + " response to '" + sRequestURL + "'");
   }
 
   protected void checkHeaders (@Nonnull final String sRequestURL,
                                @Nonnull final Map <StringList <String>> aHeaders,
                               final int nStatusCode)
  {
    if (nStatusCode != . && !aHeaders.isEmpty ())
      .warn ("Headers on " + nStatusCode + " response to '" + sRequestURL + "': " + aHeaders);
  }
  private void _checkResults (@Nonnull final HttpServletRequest aHttpRequest,
                              @Nonnull final StatusAwareHttpResponseWrapper aHttpResponse)
  {
    final String sRequestURL = RequestHelper.getURL (aHttpRequest);
    final int nStatusCode = aHttpResponse.getStatusCode ();
    final Map <StringList <String>> aHeaders = aHttpResponse.getHeaderMap ().getAllHeaders ();
    final String sCharacterEncoding = aHttpResponse.getCharacterEncoding ();
    final String sContentType = aHttpResponse.getContentType ();
    checkStatusCode (sRequestURLnStatusCode);
    checkCharacterEncoding (sRequestURLsCharacterEncodingnStatusCode);
    checkContentType (sRequestURLsContentTypenStatusCode);
    checkHeaders (sRequestURLaHeadersnStatusCode);
  }
  public void doFilter (@Nonnull final ServletRequest aRequest,
                        @Nonnull final ServletResponse aResponse,
                        final FilterChain aChainthrows IOExceptionServletException
  {
    // Works only for HTTP requests
    if (aRequest instanceof HttpServletRequest && aResponse instanceof HttpServletResponse)
    {
      // Create a response wrapper
      final StatusAwareHttpResponseWrapper aWrapper = new StatusAwareHttpResponseWrapper ((HttpServletResponseaResponse);
      aChain.doFilter (aRequestaWrapper);
      // Evaluate the result
      _checkResults ((HttpServletRequestaRequestaWrapper);
    }
    else
    {
      // Do nothing
      aChain.doFilter (aRequestaResponse);
    }
  }
  public void destroy ()
  {}
New to GrepCode? Check out our FAQ X