I have converted WSDL to Java classes, however, I needed to use binding file and suffix added to resolve conflicts. I received classes successfully however with slightly changed type names. When I try to create WebService using JaxWsProxyFactoryBean then I put URL of origin WSDL that has origin names and it gives errors like this:
ERROR 6792 --- [nio-5500-exec-1] o.a.c.w.s.f.ReflectionServiceFactoryBean : Schema element {http://tempuri.org/}SearchMagistratesCourtRequest references undefined type SearchMagistratesCourtRequest for service {http://tempuri.org/}WebServiceService.
And that it right because my generated class has the name "SearchMagistratesCourtRequestType"- with "Type" at the end.
So my binding file used the following customization:
<jaxb:bindings schemaLocation="../xsd/egrul.xsd">
<jaxb:schemaBindings>
<jaxb:package name="ru.spi2.javaee.custom.pravoru.classes.egrul"/>
<jaxb:nameXmlTransform>
<jaxb:typeName suffix="Type"/>
<jaxb:elementName suffix="Element"/> //this one is not needed actually
</jaxb:nameXmlTransform>
</jaxb:schemaBindings>
</jaxb:bindings>
Here was used the suffix.
I create my WebService like this:
JaxWsProxyFactoryBean portFactory = new JaxWsProxyFactoryBean();
portFactory.setAddress(WSDL_URL);
portFactory.setServiceClass(WebService.class);
webService = (WebService) portFactory.create();
Here I put origin WSDL_URL and receive the described errors.
How can I take into account here the binding customization that was used to generate Java classes? Or what might be a solution?
This issue was resolved by another initialization of the service. The binding works properly. So the correct working initialization of SOAP service in my case is the following:
private void initiateService() throws Exception{
WebService_Service webService_service = new WebService_Service();
webService_service.setHandlerResolver(new HandlerResolver() {
@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> handlerList = new ArrayList<>();
SOAPLoggingHandler soapLoggingHandler = new SOAPLoggingHandler();
handlerList.add(soapLoggingHandler);
return handlerList;
}
});
webServicePort = webService_service.getPort(WebService.class);
Client client = ClientProxy.getClient(webServicePort);
Endpoint cxfEndpoint = client.getEndpoint();
Map<String, Object> props = ((BindingProvider) webServicePort).getRequestContext();
props.put("ws-security.username", PravoRuConstants.USERNAME);
props.put("ws-security.password", PravoRuConstants.PASSWORD);
props.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
props.put(WSHandlerConstants.USER, PravoRuConstants.USERNAME);
props.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
props.put(WSHandlerConstants.PW_CALLBACK_CLASS, PravoRuPasswordHandler.class.getName());
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(props);
cxfEndpoint.getOutInterceptors().add(wssOut);
}
SOAPLoggingHandler:
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import java.io.IOException;
import java.util.Set;
public class SOAPLoggingHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public Set<QName> getHeaders() {
return null;
}
@Override
public boolean handleMessage(SOAPMessageContext context) {
SOAPMessage message= context.getMessage();
boolean isOutboundMessage = (Boolean)context.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if(isOutboundMessage){
System.out.println("OUTBOUND MESSAGE\n");
}else{
System.out.println("INBOUND MESSAGE\n");
}
try {
message.writeTo(System.out);
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
SOAPMessage message= context.getMessage();
try {
message.writeTo(System.out);
} catch (SOAPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
@Override
public void close(MessageContext context) { }
}
PravoRuPasswordHandler:
import com.kirillch.constants.PravoRuConstants;
import org.apache.wss4j.common.ext.WSPasswordCallback;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import java.io.IOException;
public class PravoRuPasswordHandler implements CallbackHandler {
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
pc.setPassword(PravoRuConstants.PASSWORD);
// for(int i=0; i<callbacks.length; i++) {
// WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
// if(pc.getIdentifier().equals(PravoRuConstants.USERNAME)){
// pc.setPassword(PravoRuConstants.PASSWORD);
// return;
// }
// }
}
}