1 /* 2 * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/examples/InteractiveAuthenticationExample.java,v 1.2 2004/02/22 18:08:45 olegk Exp $ 3 * $Revision: 480424 $ 4 * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $ 5 * ==================================================================== 6 * 7 * Licensed to the Apache Software Foundation (ASF) under one or more 8 * contributor license agreements. See the NOTICE file distributed with 9 * this work for additional information regarding copyright ownership. 10 * The ASF licenses this file to You under the Apache License, Version 2.0 11 * (the "License"); you may not use this file except in compliance with 12 * the License. You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, software 17 * distributed under the License is distributed on an "AS IS" BASIS, 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 * See the License for the specific language governing permissions and 20 * limitations under the License. 21 * ==================================================================== 22 * 23 * This software consists of voluntary contributions made by many 24 * individuals on behalf of the Apache Software Foundation. For more 25 * information on the Apache Software Foundation, please see 26 * <http://www.apache.org/>. 27 * 28 * [Additional notices, if required by prior licensing conditions] 29 * 30 */ 31 32 import java.io.BufferedReader; 33 import java.io.IOException; 34 import java.io.InputStreamReader; 35 36 import org.apache.commons.httpclient.Credentials; 37 import org.apache.commons.httpclient.HttpClient; 38 import org.apache.commons.httpclient.NTCredentials; 39 import org.apache.commons.httpclient.UsernamePasswordCredentials; 40 import org.apache.commons.httpclient.auth.AuthScheme; 41 import org.apache.commons.httpclient.auth.CredentialsProvider; 42 import org.apache.commons.httpclient.auth.CredentialsNotAvailableException; 43 import org.apache.commons.httpclient.auth.NTLMScheme; 44 import org.apache.commons.httpclient.auth.RFC2617Scheme; 45 import org.apache.commons.httpclient.methods.GetMethod; 46 47 /** 48 * A simple example that uses HttpClient to perform interactive 49 * authentication. 50 * 51 * @author Oleg Kalnichevski 52 */ 53 public class InteractiveAuthenticationExample { 54 55 /** 56 * Constructor for InteractiveAuthenticationExample. 57 */ InteractiveAuthenticationExample()58 public InteractiveAuthenticationExample() { 59 super(); 60 } 61 main(String[] args)62 public static void main(String[] args) throws Exception { 63 64 InteractiveAuthenticationExample demo = new InteractiveAuthenticationExample(); 65 demo.doDemo(); 66 } 67 doDemo()68 private void doDemo() throws IOException { 69 70 HttpClient client = new HttpClient(); 71 client.getParams().setParameter( 72 CredentialsProvider.PROVIDER, new ConsoleAuthPrompter()); 73 GetMethod httpget = new GetMethod("http://target-host/requires-auth.html"); 74 httpget.setDoAuthentication(true); 75 try { 76 // execute the GET 77 int status = client.executeMethod(httpget); 78 // print the status and response 79 System.out.println(httpget.getStatusLine().toString()); 80 System.out.println(httpget.getResponseBodyAsString()); 81 } finally { 82 // release any connection resources used by the method 83 httpget.releaseConnection(); 84 } 85 } 86 87 public class ConsoleAuthPrompter implements CredentialsProvider { 88 89 private BufferedReader in = null; ConsoleAuthPrompter()90 public ConsoleAuthPrompter() { 91 super(); 92 this.in = new BufferedReader(new InputStreamReader(System.in)); 93 } 94 readConsole()95 private String readConsole() throws IOException { 96 return this.in.readLine(); 97 } 98 getCredentials( final AuthScheme authscheme, final String host, int port, boolean proxy)99 public Credentials getCredentials( 100 final AuthScheme authscheme, 101 final String host, 102 int port, 103 boolean proxy) 104 throws CredentialsNotAvailableException 105 { 106 if (authscheme == null) { 107 return null; 108 } 109 try{ 110 if (authscheme instanceof NTLMScheme) { 111 System.out.println(host + ":" + port + " requires Windows authentication"); 112 System.out.print("Enter domain: "); 113 String domain = readConsole(); 114 System.out.print("Enter username: "); 115 String user = readConsole(); 116 System.out.print("Enter password: "); 117 String password = readConsole(); 118 return new NTCredentials(user, password, host, domain); 119 } else 120 if (authscheme instanceof RFC2617Scheme) { 121 System.out.println(host + ":" + port + " requires authentication with the realm '" 122 + authscheme.getRealm() + "'"); 123 System.out.print("Enter username: "); 124 String user = readConsole(); 125 System.out.print("Enter password: "); 126 String password = readConsole(); 127 return new UsernamePasswordCredentials(user, password); 128 } else { 129 throw new CredentialsNotAvailableException("Unsupported authentication scheme: " + 130 authscheme.getSchemeName()); 131 } 132 } catch (IOException e) { 133 throw new CredentialsNotAvailableException(e.getMessage(), e); 134 } 135 } 136 } 137 } 138