Solved

Authenticator.setDefault( new MyAuthenticator()) not working

Posted on 2004-03-30
5
1,877 Views
Last Modified: 2008-03-03
Hi,

I have written a simple web app that calls a web page on another web server that returns html.  I have extended the Authenticator class as follows and I set it as the default in the init() method.


public class MyAuthenticator extends Authenticator
{
  public MyAuthenticator()
  {
    System.out.println("New MyAuthenticator");
  }

  protected PasswordAuthentication getPasswordAuthentication()
  {
    System.out.println("Obtaining Authentication data");
    return new PasswordAuthentication("EcoRobotic", "tester01".toCharArray());
  }
}


public void init() throws ServletException
{
    Authenticator.setDefault(new MyAuthenticator());
}


The actual HttpURLCOnnection is obtained as follows.

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    try
    {
      //Obtain the connection
      URL url = new URL("http://url-of-web-page");
      HttpURLConnection connection = (HttpURLConnection)url.openConnection();

      //Read the data from the connection and dump it out
      InputStreamReader is = new InputStreamReader(connection.getInputStream());
      BufferedReader in = new BufferedReader(is);

      String inputLine;
      while ((inputLine = in.readLine()) != null)
      {
        System.out.println(inputLine);
      }
    }
    catch(Exception e)
    {
      System.out.println(e.toString());
    }
}


When I call the page from the browser I get the following output.

java.io.FileNotFoundException: Response: '401: Unauthorized xxx' for url: 'http: <url name>"

What is really strange is that the System.out.println() declared in the getPasswordAuthentication() method of MyAuthenticator is never called.


Any ideas, as I have no idea why this is failing.  If I call a non Authenticated URL it works fine.

Thanks
0
Comment
Question by:flanagaj
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10713742
Try recycling the container to make sure init's getting called
0
 

Author Comment

by:flanagaj
ID: 10713819
Have tried that, makes no difference.  I have got it to work by setting a Header Request Param

      URL url = new URL("http://<lan url>");
      String userPassword = "username:password";

     // Encode String
      String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());

     // Need to work with URLConnection to set request property
      URLConnection uc = url.openConnection();
      uc.setRequestProperty("Authorization", "Basic " + encoding);
      InputStream content = (InputStream)uc.getInputStream();
      BufferedReader in   = new BufferedReader (new InputStreamReader(content));
     
      String line;
      while ((line = in.readLine()) != null)
      {
        System.out.println(line);
      }

This worked first time.  I wonder if it something to do with the config of BEA Weblogic.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10713965
>>wonder if it something to do with the config of BEA Weblogic.

It's possible yes. If you're OK now, please feel free to get the q deleted
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 10908983
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows 10 IE Certificate Issue 10 51
Way to decrease size of apk file 9 86
hibernate example issues from command prompt 10 50
iterator/ListIterator approach 17 39
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question