GetAuthInfo.java
Upload User: demmber
Upload Date: 2007-12-22
Package Size: 717k
Code Size: 3k
Category:

Java Develop

Development Platform:

Java

  1. import HTTPClient.HTTPConnection;
  2. import HTTPClient.HTTPResponse;
  3. import HTTPClient.URI;
  4. import HTTPClient.NVPair;
  5. import HTTPClient.AuthorizationInfo;
  6. import HTTPClient.DefaultAuthHandler;
  7. import HTTPClient.AuthorizationPrompter;
  8. public class GetAuthInfo
  9. {
  10.     public static void main(String args[])  throws Exception
  11.     {
  12. String pa_name = null, pa_pass = null;
  13. if (args.length == 4  &&  "-proxy_auth".startsWith(args[0]))
  14. {
  15.     pa_name = args[1];
  16.     pa_pass = args[2];
  17.     String[] tmp = { args[3] };
  18.     args = tmp;
  19. }
  20. if (args.length != 1  ||  args[0].equalsIgnoreCase("-help"))
  21. {
  22.     System.err.println("Usage: java GetAuthInfo [-proxy_auth <username> <password>] <url>");
  23.     System.exit(1);
  24. }
  25. URI url = new URI(args[0]);
  26. DefaultAuthHandler.setAuthorizationPrompter(new MyAuthPrompter(pa_name, pa_pass));
  27. HTTPConnection con = new HTTPConnection(url);
  28. HTTPResponse   rsp = con.Head(url.getPathAndQuery());
  29. int sts = rsp.getStatusCode();
  30. if (sts < 300)
  31.     System.out.println("No authorization required to access " + url);
  32. else if (sts >= 400  &&  sts != 401  &&  sts != 407)
  33.     System.out.println("Error trying to access " + url + ":n" + rsp);
  34.     }
  35. }
  36. class MyAuthPrompter implements AuthorizationPrompter
  37. {
  38.     private String  pa_name, pa_pass;
  39.     private boolean been_here = false;
  40.     MyAuthPrompter(String pa_name, String pa_pass)
  41.     {
  42. this.pa_name = pa_name;
  43. this.pa_pass = pa_pass;
  44.     }
  45.     public NVPair getUsernamePassword(AuthorizationInfo challenge, boolean forProxy) {
  46. if (forProxy  &&  pa_name != null)
  47. {
  48.     if (been_here)
  49.     {
  50. System.out.println();
  51. System.out.println("Proxy authorization failed");
  52. return null;
  53.     }
  54.     been_here = true;
  55.     return new NVPair(pa_name, pa_pass);
  56. }
  57. if (been_here)
  58. {
  59.     System.out.println();
  60.     System.out.println("Proxy authorization succeeded");
  61. }
  62. // print out all challenge info
  63. System.out.println();
  64. if (forProxy)
  65.     System.out.println("The proxy requires authorization");
  66. else
  67.     System.out.println("The server requires authorization for this resource");
  68. System.out.println();
  69. System.out.println("Scheme: " + challenge.getScheme());
  70. System.out.println("Realm:  " + challenge.getRealm());
  71. System.out.println();
  72. System.out.println("Add the following line near the beginning of your application:");
  73. System.out.println();
  74. if (challenge.getScheme().equalsIgnoreCase("Basic"))
  75.     System.out.println("    AuthorizationInfo.addBasicAuthorization(""+
  76.        challenge.getHost() + "", " +
  77.        challenge.getPort() + ", "" +
  78.        challenge.getRealm() + "", " +
  79.        "<username>, <password>);");
  80. else if (challenge.getScheme().equalsIgnoreCase("Digest"))
  81.     System.out.println("    AuthorizationInfo.addDigestAuthorization("" +
  82.        challenge.getHost() + "", " +
  83.        challenge.getPort() + ", "" +
  84.        challenge.getRealm() + "", " +
  85.        "<username>, <password>);");
  86. else
  87.     System.out.println("    AuthorizationInfo.addAuthorization("" +
  88.        challenge.getHost() + "", " +
  89.        challenge.getPort() + ", "" +
  90.        challenge.getScheme() + "", "" +
  91.        challenge.getRealm() + "", " +
  92.        "...);");
  93. System.out.println();
  94. return null;
  95.     }
  96. }