1

I am trying to get a file in java code as stream. The URI is returning a PDF file. Below is my code snippet :

public void downloadToLocal() {

    try{
        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("test",
                        "test".toCharArray());
            }
        });
        URL url = new URL("http://***");

        /*String encodedAuthorizedUser = new String(com.sun.jersey.core.util.Base64.encode("***" + ":" + "***"));
        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
        httpCon.setRequestProperty("Authorization", "Basic " + encodedAuthorizedUser);*/

        InputStream is = url.openStream();
        FileOutputStream fos = new FileOutputStream("C:\\db\\test.pdf");

        byte[] buffer = new byte[1024];
        int noOfBytes = 0;

        while ((noOfBytes = is.read(buffer)) != -1) {
            fos.write(buffer, 0, noOfBytes);
        }

        fos.close();
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Below is the exception I am getting :

java.io.FileNotFoundException: Response: '401: Unauthorized' for url: '*********'
    at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:487)
    at weblogic.net.http.SOAPHttpURLConnection.getInputStream(SOAPHttpURLConnection.java:37)
    at java.net.URL.openStream(URL.java:1010)
    at com.bt.ngwfmt.framework.services.dlite.DliteServiceClient.downloadToLocal(DliteServiceClient.java:882)
    at com.bt.ngwfmt.framework.rest.DliteRestHandler.persistDCV(DliteRestHandler.java:167)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:183)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3717)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)

From here I am taking reference. I tried both method from there. I found a similar issue here also. But couldn't get what I need to do to skip this password authentication. This is working when I am hitting url in browser and providing username and password manually.

Any help would be highly appreciated. :)

Note : I am using weblogic 10.3.5. Thanks

Community
  • 1
  • 1

2 Answers2

2

It looks like Weblogic-specific issue with default authenticator
Try to explicitly set the auth header:

URLConnection conn = url.openConnection();
String creds = user + ":" + pass;
String encodedCreds = "Basic " + new String(new Base64().encode(creds.getBytes()));
conn.setRequestProperty ("Authorization", encodedCreds);
InputStream in = conn.getInputStream();
Denys Kurochkin
  • 1,360
  • 1
  • 18
  • 34
  • Another way is to use custom handler as it proposed here - https://community.oracle.com/thread/1668714?start=0 – Denys Kurochkin Aug 25 '16 at 11:10
  • If your username and password don't contain s[ecial symbols you can specify creds just in the URL - http://user:pass@domain.com/url Or use third-party http client, apache for example – Denys Kurochkin Aug 25 '16 at 11:33
1

This is because WebLogic uses its own implementation of HttpURLConnection and has got some problem with that.

So another solution is to force Weblogic to use standard Sun’s HttpURLConnection
Set the option UseSunHttpHandler to true

Source - https://pravejay.wordpress.com/2012/09/21/weblogic-javax-xml-ws-webserviceexception-failed-to-access-the-wsdl-response-401-unauthorized/

Denys Kurochkin
  • 1,360
  • 1
  • 18
  • 34