|
Axis 2 example source code file (MessageUtils.java)
The Axis 2 MessageUtils.java source code/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.axis2.jaxws.message.util; import org.apache.axiom.attachments.Attachments; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMDocument; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMText; import org.apache.axiom.om.OMXMLParserWrapper; import org.apache.axiom.om.impl.MTOMConstants; import org.apache.axiom.om.impl.builder.StAXBuilder; import org.apache.axiom.soap.SOAP11Constants; import org.apache.axiom.soap.SOAPBody; import org.apache.axiom.soap.SOAPEnvelope; import org.apache.axiom.soap.SOAPFactory; import org.apache.axis2.AxisFault; import org.apache.axis2.Constants; import org.apache.axis2.Constants.Configuration; import org.apache.axis2.context.MessageContext; import org.apache.axis2.jaxws.ExceptionFactory; import org.apache.axis2.jaxws.handler.AttachmentsAdapter; import org.apache.axis2.jaxws.handler.TransportHeadersAdapter; import org.apache.axis2.jaxws.message.Message; import org.apache.axis2.jaxws.message.Protocol; import org.apache.axis2.jaxws.message.attachments.AttachmentUtils; import org.apache.axis2.jaxws.message.factory.MessageFactory; import org.apache.axis2.jaxws.registry.FactoryRegistry; import org.apache.axis2.jaxws.utility.JavaUtils; import org.apache.axis2.transport.http.HTTPConstants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.activation.DataHandler; import javax.xml.namespace.QName; import javax.xml.soap.AttachmentPart; import javax.xml.soap.MimeHeader; import javax.xml.soap.MimeHeaders; import javax.xml.soap.SOAPMessage; import javax.xml.ws.WebServiceException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** Miscellaneous Utilities that may be useful inside and outside the Message subcomponent. */ public class MessageUtils { private static final Log log = LogFactory.getLog(MessageUtils.class); /** * Get an axiom SOAPFactory for the specified element * * @param e OMElement * @return SOAPFactory */ public static SOAPFactory getSOAPFactory(OMElement e) { // Getting a factory from a SOAPEnvelope is not straight-forward. // Please change this code if an easier mechanism is discovered. OMXMLParserWrapper builder = e.getBuilder(); if (builder instanceof StAXBuilder) { StAXBuilder staxBuilder = (StAXBuilder)builder; OMDocument document = staxBuilder.getDocument(); if (document != null) { OMFactory factory = document.getOMFactory(); if (factory instanceof SOAPFactory) { return (SOAPFactory)factory; } } } // Flow to here indicates that the envelope does not have // an accessible factory. Create a new factory based on the // protocol. while (e != null && !(e instanceof SOAPEnvelope)) { e = (OMElement)e.getParent(); } if (e instanceof SOAPEnvelope) { if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI. equals(e.getNamespace().getNamespaceURI())) { return OMAbstractFactory.getSOAP11Factory(); } else { return OMAbstractFactory.getSOAP12Factory(); } } return null; } /** * Create an SAAJ AttachmentPart from a JAXWS Attachment * @param cid String content id * @param dh DataHandler * @param message SOAPMessage * @return AttachmentPart */ public static AttachmentPart createAttachmentPart(String cid, DataHandler dh, SOAPMessage message) { // Create the Attachment Part AttachmentPart ap = message.createAttachmentPart(dh); // REVIEW // Do we need to copy the content type from the datahandler ? // Preserve the original content id ap.setContentId(cid); return ap; } /** * Create a JAX-WS Message from the information on an Axis 2 Message Context * * @param msgContext * @return Message */ public static Message getMessageFromMessageContext(MessageContext msgContext) throws WebServiceException { if (log.isDebugEnabled()) { log.debug("Start getMessageFromMessageContext"); } Message message = null; // If the Axis2 MessageContext that was passed in has a SOAPEnvelope // set on it, grab that and create a JAX-WS Message out of it. SOAPEnvelope soapEnv = msgContext.getEnvelope(); if (soapEnv != null) { MessageFactory msgFactory = (MessageFactory)FactoryRegistry.getFactory(MessageFactory.class); try { Protocol protocol = msgContext.isDoingREST() ? Protocol.rest : null; message = msgFactory.createFrom(soapEnv, protocol); } catch (Exception e) { throw ExceptionFactory.makeWebServiceException("Could not create new Message"); } Object property = msgContext.getProperty(Constants.Configuration.ENABLE_MTOM); if (property != null && JavaUtils.isTrueExplicitly(property)) { message.setMTOMEnabled(true); } // Add all the MimeHeaders from the Axis2 MessageContext Map headerMap = (Map)msgContext.getProperty(MessageContext.TRANSPORT_HEADERS); if (headerMap != null) { message.setMimeHeaders(headerMap); } // TODO: This is a WORKAROUND for missing SOAPFault data. If we do a toString on the // SOAPEnvelope, then all the data will be available to the provider. Otherwise, it // will be missing the <Reason> element corresponding to the Other Axis 2 examples (source code examples)Here is a short list of links related to this Axis 2 MessageUtils.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.