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

    1   /*
    2   *  Licensed to the Apache Software Foundation (ASF) under one
    3   *  or more contributor license agreements.  See the NOTICE file
    4   *  distributed with this work for additional information
    5   *  regarding copyright ownership.  The ASF licenses this file
    6   *  to you under the Apache License, Version 2.0 (the
    7   *  "License"); you may not use this file except in compliance
    8   *  with the License.  You may obtain a copy of the License at
    9   *
   10   *   http://www.apache.org/licenses/LICENSE-2.0
   11   *
   12   *  Unless required by applicable law or agreed to in writing,
   13   *  software distributed under the License is distributed on an
   14   *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15   *  KIND, either express or implied.  See the License for the
   16   *  specific language governing permissions and limitations
   17   *  under the License.
   18   */
   19   package org.apache.synapse.transport.nhttp.util;
   20   
   21   import org.apache.axiom.om.OMOutputFormat;
   22   import org.apache.axis2.AxisFault;
   23   import org.apache.axis2.context.MessageContext;
   24   import org.apache.axis2.transport.MessageFormatter;
   25   import org.apache.commons.logging.Log;
   26   import org.apache.commons.logging.LogFactory;
   27   
   28   import java.io.IOException;
   29   import java.io.OutputStream;
   30   import java.net.URL;
   31   import java.util.zip.GZIPOutputStream;
   32   
   33   /**
   34    * This is the decorator for message formatter and it is need because there isn't any mechanism
   35    * that can process the message before formatting is occurred.  For handle 'Accept-Encoding' some
   36    * kind of this is  required. This decorator encapsulates the message formatter and add the whatever
   37    * functionally when as required without effecting message formatter functionality.
   38    * This enable to serialize message in Gzip format .
   39    */
   40   
   41   public class GzipMessageFormatterDecorator implements MessageFormatter {
   42   
   43       private static final Log log = LogFactory.getLog(GzipMessageFormatterDecorator.class);
   44   
   45       /* The encapsulated message formatter instance */
   46       private MessageFormatter messageFormatter;
   47   
   48       public GzipMessageFormatterDecorator(MessageFormatter messageFormatter) {
   49           this.messageFormatter = messageFormatter;
   50       }
   51   
   52       public byte[] getBytes(MessageContext messageContext, OMOutputFormat format) throws AxisFault {
   53           return messageFormatter.getBytes(messageContext, format);
   54       }
   55   
   56       public void writeTo(MessageContext messageContext, OMOutputFormat format, OutputStream outputStream, boolean preserve) throws AxisFault {
   57   
   58           try {
   59   
   60               // Writes message as an GZIP stream
   61               if (log.isDebugEnabled()) {
   62                   log.debug("Serialize message in to a GZIP stream");
   63               }
   64   
   65               OutputStream out = new GZIPOutputStream(outputStream);
   66               messageFormatter.writeTo(messageContext, format, out, preserve);
   67   
   68               try {
   69                   out.flush();
   70                   out.close();
   71               } catch (IOException e) {
   72                   handleException("Error closing message stream", e);
   73               }
   74   
   75           } catch (IOException e) {
   76               handleException("Error getting GZIP output stream", e);
   77           }
   78   
   79       }
   80   
   81       public String getContentType(MessageContext messageContext, OMOutputFormat format, String soapAction) {
   82           return messageFormatter.getContentType(messageContext, format, soapAction);
   83       }
   84   
   85       public URL getTargetAddress(MessageContext messageContext, OMOutputFormat format, URL targetURL) throws AxisFault {
   86           return messageFormatter.getTargetAddress(messageContext, format, targetURL);
   87       }
   88   
   89       public String formatSOAPAction(MessageContext messageContext, OMOutputFormat format, String soapAction) {
   90           return messageFormatter.formatSOAPAction(messageContext, format, soapAction);
   91       }
   92   
   93       private void handleException(String msg, Exception e) throws AxisFault {
   94           log.error(msg, e);
   95           throw new AxisFault(msg, e);
   96       }
   97   }

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