?
Solved

Authenticator.setDefault( new MyAuthenticator()) not working

Posted on 2004-03-30
5
Medium Priority
?
1,982 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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 …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

777 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