SoapFaultClientException : Failed to find header - java

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.

Related

Soap Web services with Apache Camel and Apache CXF

I am implementing SOAP web services with Apache CXF. I am using Jboss EAP server. I have used following code to expose SOAP web services.
CxfComponent cxfComponent = new CxfComponent(context);
CxfEndpoint serviceEndpoint = new CxfEndpoint(FPSoapServiceConstants.WSDL_CONFIG_URI, cxfComponent);
serviceEndpoint.setDataFormat(DataFormat.PAYLOAD);
serviceEndpoint.setServiceClass(com.fp.en.webservices.fulfillment.FulfillmentService.class);
HashMap<String, Object> properties = new HashMap<>();
properties.put("faultStackTraceEnabled", true);
properties.put("exceptionMessageCauseEnabled", true);
serviceEndpoint.configureProperties(properties);
serviceEndpoint.setLoggingFeatureEnabled(true);
context.addEndpoint(FPSoapServiceConstants.SOAP_ENDPOINT_FULFILLMENT_SERVICE, serviceEndpoint);
I am using apache camel to process incoming soap message
route.process(fpSOAPRequestProcessor).process(xyzProcessor).process(fpSOAPResponseProcessor)
I want to get all parameters in an object I created a class and try to get body
BuyProductRequest buyRequest = message.getBody(BuyProductRequest.class);
but this is giving me null. But when I try to get
String buyRequest = message.getBody(String.class);
It is giving me SOAP message So I have to convert xml SOAP message to Object by JAXB Marshaller.
Processor code is as follows
public class FPSoapRequestProcessor implements Processor{
#Override
public void process(Exchange exchange) throws Exception {
Message message = exchange.getIn();
String operation = String.valueOf(exchange.getIn().getHeader("operationName"));
if(FPSoapServiceConstants.BUY_PRODUCT_SOAP_OPERATION.equalsIgnoreCase(operation)) {
populateBuyProductOperationProperties(message);
}
}
private void populateBuyProductOperationProperties(Message message) {
String buyRequest = message.getBody(String.class);
BuyProductRequest productInfo= parseRequest(buyRequest);
message.setHeader("MSISDN", productInfo.getMsisdn());
message.setHeader("iname", productInfo.getIname());
message.setHeader("input", productInfo.getInput());
message.setHeader("username", productInfo.getUserName());
message.setHeader("password", productInfo.getPassword());
message.setHeader("soapConversion", true);
}
private BuyProductRequest parseRequest(String soapRequest){
try(InputStream is = new ByteArrayInputStream(soapRequest.getBytes())) {
JAXBContext jaxbContext = JAXBContext.newInstance(BuyProductRequest.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (BuyProductRequest) jaxbUnmarshaller.unmarshal(is);
} catch (Exception e) {
throw new RuntimeException("SOAP Request Object Resolving Error",e);
}
}
}
So Is there any simple way to construct request object and Similarly at that time when I am done with processing, in fpSOAPResponseProcessor I have to convert my object into soap string then I am sending it.
fpSoapResponseProcessor code is as follows
public class FDPSoapResponseProcessor implements Processor{
#Override
public void process(Exchange exchange) throws Exception {
Message message = exchange.getIn();
FulfillmentResponse response = XmlUtil.unmarshall(message.getBody(String.class), FulfillmentResponse.class);
BuyProductResponse buyProductResponse = new BuyProductResponse();
buyProductResponse.setProductResponse(response);
String soapResponse = parse(buyProductResponse);
exchange.getOut().setBody(soapResponse);
}
private String parse(BuyProductResponse buyProductResponse) {
try(StringWriter writer = new StringWriter()){
JAXBContext jContext = JAXBContext.newInstance(BuyProductResponse.class);
Marshaller marshallObj = jContext.createMarshaller();
marshallObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshallObj.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
marshallObj.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshallObj.setProperty("com.sun.xml.bind.namespacePrefixMapper", new FulfillmentResponseMapper());
marshallObj.marshal(buyProductResponse, writer);
return writer.toString();
} catch(Exception e) {
throw new RuntimeException("SOAP Request String Parsing Error",e);
}
}
private static class FulfillmentResponseMapper extends NamespacePrefixMapper {
#Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
if(FPSoapServiceConstants.SOAP_NAMESPACE_URI.equalsIgnoreCase(namespaceUri)) {
return FPSoapServiceConstants.SOAP_PREFIX;
}
return suggestion;
}
#Override
public String[] getPreDeclaredNamespaceUris() {
return new String[] { FPSoapServiceConstants.SOAP_NAMESPACE_URI};
}
}
}
Please suggest a proper simple way If there is?
AFAICS, this is the simple way.

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

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?

How to make a RPC-JSON java server

I have a client RPC-JSON in Android and I am trying make a RPC-JSON Server with a library for Java (http://software.dzhuvinov.com/json-rpc-2.0-server.html). This is the official example:
// The JSON-RPC 2.0 Base classes that define the
// JSON-RPC 2.0 protocol messages
import com.thetransactioncompany.jsonrpc2.*;
// The JSON-RPC 2.0 server framework package
import com.thetransactioncompany.jsonrpc2.server.*;
import java.text.*;
import java.util.*;
/**
* Demonstration of the JSON-RPC 2.0 Server framework usage. The request
* handlers are implemented as static nested classes for convenience, but in
* real life applications may be defined as regular classes within their old
* source files.
*
* #author Vladimir Dzhuvinov
* #version 2011-03-05
*/
public class Example {
// Implements a handler for an "echo" JSON-RPC method
public static class EchoHandler implements RequestHandler {
// Reports the method names of the handled requests
public String[] handledRequests() {
return new String[]{"echo"};
}
// Processes the requests
public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {
if (req.getMethod().equals("echo")) {
// Echo first parameter
List params = (List)req.getParams();
Object input = params.get(0);
return new JSONRPC2Response(input, req.getID());
}
else {
// Method name not supported
return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
}
}
}
// Implements a handler for "getDate" and "getTime" JSON-RPC methods
// that return the current date and time
public static class DateTimeHandler implements RequestHandler {
// Reports the method names of the handled requests
public String[] handledRequests() {
return new String[]{"getDate", "getTime"};
}
// Processes the requests
public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {
if (req.getMethod().equals("getDate")) {
DateFormat df = DateFormat.getDateInstance();
String date = df.format(new Date());
return new JSONRPC2Response(date, req.getID());
}
else if (req.getMethod().equals("getTime")) {
DateFormat df = DateFormat.getTimeInstance();
String time = df.format(new Date());
return new JSONRPC2Response(time, req.getID());
}
else {
// Method name not supported
return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
}
}
}
public static void main(String[] args) {
// Create a new JSON-RPC 2.0 request dispatcher
Dispatcher dispatcher = new Dispatcher();
// Register the "echo", "getDate" and "getTime" handlers with it
dispatcher.register(new EchoHandler());
dispatcher.register(new DateTimeHandler());
// Simulate an "echo" JSON-RPC 2.0 request
List echoParam = new LinkedList();
echoParam.add("Hello world!");
JSONRPC2Request req = new JSONRPC2Request("echo", echoParam, "req-id-01");
System.out.println("Request: \n" + req);
JSONRPC2Response resp = dispatcher.process(req, null);
System.out.println("Response: \n" + resp);
// Simulate a "getDate" JSON-RPC 2.0 request
req = new JSONRPC2Request("getDate", "req-id-02");
System.out.println("Request: \n" + req);
resp = dispatcher.process(req, null);
System.out.println("Response: \n" + resp);
// Simulate a "getTime" JSON-RPC 2.0 request
req = new JSONRPC2Request("getTime", "req-id-03");
System.out.println("Request: \n" + req);
resp = dispatcher.process(req, null);
System.out.println("Response: \n" + resp);
}
}
I search in the manual and Google but I don't understand how I can do that the server is waiting for a request and send a response. How I can do it?

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());
}
}

How to add soap header when making a soap request using the java objects generated by wsdl

I generated client java objects using JAX-WS RI. I am trying to make a SOAP request to a web service. Service requires authentication in the header which looks like below:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<xsd:authHeader>
<xsd:user>username#gmail.com</xsd:user>
<xsd:password>password1</xsd:password>
</xsd:authHeader>
</soapenv:Header>
<soapenv:Body>
<ns:searchAssetsParam>
<ns:includeSubfolders>true</ns:includeSubfolders>
<ns:resultsPage>2</ns:resultsPage>
</ns:searchAssetsParam>
</soapenv:Body>
</soapenv:Envelope>
The generated java objects have methods for calling the service, creating the objects and constructing the header. But, I am having trouble setting the header while making the call.
Here's the code that I am using:
IpsApiService service = new IpsApiService();
IpsApiPortType port = service.getIpsApiSoapPort();
SearchAssetsParam searchAssetsParam = buildSearchAssetsParam();
SearchAssetsReturn response = port.searchAssets(searchAssetsParam);
buildSearchAssetsParam() constructs the request object.
I created the header object as follows:
AuthHeader header = new AuthHeader();
header.setUser("username#gmail.com");
header.setPassword("password1");
How do I set this AuthHeader to the service request?
Thanks,
Venu
Once I had the same problem. I needed to modify the JAX-WS web service SOAP header at every request. To solve this problem I have created a handler like this:
public class MyHandler implements SOAPHandler<SOAPMessageContext> {
private static final Logger LOGGER = LoggerFactory.getLogger(MyHandler.class);
private String username;
private String password;
#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;
}
}
It adds the needed parameters to my SOAP header and it is invoked on every request. All you need to do is to modify handleMessage method to suit your needs.
It works for me by overriding the public void setAttribute(String namespace, String localName, String value) method.
import javax.xml.namespace.QName;
import org.apache.axis.Constants;
import org.apache.axis.message.SOAPHeaderElement;
#SuppressWarnings("serial")
public class ADESHeaderElement extends SOAPHeaderElement
{
public ADESHeaderElement(QName qname, Object value)
{
super(qname, value);
}
#Override
public void setAttribute(String namespace, String localName, String value)
{
if (!Constants.ATTR_MUST_UNDERSTAND.equals(localName))
{ // Or any other attribute name you'd want to avoid
super.setAttribute(namespace, localName, value);
}
}
}
Create header element like this:
ADESHeaderElement custheader = new ADESHeaderElement(qname, clientserv);
custheader.setActor(null);
When you create your service from classess generated by cxf, add custom interceptor
Service service = new MyService(wsdlURL, new QName("http://myservice.com/MyService/", "MyService"));
MyPort port = service.getMyPort();
Client client = ClientProxy.getClient(port);
// adding interceptor programmatically
client.getOutInterceptors().add(new MyHeaderHandler());
Your can extend AbstractSoapInterceptor to implement your custom interceptor to handle message.
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.headers.Header;
import org.apache.cxf.jaxb.JAXBDataBinding;
import org.apache.cxf.phase.Phase;
import com.rpc.core.utils.DomainContext;
public class MyHeaderHandler extends AbstractSoapInterceptor {
/**
* Constructor
*/
public MyHeaderHandler() {
super(Phase.PRE_LOGICAL);
}
#Override
public void handleMessage(org.apache.cxf.binding.soap.SoapMessage message) throws org.apache.cxf.interceptor.Fault {
try {
message.getHeaders().add(new Header(new QName("MyCustomHeader"),"value", new JAXBDataBinding(String.class)));
} catch (JAXBException e) {
e.printStackTrace();
}
};
}
}
Yes, I did the same that Rangappa Tungal, following this example:
Service w = new ServiceLocator();
ServiceSoap ws = new ServiceSoapStub(new URL(w.getServiceSoapAddress()),w); Stub mystub = (Stub) ws;
AuthHeader up = new AuthHeader("user","pass");
mystub.setHeader("namespace", "AuthHeader", up);
ws.get***();
Link to the example!

Categories