001: package com.sun.portal.wireless.tests;
002:
003: import java.io.*;
004: import java.net.*;
005: import java.util.*;
006:
007: /**
008: * Represents a client session. The order of operations is:
009: * <ol>
010: * <li> new ClientSession(Device) </li>
011: * <li> connectLogin() </li>
012: * <li> sendCredentials() </li>
013: * <li> ... (add more as necessary) </li>
014: * </ol>
015: */
016: public class ClientSession {
017: private Device device = null;
018: private HttpURLConnection conn = null;
019: private String cookie = null;
020:
021: public ClientSession(Device device) {
022: this .device = device;
023: }
024:
025: public HttpURLConnection getConnection() {
026: return (conn);
027: }
028:
029: public String getCookie() {
030: return (cookie);
031: }
032:
033: /**
034: * Connects to the Identity Server login page, using the following
035: * parameters from the <code>Configuration</code> class:
036: * <ul>
037: * <li>host</li>
038: * <li>port</li>
039: * <li>loginUrl</li>
040: * </ul>
041: *
042: * After this method is called, <code>getConnection()</code> and
043: * <code>getCookie()</code> will return non-null values if the login
044: * connection was successful.
045: */
046: public void login() throws Exception {
047: // set up the connection
048: String urlStr = "http://" + Configuration.host + ":"
049: + Configuration.port + "/" + Configuration.loginUrl;
050: HttpURLConnection lconn = setupConnection(urlStr);
051: lconn.connect();
052:
053: // check for redirect
054: String location = lconn.getHeaderField("Location");
055: if (location != null) {
056: lconn = setupConnection(location);
057: lconn.connect();
058: }
059:
060: // save off the cookie
061: String key = null;
062: for (int i = 1; (key = lconn.getHeaderFieldKey(i)) != null; i++) {
063: if (key.equals("Set-cookie")) {
064: String value = lconn.getHeaderField(i);
065: if (value.startsWith("AMAuthCookie")) {
066: int truncate = value.indexOf(";");
067: if (truncate > -1) {
068: cookie = value.substring(0, truncate);
069: } else {
070: cookie = value;
071: }
072: }
073: }
074: }
075:
076: conn = lconn;
077: }
078:
079: /**
080: * Disconnects from the Identity Server, using the following
081: * parameters from the <code>Configuration</code> class:
082: * <ul>
083: * <li>host</li>
084: * <li>port</li>
085: * <li>loginUrl</li>
086: * </ul>
087: *
088: */
089: public void logout() throws Exception {
090: // set up the connection
091: String urlStr = "http://" + Configuration.host + ":"
092: + Configuration.port + "/" + Configuration.logoutUrl;
093: HttpURLConnection lconn = setupConnection(urlStr);
094: lconn.connect();
095:
096: }
097:
098: /**
099: * Sends user login credentials to an Identity Server auth module,
100: * using the following parameters from the <code>Configuration</code> class:
101: * <ul>
102: * <li>loginUrl</li>
103: * <li>testLoginA</li>
104: * <li>testPasswdA</li>
105: * </ul>
106: *
107: * <code>connectLogin</code> must be called first. The auth module is
108: * assumed to take two tokens:
109: * <ul>
110: * <li>IDToken1</li>
111: * <li>IDToken2</li>
112: * </ul>
113: */
114: public void sendCredentials() throws Exception {
115: if (conn == null) {
116: return;
117: }
118:
119: String urlStr = Configuration.loginUrl;
120:
121: HashMap params = new HashMap();
122: params.put("IDButton", "Submit");
123: params.put("IDToken1", Configuration.testLoginA);
124: params.put("IDToken2", Configuration.testPasswdA);
125:
126: // send the credentials
127: doPost(urlStr, params);
128: }
129:
130: /**
131: * Gets the portal desktop using the following parameters from the <code>
132: * Configuration</code> class:
133: * <ul>
134: * <li>host</li>
135: * <li>port</li>
136: * <li>loginUrl</li>
137: * <li>testLoginA</li>
138: * <li>testPasswdA</li>
139: * <li>dtUrl</li>
140: * </ul>
141: *
142: * This method will first call <code>connectLogin</code> and <code>sendCredentials
143: * </code> before sending the <code>GET</code> request to the desktop.
144: */
145: public void getDesktop() throws Exception {
146: login();
147: sendCredentials();
148: conn.getInputStream();
149:
150: // send the desktop request
151: doPost(Configuration.dtUrl, null);
152: }
153:
154: /**
155: * Sends a <code>GET</code> request with the given parameters to a URL constructed with the
156: * given path. The URL is constructed as <code>"http://" + Configuration.host
157: * + ":" + Configuration.port + "/" + path</code>.
158: *
159: * @param path the path used to construct the URL to <code>GET</code> the request
160: * @param params a Map of parameters and values
161: */
162: public void doGet(String path, Map params) throws Exception {
163: String urlStr = "http://" + Configuration.host + ":"
164: + Configuration.port + "/" + path;
165:
166: if (cookie != null) {
167: urlStr += "?" + cookie;
168: }
169:
170: if (params != null) {
171: for (Iterator i = params.keySet().iterator(); i.hasNext();) {
172: String param = (String) i.next();
173: String value = (String) params.get(param);
174: urlStr += "&" + param + "=" + value;
175: }
176: }
177:
178: conn = setupConnection(urlStr);
179: conn.setRequestProperty("Cookie", cookie);
180: conn.setRequestMethod("GET");
181: conn.setDoOutput(true);
182: conn.connect();
183: }
184:
185: /**
186: * Sends a <code>POST</code> request with the given parameters to a URL constructed
187: * with the given path. The URL is constructed as <code>"http://" +
188: * Configuration.host + ":" + Configuration.port + "/" + path</code>.
189: *
190: * @param path the path used to construct the URL to <code>POST</code> the request
191: * @param params a Map of parameters and values
192: */
193: public void doPost(String path, Map params) throws Exception {
194: String urlStr = "http://" + Configuration.host + ":"
195: + Configuration.port + "/" + path;
196:
197: if (cookie != null) {
198: urlStr += "?" + cookie;
199: }
200: conn = setupConnection(urlStr);
201: conn.setRequestProperty("Cookie", cookie);
202: conn.setRequestMethod("POST");
203: conn.setDoOutput(true);
204: conn.connect();
205:
206: if (params == null) {
207: return;
208: }
209:
210: PrintStream req = new PrintStream(conn.getOutputStream());
211: for (Iterator i = params.keySet().iterator(); i.hasNext();) {
212: String param = (String) i.next();
213: String value = (String) params.get(param);
214: req.print(param + "=" + value);
215: if (i.hasNext()) {
216: req.print("&");
217: }
218: }
219: }
220:
221: private HttpURLConnection setupConnection(String urlStr)
222: throws Exception {
223: URL url = new URL(urlStr);
224: HttpURLConnection conn = (HttpURLConnection) url
225: .openConnection();
226: String[][] reqHeaders = device.getHeaders();
227: for (int i = 0; i < reqHeaders.length; i++) {
228: conn.setRequestProperty(reqHeaders[i][0], reqHeaders[i][1]);
229: }
230: conn.setInstanceFollowRedirects(false);
231: return (conn);
232: }
233:
234: }
|