001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.sso;
018:
019: import org.apache.commons.httpclient.Cookie;
020: import org.apache.commons.httpclient.Header;
021: import org.apache.commons.httpclient.HttpClient;
022: import org.apache.commons.httpclient.HttpConnection;
023: import org.apache.commons.httpclient.HttpState;
024: import org.apache.commons.httpclient.UsernamePasswordCredentials;
025: import org.apache.commons.httpclient.auth.AuthScheme;
026: import org.apache.commons.httpclient.auth.HttpAuthenticator;
027: import org.apache.commons.httpclient.methods.GetMethod;
028:
029: import junit.framework.Test;
030: import junit.framework.TestCase;
031: import junit.framework.TestSuite;
032:
033: /**
034: * TestBasicSSO
035: *
036: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
037: * @version $Id: TestBasicSSO.java 517719 2007-03-13 15:05:48Z ate $
038: */
039: public class TestBasicSSO extends TestCase {
040: public static Test suite() {
041: return new TestSuite(TestBasicSSO.class);
042: }
043:
044: public void testBasicSSO() throws Exception {
045: System.out.println("Testing SSO");
046: // connect("http://localhost:8080/demo/sso-basic", "tomcat", "tomcat");
047: }
048:
049: public void connect(String server, String username, String password)
050: throws Exception {
051: HttpClient client = new HttpClient();
052: UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
053: username, password);
054: StringBuffer authenticationHeader = new StringBuffer("BASIC");
055: authenticationHeader.append(" realm=\"");
056: authenticationHeader.append("jetspeed");
057: authenticationHeader.append("\"");
058: Header[] headers = { new Header("WWW-Authenticate",
059: authenticationHeader.toString()) };
060: AuthScheme scheme = null;
061:
062: client.getState().setCredentials(null, null, credentials);
063: GetMethod get = new GetMethod(server);
064: // post = new MultipartPostMethod(server);
065: get.setDoAuthentication(true);
066:
067: try {
068: scheme = HttpAuthenticator.selectAuthScheme(headers);
069: HttpConnection conn = client.getHttpConnectionManager()
070: .getConnection(get.getHostConfiguration());
071: boolean authenticated = HttpAuthenticator.authenticate(
072: scheme, get, conn, client.getState());
073: if (!authenticated) {
074: throw new Exception(
075: "Failed to create authentication headers to HTTP Connection");
076: }
077: client.executeMethod(get);
078: System.out.println("response = ["
079: + get.getResponseBodyAsString() + "]");
080:
081: Cookie[] cookies = client.getState().getCookies();
082: Cookie mycookie = null;
083: // Display the cookies
084: System.out.println("Present cookies: ");
085: for (int i = 0; i < cookies.length; i++) {
086: System.out.println(" - " + cookies[i].toExternalForm());
087: mycookie = cookies[i];
088: }
089: // get.releaseConnection();
090: // client.endSession();
091:
092: HttpState initialState = new HttpState();
093: initialState.addCookie(mycookie);
094: // client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
095: client = new HttpClient();
096: client.setState(initialState);
097: get = new GetMethod(server);
098:
099: client.executeMethod(get);
100: System.out.println("response = ["
101: + get.getResponseBodyAsString() + "]");
102:
103: cookies = client.getState().getCookies();
104: // Display the cookies
105: System.out.println("Present cookies: ");
106: for (int i = 0; i < cookies.length; i++) {
107: System.out.println(" - " + cookies[i].toExternalForm());
108: }
109: get.releaseConnection();
110: } catch (Throwable t) {
111: throw new Exception(
112: "Unexpected exception in creating HTTP authentication headers",
113: t);
114: }
115: }
116:
117: }
|