Spring-WS: How to include a soapheader in the response message - java

I'm using Spring-WS to build a web service (contract first). I defined an endpoint like below
#Endpoint
public class ReportingEndpoint {
private static final Logger LOGGER = LoggerFactory.getLogger(ReportingEndpoint.class);
private static final String NAMESPACE_URI = "http://localhost/reporting";
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "RequestDocument")
#ResponsePayload
public ResponseDocument accountReporting(
#RequestPayload JAXBElement<RequestDocument> request,
#SoapHeader(value = "{http://localhost/reporting}Hdr") SoapHeaderElement header) {
try {
ApplicationHeader headers = ((JAXBElement<ApplicationHeader>) JAXBUtils
.unmarshal(header.getSource(), ObjectFactory.class)).getValue();
LOGGER.info(headers.getSystemName());
LOGGER.info("Hello world.");
ResponseDocument response = new ResponseDocument();
response.setReportTitle("Report Title");
return response;
} catch (Exception ex) {
return null;
}
}
}
This code can receive and read the soap header sent from client but when I return a response message, I don't know how to send back to client the server soap header as the client did.
Can anybody help me to solve this issue?

Related

SoapFaultClientException : Failed to find header

A SOAP Web-service, which accepts request in following format -
<?xml version = "1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
xmlns:ns="http://...." xmlns:ns1="http://...." xmlns:ns2="http://...."
xmlns:ns3="http://....">
<SOAP-ENV:Header>
<ns:EMContext>
<messageId>1</messageId>
<refToMessageId>ABC123</refToMessageId>
<session>
<sessionId>3</sessionId>
<sessionSequenceNumber>2021-02-24T00:00:00.000+5:00</sessionSequenceNumber>
</session>
<invokerRef>CRS</invokerRef>
</ns:EMContext>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:getEmployee>
<ns:empId>111</ns:empId>
</ns1:getEmployee>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
When trying to make a SOAP request to it using JAXB2, it is giving org.springframework.ws.soap.client.SoapFaultClientException: EMContext Header is missing
I am using
pring-boot-starter
spring-boot-starter-web-services
org.jvnet.jaxb2.maven2 : maven-jaxb2-plugin : 0.14.0
and
Client -
public class MyClient extends WebServiceGatewaySupport {
public GetEmployeeResponse getEmployee(String url, Object request){
GetEmployeeResponse res = (GetEmployeeResponse) getWebServiceTemplate().marshalSendAndReceive(url, request);
return res;
}
}
Configuration -
#Configuration
public class EmpConfig {
#Bean
public Jaxb2Marshaller marshaller(){
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setContextPath("com.crsardar.java.soap.client.request");
return jaxb2Marshaller;
}
#Bean
public MyClient getClient(Jaxb2Marshaller jaxb2Marshaller){
MyClient myClient = new MyClient();
myClient.setDefaultUri("http://localhost:8080/ws");
myClient.setMarshaller(jaxb2Marshaller);
myClient.setUnmarshaller(jaxb2Marshaller);
return myClient;
}
}
App -
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
#Bean
CommandLineRunner lookup(MyClient myClient){
return args -> {
GetEmployeeRequest getEmployeeRequest = new GetEmployeeRequest();
getEmployeeRequest.setId(1);
GetEmployeeResponse employee = myClient.getEmployee("http://localhost:8080/ws", getEmployeeRequest);
System.out.println("Response = " + employee.getEmployeeDetails().getName());
};
}
}
How can I add EMContext Header to the SOAP request?
The server is complaining because your Web Service client is not sending the EMContext SOAP header in your SOAP message.
Unfortunately, currently Spring Web Services lack of support for including SOAP headers in a similar way as the SOAP body information is processed using JAXB, for example.
As a workaround, you can use WebServiceMessageCallback. From the docs:
To accommodate the setting of SOAP headers and other settings on the message, the WebServiceMessageCallback interface gives you access to the message after it has been created, but before it is sent.
In your case, you can use something like:
public class MyClient extends WebServiceGatewaySupport {
public GetEmployeeResponse getEmployee(String url, Object request){
// Obtain the required information
String messageId = "1";
String refToMessageId = "ABC123";
String sessionId = "3";
String sessionSequenceNumber = "2021-02-24T00:00:00.000+5:00";
String invokerRef = "CRS";
GetEmployeeResponse res = (GetEmployeeResponse) this.getWebServiceTemplate().marshalSendAndReceive(url, request, new WebServiceMessageCallback() {
#Override
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
// Include the SOAP header content for EMContext
try {
SoapMessage soapMessage = (SoapMessage)message;
SoapHeader header = soapMessage.getSoapHeader();
StringSource headerSource = new StringSource(
"<EMContext xmlns:ns=\"http://....\">" +
"<messageId>" + messageId + "</messageId>" +
"<refToMessageId>" + refToMessageId + "</refToMessageId>" +
"<session>" +
"<sessionId>" + sessionId + "</sessionId>" +
"<sessionSequenceNumber>" + sessionSequenceNumber + "</sessionSequenceNumber>" +
"</session>" +
"<invokerRef>" + invokerRef + "</invokerRef>" +
"</EMContext>"
);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(headerSource, header.getResult());
} catch (Exception e) {
// handle the exception as appropriate
e.printStackTrace();
}
}
});
return res;
}
}
Similar questions have been posted in SO. Consider for instance review this or this other.

Retreiving the SOAP message context from inside the service endpoint

I have a spring boot application which expose SOAP web service using spring-boot-starter-web-services.
I'am getting the request's messageContext using EndpointInterceptor
#Component
public class GlobalEndpointInterceptor implements EndpointInterceptor {
#Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
//Here I get the messageContext
}
}
In my service EndPoint I have :
#Endpoint
public class CountryEndpoint {
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "addCountryRequest")
#ResponsePayload
public AddCountryResponse addCountry(#RequestPayload AddCountryRequest request) {
//Insert country and get the autogenerated ID
//Insert the country ID along with the messageContext retreived from the intercepter. I can't get the messageContext here !
}
}
How can I retreive the message context inside my service endpoint
You can do the following to get the SOAP Envelope:
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "addCountryRequest")
#ResponsePayload
public AddCountryResponse addCountry(#RequestPayload AddCountryRequest request, MessageContext context) {
//Insert country and get the autogenerated ID
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
messageContext.getRequest().writeTo(outputStream);
String httpMessage = new String(outputStream.toByteArray());
System.out.println(httpMessage);
} catch (IOException e) {
e.printStackTrace();
}
}

Spring web app javax.ws.rs.ProcessingException: HTTP 500 Internal Server Error

I'm having some issues running a web app which is basically a URL shortener.
The server have a functionality that allows to upload a CSV file with a list of URLs to short. The code bellow is a method that takes a CSV file from a queue, it reads the file and shorts the URLs in it. The problem comes when I try to send a post request to on of the controllers in my server. The exception that appears is the following:
javax.ws.rs.ProcessingException: HTTP 500 Internal Server Error
Here is the code of the method I mentioned:
while(true){
QueueObject qo = csvQueue.take();
copyFile(qo.getFile());
File f = new File("temp");
Scanner sc = new Scanner(f);
sc.useDelimiter(",|\\s");
Client client = ClientBuilder.newClient();
while(sc.hasNext()){
String url = sc.next();
ResponseEntity<ShortURL> res = shortener(url, null, null, null, null, null);
if(res!=null && ((res.getStatusCode()).toString()).equals("400")){
String stat = url + " : Failed";
UpdateMessage um = new UpdateMessage(stat, qo.getUser());
Response response = client.target("http://localhost:8080/urlUploads")
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(um, MediaType.APPLICATION_JSON));
}
else{
String stat = url + " : Success";
UpdateMessage um = new UpdateMessage(stat, qo.getUser());
Response response = client.target("http://localhost:8080/urlUploads")
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(um, MediaType.APPLICATION_JSON));
}
}
f.delete();
}
As I said, the problem is on this specific request (both are basically the same):
Response response = client.target("http://localhost:8080/urlUploads")
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(um, MediaType.APPLICATION_JSON));
The controller I'm trying to reach is this one:
#Controller
public class WebSocketController {
private SimpMessagingTemplate template;
private static final Logger logger = LoggerFactory.getLogger(WebSocketController.class);
#Autowired
public WebSocketController(SimpMessagingTemplate template) {
this.template = template;
}
#RequestMapping(value="/urlUploads", method=RequestMethod.POST)
public void greet(UpdateMessage update) {
this.template.convertAndSendToUser(update.getUser(), "/sockets/urlUploads", update.getStatus());
}
}

SOAPResponse does not retain soap headers

When I add soap headers from handler.handlResponse(), I can see the headers added in the handler but these headers do not make it to the client.
Here is my handleResponse() method.
public static final String WEB_SERVICE_NAMESPACE_PREIFX = "dm";
public static final String WEB_SERVICE_NAMESPACE_URI = "urn:com.qwest.dms.dto";
public boolean handleResponse(MessageContext context)
{
logger.debug("TransactionLoggerHandler.handleResponse invoked");
try
{
SOAPMessageContext soapContext;
soapContext = (SOAPMessageContext)context;
SOAPMessage message = soapContext.getMessage();
SOAPHeader soapHeader = message.getSOAPHeader();
String version = "version";
SOAPHeaderElement header;
SOAPFactory soapFactory;
Name name;
logger.debug("Adding soap header ["+version+"] with value [2.0].");
soapHeader.addNamespaceDeclaration(Constants.WEB_SERVICE_NAMESPACE_PREIFX, Constants.WEB_SERVICE_NAMESPACE_URI)
SOAPHeaderElement headerElement
= (SOAPHeaderElement)message.getSOAPPart().getEnvelope().getHeader().addChildElement(
"version",
Constants.WEB_SERVICE_NAMESPACE_PREIFX,
Constants.WEB_SERVICE_NAMESPACE_URI );
headerElement.addTextNode("2.0");
String headerName="protocol";
String headerValue="2.0.0";
logger.debug("Adding soap header ["+headerName+"] with value ["+headerValue+"].");
soapFactory = SOAPFactory.newInstance();
name = soapFactory.createName(headerName,
Constants.WEB_SERVICE_NAMESPACE_PREIFX,
Constants.WEB_SERVICE_NAMESPACE_URI );
header = soapHeader.addHeaderElement( name );
header.addTextNode(headerValue);
message.saveChanges();
DmsUtil.printSOAPMessage(message);
logger.debug("Soap header ["+version+"] with value [2.0] added.");
}
catch (Exception e)
{
logger.error(e);
}
return true;
}
I see the output from this method as the following:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:dm="urn:com.qwest.dms.dto">
<dm:version>2.0</dm:version>
<dm:protocol>2.0.0</dm:protocol>
</env:Header>
<env:Body>
From the Client i get the following:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
</env:Envelope>
I am not sure why these headers not sent over the wire. Any help is appreciated. BTW, I am using jax rpc webservices under jboss4 (I know, i have to upgrade this but can not due to some constraints :( ).

Adding elements in SOAP Header request for authentication

I need to incorporate an authentication header (i.e. as a part of SOAP header request) in my new web service. That authentication header will verify the userId and password details. I have to verify the content of request header details for authentication in my Web Service. If authenticated, then the SOAP body of the request will be processed, else Invalid Authentication message will be send back by the Web Service to the client application invoking the service.
I am not able to understand how to create a web service where the SOAP Header will contain some elements(in my case, authentication elements such as userId and password).
Normally, whatever method exposed in the service will come as a part of the SOAP Body. Hence confused how to proceed with adding authentication elements in the SOAP Header.
Please help
Regards,
Recently I have wrote a class which adds user credentials to SOAP header. To do that you need to create a class which implements SOAPHandler<SOAPMessageContext> interface. For e.g.:
public class MyHandler implements SOAPHandler<SOAPMessageContext> {
private static final Logger LOGGER = LoggerFactory.getLogger(MyHandler.class);
private String username;
private String password;
/**
* Handles SOAP message. If SOAP header does not already exist, then method will created new SOAP header. The
* username and password is added to the header as the credentials to authenticate user. If no user credentials is
* specified every call to web service will fail.
*
* #param context SOAP message context to get SOAP message from
* #return true
*/
#Override
public boolean handleMessage(SOAPMessageContext context) {
try {
SOAPMessage message = context.getMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
if (header == null) {
header = envelope.addHeader();
}
QName qNameUserCredentials = new QName("https://your.target.namespace/", "UserCredentials");
SOAPHeaderElement userCredentials = header.addHeaderElement(qNameUserCredentials);
QName qNameUsername = new QName("https://your.target.namespace/", "Username");
SOAPHeaderElement username = header.addHeaderElement(qNameUsername );
username.addTextNode(this.username);
QName qNamePassword = new QName("https://your.target.namespace/", "Password");
SOAPHeaderElement password = header.addHeaderElement(qNamePassword);
password.addTextNode(this.password);
userCredentials.addChildElement(username);
userCredentials.addChildElement(password);
message.saveChanges();
//TODO: remove this writer when the testing is finished
StringWriter writer = new StringWriter();
message.writeTo(new StringOutputStream(writer));
LOGGER.debug("SOAP message: \n" + writer.toString());
} catch (SOAPException e) {
LOGGER.error("Error occurred while adding credentials to SOAP header.", e);
} catch (IOException e) {
LOGGER.error("Error occurred while writing message to output stream.", e);
}
return true;
}
//TODO: remove this class after testing is finished
private static class StringOutputStream extends OutputStream {
private StringWriter writer;
public StringOutputStream(StringWriter writer) {
this.writer = writer;
}
#Override
public void write(int b) throws IOException {
writer.write(b);
}
}
#Override
public boolean handleFault(SOAPMessageContext context) {
LOGGER.debug("handleFault has been invoked.");
return true;
}
#Override
public void close(MessageContext context) {
LOGGER.debug("close has been invoked.");
}
#Override
public Set<QName> getHeaders() {
LOGGER.debug("getHeaders has been invoked.");
return null;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
}
Note that I am just adding the credentials to the header and returning true. You do what ever you want with whole message and return false if something that is expected fails.
I have implemented this one the client:
<bean id="soapHandler" class="your.package.MyHandler">
<property name="username" value="testUser"/>
<property name="password" value="testPassword"/>
</bean>
<jaxws:client "...">
<jaxws:handlers>
<ref bean="soapHandler"/>
</jaxws:handlers>
</jaxws:client>
But it also can be implemented on the endpoint.
We can get header from the envelop only not from soap message.

Categories