Solved

Authenticator.setDefault( new MyAuthenticator()) not working

Posted on 2004-03-30
5
1,948 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This video teaches viewers about errors in exception handling.

705 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