Home » synapse-1.2-src » org.apache.synapse.format.hessian » [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   
   20   package org.apache.synapse.format.hessian;
   21   
   22   import org.apache.axiom.om.OMElement;
   23   import org.apache.axiom.om.OMNode;
   24   import org.apache.axiom.om.OMOutputFormat;
   25   import org.apache.axiom.om.OMText;
   26   import org.apache.axiom.soap.SOAPFault;
   27   import org.apache.axis2.AxisFault;
   28   import org.apache.axis2.Constants;
   29   import org.apache.axis2.context.MessageContext;
   30   import org.apache.axis2.transport.MessageFormatter;
   31   import org.apache.axis2.transport.http.util.URLTemplatingUtil;
   32   import org.apache.commons.logging.Log;
   33   import org.apache.commons.logging.LogFactory;
   34   import org.apache.synapse.util.SynapseBinaryDataSource;
   35   
   36   import javax.activation.DataHandler;
   37   import java.io.BufferedOutputStream;
   38   import java.io.IOException;
   39   import java.io.InputStream;
   40   import java.io.OutputStream;
   41   import java.net.URL;
   42   import java.util.Iterator;
   43   
   44   /**
   45    * Enables a message encoded using the Hessian binary protocol to be written to transport by
   46    * axis2/synapse and this formats the HessianDataSource to a hessian message
   47    *
   48    * @see org.apache.axis2.transport.MessageFormatter
   49    * @see org.apache.synapse.util.SynapseBinaryDataSource
   50    */
   51   public class HessianMessageFormatter implements MessageFormatter {
   52   
   53   	private static final Log log = LogFactory.getLog(HessianMessageFormatter.class);
   54   
   55       /**
   56   	 * Formats the content type to be written in to the transport
   57   	 *
   58   	 * @param msgCtxt message of which the content type has to be formatted
   59   	 * @param format fomat of the expected formatted message
   60   	 * @param soapActionString soap action of the message
   61   	 * @return contentType formatted content type as a String
   62   	 */
   63   	public String getContentType(MessageContext msgCtxt, OMOutputFormat format,
   64           String soapActionString) {
   65   
   66           String contentType = (String) msgCtxt.getProperty(Constants.Configuration.CONTENT_TYPE);
   67   		String encoding = format.getCharSetEncoding();
   68   
   69           if (contentType == null) {
   70   			contentType = HessianConstants.HESSIAN_CONTENT_TYPE;
   71   		}
   72   
   73           if (encoding != null) {
   74   			contentType += "; charset=" + encoding;
   75   		}
   76   
   77           return contentType;
   78   	}
   79   
   80   	/**
   81   	 * Extract Hessian bytes from the received SOAP message and write it onto the wire
   82   	 *
   83   	 * @param msgCtxt message from which the hessian message has to be extracted
   84   	 * @param format mwssage format to be written
   85   	 * @param out stream to which the message is written
   86   	 * @param preserve whether to preserve the indentations
   87   	 * @throws AxisFault in case of a failure in writing the message to the provided stream
   88   	 */
   89   	public void writeTo(MessageContext msgCtxt, OMOutputFormat format, OutputStream out,
   90           boolean preserve) throws AxisFault {
   91   
   92           if (log.isDebugEnabled()) {
   93   			log.debug("Start writing the message to OutputStream");
   94   		}
   95   
   96           // Check whther the message to be written is a fault message
   97           if (msgCtxt.getFLOW() == MessageContext.OUT_FAULT_FLOW
   98                   || msgCtxt.getEnvelope().hasFault()) {
   99   
  100               try {
  101   
  102                   SOAPFault fault = msgCtxt.getEnvelope().getBody().getFault();
  103                   String hessianFaultDetail = "";
  104                   String hessianFaultMessage = "";
  105                   String hessianFaultCode = "500";
  106   
  107                   if (fault.getDetail() != null) {
  108   					hessianFaultDetail = fault.getDetail().getText();
  109   				}
  110   
  111                   if (fault.getReason() != null) {
  112   					hessianFaultMessage = fault.getReason().getText();
  113   				}
  114   
  115                   if (fault.getCode() != null) {
  116                       hessianFaultCode = fault.getCode().getText();
  117                   }
  118   
  119                   BufferedOutputStream faultOutStream = new BufferedOutputStream(out);
  120                   HessianUtils.writeFault(
  121                           hessianFaultCode, hessianFaultMessage, hessianFaultDetail, faultOutStream);
  122                   faultOutStream.flush();
  123                   faultOutStream.close();
  124   
  125               } catch (IOException e) {
  126   				handleException("Unalbe to write the fault as a hessian message", e);
  127   			}
  128   
  129               // if the message is not a fault extract the Hessian bytes and write it to the wire
  130           } else {
  131   
  132               OMText hessianOMText = null;
  133               OMElement omElement = msgCtxt.getEnvelope().getBody().getFirstElement();
  134   
  135               Iterator it = omElement.getChildren();
  136               while (it.hasNext()) {
  137   
  138                   OMNode hessianElement =  (OMNode) it.next();
  139                   if (hessianElement instanceof OMText) {
  140   
  141                       OMText tempNode = (OMText) hessianElement;
  142                       if (tempNode.getDataHandler() != null && ((DataHandler) tempNode
  143                               .getDataHandler()).getDataSource() instanceof SynapseBinaryDataSource) {
  144   
  145                           hessianOMText = tempNode;
  146                       }
  147                   }
  148               }
  149   
  150               if (hessianOMText != null) {
  151   
  152                   try {
  153   
  154                       SynapseBinaryDataSource synapseBinaryDataSource = (SynapseBinaryDataSource) (
  155                               (DataHandler) hessianOMText.getDataHandler()).getDataSource();
  156   
  157                       InputStream inputStream = synapseBinaryDataSource.getInputStream();
  158                       BufferedOutputStream outputStream = new BufferedOutputStream(out);
  159   
  160                       byte[] buffer = new byte[1024];
  161                       int byteCount;
  162                       while ((byteCount=inputStream.read(buffer)) != -1) {
  163                           outputStream.write(buffer, 0, byteCount);
  164                       }
  165   
  166                       inputStream.close();
  167                       outputStream.flush();
  168                       outputStream.close();
  169   
  170                   } catch (IOException e) {
  171                       handleException("Couldn't get the bytes from the HessianDataSource", e);
  172                   }
  173   
  174               } else {
  175                   handleException("Unable to find the hessian content in the payload");
  176               }
  177   
  178           }
  179   
  180           if (log.isDebugEnabled()) {
  181               log.debug("Writing message as a hessian message is successful");
  182           }
  183       }
  184   
  185       /**
  186        * This method is not supported because of large file handling limitations
  187        *
  188        * @param msgCtxt message which contains the Hessian message inside the HessianDataSource
  189        * @param format message fromat to be written
  190        * @return Hessian binary bytes of the message
  191        * @throws AxisFault for any invoke
  192        */
  193       public byte[] getBytes(MessageContext msgCtxt, OMOutputFormat format) throws AxisFault {
  194           throw new AxisFault("Method not supported. Use the " +
  195                   "HessianMessageFormatter#writeTo method instead");
  196       }
  197   
  198       public String formatSOAPAction(MessageContext messageContext, OMOutputFormat format,
  199           String soapAction) {
  200   
  201           return soapAction;
  202   	}
  203   
  204       public URL getTargetAddress(MessageContext messageContext, OMOutputFormat format,
  205           URL targetURL) throws AxisFault {
  206   
  207           return URLTemplatingUtil.getTemplatedURL(targetURL, messageContext, false);
  208   	}
  209   
  210       private void handleException(String msg, Throwable e) throws AxisFault {
  211           log.error(msg, e);
  212           throw new AxisFault(msg, e);
  213       }
  214   
  215       private void handleException(String msg) throws AxisFault {
  216           log.error(msg);
  217           throw new AxisFault(msg);
  218       }
  219   }

Home » synapse-1.2-src » org.apache.synapse.format.hessian » [javadoc | source]