001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.authenticate;
007:
008: import java.net.URL;
009: import java.net.URLEncoder;
010: import java.net.URLConnection;
011: import java.net.HttpURLConnection;
012: import java.net.MalformedURLException;
013: import java.io.IOException;
014: import java.util.HashMap;
015: import java.util.Iterator;
016:
017: public class PortletTCKAuthenticationImpl implements
018: PortletTCKAuthentication {
019:
020: private String m_machine;
021: private String m_port;
022:
023: public PortletTCKAuthenticationImpl(String machine, String port) {
024: m_machine = machine;
025: m_port = port;
026: }
027:
028: public String authenticate(String user, String password) {
029:
030: HashMap cookies = new HashMap();
031: HttpURLConnection conn = null;
032: String urlprefix = null;
033:
034: StringBuffer sb = new StringBuffer("http://");
035: sb.append(m_machine);
036: sb.append(":");
037: sb.append(m_port);
038:
039: urlprefix = sb.toString();
040:
041: sb.append("/amserver/UI/Login?gx_charset=UTF-8&Login.Token1=");
042: sb.append(URLEncoder.encode(user));
043: sb.append("&Login.Token2=");
044: sb.append(URLEncoder.encode(password));
045:
046: conn = getConnection(sb.toString(), cookies);
047: getCookies(conn, cookies);
048:
049: sb = new StringBuffer(urlprefix);
050: sb.append("/portal/dt?provider=JSPTabContainer");
051: //System.out.println( "cookie " + getCookiesString( cookies ) );
052: conn = getConnection(sb.toString(), cookies);
053: getCookies(conn, cookies);
054: //return getCookiesString( cookies );
055:
056: String cookieString = getCookiesString(cookies);
057:
058: //sb = new StringBuffer( urlprefix );
059: //sb.append( "/amserver/UI/Logout" );
060: //conn = getConnection( sb.toString(), cookies );
061:
062: return cookieString;
063: }
064:
065: void getCookies(HttpURLConnection conn, HashMap cookies) {
066:
067: int i = 1;
068: String hdrKey = null;
069: while ((hdrKey = conn.getHeaderFieldKey(i)) != null) {
070: String value = conn.getHeaderField(hdrKey);
071: String cookie = null;
072: if (hdrKey.equals("Set-cookie")) {
073: if (value.indexOf(";") != -1) {
074: cookie = value.substring(0, value.indexOf(";"));
075: } else {
076: cookie = value;
077: }
078: cookies.put(cookie.substring(0, cookie.indexOf("=")),
079: cookie.substring(cookie.indexOf("=") + 1));
080: }
081:
082: i++;
083: }
084:
085: }
086:
087: String getCookiesString(HashMap cookies) {
088: StringBuffer sb = new StringBuffer();
089: Iterator iter = cookies.keySet().iterator();
090: while (iter.hasNext()) {
091: String key = (String) iter.next();
092: sb.append(key);
093: sb.append("=");
094: sb.append(cookies.get(key));
095: if (iter.hasNext()) {
096: sb.append(";");
097: }
098: }
099: return sb.toString();
100: }
101:
102: HttpURLConnection getConnection(String urlStr, HashMap cookies) {
103: URL url = null;
104: HttpURLConnection conn = null;
105: //System.out.println( "Hitting URL " + urlStr );
106: try {
107: url = new URL(urlStr);
108: } catch (MalformedURLException mfue) {
109: System.out.println("MalformedURLException " + urlStr);
110: }
111:
112: try {
113: conn = (HttpURLConnection) url.openConnection();
114: if (cookies.size() > 0) {
115: conn.setRequestProperty("Cookie",
116: getCookiesString(cookies));
117: //System.out.println( "-> sending cookie " + getCookiesString( cookies ) );
118: }
119: conn.setRequestMethod("GET");
120: conn.setInstanceFollowRedirects(false);
121: conn.connect();
122: //System.out.println( "Content length: " + conn.getContentLength() );
123: //System.out.println( "Status : " + conn.getResponseMessage() );
124: } catch (IOException ioe) {
125: System.out.println("IOException");
126: }
127: //System.out.println( "===========================================" );
128: return conn;
129: }
130: }
|