001: package com.sun.portal.samples.asc.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: cookie = "iPlanetDirectoryPro";
067: int start = value.indexOf("=");
068: int truncate = value.indexOf(";");
069: if (truncate > -1) {
070: cookie += value.substring(start, truncate);
071: } else {
072: cookie += value.substring(start);
073: }
074: }
075: }
076: }
077:
078: conn = lconn;
079: }
080:
081: /**
082: * Disconnects from the Identity Server, using the following
083: * parameters from the <code>Configuration</code> class:
084: * <ul>
085: * <li>host</li>
086: * <li>port</li>
087: * <li>loginUrl</li>
088: * </ul>
089: *
090: */
091: public void logout() throws Exception {
092: // set up the connection
093: String urlStr = "http://" + Configuration.host + ":"
094: + Configuration.port + "/" + Configuration.logoutUrl;
095: HttpURLConnection lconn = setupConnection(urlStr);
096: lconn.connect();
097:
098: }
099:
100: /**
101: * Sends user login credentials to an Identity Server auth module,
102: * using the following parameters from the <code>Configuration</code> class:
103: * <ul>
104: * <li>loginUrl</li>
105: * <li>testLoginA</li>
106: * <li>testPasswdA</li>
107: * </ul>
108: *
109: * <code>connectLogin</code> must be called first. The auth module is
110: * assumed to take two tokens:
111: * <ul>
112: * <li>IDToken1</li>
113: * <li>IDToken2</li>
114: * </ul>
115: */
116: public void sendCredentials() throws Exception {
117: if (conn == null) {
118: return;
119: }
120:
121: String urlStr = Configuration.loginUrl;
122:
123: HashMap params = new HashMap();
124: params.put("IDButton", "Submit");
125: params.put("IDToken1", Configuration.testLoginA);
126: params.put("IDToken2", Configuration.testPasswdA);
127:
128: // send the credentials
129: doPost(urlStr, params);
130: }
131:
132: /**
133: * Gets the portal desktop using the following parameters from the <code>
134: * Configuration</code> class:
135: * <ul>
136: * <li>host</li>
137: * <li>port</li>
138: * <li>loginUrl</li>
139: * <li>testLoginA</li>
140: * <li>testPasswdA</li>
141: * <li>dtUrl</li>
142: * </ul>
143: *
144: * This method will first call <code>connectLogin</code> and <code>sendCredentials
145: * </code> before sending the <code>GET</code> request to the desktop.
146: */
147: public void getDesktop() throws Exception {
148: login();
149: sendCredentials();
150: conn.getInputStream();
151:
152: // send the desktop request
153: doPost(Configuration.dtUrl, null);
154: }
155:
156: /**
157: * Sends a <code>GET</code> request to the fully-qualified location provided
158: *
159: * @param location - fully-qualified URL
160: * @throws Exception
161: */
162: public void doGet(String location) throws Exception {
163:
164: conn = setupConnection(location);
165: // set Cookie in header
166: conn.setRequestProperty("Cookie", cookie);
167: conn.setRequestMethod("GET");
168: conn.setDoOutput(true);
169: conn.setInstanceFollowRedirects(true);
170: conn.connect();
171:
172: }
173:
174: /**
175: * Sends a <code>GET</code> request with the given parameters to a URL constructed with the
176: * given path. The URL is constructed as <code>"http://" + Configuration.host
177: * + ":" + Configuration.port + "/" + path</code>.
178: *
179: * @param path the path used to construct the URL to <code>GET</code> the request
180: * @param params a Map of parameters and values
181: */
182: public void doGet(String path, Map params) throws Exception {
183: String urlStr = "http://" + Configuration.host + ":"
184: + Configuration.port + "/" + path;
185:
186: // All our clients handle cookies, don't need to send on URL.
187: /*
188: if (cookie != null) {
189: urlStr += "?" + cookie;
190: }
191:
192: */
193: urlStr += "?";
194: if (params != null) {
195: String amp = "";
196: for (Iterator i = params.keySet().iterator(); i.hasNext();) {
197: String param = (String) i.next();
198: String value = (String) params.get(param);
199: urlStr += amp + param + "=" + value;
200: amp = "&";
201: }
202: }
203:
204: conn = setupConnection(urlStr);
205: // set Cookie in header
206: conn.setRequestProperty("Cookie", cookie);
207: conn.setRequestMethod("GET");
208: conn.setDoOutput(true);
209: conn.setInstanceFollowRedirects(true);
210: conn.connect();
211:
212: }
213:
214: /**
215: * Sends a <code>POST</code> request with the given parameters to a URL constructed
216: * with the given path. The URL is constructed as <code>"http://" +
217: * Configuration.host + ":" + Configuration.port + "/" + path</code>.
218: *
219: * @param path the path used to construct the URL to <code>POST</code> the request
220: * @param params a Map of parameters and values
221: */
222: public void doPost(String path, Map params) throws Exception {
223: String urlStr = "http://" + Configuration.host + ":"
224: + Configuration.port + "/" + path;
225:
226: // All our clients handle cookies, don't need to send on URL.
227: /*
228: if (cookie != null) {
229: urlStr += "?" + cookie;
230: }
231: */
232: conn = setupConnection(urlStr);
233: // set Cookie in header
234: conn.setRequestProperty("Cookie", cookie);
235: conn.setRequestMethod("POST");
236: conn.setDoOutput(true);
237: conn.connect();
238:
239: if (params == null) {
240: return;
241: }
242:
243: PrintStream req = new PrintStream(conn.getOutputStream());
244: for (Iterator i = params.keySet().iterator(); i.hasNext();) {
245: String param = (String) i.next();
246: String value = (String) params.get(param);
247: req.print(param + "=" + value);
248: if (i.hasNext()) {
249: req.print("&");
250: }
251: }
252: }
253:
254: public void followRedirect(String urlStr) throws Exception {
255: conn = setupConnection(urlStr);
256: conn.setRequestProperty("Cookie", cookie);
257: conn.setRequestMethod("GET");
258: conn.setDoOutput(true);
259: conn.connect();
260: }
261:
262: private HttpURLConnection setupConnection(String urlStr)
263: throws Exception {
264: URL url = new URL(urlStr);
265: HttpURLConnection conn = (HttpURLConnection) url
266: .openConnection();
267: String[][] reqHeaders = device.getHeaders();
268: for (int i = 0; i < reqHeaders.length; i++) {
269: conn.setRequestProperty(reqHeaders[i][0], reqHeaders[i][1]);
270: }
271: conn.setInstanceFollowRedirects(false);
272: return (conn);
273: }
274:
275: }
|