Search code examples
javaweb-servicessoapwsdljax-ws

JAX-WS SOAP Client Authentication


I am hoping you can help me.

I am trying to create a Java SOAP client using JAX-WS.

I have imported all of the Web Service functions using wsimport, for this I had to supply an auth file using -Xauthfile, the auth file contained the following:

http://username:password@server.com:80/Windchill/servlet/MathService?wsdl

I have imported all of the classes into eclipse and I am trying to call the MathService add function using the following code:

import javax.xml.ws.BindingProvider;
import com.ptc.jws.service.org.myorg.mathservice.MathServiceImpl;
import com.ptc.jws.service.org.myorg.mathservice.MathServiceImplService;

public class ClientStart {

public static void main(String[] args) throws Exception {

 MathServiceImplService service = new MathServiceImplService();
 MathServiceImpl port = service.getMathServiceImplPort();

 // Configure service endpoint (override defined one of the WSDL) 
 BindingProvider binding = (BindingProvider) port;
 binding.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://server.com/Windchill/servlet/MathService");


 // Add HTTP Basic Authentification credentials to this request      
 binding.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "username");   
 binding.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "password");
 port.add(1, 1);

}

}

I have tried with/without overriding the service endpoint

When I run the code I get the following error:

Exception in thread "main" javax.xml.ws.WebServiceException: Failed to  access the WSDL at: http://server.com/Windchill/servlet/MathService?wsdl. It failed with: 
Got Server returned HTTP response code: 401 for URL: http://server.com/Windchill/servlet/MathService?wsdl while opening stream from http://server.com/Windchill/servlet/MathService?wsdl.

I also get this error:

WARNING: WSP0075: Policy assertion "{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}EncryptedParts" was evaluated as "UNKNOWN".
Mar 01, 2016 4:29:13 PM     [com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector]  selectAlternatives

The MathServiceImpl.java file is as follows:

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.4-b01
 * Generated source version: 2.2
 * 
 */
@WebServiceClient(name = "MathServiceImplService", targetNamespace = "http://MathService.myorg.org.service.jws.ptc.com/", wsdlLocation = "http://server.com/Windchill/servlet/MathService?wsdl")
public class MathServiceImplService extends Service
{

private final static URL MATHSERVICEIMPLSERVICE_WSDL_LOCATION;
private final static WebServiceException MATHSERVICEIMPLSERVICE_EXCEPTION;
private final static QName MATHSERVICEIMPLSERVICE_QNAME = new QName("http://MathService.myorg.org.service.jws.ptc.com/", "MathServiceImplService");

static {
    URL url = null;
    WebServiceException e = null;
    try {
        url = new URL("http://server.com/Windchill/servlet/MathService?wsdl");
    } catch (MalformedURLException ex) {
        e = new WebServiceException(ex);
    }
    MATHSERVICEIMPLSERVICE_WSDL_LOCATION = url;
    MATHSERVICEIMPLSERVICE_EXCEPTION = e;
}

public MathServiceImplService() {
    super(__getWsdlLocation(), MATHSERVICEIMPLSERVICE_QNAME);
}

public MathServiceImplService(WebServiceFeature... features) {
    super(__getWsdlLocation(), MATHSERVICEIMPLSERVICE_QNAME, features);
}

public MathServiceImplService(URL wsdlLocation) {
    super(wsdlLocation, MATHSERVICEIMPLSERVICE_QNAME);
}

public MathServiceImplService(URL wsdlLocation, WebServiceFeature... features) {
    super(wsdlLocation, MATHSERVICEIMPLSERVICE_QNAME, features);
}

public MathServiceImplService(URL wsdlLocation, QName serviceName) {
    super(wsdlLocation, serviceName);
}

public MathServiceImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
    super(wsdlLocation, serviceName, features);
}

/**
 * 
 * @return
 *     returns MathServiceImpl
 */
@WebEndpoint(name = "MathServiceImplPort")
public MathServiceImpl getMathServiceImplPort() {
    return super.getPort(new QName("http://MathService.myorg.org.service.jws.ptc.com/", "MathServiceImplPort"), MathServiceImpl.class);
}

/**
 * 
 * @param features
 *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
 * @return
 *     returns MathServiceImpl
 */
@WebEndpoint(name = "MathServiceImplPort")
public MathServiceImpl getMathServiceImplPort(WebServiceFeature... features) {
    return super.getPort(new QName("http://MathService.myorg.org.service.jws.ptc.com/", "MathServiceImplPort"), MathServiceImpl.class, features);
}

private static URL __getWsdlLocation() {
    if (MATHSERVICEIMPLSERVICE_EXCEPTION!= null) {
        throw MATHSERVICEIMPLSERVICE_EXCEPTION;
    }
    return MATHSERVICEIMPLSERVICE_WSDL_LOCATION;
}

}

Any help would be greatly appreciated! Thank you!

Tim


Solution

  • The 401 HTTP response seems to come from the application that serves the WSDL definition itself. I think it's best (when possible) to download the WSDL and bundle it in the application resources.

    Then you have to change the URL in the JAX-WS generated file (likely MathServiceImplService.java) from http://server.com/xxx.wsdl to some URL like

    MathServiceImplService.class.getResource("/path/to/wsdl.wsdl");