Home » synapse-1.2-src » org.apache.synapse.transport.nhttp.util » [javadoc | source]

    1   /*
    2    * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/contrib/src/main/java/org/apache/http/contrib/compress/ResponseGzipCompress.java $
    3    * $Revision: 558111 $
    4    * $Date: 2007-07-21 01:31:50 +0530 (Sat, 21 Jul 2007) $
    5    *
    6    * ====================================================================
    7    * Licensed to the Apache Software Foundation (ASF) under one
    8    * or more contributor license agreements.  See the NOTICE file
    9    * distributed with this work for additional information
   10    * regarding copyright ownership.  The ASF licenses this file
   11    * to you under the Apache License, Version 2.0 (the
   12    * "License"); you may not use this file except in compliance
   13    * with the License.  You may obtain a copy of the License at
   14    *
   15    *   http://www.apache.org/licenses/LICENSE-2.0
   16    *
   17    * Unless required by applicable law or agreed to in writing,
   18    * software distributed under the License is distributed on an
   19    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   20    * KIND, either express or implied.  See the License for the
   21    * specific language governing permissions and limitations
   22    * under the License.
   23    * ====================================================================
   24    *
   25    * This software consists of voluntary contributions made by many
   26    * individuals on behalf of the Apache Software Foundation.  For more
   27    * information on the Apache Software Foundation, please see
   28    * <http://www.apache.org/>.
   29    *
   30    */
   31   
   32   package org.apache.synapse.transport.nhttp.util;
   33   
   34   import org.apache.axis2.context.MessageContext;
   35   import org.apache.commons.logging.Log;
   36   import org.apache.commons.logging.LogFactory;
   37   import org.apache.http.protocol.HTTP;
   38   
   39   import java.util.HashMap;
   40   import java.util.Map;
   41   
   42   /**
   43    * Process the AcceptEncoding in the received Message Context. This uses the headers in the
   44    * received message context to decide the content encoding that client expects.
   45    */
   46   public class ResponseAcceptEncodingProcessor {
   47   
   48       private static final Log log = LogFactory.getLog(ResponseAcceptEncodingProcessor.class);
   49   
   50       private static final String ACCEPT_ENCODING = "Accept-Encoding";
   51       private static final String GZIP_CODEC = "gzip";
   52   
   53       public static void process(final MessageContext response, final MessageContext request) {
   54   
   55           if (response == null) {
   56               throw new IllegalArgumentException("Response Message Context cannot be null");
   57           }
   58   
   59           if (request == null) {
   60               throw new IllegalArgumentException("Request Message context cannot be null");
   61           }
   62   
   63           Object o = request.getProperty(MessageContext.TRANSPORT_HEADERS);
   64           if (o != null && o instanceof Map) {
   65               Map headers = (Map) o;
   66   
   67               String encode = (String) headers.get(ACCEPT_ENCODING);
   68               if (encode != null) {
   69   
   70                   //If message  contains 'Accept-Encoding' header and  if it's value is 'qzip'
   71                   if (GZIP_CODEC.equals(encode)) {
   72   
   73                       Object obj = response.getProperty(MessageContext.TRANSPORT_HEADERS);
   74                       Map responseHeaders;
   75                       
   76                       if (obj != null && obj instanceof Map) {
   77                           responseHeaders = (Map) obj;
   78                       } else {
   79                           responseHeaders = new HashMap();
   80                           response.setProperty(MessageContext.TRANSPORT_HEADERS, responseHeaders);
   81                       }
   82   
   83                       if (log.isDebugEnabled()) {
   84                           log.debug("Sets the 'Content-Encoding' header as ' " + GZIP_CODEC + " '");
   85                       }
   86   
   87                       responseHeaders.put(HTTP.CONTENT_ENCODING, GZIP_CODEC);
   88   
   89                   }
   90                   //if there are any type for 'Accept-Encoding' , those should go here
   91               }
   92           }
   93       }
   94   }

Home » synapse-1.2-src » org.apache.synapse.transport.nhttp.util » [javadoc | source]