001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/examples/CustomAuthenticationExample.java,v 1.1 2004/09/06 20:10:02 mbecke Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030: import java.util.ArrayList;
031: import java.util.Collection;
032:
033: import org.apache.commons.httpclient.Credentials;
034: import org.apache.commons.httpclient.HttpMethod;
035: import org.apache.commons.httpclient.auth.AuthPolicy;
036: import org.apache.commons.httpclient.auth.AuthScheme;
037: import org.apache.commons.httpclient.auth.AuthenticationException;
038: import org.apache.commons.httpclient.auth.MalformedChallengeException;
039: import org.apache.commons.httpclient.params.DefaultHttpParams;
040: import org.apache.commons.httpclient.params.HttpParams;
041:
042: /**
043: * A simple custom AuthScheme example. The included auth scheme is meant
044: * for demonstration purposes only. It does not actually implement a usable
045: * authentication method.
046: */
047: public class CustomAuthenticationExample {
048:
049: public static void main(String[] args) {
050:
051: // register the auth scheme
052: AuthPolicy.registerAuthScheme(SecretAuthScheme.NAME,
053: SecretAuthScheme.class);
054:
055: // include the scheme in the AuthPolicy.AUTH_SCHEME_PRIORITY preference,
056: // this can be done on a per-client or per-method basis but we'll do it
057: // globally for this example
058: HttpParams params = DefaultHttpParams.getDefaultParams();
059: ArrayList schemes = new ArrayList();
060: schemes.add(SecretAuthScheme.NAME);
061: schemes.addAll((Collection) params
062: .getParameter(AuthPolicy.AUTH_SCHEME_PRIORITY));
063: params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, schemes);
064:
065: // now that our scheme has been registered we can execute methods against
066: // servers that require "Secret" authentication...
067: }
068:
069: /**
070: * A custom auth scheme that just uses "Open Sesame" as the authentication
071: * string.
072: */
073: private class SecretAuthScheme implements AuthScheme {
074:
075: public static final String NAME = "Secret";
076:
077: public SecretAuthScheme() {
078: // All auth schemes must have a no arg constructor.
079: }
080:
081: public String authenticate(Credentials credentials,
082: HttpMethod method) throws AuthenticationException {
083: return "Open Sesame";
084: }
085:
086: public String authenticate(Credentials credentials,
087: String method, String uri)
088: throws AuthenticationException {
089: return "Open Sesame";
090: }
091:
092: public String getID() {
093: return NAME;
094: }
095:
096: public String getParameter(String name) {
097: // this scheme does not use parameters, see RFC2617Scheme for an example
098: return null;
099: }
100:
101: public String getRealm() {
102: // this scheme does not use realms
103: return null;
104: }
105:
106: public String getSchemeName() {
107: return NAME;
108: }
109:
110: public boolean isConnectionBased() {
111: return false;
112: }
113:
114: public void processChallenge(String challenge)
115: throws MalformedChallengeException {
116: // Nothing to do here, this is not a challenge based
117: // auth scheme. See NTLMScheme for a good example.
118: }
119:
120: public boolean isComplete() {
121: // again we're not a challenge based scheme so this is always true
122: return true;
123: }
124: }
125: }
|