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

de.codecentric.cxf.soaprawclient.SoapRawClient Maven / Gradle / Ivy

Go to download

Boot starter for SOAP-Webservices with Apache CXF using JAX-WS & JAXB with Annotations only

There is a newer version: 2.7.0
Show newest version
package de.codecentric.cxf.soaprawclient;

import java.io.InputStream;

import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import de.codecentric.cxf.common.BootStarterCxfException;
import de.codecentric.cxf.common.XmlUtils;

/**
 * Simple SOAP-Client for testing SOAP-APIs, how they would behave without XML schema compliant requests,
 * that couldn´t be marshalled to the JAX-B Objects. 
 * 
 * @author Jonas Hecht
 *
 */
@Component
public class SoapRawClient {

	private static final Logger LOGGER = LoggerFactory.getLogger(SoapRawClient.class);
	private String soapAction;
	private String soapServiceUrl;
	
	public  SoapRawClient(String soapServiceUrl, Class jaxWsServiceInterfaceClass) throws BootStarterCxfException {
	    this.soapAction = XmlUtils.getSoapActionFromJaxWsServiceInterface(jaxWsServiceInterfaceClass);
	    this.soapServiceUrl = soapServiceUrl;
	}
	
	// Invisible Constructor, because soapServiceUrl is mandatory
	private SoapRawClient() {};
	
	public SoapRawClientResponse callSoapService(InputStream xmlFile) throws BootStarterCxfException {
		SoapRawClientResponse easyRawSoapResponse = new SoapRawClientResponse();
		
		LOGGER.debug("Calling SoapService with POST on Apache HTTP-Client and configured URL: {}", soapServiceUrl);
		
		try {
			Response httpResponseContainer = Request
					.Post(soapServiceUrl)
					.bodyStream(xmlFile, contentTypeTextXmlUtf8())
					.addHeader("SOAPAction", "\"" + soapAction + "\"")
					.execute();
			
			HttpResponse httpResponse = httpResponseContainer.returnResponse();			
			easyRawSoapResponse.setHttpStatusCode(httpResponse.getStatusLine().getStatusCode());
			easyRawSoapResponse.setHttpResponseBody(XmlUtils.parseFileStream2Document(httpResponse.getEntity().getContent()));
			
		} catch (Exception exception) {
			throw new BootStarterCxfException("Some Error accured while trying to Call SoapService for test: " + exception.getMessage());
		}		
		return easyRawSoapResponse;
	}

	private ContentType contentTypeTextXmlUtf8() {
		return ContentType.create(ContentType.TEXT_XML.getMimeType(), Consts.UTF_8);
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy