Home » synapse-1.2-src » org.apache.synapse.format » [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.format;
   20   
   21   import java.io.ByteArrayOutputStream;
   22   import java.io.IOException;
   23   import java.io.OutputStream;
   24   import java.net.URL;
   25   
   26   import javax.activation.DataHandler;
   27   import javax.activation.DataSource;
   28   
   29   import org.apache.axiom.om.OMElement;
   30   import org.apache.axiom.om.OMNode;
   31   import org.apache.axiom.om.OMOutputFormat;
   32   import org.apache.axiom.om.OMText;
   33   import org.apache.axis2.AxisFault;
   34   import org.apache.axis2.context.MessageContext;
   35   import org.apache.axis2.transport.http.util.URLTemplatingUtil;
   36   import org.apache.synapse.transport.base.BaseConstants;
   37   
   38   public class BinaryFormatter implements MessageFormatterEx {
   39       public byte[] getBytes(MessageContext messageContext, OMOutputFormat format) throws AxisFault {
   40           ByteArrayOutputStream baos = new ByteArrayOutputStream();
   41           writeTo(messageContext, format, baos, true);
   42           return baos.toByteArray();
   43       }
   44   
   45       private DataHandler getDataHandler(MessageContext messageContext) {
   46           OMElement firstChild = messageContext.getEnvelope().getBody().getFirstElement();
   47           if (BaseConstants.DEFAULT_BINARY_WRAPPER.equals(firstChild.getQName())) {
   48               OMNode omNode = firstChild.getFirstOMChild();
   49               if (omNode != null && omNode instanceof OMText) {
   50                   Object dh = ((OMText)omNode).getDataHandler();
   51                   if (dh != null && dh instanceof DataHandler) {
   52                       return (DataHandler)dh;
   53                   }
   54               }
   55           }
   56           return null;
   57       }
   58       
   59       public void writeTo(MessageContext messageContext, OMOutputFormat format,
   60               OutputStream outputStream, boolean preserve) throws AxisFault {
   61           DataHandler dh = getDataHandler(messageContext);
   62           if (dh != null) {
   63               try {
   64                   ((DataHandler)dh).writeTo(outputStream);
   65               } catch (IOException e) {
   66                   throw new AxisFault("Error serializing binary content of element : " +
   67                                   BaseConstants.DEFAULT_BINARY_WRAPPER, e);
   68               }
   69           }        
   70       }
   71   
   72       public String getContentType(MessageContext messageContext,
   73               OMOutputFormat format, String soapAction) {
   74           DataHandler dh = getDataHandler(messageContext);
   75           if (dh != null) {
   76               return dh.getContentType();
   77           } else {
   78               return null;
   79           }
   80       }
   81   
   82       public URL getTargetAddress(MessageContext messageContext,
   83               OMOutputFormat format, URL targetURL) throws AxisFault {
   84           return URLTemplatingUtil.getTemplatedURL(targetURL, messageContext, false);
   85       }
   86   
   87       public String formatSOAPAction(MessageContext messageContext,
   88               OMOutputFormat format, String soapAction) {
   89           return null;
   90       }
   91   
   92       public DataSource getDataSource(MessageContext messageContext,
   93               OMOutputFormat format, String soapAction) throws AxisFault {
   94           return getDataHandler(messageContext).getDataSource();
   95       }
   96   }

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