All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.petals.se.jsr181.utils.AxiomHelper Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
/**
 * Copyright (c) 2008-2012 EBM WebSourcing, 2012-2016 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.se.jsr181.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Iterator;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;

/**
 * An Apache AXIOM Helper.
 * @author Christophe HAMERLING - EBM WebSourcing
 */
public class AxiomHelper {

	/**
	 * Creates the content of a JBI message (as a source) from a SOAP message.
	 * @param envelope the SOAP envelope
	 * @return a source
	 * @throws Exception if something went wrong
	 */
	public static Source createSource( final SOAPEnvelope envelope ) throws Exception {

		if( envelope == null )
			throw new Exception( "The envelope cannot be null to create a source." );

		Source result = null;
		final SOAPBody body = envelope.getBody();
		final OMNamespace namespace = envelope.getNamespace();
		final Iterator envNS = envelope.getAllDeclaredNamespaces();
		final Iterator bodyNS = body.getAllDeclaredNamespaces();

		OMElement rootElement = body.getFirstElement();
		if( rootElement != null ) {
			rootElement.declareNamespace(namespace);
			while( envNS.hasNext())
				rootElement.declareNamespace((OMNamespace) envNS.next());

			while( bodyNS.hasNext())
				rootElement.declareNamespace((OMNamespace) bodyNS.next());

			final ByteArrayOutputStream os = new ByteArrayOutputStream();
			rootElement.serialize( os );
			os.toByteArray();
			result = new StreamSource( new ByteArrayInputStream( os.toByteArray()));
		}

		return result;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy