?
Solved

Java - Constructor question

Posted on 2008-11-11
10
Medium Priority
?
286 Views
Last Modified: 2013-11-23
Hi experts,
I have a constructor as shown below.
>>public static String sendRequestString(String address)
      {
          System.out.println("Inside constructor");
            Authenticator.setDefault(new MyAuthenticator());
                ........................................
                ........................................

class MyAuthenticator extends Authenticator {
    protected PasswordAuthentication getPasswordAuthentication() {
          System.out.println("Inside authenticator");
               ........................................
                ........................................

When I call the constructor (sendRequestString(String address))  for the first time, both the SOP's get printed. But for subsequent calls of this constructor, the SOP inside authenticator class does not get printed.
Why does this happen and how can I overcome this?
Any help in this regard will be well appreciated with points.

Warm Regards,
Anees


0
Comment
Question by:Sreejith22
  • 4
  • 3
  • 3
10 Comments
 
LVL 15

Expert Comment

by:quincydude
ID: 22929500
Hi,
It depends on the behaviour of Authenticator.setDefault() or MyAuthenticator() constructor. Any one of them would invoke getPasswordAuthentication() at some point. Please check your code as the information given is not enough for me.
0
 

Author Comment

by:Sreejith22
ID: 22929633
Thanks for your valuable comment.
I am attaching my java source here.
Please have a look.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import com.lynxoft.dld.utils.UtilsHTTPS;
 
public class GetAuthenticatedData
{
	public static String sendRequestString(String address)
	{
	    System.out.println("Inside constructor");
		Authenticator.setDefault(new MyAuthenticator());
	    String str = null;
	    try {
	        URL url = new URL(address);
	        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
	        
	        while (( str = in.readLine()) != null) {
	        	System.out.println("string returned by server is "+str);
	        	return str;
	        }
	        in.close();
	    } catch (MalformedURLException e) {
	    } catch (IOException e) {
	    }
	    Authenticator.setDefault(null);
		return str;
	}
}
class MyAuthenticator extends Authenticator {
    protected PasswordAuthentication getPasswordAuthentication() {
    	System.out.println("Inside authenticator");
    	System.out.println("pwdddddddddd"+UtilsHTTPS.password);
        return new PasswordAuthentication(UtilsHTTPS.username, UtilsHTTPS.password.toCharArray());
    }
}

Open in new window

0
 
LVL 15

Expert Comment

by:quincydude
ID: 22929727
The given code does not show when is  getPasswordAuthentication() called,
do you have the access to Authenticator.setDefault() method?
0
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!

 

Author Comment

by:Sreejith22
ID: 22929755
>>do you have the access to Authenticator.setDefault() method

It is the method inside  Authenticator class which is a JDK class.
0
 
LVL 15

Expert Comment

by:quincydude
ID: 22929802
Then I think this method invoke getPasswordAuthentication()  when being called.
So does it cause any problem if it's only invoked once?
0
 

Author Comment

by:Sreejith22
ID: 22929826
>>So does it cause any problem if it's only invoked once?

Yes. When I logout from the application and log in as other user, the first users authentication details persist since new MyAuthenticator() is not called.
0
 
LVL 1

Expert Comment

by:rbrindl
ID: 22930947
Are you using HTTP Protocol?
The problem might be that the authentication is only called when needed (see javadoc forjava.net.Authenticator), i.e. when the server requests authentication.
In a webbrowser, cookies or url-rewriting are used to maintain session state, I have not looked into URLConnection (or better sun.net.www.protocol.http.HTTPURLConnection). Might be, it also stores cookie in some static cache. There is no source available for the sun implementation classes, so the only way would be to decompile and look into that.

So the only way to get unauthenticated without hacking into the implementation classes is to provide a URL that does the logoff on the server side, like you would do with a web application.

BTW: There is not a single constructor in your code, just methods. This was a bit confusing.
0
 
LVL 1

Expert Comment

by:rbrindl
ID: 22930995
some more details can be found on:
http://httpd.apache.org/docs/1.3/howto/auth.html#basicworks

I was not quite correct in the last post: not the server recognizes that you are authenticated. It sends a 401 authentication request each time. But the client (in this case the HTTPURLConnection implementation class) recognizes that it is already authenticated against that realm and resends username/password (which it caches, not even the PasswordAuthentication is called a second time).
0
 
LVL 15

Accepted Solution

by:
quincydude earned 2000 total points
ID: 22936127
You better check if Authenticator.setDefault() is used correctly in the way in your code. As it's clear that this method do not necessarily call getPasswordAuthentication() everytime it's being called.
0
 
LVL 1

Expert Comment

by:rbrindl
ID: 22949074
@quincydude: I dont understand that completely, could you please explain?  What do you mean with "Authenticator.setDefault() used correctly" ?  I understand that getPasswordAuthentication() is not called each time (as i wrote above), but I don't see a relationship to Authenticator.setDefault.

@Sreejith22: does it work now? could you post the working code? i am quite interested in it!

Thank you both!
0

Featured Post

Technology Partners: 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 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