001: package com.sun.portal.netlet.client.jnlp.connect;
002:
003: import java.io.*;
004: import com.sun.portal.log.common.PortalLogger;
005: import java.util.Vector;
006: import java.net.Socket;
007:
008: public class HTTP_1_0_Handler implements ProtocolHandlerI {
009: public void handleRequest(HTTPFields reqFields,
010: HTTPRequestStream clientIn, OutputStream clientOut)
011: throws IOException {
012: HTTPRequestStream gatewayIn = null;
013: HTTPResponseStream gatewayOut = null;
014: Socket tunnelSocket = null;
015:
016: try {
017: //
018: // Set the header line
019: //
020: setFirstLine(reqFields);
021:
022: //reqFields.appendField("Cookie",
023: // GlobalConfigMgr.getCookie().getName() + "=" +
024: // GlobalConfigMgr.getCookie().getValue() );
025:
026: //
027: // Remove Proxy specific headers
028: //
029: reqFields.removeField("Proxy-Connection");
030: reqFields.removeField("Connection");
031: //
032: // Read rest of the content
033: //
034: int contentLength = reqFields.getIntField("Content-Length");
035: byte requestData[] = new byte[contentLength];
036: int i;
037: for (int read = 0; read < contentLength; read += i) {
038: i = clientIn.read(requestData, read, contentLength
039: - read);
040: if (i == -1)
041: break;
042: }
043: //
044: // Add custom header to indicate that request is getting routed
045: // through proxylet
046: //
047: reqFields.addField("PS-Proxylet", "RT-Proxylet=true");
048:
049: //
050: // Write header and data to end server
051: //
052: reqFields.write(gatewayOut);
053: gatewayOut.write(requestData, 0, requestData.length);
054: gatewayOut.flush();
055: //
056: // Read response header from end server
057: //
058: HTTPFields responseFields = new HTTPFields();
059: responseFields.read(gatewayIn);
060:
061: //
062: // Parse Set-Cookie header for iPlanetDirectoryPro
063: //
064: Vector setCookieHeaderList = responseFields
065: .getFieldValues("Set-cookie");
066: if (setCookieHeaderList != null
067: && !setCookieHeaderList.isEmpty()) {
068: Cookie iplanetPro = Cookie.getCookieByName(
069: "iPlanetDirectoryPro", setCookieHeaderList);
070: //if( iplanetPro != null )
071: //GlobalConfigMgr.setCookie(iplanetPro);
072: }
073:
074: //
075: // Read response body from server
076: //
077: byte buf[] = new byte[8192];
078: int read = 0;
079: ByteArrayOutputStream respbody = new ByteArrayOutputStream();
080: while ((read = gatewayIn.read(buf, 0, 8192)) != -1) {
081: respbody.write(buf, 0, read);
082: }
083: //
084: //Write response back to browser client
085: //
086: ByteArrayOutputStream bytearrayheader = new ByteArrayOutputStream();
087: responseFields
088: .write(new HTTPResponseStream(bytearrayheader));
089: try {
090: clientOut.write(bytearrayheader.toByteArray(), 0,
091: bytearrayheader.size());
092: clientOut.write(respbody.toByteArray(), 0, respbody
093: .size());
094: clientOut.flush();
095: } catch (Exception e) {
096: //Log.message(reqFields.toString());
097: //Log.message(e.getMessage());
098: throw new IOException(e.getMessage());
099: }
100:
101: } catch (IOException ie) {
102: throw ie;
103: } finally {
104: try {
105: clientIn.close();
106: clientOut.close();
107: gatewayIn.close();
108: gatewayOut.close();
109: if (tunnelSocket != null)
110: tunnelSocket.close();
111: } catch (Exception e) {
112: }
113:
114: }
115:
116: }
117:
118: private void setFirstLine(HTTPFields reqFields) {
119: //String gwURL = "http://" + GlobalConfigMgr.getGatewayHost() + ":" + GlobalConfigMgr.getGatewayPort() ;
120: String url = "http://"
121: + reqFields.getHost()
122: + ":"
123: + (reqFields.getPort() == -1 ? 80 : reqFields.getPort());
124:
125: /**
126: * If gateway is running in non-secure mode and the client has proxy then you need to append the
127: * gateway URL. In other cases you would just be tunneling the data through.
128: * Don't have to append the gateway URL in that case.
129: */
130: /*
131: if (GlobalConfigMgr.getUseClientProxy() && !GlobalConfigMgr.getGatewayMode().equalsIgnoreCase("secure")) {
132: reqFields.setFirstLine(reqFields.getScheme() +
133: " " +
134: gwURL +
135: "/" +
136: url +
137: reqFields.getFile() +
138: " " +
139: reqFields.getHttpVersion());
140: } else {
141: */
142: reqFields.setFirstLine(reqFields.getScheme() + " " + "/" + url
143: + reqFields.getFile() + " "
144: + reqFields.getHttpVersion());
145: //}
146: }
147:
148: /*
149: private Socket createTunnelSocket()
150: throws IOException
151: {
152: Socket tunnel = null ;
153:
154: try{
155: tunnel = new Socket(GlobalConfigMgr.getClientProxyHost(), GlobalConfigMgr.getClientProxyPort());
156: }catch(Exception e){
157: throw new IOException("Could not establish a connection with proxy");
158: }
159:
160: OutputStream out = tunnel.getOutputStream();
161: String msg = "CONNECT " + GlobalConfigMgr.getGatewayHost() + ":" + GlobalConfigMgr.getGatewayPort() + " HTTP/1.0\n"
162: + "User-Agent: "
163: + sun.net.www.protocol.http.HttpURLConnection.userAgent
164: + "\r\n\r\n";
165:
166:
167: byte b[];
168: try {
169: /*
170: * We really do want ASCII7 -- the http protocol doesn't change
171: * with locale.
172: */
173: /*
174: b = msg.getBytes("ASCII7");
175: } catch (UnsupportedEncodingException ignored) {
176: /*
177: * If ASCII7 isn't there, something serious is wrong, but
178: * Paranoia Is Good (tm)
179: */
180: /*
181: b = msg.getBytes();
182: }
183: out.write(b);
184: out.flush();
185:
186: byte reply[] = new byte[200];
187: int replyLen = 0;
188: int newlinesSeen = 0;
189: boolean headerDone = false; /* Done on first newline */
190: /*
191: boolean error = false;
192: InputStream in = tunnel.getInputStream();
193: while (newlinesSeen < 2) {
194: int i = in.read();
195: if (i < 0) {
196: throw new IOException("Unexpected EOF from proxy");
197: }
198: if (i == '\n') {
199: headerDone = true;
200: ++newlinesSeen;
201: } else if (i != '\r') {
202: newlinesSeen = 0;
203: if (!headerDone && replyLen < reply.length) {
204: reply[replyLen++] = (byte) i;
205: }
206: }
207: }
208:
209: /*
210: * Converting the byte array to a string is slightly wasteful
211: * in the case where the connection was successful, but it's
212: * insignificant compared to the network overhead.
213: */
214: /*
215: String replyStr;
216: try {
217: replyStr = new String(reply, 0, replyLen, "ASCII7");
218: } catch (UnsupportedEncodingException ignored) {
219: replyStr = new String(reply, 0, replyLen);
220: }
221:
222: /* Look for 200 connection established */
223: /*
224: if(replyStr.toLowerCase().indexOf("200 connection established") == -1){
225: throw new IOException("Unable to tunnel through "
226: + GlobalConfigMgr.getClientProxyHost()+ ":" + GlobalConfigMgr.getClientProxyPort()
227: + ". Proxy returns \"" + replyStr + "\"");
228: }
229:
230: return tunnel;
231:
232: }
233:
234: */
235:
236: }
|