001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: import java.io.BufferedReader;
033: import java.io.IOException;
034: import java.io.InputStreamReader;
035:
036: import org.apache.commons.httpclient.Credentials;
037: import org.apache.commons.httpclient.HttpClient;
038: import org.apache.commons.httpclient.NTCredentials;
039: import org.apache.commons.httpclient.UsernamePasswordCredentials;
040: import org.apache.commons.httpclient.auth.AuthScheme;
041: import org.apache.commons.httpclient.auth.CredentialsProvider;
042: import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
043: import org.apache.commons.httpclient.auth.NTLMScheme;
044: import org.apache.commons.httpclient.auth.RFC2617Scheme;
045: import org.apache.commons.httpclient.methods.GetMethod;
046:
047: /**
048: * A simple example that uses HttpClient to perform interactive
049: * authentication.
050: *
051: * @author Oleg Kalnichevski
052: */
053: public class InteractiveAuthenticationExample {
054:
055: /**
056: * Constructor for InteractiveAuthenticationExample.
057: */
058: public InteractiveAuthenticationExample() {
059: super ();
060: }
061:
062: public static void main(String[] args) throws Exception {
063:
064: InteractiveAuthenticationExample demo = new InteractiveAuthenticationExample();
065: demo.doDemo();
066: }
067:
068: private void doDemo() throws IOException {
069:
070: HttpClient client = new HttpClient();
071: client.getParams().setParameter(CredentialsProvider.PROVIDER,
072: new ConsoleAuthPrompter());
073: GetMethod httpget = new GetMethod(
074: "http://target-host/requires-auth.html");
075: httpget.setDoAuthentication(true);
076: try {
077: // execute the GET
078: int status = client.executeMethod(httpget);
079: // print the status and response
080: System.out.println(httpget.getStatusLine().toString());
081: System.out.println(httpget.getResponseBodyAsString());
082: } finally {
083: // release any connection resources used by the method
084: httpget.releaseConnection();
085: }
086: }
087:
088: public class ConsoleAuthPrompter implements CredentialsProvider {
089:
090: private BufferedReader in = null;
091:
092: public ConsoleAuthPrompter() {
093: super ();
094: this .in = new BufferedReader(new InputStreamReader(
095: System.in));
096: }
097:
098: private String readConsole() throws IOException {
099: return this .in.readLine();
100: }
101:
102: public Credentials getCredentials(final AuthScheme authscheme,
103: final String host, int port, boolean proxy)
104: throws CredentialsNotAvailableException {
105: if (authscheme == null) {
106: return null;
107: }
108: try {
109: if (authscheme instanceof NTLMScheme) {
110: System.out.println(host + ":" + port
111: + " 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,
119: domain);
120: } else if (authscheme instanceof RFC2617Scheme) {
121: System.out
122: .println(host
123: + ":"
124: + port
125: + " requires authentication with the realm '"
126: + authscheme.getRealm() + "'");
127: System.out.print("Enter username: ");
128: String user = readConsole();
129: System.out.print("Enter password: ");
130: String password = readConsole();
131: return new UsernamePasswordCredentials(user,
132: password);
133: } else {
134: throw new CredentialsNotAvailableException(
135: "Unsupported authentication scheme: "
136: + authscheme.getSchemeName());
137: }
138: } catch (IOException e) {
139: throw new CredentialsNotAvailableException(e
140: .getMessage(), e);
141: }
142: }
143: }
144: }
|