I am trying to change prefix for soap envelope in response of web-service from
S="http://schemas.xmlsoap.org/soap/envelope/"
to
soap="http://schemas.xmlsoap.org/soap/envelope/":
so here's how the response looks like now:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<n:soaprequestResponse xmlns:n="http://tempuri.org/soaprequest">
<n:soaprequestResult/>
</n:soaprequestResponse>
</S:Body>
</S:Envelope>
and here's how it must look like:
<soap:Envelope xmlns:soap:="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<n:soaprequestResponse xmlns:n="http://tempuri.org/soaprequest">
<n:soaprequestResult/>
</n:soaprequestResponse>
</soap:Body>
</soap:Envelope>
How can this be attained?
EDIT:
I added soap handler class and the problem starts when I'm trying to get envelope:
package org.tempuri.soaprequest;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class SoapHandler implements SOAPHandler<SOAPMessageContext> {
#Override
public Set<QName> getHeaders() {
//do nothing
return null;
}
#Override
public boolean handleMessage(SOAPMessageContext context) {
if ((boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) { //Check here that the message being intercepted is an outbound message from your service, otherwise ignore.
try {
SOAPEnvelope msg = context.getMessage().getSOAPPart().getEnvelope(); //just trying to get envelope
} catch (SOAPException ex) {
ex.printStackTrace();
}
return true; //indicates to the context to proceed with (normal)message processing
}
#Override
public boolean handleFault(SOAPMessageContext context) {
//do nothing
return true;
}
#Override
public void close(MessageContext context) {
//do nothing
}
}
SoapUI throws:
<S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
<faultcode>S:Server</faultcode>
<faultstring>JVMVRFY012 stack shapeinconsistent;class=com/sun/xml/messaging/saaj/soap/SOAPDocumentImpl, method=createDocumentFragment()Lorg/w3c/dom/DocumentFragment;, pc=5
</faultstring>
</S:Fault>
Tomcat log has no errors.
It doesn't occur without custom soap handler.
Perhaps the reason is in the way I implemented web method. It creates a new thread with an object that processes request and then returns empty response, thus releasing client from waiting for request processing is over:
#WebResult(name="soaprequestResult", targetNamespace="http://tempuri.org/soaprequest")
public SoaprequestResponse.SoaprequestResult soaprequest(#WebParam(name="streams", targetNamespace="http://tempuri.org/soaprequest") SoaprequestStreams streams) {
try {
new Thread(new MyProcess(streams)).start();
return new SoaprequestResponse().getSoaprequestResult();
} catch(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String stackTrace = sw.toString();
return new SoaprequestResponse().getSoaprequestResult();
}
}
and MyProcess is class where request processing really is and stmt.executeUpdate is executed.
I think Customising JAX-WS prefix of a SOAP response summarizes your options.
Option 1: I think you just need to put this above your package.
#javax.xml.bind.annotation.XmlSchema(namespace = "http://schemas.xmlsoap.org/soap/envelope/",
xmlns = {
#javax.xml.bind.annotation.XmlNs(prefix = "soap",
namespaceURI="http://schemas.xmlsoap.org/soap/envelope/")
}
)
Option 2: Alternatively (also mentioned in the link), you could use a SOAPHandler. You could add a configuration file to bind these handlers. But in fact, you can just add them at runtime. I think this needs some explanation: The trick is to get an instance of the BindingProvider. This is different if you are a consumer or provider of the webservice.
If you are server (i.e. providing a webservice):
webservice = Class.forName(serviceClassName).newInstance();
Endpoint e = Endpoint.create(webservice);
BindingProvider bp = (BindingProvider)e.getBinding();
e.publish("http://localhost:" + serverPort + servicePath);
If you are client (i.e. consuming a webservice):
Service service = new Service(url, qname);
Port port = service.getPort();
BindingProvider bp = ((BindingProvider) port);
When you have the bindingprovider, you can bind the handler as follows.
List<Handler> chain = bp.getHandlerChain();
if (chain == null) chain = new ArrayList<Handler>();
chain.add(myCustomHandler);
bp.setHandlerChain(chain);
Now, for the chained handler itself, you should implement SOAPHandler<SOAPMessageContext>. There you can manipulate your messages however you like. (see linked post above for an example).
Related
I'm relative new to the webservices world and my research seems to have confused me more than enlighten me, my problem is that I was given a library(jar) which I have to extend with some webservice functionality.
This library will be shared to other developers, and among the classes in the jar will be classes that have a method which calls a webservice (that essentially sets an attribute of the class, does some business logic, like storing the object in a db, etc and sends back the object with those modifications). I want to make the call to this service as simple as possible, hopefully as simple so that the developer using the class only need to do.
Car c = new Car("Blue");
c.webmethod();
I have been studying JAX-WS to use on the server but seems to me that I don't need to create a wsimport in the server nor the wsimport on the client, since I know that both have the classes, I just need some interaction between classes shared in both the server and the client. How do you think makes sense to do the webservice and the call in the class?
I understand your problem boils down to how to call a SOAP (JAX-WS) web service from Java and get its returning object. In that case, you have two possible approaches:
Generate the Java classes through wsimport and use them; or
Create a SOAP client that:
Serializes the service's parameters to XML;
Calls the web method through HTTP manipulation; and
Parse the returning XML response back into an object.
About the first approach (using wsimport):
I see you already have the services' (entities or other) business classes, and it's a fact that the wsimport generates a whole new set of classes (that are somehow duplicates of the classes you already have).
I'm afraid, though, in this scenario, you can only either:
Adapt (edit) the wsimport generated code to make it use your business classes (this is difficult and somehow not worth it - bear in mind everytime the WSDL changes, you'll have to regenerate and readapt the code); or
Give up and use the wsimport generated classes. (In this solution, you business code could "use" the generated classes as a service from another architectural layer.)
About the second approach (create your custom SOAP client):
In order to implement the second approach, you'll have to:
Make the call:
Use the SAAJ (SOAP with Attachments API for Java) framework (see below, it's shipped with Java SE 1.6 or above) to make the calls; or
You can also do it through java.net.HttpUrlconnection (and some java.io handling).
Turn the objects into and back from XML:
Use an OXM (Object to XML Mapping) framework such as JAXB to serialize/deserialize the XML from/into objects
Or, if you must, manually create/parse the XML (this can be the best solution if the received object is only a little bit differente from the sent one).
Creating a SOAP client using classic java.net.HttpUrlConnection is not that hard (but not that simple either), and you can find in this link a very good starting code.
I recommend you use the SAAJ framework:
SOAP with Attachments API for Java (SAAJ) is mainly used for dealing directly with SOAP Request/Response messages which happens behind the scenes in any Web Service API. It allows the developers to directly send and receive soap messages instead of using JAX-WS.
See below a working example (run it!) of a SOAP web service call using SAAJ. It calls this web service.
import javax.xml.soap.*;
public class SOAPClientSAAJ {
// SAAJ - SOAP Client Testing
public static void main(String args[]) {
/*
The example below requests from the Web Service at:
https://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit
To call other WS, change the parameters below, which are:
- the SOAP Endpoint URL (that is, where the service is responding from)
- the SOAP Action
Also change the contents of the method createSoapEnvelope() in this class. It constructs
the inner part of the SOAP envelope that is actually sent.
*/
String soapEndpointUrl = "https://www.w3schools.com/xml/tempconvert.asmx";
String soapAction = "https://www.w3schools.com/xml/CelsiusToFahrenheit";
callSoapWebService(soapEndpointUrl, soapAction);
}
private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
SOAPPart soapPart = soapMessage.getSOAPPart();
String myNamespace = "myNamespace";
String myNamespaceURI = "https://www.w3schools.com/xml/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);
/*
Constructed SOAP Request Message:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myNamespace="https://www.w3schools.com/xml/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<myNamespace:CelsiusToFahrenheit>
<myNamespace:Celsius>100</myNamespace:Celsius>
</myNamespace:CelsiusToFahrenheit>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("CelsiusToFahrenheit", myNamespace);
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Celsius", myNamespace);
soapBodyElem1.addTextNode("100");
}
private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);
// Print the SOAP Response
System.out.println("Response SOAP Message:");
soapResponse.writeTo(System.out);
System.out.println();
soapConnection.close();
} catch (Exception e) {
System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
createSoapEnvelope(soapMessage);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", soapAction);
soapMessage.saveChanges();
/* Print the request message, just for debugging purposes */
System.out.println("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println("\n");
return soapMessage;
}
}
About using JAXB for serializing/deserializing, it is very easy to find information about it. You can start here: http://www.mkyong.com/java/jaxb-hello-world-example/.
Or just use Apache CXF's wsdl2java to generate objects you can use.
It is included in the binary package you can download from their website. You can simply run a command like this:
$ ./wsdl2java -p com.mynamespace.for.the.api.objects -autoNameResolution http://www.someurl.com/DefaultWebService?wsdl
It uses the wsdl to generate objects, which you can use like this (object names are also grabbed from the wsdl, so yours will be different a little):
DefaultWebService defaultWebService = new DefaultWebService();
String res = defaultWebService.getDefaultWebServiceHttpSoap11Endpoint().login("webservice","dadsadasdasd");
System.out.println(res);
There is even a Maven plug-in which generates the sources: https://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html
Note: If you generate sources using CXF and IDEA, you might want to look at this: https://stackoverflow.com/a/46812593/840315
Might help for someone who have xml request as string.
if you have WSDL, You can create a new soap request in SoapUI with that WSDL file.
It would automatically generate the Structure/XML for input request.
Here is some simple version of Java code you can use to call Soap service if you have the input request xml from SoapUI:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class SimpleSoapClient {
public static void main(String args[]) throws IOException {
String address="Hyderabad";
/* place your xml request from soap ui below with necessary changes in parameters*/
String xml="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://www.YourUrlAsPerWsdl.com/\">\r\n" +
" <soapenv:Header/>\r\n" +
" <soapenv:Body>\r\n" +
" <ws:callRest>\r\n" +
" <name>"+"Hello"+"</name>\r\n" +
" <address>"+address+"</address>\r\n" +
" </ws:callRest>\r\n" +
" </soapenv:Body>\r\n" +
"</soapenv:Envelope>";
String responseF=callSoapService(xml);
System.out.println(responseF);
}
}
static String callSoapService(String soapRequest) {
try {
String url = "https://gogle.com/service/hello"; // replace your URL here
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// change these values as per soapui request on top left of request, click on RAW, you will find all the headers
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","text/xml; charset=utf-8");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(soapRequest);
wr.flush();
wr.close();
String responseStatus = con.getResponseMessage();
System.out.println(responseStatus);
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// You can play with response which is available as string now:
String finalvalue= response.toString();
// or you can parse/substring the required tag from response as below based your response code
finalvalue= finalvalue.substring(finalvalue.indexOf("<response>")+10,finalvalue.indexOf("</response>")); */
return finalvalue;
}
catch (Exception e) {
return e.getMessage();
}
}
}
I found a much simpler alternative way to generating soap message.
Given a Person Object:
import com.fasterxml.jackson.annotation.JsonInclude;
#JsonInclude(JsonInclude.Include.NON_NULL)
public class Person {
private String name;
private int age;
private String address; //setter and getters below
}
Below is a simple Soap Message Generator:
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
#Slf4j
public class SoapGenerator {
protected static final ObjectMapper XML_MAPPER = new XmlMapper()
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.registerModule(new JavaTimeModule());
private static final String SOAP_BODY_OPEN = "<soap:Body>";
private static final String SOAP_BODY_CLOSE = "</soap:Body>";
private static final String SOAP_ENVELOPE_OPEN = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
private static final String SOAP_ENVELOPE_CLOSE = "</soap:Envelope>";
public static String soapWrap(String xml) {
return SOAP_ENVELOPE_OPEN + SOAP_BODY_OPEN + xml + SOAP_BODY_CLOSE + SOAP_ENVELOPE_CLOSE;
}
public static String soapUnwrap(String xml) {
return StringUtils.substringBetween(xml, SOAP_BODY_OPEN, SOAP_BODY_CLOSE);
}
}
You can use by:
public static void main(String[] args) throws Exception{
Person p = new Person();
p.setName("Test");
p.setAge(12);
String xml = SoapGenerator.soapWrap(XML_MAPPER.writeValueAsString(p));
log.info("Generated String");
log.info(xml);
}
I have task where I need to transform custom SOAP message. At the beginning I have to get this custom SOAP message, then I need to transform this message using XSLT, then process this message in my WebService. The reverse process is repeated.
I have XSLT file and method for transforming SOAP message, but I don't know where I need to call this method for transforming. How do I intercept SOAP message and where I have to do it? Because I have only class with one method (example below) and i don't understand how I can transform this message before processing in webservice.
#WebService
public class Calculator {
public String showCard(final CreditCard creditCard) {
return creditCard.toString();
}
}
Here is the technical mapping what I need to do.
How do I intercept SOAP message and where I have to do it?
You need a SoapHandler to capture the soap request before the execution of the bussiness logic of the endpoint, and other SoapHandler to transform the outbound response.
Define a SoapHandler class (example extracted from mkyong)
public class CalculatorSoapHandler implements SOAPHandler<SOAPMessageContext>{
#Override
public boolean handleMessage(SOAPMessageContext context) {
Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
//for response message only, true for outbound messages, false for inbound
if(!isRequest){
try{
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
//process....
}catch(SOAPException e){
System.err.println(e);
}catch(IOException e){
System.err.println(e);
}
}
//continue other handler chain
return true;
}
#Override
public boolean handleFault(SOAPMessageContext context) {
}
#Override
public void close(MessageContext context) {
}
#Override
public Set<QName> getHeaders() {
}
}
Create a soap handler XML file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains
xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<javaee:handler-chain>
<javaee:handler>
<javaee:handler-class>com.CalculatorSoapHandler</javaee:handler-class>
</javaee:handler>
</javaee:handler-chain>
</javaee:handler-chains>
Attach SOAP Handler to Web Service using #HandlerChain
#WebService
#HandlerChain(file="handler-chain.xml")
public class Calculator {
If you use a jax-ws framework like CXF or spring-WS, check the documentation to see specific configuration
Currently, when successfully connecting to an endpoint, I am passing a custom token (called access-token) back to the client via a response header. This header is being set correctly and I can verify the header by analyzing the HTTP response.
However, when trying to get the header from the frame object the header is not set (see the JavaScript below):
stompClient.connect(headers,
function(frame) {
console.log('=========================================');
console.log(frame.headers['access-token']);
console.log(frame);
console.log('=========================================');
stompClient.subscribe('/topic/test', function(stuff){
console.debug(stuff);
});
},
function(error) {
//error code
}
);
I am setting the response header as follows on the server:
public class HttpSessionHandshakeInterceptorImpl extends HttpSessionHandshakeInterceptor {
#Override
public boolean beforeHandshake(ServerHttpRequest request,
ServerHttpResponse response, WebSocketHandler wsHandler,
Map<String, Object> attributes) throws Exception {
response.getHeaders().set("access-token", token);
return super.beforeHandshake(request, response, wsHandler, attributes);
I stripped out some code and can confirm that the interceptor is being called. I take it that this is not the correct way to pass a header value back to the client when the connect function is called? I can't seem to find any documentation on how to accomplish this. Thanks.
ServerHttpResponse response - this is the response to a HTTP request, which you can inspect in more detail (by logging or in debug mode). Setting its headers will affect the HTTP response, not the STOMP stream which flows over the WebSocket connection. The STOMP headers of the CONNECTED are set by the message broker.
I do not know it for sure but I doubt that Spring is capable of inserting extra headers in the STOMP frames. (please correct me if I am wrong)
You need to implement ChannelInterceptor and register it with output bound channel
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MultiValueMap;
#Configuration
public class WebSocketServiceCustomHeaderInterceptor implements ChannelInterceptor {
private static final Logger logger = LoggerFactory.getLogger(WebSocketServiceCustomHeaderInterceptor.class);
#Override
#Nullable
public Message<?> preSend(Message<?> message, MessageChannel channel) {
final StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(message);
final StompCommand command = headerAccessor.getCommand();
if (command != null && command == StompCommand.CONNECTED) {
final StompHeaderAccessor accessor = StompHeaderAccessor.create(command);
accessor.setSessionId(headerAccessor.getSessionId());
#SuppressWarnings("unchecked")
final MultiValueMap<String, String> nativeHeaders = (MultiValueMap<String, String>) headerAccessor
.getHeader(StompHeaderAccessor.NATIVE_HEADERS);
accessor.addNativeHeaders(nativeHeaders);
// add custom headers
try {
accessor.addNativeHeader("HOSTNAME", InetAddress.getLocalHost().getHostName());
} catch (UnknownHostException e) {
logger.error("Error getting host name ", e);
}
final Message<?> newMessage = MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
return newMessage;
}
return message;
}
}
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketBrokerConfig implements WebSocketMessageBrokerConfigurer {
#Bean
public WebSocketServiceCustomHeaderInterceptor webSocketServiceCustomHeaderInterceptor() {
return new WebSocketServiceCustomHeaderInterceptor();
}
#Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
registration.interceptors(webSocketServiceCustomHeaderInterceptor());
registration.taskExecutor().corePoolSize(outboundPoolCoreSize).maxPoolSize(outboundPoolMaxSize);
}
}
I'm afraid this is not possible without a major hack. The issue is, the headers are completely rebuilt AFTER the ChannelInterceptors are called. See method StompSubProtocolHandler.convertConnectAcktoStompConnected
The method StompSubProtocolHandler.handleMessageToClient will also add a few headers depending on the type of message, for example by calling afterStompSessionConnected which sets the header "user-name" if the authentication is done.
I've tried adding headers as well but I'm about to give up... My guess is that the people who wrote the STOMP implementation in Spring wanted to strictly respect the specifications
I am trying to use eway payment gateway. In this i am using Recurring payment. For recurring they provide WSDL file, by using Maven Generator, i was creating java classes from WSDL file. When i was trying to call services, it generate an error, because the service need authentication information in SOAP header. From this Link i found the Solution to add header in SOAP request using Jaxb Object. After that, when i call the SOAP services it generates different error, which confused me. following is my code for handle eway recurring services:
1. SOAP Handler
public class EwaySOAPHandler implements SOAPHandler<SOAPMessageContext>{
private JAXBElement<EWAYHeader> jaxbElement = null;
public EwaySOAPHandler(JAXBElement<EWAYHeader> jaxbElement) {
this.jaxbElement = jaxbElement;
}
public boolean handleMessage(SOAPMessageContext context) {
Boolean outBoundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
try{
if(outBoundProperty != null && outBoundProperty.booleanValue()){
Marshaller marshaller = JAXBContext.newInstance(EWAYHeader.class).createMarshaller();
SOAPHeader header = context.getMessage().getSOAPPart().getEnvelope().addHeader();
marshaller.marshal(jaxbElement, header);
}
return false;
}catch(Exception ex){
System.out.println("Problem In Handel Message");
ex.printStackTrace();
}
return true;
}
public boolean handleFault(SOAPMessageContext context) {
throw new UnsupportedOperationException("Not Supported Yet");
}
public void close(MessageContext context) {
}
public Set<QName> getHeaders() {
return new TreeSet<QName>();
}
}
2. SOAP Client
public class SOAPClient {
#Test
public void test() {
ManageRebill manageRebill = new ManageRebill();
ManageRebillSoap manageRebillSoap = manageRebill.getManageRebillSoap();
Binding binding = ((BindingProvider) manageRebillSoap).getBinding();
List<Handler> handlersList = new ArrayList<Handler>();
EWAYHeader header = new EWAYHeader();
header.setEWAYCustomerID("87654321");
header.setPassword("test");
header.setUsername("test#eway.com.au");
ObjectFactory factory = new ObjectFactory();
JAXBElement<EWAYHeader> jaxbElement = factory.createEWAYHeader(header);
EwaySOAPHandler ewaySOAPHandler = new EwaySOAPHandler(jaxbElement);
handlersList.add(ewaySOAPHandler);
binding.setHandlerChain(handlersList);
manageRebillSoap.createRebillCustomer("Mr", "Pritpal", "Singh",
"Mohali", "CHD", "Punjab", "netsol", "1610032", "india",
"abc#abc.com", "123456789", "123456789", "987654321", "Ref123",
"JavaEE Developer", "comments", "http://google.com");
}}
Following error is generate when i try to run SOAPClient test case:
com.sun.xml.internal.ws.streaming.XMLStreamReaderException: unexpected XML tag. expected: {http://www.eway.com.au/gateway/rebill/manageRebill}CreateRebillCustomerResponse but found: {http://www.eway.com.au/gateway/rebill/manageRebill}CreateRebillCustomer.
According to this error, in response they need CreateRebillCustomerResponse but return CreateRebillCustomer object. the problem is that, how i change the object and where these objects are define ?.
Java - JDK 1.6.0.7 - WSGEN -version: JAX-WS RI 2.2.3-b01-
I have the following problem:
SOAPBinding binding = (SOAPBinding)((BindingProvider)port).getBinding();
binding.setMTOMEnabled(true);
List<Handler> handlerChain = new ArrayList<Handler>();
handlerChain.addAll(binding.getHandlerChain());
handlerChain.add(new MyHandlerSecurity("admin", "admin"));
binding.setHandlerChain(handlerChain);
With this code the SoapHeader is correct, but the attachment is always a inline base64 text.
//List<Handler> handlerChain = new ArrayList<Handler>();
//handlerChain.addAll(binding.getHandlerChain());
//handlerChain.add(new MyHandlerSecurity("admin", "admin"));
//binding.setHandlerChain(handlerChain);
When handlerChain is commented out, you will see the attachment as an xop reference, but there is no SoapHeader and thus, the client is not authenticated...
How can I add a handler on the client side and enable MTOM correct?
Im not sure if i got the question right, but i think i had your same problem a couple of months ago, so here is my solution:
First you need a HeaderHandler class , wich creates the soap header element, it should look like this:
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {
public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
String AUTH_TK = "http://www.myurl.com:port/subdir/etc/";
String NOPREFIX="";//no prefix
String PREFIX_XMLNS="xmlns";
String value = "123456";
if (outboundProperty.booleanValue()) {
try {
SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.addHeader();
//<AuthorizationToken xmlns="http://www.myurl.com:port/subdir/etc/">
SOAPElement authorizationToken = header.addChildElement("AuthorizationToken", PREFIX_XMLNS, AUTH_TK);
//<Token>value</Token>
SOAPElement usernameToken =
authorizationToken.addChildElement("Token", NOPREFIX);
usernameToken.addTextNode(value);
//<Token>value</Token>
SOAPElement usernameToken =
authorizationToken.addChildElement("Token", PREFIX);
usernameToken.addTextNode(value);
} catch (Exception e) {
e.printStackTrace();
}
}
return outboundProperty;
}
public Set<QName> getHeaders() {
return null;
}
public void close(MessageContext arg0) {
}
public boolean handleFault(SOAPMessageContext arg0) {
return false;
}
}
After that you create a HeaderHandlerResolver to handle the header creation and insert it in a handler chain:
import java.util.ArrayList;
import java.util.List;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;
public class HeaderHandlerResolver implements HandlerResolver {
#SuppressWarnings("unchecked")
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> handlerChain = new ArrayList<Handler>();
HeaderHandler hh = new HeaderHandler();
handlerChain.add(hh);
return handlerChain;
}
}
After that, you add in the Client:
try{
//new service instance (your service should be extending javax.xml.ws.Service;)
YourServiceProxy service = new YourServiceProxy();
//calls the header handler resolver ;)
service.setHandlerResolver(new HeaderHandlerResolver());
//get the service
YourService port = (YourService)service.getYourService();
//call the service
port.yourMethod()
} catch (Exception e) {
e.printStackTrace();
}
By the way, i didn't tested this particular header, i modified a previous header handler i had, so it may be not accurate, but i think it's pretty close, i really hope it helps you, try it out and tell us how it comes, i'll try to help you if it still doesn't works.