use of javax.xml.soap.SOAPException in project photon-model by vmware.
the class VcSessionHandler method handleMessage.
@Override
public boolean handleMessage(SOAPMessageContext smc) {
if (isOutgoingMessage(smc)) {
try {
SOAPHeader header = getSOAPHeader(smc);
SOAPElement vcsessionHeader = header.addChildElement(new javax.xml.namespace.QName("#", "vcSessionCookie"));
vcsessionHeader.setValue(this.vcSessionCookie);
} catch (DOMException e) {
throw new RuntimeException(e);
} catch (SOAPException e) {
throw new RuntimeException(e);
}
}
return true;
}
use of javax.xml.soap.SOAPException in project cerberus-source by cerberustesting.
the class SoapService method callSOAP.
@Override
public AnswerItem<AppService> callSOAP(String envelope, String servicePath, String soapOperation, String attachmentUrl, List<AppServiceHeader> header, String token, int timeOutMs, String system) {
AnswerItem result = new AnswerItem();
String unescapedEnvelope = StringEscapeUtils.unescapeXml(envelope);
boolean is12SoapVersion = SOAP_1_2_NAMESPACE_PATTERN.matcher(unescapedEnvelope).matches();
AppService serviceSOAP = factoryAppService.create("", AppService.TYPE_SOAP, null, "", "", envelope, "", servicePath, "", soapOperation, "", null, "", null);
serviceSOAP.setTimeoutms(timeOutMs);
ByteArrayOutputStream out = null;
MessageEvent message = null;
if (StringUtil.isNullOrEmpty(servicePath)) {
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_SERVICEPATHMISSING);
result.setResultMessage(message);
return result;
}
if (StringUtil.isNullOrEmpty(envelope)) {
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP_ENVELOPEMISSING);
result.setResultMessage(message);
return result;
}
// If header is null we create the list empty.
if (header == null) {
header = new ArrayList<>();
}
// We feed the header with token + Standard SOAP header.
if (token != null) {
header.add(factoryAppServiceHeader.create(null, "cerberus-token", token, "Y", 0, "", "", null, "", null));
}
if (StringUtil.isNullOrEmpty(soapOperation)) {
header.add(factoryAppServiceHeader.create(null, "SOAPAction", "", "Y", 0, "", "", null, "", null));
} else {
header.add(factoryAppServiceHeader.create(null, "SOAPAction", "\"" + soapOperation + "\"", "Y", 0, "", "", null, "", null));
}
header.add(factoryAppServiceHeader.create(null, "Content-Type", is12SoapVersion ? SOAPConstants.SOAP_1_2_CONTENT_TYPE : SOAPConstants.SOAP_1_1_CONTENT_TYPE, "Y", 0, "", "", null, "", null));
serviceSOAP.setHeaderList(header);
SOAPConnectionFactory soapConnectionFactory;
SOAPConnection soapConnection = null;
try {
// Initialize SOAP Connection
soapConnectionFactory = SOAPConnectionFactory.newInstance();
soapConnection = soapConnectionFactory.createConnection();
LOG.debug("Connection opened");
// Create SOAP Request
LOG.debug("Create request");
SOAPMessage input = createSoapRequest(envelope, soapOperation, header, token);
// Add attachment File if specified
if (!StringUtil.isNullOrEmpty(attachmentUrl)) {
this.addAttachmentPart(input, attachmentUrl);
// Store the SOAP Call
out = new ByteArrayOutputStream();
input.writeTo(out);
LOG.debug("WS call with attachement : " + out.toString());
serviceSOAP.setServiceRequest(out.toString());
} else {
// Store the SOAP Call
out = new ByteArrayOutputStream();
input.writeTo(out);
LOG.debug("WS call : " + out.toString());
}
// We already set the item in order to keep the request message in case of failure of SOAP calls.
serviceSOAP.setService(servicePath);
result.setItem(serviceSOAP);
// Call the WS
LOG.debug("Calling WS.");
// Reset previous Authentification.
Authenticator.setDefault(null);
serviceSOAP.setProxyWithCredential(false);
serviceSOAP.setProxyUser(null);
SOAPMessage soapResponse = null;
if (proxyService.useProxy(servicePath, system)) {
// Get Proxy host and port from parameters.
String proxyHost = parameterService.getParameterStringByKey("cerberus_proxy_host", system, DEFAULT_PROXY_HOST);
int proxyPort = parameterService.getParameterIntegerByKey("cerberus_proxy_port", system, DEFAULT_PROXY_PORT);
serviceSOAP.setProxy(true);
serviceSOAP.setProxyHost(proxyHost);
serviceSOAP.setProxyPort(proxyPort);
// Create the Proxy.
SocketAddress sockaddr = new InetSocketAddress(proxyHost, proxyPort);
try (Socket socket = new Socket()) {
socket.connect(sockaddr, 10000);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(socket.getInetAddress(), proxyPort));
if (parameterService.getParameterBooleanByKey("cerberus_proxyauthentification_active", system, DEFAULT_PROXYAUTHENT_ACTIVATE)) {
// Get the credentials from parameters.
String proxyUser = parameterService.getParameterStringByKey("cerberus_proxyauthentification_user", system, DEFAULT_PROXYAUTHENT_USER);
String proxyPass = parameterService.getParameterStringByKey("cerberus_proxyauthentification_password", system, DEFAULT_PROXYAUTHENT_PASSWORD);
serviceSOAP.setProxyWithCredential(true);
serviceSOAP.setProxyUser(proxyUser);
// Define the credential to the proxy.
initializeProxyAuthenticator(proxyUser, proxyPass);
}
// Call with Proxy.
soapResponse = sendSOAPMessage(input, servicePath, proxy, timeOutMs);
} catch (Exception e) {
LOG.warn(e.toString());
}
} else {
serviceSOAP.setProxy(false);
serviceSOAP.setProxyHost(null);
serviceSOAP.setProxyPort(0);
// Call without proxy.
soapResponse = sendSOAPMessage(input, servicePath, null, timeOutMs);
// soapResponse = soapConnection.call(input, servicePath);
}
LOG.debug("Called WS.");
out = new ByteArrayOutputStream();
// Store the response
soapResponse.writeTo(out);
LOG.debug("WS response received.");
LOG.debug("WS response : " + out.toString());
message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CALLSOAP);
message.setDescription(message.getDescription().replace("%SERVICEPATH%", servicePath).replace("%SOAPMETHOD%", soapOperation));
result.setResultMessage(message);
// soapResponse.getSOAPPart().getEnvelope().getBody().getFault().getFaultCode();
// We save convert to string the final response from SOAP request.
serviceSOAP.setResponseHTTPBody(SoapUtil.convertSoapMessageToString(soapResponse));
// Get result Content Type.
serviceSOAP.setResponseHTTPBodyContentType(appServiceService.guessContentType(serviceSOAP, AppService.RESPONSEHTTPBODYCONTENTTYPE_XML));
result.setItem(serviceSOAP);
} catch (SOAPException | UnsupportedOperationException | IOException | SAXException | ParserConfigurationException | CerberusException e) {
LOG.error(e.toString());
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSOAP);
message.setDescription(message.getDescription().replace("%SERVICEPATH%", servicePath).replace("%SOAPMETHOD%", soapOperation).replace("%DESCRIPTION%", e.getMessage()));
result.setResultMessage(message);
return result;
} finally {
try {
if (soapConnection != null) {
soapConnection.close();
}
if (out != null) {
out.close();
}
} catch (SOAPException | IOException ex) {
LOG.error(ex);
} finally {
result.setResultMessage(message);
}
}
return result;
}
use of javax.xml.soap.SOAPException in project cerberus-source by cerberustesting.
the class SoapService method sendSOAPMessage.
private static SOAPMessage sendSOAPMessage(SOAPMessage message, String url, final Proxy p, final int timeoutms) throws SOAPException, MalformedURLException {
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = factory.createConnection();
URL endpoint = new URL(null, url, new URLStreamHandler() {
protected URLConnection openConnection(URL url) throws IOException {
// The url is the parent of this stream handler, so must
// create clone
URL clone = new URL(url.toString());
URLConnection connection = null;
if (p == null) {
connection = clone.openConnection();
} else if (p.address().toString().equals("0.0.0.0/0.0.0.0:80")) {
connection = clone.openConnection();
} else {
connection = clone.openConnection(p);
}
// Set Timeout
connection.setConnectTimeout(timeoutms);
connection.setReadTimeout(timeoutms);
// connection.addRequestProperty("Developer-Mood", "Happy");
return connection;
}
});
try {
SOAPMessage response = connection.call(message, endpoint);
connection.close();
return response;
} catch (Exception e) {
// Re-try if the connection failed
SOAPMessage response = connection.call(message, endpoint);
connection.close();
return response;
}
}
use of javax.xml.soap.SOAPException in project arctic-sea by 52North.
the class AbstractSoapDecoder method getSOAPBodyContent.
/**
* Parses the SOAPBody content to a text representation
*
* @param message
* SOAP message
*
* @return SOAPBody content as text
*
* @throws DecodingException
* * if an error occurs.
*/
protected OwsServiceRequest getSOAPBodyContent(SOAPMessage message) throws DecodingException {
try {
Document bodyRequestDoc = message.getSOAPBody().extractContentAsDocument();
String xmlString = W3cHelper.nodeToXmlString(bodyRequestDoc.getDocumentElement());
return decodeXmlElement(XmlObject.Factory.parse(xmlString, getXmlOptions()));
} catch (SOAPException | XmlException | IOException e) {
throw new DecodingException("Error while parsing SOAPMessage body content!", e);
}
}
use of javax.xml.soap.SOAPException in project arctic-sea by 52North.
the class Soap11Encoder method encode.
@Override
@SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE")
public SOAPMessage encode(SoapResponse soapResponse, EncodingContext additionalValues) throws EncodingException {
if (soapResponse == null) {
throw new UnsupportedEncoderInputException(this, soapResponse);
}
String soapVersion = soapResponse.getSoapVersion();
SOAPMessage soapResponseMessage;
String action = null;
try {
soapResponseMessage = SoapHelper.getSoapMessageForProtocol(soapVersion);
if (soapResponse.getSoapFault() != null) {
createSOAPFault(soapResponseMessage.getSOAPBody().addFault(), soapResponse.getSoapFault());
} else {
if (soapResponse.getException() != null) {
action = createSOAPFaultFromExceptionResponse(soapResponseMessage.getSOAPBody().addFault(), soapResponse.getException());
addSchemaLocationForExceptionToSOAPMessage(soapResponseMessage);
} else {
action = createSOAPBody(soapResponseMessage, soapResponse, soapResponse.getSoapAction());
}
}
if (soapResponse.getHeader() != null) {
List<SoapHeader> headers = soapResponse.getHeader();
for (SoapHeader header : headers) {
if (WsaConstants.NS_WSA.equals(header.getNamespace()) && header instanceof WsaActionHeader) {
((WsaHeader) header).setValue(action);
}
Encoder<Map<QName, String>, SoapHeader> encoder = getEncoder(CodingHelper.getEncoderKey(header.getNamespace(), header));
if (encoder != null) {
Map<QName, String> headerElements = encoder.encode(header);
for (Entry<QName, String> entry : headerElements.entrySet()) {
QName qName = entry.getKey();
soapResponseMessage.getSOAPHeader().addChildElement(qName).setTextContent(headerElements.get(qName));
}
}
}
} else {
soapResponseMessage.getSOAPHeader().detachNode();
}
soapResponseMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, String.valueOf(true));
return soapResponseMessage;
} catch (SOAPException soape) {
throw new EncodingException("Error while encoding SOAPMessage!", soape);
}
}
Aggregations