| 1 |
/*
|
| 2 |
* $HeadURL$
|
| 3 |
* $Revision$
|
| 4 |
* $Date$
|
| 5 |
*
|
| 6 |
* ====================================================================
|
| 7 |
*
|
| 8 |
* Licensed to the Apache Software Foundation (ASF) under one or more
|
| 9 |
* contributor license agreements. See the NOTICE file distributed with
|
| 10 |
* this work for additional information regarding copyright ownership.
|
| 11 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
| 12 |
* (the "License"); you may not use this file except in compliance with
|
| 13 |
* the License. You may obtain a copy of the License at
|
| 14 |
*
|
| 15 |
* http://www.apache.org/licenses/LICENSE-2.0
|
| 16 |
*
|
| 17 |
* Unless required by applicable law or agreed to in writing, software
|
| 18 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
| 19 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 20 |
* See the License for the specific language governing permissions and
|
| 21 |
* limitations under the License.
|
| 22 |
* ====================================================================
|
| 23 |
*
|
| 24 |
* This software consists of voluntary contributions made by many
|
| 25 |
* individuals on behalf of the Apache Software Foundation. For more
|
| 26 |
* information on the Apache Software Foundation, please see
|
| 27 |
* <http://www.apache.org/>.
|
| 28 |
*
|
| 29 |
*/
|
| 30 |
import java.util.ArrayList;
|
| 31 |
import java.util.Collection;
|
| 32 |
|
| 33 |
import org.apache.commons.httpclient.Credentials;
|
| 34 |
import org.apache.commons.httpclient.HttpMethod;
|
| 35 |
import org.apache.commons.httpclient.auth.AuthPolicy;
|
| 36 |
import org.apache.commons.httpclient.auth.AuthScheme;
|
| 37 |
import org.apache.commons.httpclient.auth.AuthenticationException;
|
| 38 |
import org.apache.commons.httpclient.auth.MalformedChallengeException;
|
| 39 |
import org.apache.commons.httpclient.params.DefaultHttpParams;
|
| 40 |
import org.apache.commons.httpclient.params.HttpParams;
|
| 41 |
|
| 42 |
/**
|
| 43 |
* A simple custom AuthScheme example. The included auth scheme is meant
|
| 44 |
* for demonstration purposes only. It does not actually implement a usable
|
| 45 |
* authentication method.
|
| 46 |
*/
|
| 47 |
public class CustomAuthenticationExample {
|
| 48 |
|
| 49 |
public static void main(String[] args) {
|
| 50 |
|
| 51 |
// register the auth scheme
|
| 52 |
AuthPolicy.registerAuthScheme(SecretAuthScheme.NAME, SecretAuthScheme.class);
|
| 53 |
|
| 54 |
// include the scheme in the AuthPolicy.AUTH_SCHEME_PRIORITY preference,
|
| 55 |
// this can be done on a per-client or per-method basis but we'll do it
|
| 56 |
// globally for this example
|
| 57 |
HttpParams params = DefaultHttpParams.getDefaultParams();
|
| 58 |
ArrayList schemes = new ArrayList();
|
| 59 |
schemes.add(SecretAuthScheme.NAME);
|
| 60 |
schemes.addAll((Collection) params.getParameter(AuthPolicy.AUTH_SCHEME_PRIORITY));
|
| 61 |
params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, schemes);
|
| 62 |
|
| 63 |
// now that our scheme has been registered we can execute methods against
|
| 64 |
// servers that require "Secret" authentication...
|
| 65 |
}
|
| 66 |
|
| 67 |
/**
|
| 68 |
* A custom auth scheme that just uses "Open Sesame" as the authentication
|
| 69 |
* string.
|
| 70 |
*/
|
| 71 |
private class SecretAuthScheme implements AuthScheme {
|
| 72 |
|
| 73 |
public static final String NAME = "Secret";
|
| 74 |
|
| 75 |
public SecretAuthScheme() {
|
| 76 |
// All auth schemes must have a no arg constructor.
|
| 77 |
}
|
| 78 |
public String authenticate(Credentials credentials, HttpMethod method)
|
| 79 |
throws AuthenticationException {
|
| 80 |
return "Open Sesame";
|
| 81 |
}
|
| 82 |
public String authenticate(Credentials credentials, String method,
|
| 83 |
String uri) throws AuthenticationException {
|
| 84 |
return "Open Sesame";
|
| 85 |
}
|
| 86 |
public String getID() {
|
| 87 |
return NAME;
|
| 88 |
}
|
| 89 |
public String getParameter(String name) {
|
| 90 |
// this scheme does not use parameters, see RFC2617Scheme for an example
|
| 91 |
return null;
|
| 92 |
}
|
| 93 |
public String getRealm() {
|
| 94 |
// this scheme does not use realms
|
| 95 |
return null;
|
| 96 |
}
|
| 97 |
public String getSchemeName() {
|
| 98 |
return NAME;
|
| 99 |
}
|
| 100 |
public boolean isConnectionBased() {
|
| 101 |
return false;
|
| 102 |
}
|
| 103 |
public void processChallenge(String challenge)
|
| 104 |
throws MalformedChallengeException {
|
| 105 |
// Nothing to do here, this is not a challenge based
|
| 106 |
// auth scheme. See NTLMScheme for a good example.
|
| 107 |
}
|
| 108 |
public boolean isComplete() {
|
| 109 |
// again we're not a challenge based scheme so this is always true
|
| 110 |
return true;
|
| 111 |
}
|
| 112 |
}
|
| 113 |
}
|