Verdi Verdi - 3 months ago 37
Java Question

JAX-WS WebService Client - "Response: '401: Unauthorized' for url"

I'm trying to instantiate a Web Service Client in Java, which its url is secured by username and password. When instantiating it, throws a WebServiceException; here is the stacktrace:

javax.xml.ws.WebServiceException: java.io.FileNotFoundException: Response: '401: Unauthorized' for url: WSDLURL
at com.sun.xml.ws.wsdl.WSDLContext.<init>(WSDLContext.java:68)
at com.sun.xml.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:207)
at com.sun.xml.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:165)
at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:49)
at weblogic.wsee.jaxws.spi.WLSProvider.createServiceDelegate(WLSProvider.java:18)
at javax.xml.ws.Service.<init>(Service.java:57)
at org.openuri.ServiceUtilityWS.<init>(ServiceUtilityWS.java:36)
at it.cartasi.pb.giustificatomotivo.util.Util.sendEmail(Util.java:110)
at it.cartasi.pb.giustificatomotivo.action.ConfermaAction.execute(ConfermaAction.java:36)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:226)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:124)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at it.cartasi.pb.giustificatomotivo.filter.SessionFilter.doFilter(SessionFilter.java:68)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(Unknown Source)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2140)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2046)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1398)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:200)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:172)
java.io.FileNotFoundException: Response: '401: Unauthorized' for url: WSDLURL
at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:467)
at weblogic.net.http.SOAPHttpURLConnection.getInputStream(SOAPHttpURLConnection.java:36)
at java.net.URL.openStream(URL.java:1007)
at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:666)
at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parseWSDL(RuntimeWSDLParser.java:152)
at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:99)
at com.sun.xml.ws.wsdl.WSDLContext.<init>(WSDLContext.java:65)
at com.sun.xml.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:207)
at com.sun.xml.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:165)
at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:49)
at weblogic.wsee.jaxws.spi.WLSProvider.createServiceDelegate(WLSProvider.java:18)
at javax.xml.ws.Service.<init>(Service.java:57)
at org.openuri.ServiceUtilityWS.<init>(ServiceUtilityWS.java:36)
at it.cartasi.pb.giustificatomotivo.util.Util.sendEmail(Util.java:110)
at it.cartasi.pb.giustificatomotivo.action.ConfermaAction.execute(ConfermaAction.java:36)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:226)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:124)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at it.cartasi.pb.giustificatomotivo.filter.SessionFilter.doFilter(SessionFilter.java:68)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(Unknown Source)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2140)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2046)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1398)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:200)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:172)


Here my code snippet (I also set the Default Authenticator):

Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
char[] charArray = password.toCharArray();
System.out.println(user);
System.out.println(charArray);
return new PasswordAuthentication(
user,
charArray);
}
});

try {
ws = new ServiceUtilityWS(new URL(wsdlUrl), new QName("ServiceUtilityWSSoap")); // Exception thrown here
} catch (MalformedURLException e) {
}


I can access it via browser using those credentials. Does anyone know why is this happening?

Note:

The application is deployed on Weblogic 10. If I run that code from main the exception is not thrown.

Answer

It was a Weblogic 10 problem due to its URLStreamHandler. I solved it by doing the following:

  • Instantiated the WSDL Url like this:

    URLStreamHandler handler = new sun.net.www.protocol.http.Handler(); //standard http handler
    URL url = null;
    try {
        url = new URL(null, wsdlUrl, handler); //forced this http handler here
        ws = ServiceUtilityWS.create(url, qName);
    } catch (MalformedURLException e) {
    }
    
  • After solving that, I had a similar exception on the service operation call. To solve that, I added a custom Handler to the SOAP Message chain:

         Binding aBinding = bindingProvider.getBinding();
         List<Handler> handlerChain = aBinding.getHandlerChain();
         handlerChain.add(new SOAPHandler<SOAPMessageContext>() {
    
           public Set<QName> getHeaders() {
             return new TreeSet<QName>();
           }
    
           public boolean handleMessage(SOAPMessageContext context) {
            final Boolean outInd = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    
            if (outInd.booleanValue()) {
                try {
    
                    context.put(BindingProvider.USERNAME_PROPERTY, user);
                    context.put(BindingProvider.PASSWORD_PROPERTY, password);
    
                } catch (final Exception e) {
                    return false;
                }
            }
    
            return true;
        }