How do you programatically encrypt SOAP messages in Java? - java

I'm creating a SOAPMessage object, and before I send it I want to encrypt the contents. I'm creating it in a similar way to many examples that can be found on SO, for example:
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
// Retrieve different parts
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
// Two ways to extract body
SOAPBody soapBody = soapEnvelope.getBody();
soapBody = soapMessage.getSOAPBody();
//Modify body
// Etc
I can't find examples of how to encrypt the contents of an object that I create like this.

Soap Encryption is different from normal encryption process we do, as Encrypted XML payload has to be in a standard structure specified by WS-Security.
Use any WS-Security implementation like Axis, WSS4J.

Related

Dynamically adding namespace to SOAP Envelope in Java

I am new to all of this but I am trying to create a SOAP message and get stuck at the off, I am using Java 8 and the standard javax.xml.soap classes but seem unable to add namespaces to the Envelope
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
envelope.addNamespaceDeclaration( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
If I try this at runtime I get the following error
NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
I have done this now by creating the required envelope definition as an XML String and then setting the SOAPPart content using that string

SOAP response showing error-SAAJ Java

I am having one function where we generate SOAP request.The request I am able to run in SOAP UI tool and its fetching.
I am creating SOAPMessage like this
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.removeAllHeaders();
headers.addHeader("Content-Type", "text/xml");
SOAPPart soapPart = soapMessage.getSOAPPart();
When I call
SOAPMessage soapResponse = soapConnection.call(soapMessage, url);
get this error:
SEVERE: SAAJ0537: Invalid Content-Type. Could be an error message instead of a SOAP message
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
could you help to sort it out?
whether that means connection unable to establish??
Most probably expected HTTP Content-Type header for SOAP messages is application/soap+xml (see XML SOAP).
The exception you get originates from SOAP service container saying that the value of Content-Type header is text/html. I think that SOAP UI set the correct request header value for you.
The header value sent by your code seem to be a default one. You should check what the expected Content-Type value is for you server and ho to set it with your framework/library.

Adding several URIs to SOAP Envelope using Java

I am trying to write a SOAP pull. I am running into difficulty formatting the message correctly.
I am only using the included javax.xml.soap.* library with Exclipse
I need the Envelope to have multiple URIs in it. This is the example provided for use with SOAPUI.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sh="http://www.website.com/ems/soap/sh" xmlns:user="http://www.website.com/ems/soap/sh/userdata" xmlns:ser="http://www.website.com/ems/soap/sh/servicedata">
However after looking through several tutorials I am only able to produce
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sh="http://www.website.com/ems/soap/sh">
I have haven't been able to find any documentation on how to achieve the required output. I am still new to SOAP and Java so I am not sure how to articulate exactly what I need.
Here is the code I have so far minus the child element portion
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
soapMessage.getSOAPPart().getEnvelope().setPrefix("soapenv");
soapMessage.getSOAPPart().getEnvelope().removeNamespaceDeclaration("SOAP-ENV");
soapMessage.getSOAPBody().setPrefix("soapenv");
soapMessage.getSOAPHeader().setPrefix("soapenv");
String serverURI = "http://www.website.com/ems/soap/sh";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("sh", serverURI );
If you want to get this soap-envelope,
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sh="http://www.website.com/ems/soap/sh" xmlns:user="http://www.website.com/ems/soap/sh/userdata" xmlns:ser="http://www.website.com/ems/soap/sh/servicedata">
Just add addNamespaceDeclaration after your code like this.
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("sh", serverURI );
//added code here
envelope.addNamespaceDeclaration("user", "http://www.website.com/ems/soap/sh/userdata" );
envelope.addNamespaceDeclaration("ser", "http://www.website.com/ems/soap/sh/servicedata" );

How do you add XML prolog in javax.xml.soap.SOAPBody

I am trying to send an XML document via SOAP to a server that requires the payload XML to contain an XML prolog e.g., <?xml version="1.0" encoding="utf-8" ?>. The following is very similar to the code I'm using:
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
SOAPPart soapPart = getSoapMessage().getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
soapHeader = envelope.getHeader();
soapBody = envelope.getBody();
soapHeader.detachNode();
SOAPBodyElement bodyElement = soapBody.addBodyElement(getEnvelope().createName("my_element_name"));
bodyElement.addAttribute(getEnvelope().createName("my_attribute_name"), "my_attribute_value");
bodyElement.addChildElement("my_child_element");
...
A XML prolog can only exist at the very start of a XML document.
From the XML Specification available here : http://www.w3.org/TR/2000/REC-xml-20001006#sec-prolog-dtd
The document type declaration must appear before the first element in the document.
Writing a prolog in the middle of a SOAP document, therefore, is invalid - and I doubt you'll find a SOAP library or implementation that will allow you to do that. There may be a requirement you and/or your "client" have misunderstood in requiring this.
You can try a XML validator softare (e.g. http://www.w3schools.com/xml/xml_validator.asp ), it should report an error for your expected result.
Here is how to add xml prolog declaration right before the first SOAP Envelope tag:
soapMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, Boolean.TRUE.toString());

JAX-WS get soap without actually sending to a web-service

Using JAX-WS and a custom WSDL, is there a way to get the message that would be sent to a web-service without actually making a call to the service? I need to generate a soap message conforming to a WSDL, but that soap message is actually embedded into another message. I was thinking I could create a local web-service that just echos back the message but it seems like there should be a way without doing this or using a handlerchain when it doens't really matter that the message is sent.
Maybe the easiest thing to do is just to generate the soap manually?
I hope this helps:
http://www.java-tips.org/java-ee-tips/java-api-for-xml-web-services/writing-a-handler-in-jax-ws.html
You can intercept message before being sent, get the body, get the header, sign it with SAML or whatever you want, and then send it to server.
maybe this example helps (from Understanding Web Services, Part 1: SOAP, IBM Developer Works, page 21):
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart SOAPPart = message.getSOAPPart();
SOAPEnvelope envelope = SOAPPart.getEnvelope();
SOAPBody body = envelope.getBody();
SOAPElement bodyElement = body.addChildElement(envelope.createName("echo", "req", "http://localhost:8080/axis2/services/MyService/"));
bodyElement.addChildElement("category").addTextNode("classifieds");
message.saveChanges();
SOAPPart SOAPpartbefore = message.getSOAPPart();
SOAPEnvelope reqenv = SOAPpartbefore.getEnvelope();
System.out.println(reqenv.toString());

Categories

Resources