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; 23 import org.apache.axis2.AxisFault; 24 import org.apache.axis2.builder.Builder; 25 import org.apache.axis2.context.MessageContext; 26 import org.apache.axis2.description.Parameter; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.synapse.SynapseConstants; 30 import org.apache.synapse.core.SynapseEnvironment; 31 import org.apache.synapse.util.SynapseBinaryDataSource; 32 33 import javax.activation.DataHandler; 34 import java.io.IOException; 35 import java.io.InputStream; 36 37 /** 38 * Enables a message encoded using the Hessian binary protocol to be received by axis2/synapse 39 * and this builds the HessianDataSource to represent the hessian message inside the SOAP info-set 40 * 41 * @see org.apache.axis2.builder.Builder 42 * @see org.apache.synapse.util.SynapseBinaryDataSource 43 */ 44 public class HessianMessageBuilder implements Builder { 45 46 private static final Log log = LogFactory.getLog(HessianMessageBuilder.class); 47 48 /** 49 * Returns an OMElement from a Hessian encoded message 50 * 51 * @param inputStream stream containg the hessian message to be built 52 * @param contentType content type of the message 53 * @param messageContext message to which the hessian message has to be attached 54 * @return OMElement containing Hessian data handler keeping the message 55 * @throws AxisFault in case of a failure in building the hessian message 56 * 57 * @see org.apache.axis2.builder.Builder#processDocument(java.io.InputStream, 58 * String, org.apache.axis2.context.MessageContext) 59 */ 60 public OMElement processDocument(InputStream inputStream, String contentType, 61 MessageContext messageContext) throws AxisFault { 62 63 if (log.isDebugEnabled()) { 64 log.debug("Start building the hessian message in to a HessianDataSource"); 65 } 66 67 OMFactory factory = OMAbstractFactory.getOMFactory(); 68 OMNamespace ns = factory.createOMNamespace(HessianConstants.HESSIAN_NAMESPACE_URI, 69 HessianConstants.HESSIAN_NS_PREFIX); 70 OMElement element = factory.createOMElement( 71 HessianConstants.HESSIAN_ELEMENT_LOCAL_NAME, ns); 72 73 try { 74 75 Parameter synEnv = messageContext.getConfigurationContext() 76 .getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_ENV); 77 78 DataHandler dataHandler; 79 if (synEnv != null && synEnv.getValue() != null) { 80 dataHandler = new DataHandler(new SynapseBinaryDataSource( 81 inputStream, contentType, (SynapseEnvironment) synEnv.getValue())); 82 } else { 83 // add Hessian data inside a data handler 84 dataHandler = new DataHandler( 85 new SynapseBinaryDataSource(inputStream,contentType)); 86 } 87 OMText textData = factory.createOMText(dataHandler, true); 88 element.addChild(textData); 89 90 } catch (IOException e) { 91 String msg = "Unable to create the HessianDataSource"; 92 log.error(msg, e); 93 throw new AxisFault(msg, e); 94 } 95 96 if (log.isDebugEnabled()) { 97 log.debug("Building the hessian message using HessianDataSource is successful"); 98 } 99 100 return element; 101 } 102 103 }