001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/api-impl/src/java/org/theospi/portfolio/presentation/export/SessionAccess.java $
003: * $Id:SessionAccess.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.portfolio.presentation.export;
021:
022: import java.io.IOException;
023: import java.io.PrintStream;
024: import java.net.HttpURLConnection;
025: import java.net.URL;
026: import java.net.URLConnection;
027: import java.util.HashMap;
028: import java.util.Map;
029: import java.util.StringTokenizer;
030:
031: import websphinx.Access;
032: import websphinx.DownloadParameters;
033: import websphinx.Link;
034:
035: /**
036: * Created by IntelliJ IDEA.
037: * User: John Ellis
038: * Date: Dec 14, 2005
039: * Time: 1:38:13 PM
040: * To change this template use File | Settings | File Templates.
041: */
042: public class SessionAccess extends Access {
043:
044: private ThreadLocal threadCookies = new ThreadLocal();
045:
046: public URLConnection openConnection(URL url) throws IOException {
047: URLConnection conn = url.openConnection();
048: connect(conn);
049: return conn;
050: }
051:
052: public URLConnection openConnection(Link link) throws IOException {
053: // get the URL
054: int method = link.getMethod();
055: URL url;
056: switch (method) {
057: case Link.GET:
058: url = link.getPageURL();
059: break;
060: case Link.POST:
061: url = link.getServiceURL();
062: break;
063: default:
064: throw new IOException("Unknown HTTP method "
065: + link.getMethod());
066: }
067:
068: // open a connection to the URL
069: URLConnection conn = url.openConnection();
070:
071: // set up request headers
072: DownloadParameters dp = link.getDownloadParameters();
073: if (dp != null) {
074: conn.setAllowUserInteraction(dp.getInteractive());
075: conn.setUseCaches(dp.getUseCaches());
076:
077: String userAgent = dp.getUserAgent();
078: if (userAgent != null)
079: conn.setRequestProperty("User-Agent", userAgent);
080:
081: String types = dp.getAcceptedMIMETypes();
082: if (types != null)
083: conn.setRequestProperty("accept", types);
084: }
085:
086: // submit the query if it's a POST (GET queries are encoded in the URL)
087: if (method == Link.POST) {
088: //#ifdef JDK1.1
089: if (conn instanceof HttpURLConnection)
090: ((HttpURLConnection) conn).setRequestMethod("POST");
091: //#endif JDK1.1
092:
093: String query = link.getQuery();
094: if (query.startsWith("?"))
095: query = query.substring(1);
096:
097: conn.setDoOutput(true);
098: conn.setRequestProperty("Content-type",
099: "application/x-www-form-urlencoded");
100: conn.setRequestProperty("Content-length", String
101: .valueOf(query.length()));
102:
103: // commence request
104: //#ifdef JDK1.1
105: PrintStream out = new PrintStream(conn.getOutputStream());
106: //#endif JDK1.1
107: /*#ifdef JDK1.0
108: PrintStream out = new PrintStream (conn.getOutputStream ());
109: #endif JDK1.0*/
110: out.print(query);
111: out.flush();
112: }
113:
114: connect(conn);
115: return conn;
116: }
117:
118: protected void connect(URLConnection conn) throws IOException {
119: setCookies(conn);
120: conn.connect();
121: saveCookies(conn);
122: }
123:
124: protected void setCookies(URLConnection urlconn) {
125: String headerfield = "";
126: String host = "";
127:
128: // get the host
129: host = urlconn.getURL().getHost();
130:
131: // get saved cookies from hashtable
132: headerfield = (String) getCookies().get(host);
133:
134: // check if there are any saved cookies
135: if (headerfield != null) {
136: // set cookie string as request property
137: urlconn.setUseCaches(false);
138: urlconn.setRequestProperty("Cookie", headerfield);
139: }
140: }
141:
142: protected Map getCookies() {
143: if (threadCookies.get() == null) {
144: threadCookies.set(new HashMap());
145: }
146: return (Map) threadCookies.get();
147: }
148:
149: protected void saveCookies(URLConnection urlconn) {
150: int i = 0;
151: String key = null;
152: String cookie = null;
153: String host = null;
154: String headerfield = null;
155: StringTokenizer tok = null;
156:
157: // get the host
158: host = urlconn.getURL().getHost();
159:
160: // forward pass any starting null values
161: while (urlconn.getHeaderFieldKey(i) == null)
162: i++;
163:
164: // check all the headerfields until there are no more
165: while (urlconn.getHeaderFieldKey(i) != null) {
166: key = urlconn.getHeaderFieldKey(i);
167:
168: // check if it is a Set-Cookie header
169: if (key.equalsIgnoreCase("set-cookie")) {
170: headerfield = urlconn.getHeaderField(i);
171: if (headerfield != null) { // can the headerfield be null here ?
172: // parse out only the name=value pair and ignore the rest
173: tok = new StringTokenizer(headerfield, ";", false);
174:
175: // if this is anything but the first cookie add a semicolon and a space
176: // before we add the next cookie.
177: cookie = (cookie != null ? cookie + "; "
178: + tok.nextToken() : tok.nextToken());
179: }
180: }
181: i++;
182: }
183:
184: // save the cookies in our cookie collection with the hostname as key
185: if (cookie != null) {
186: getCookies().put(host, cookie);
187: }
188: }
189:
190: }
|