Solved

Authenticator.setDefault( new MyAuthenticator()) not working

Posted on 2004-03-30
5
1,905 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 75
how to debug htl and js pages 8 58
Is there a simpler dropbox system? 10 44
Setup GlassFish 4 37
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

730 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