001: package com.sun.portal.netlet.client.jnlp.connect;
002:
003: import com.sun.portal.netlet.client.jnlp.ClientManager;
004: import com.sun.portal.log.common.PortalLogger;
005:
006: import java.net.*;
007: import java.io.*;
008:
009: /*
010: * ConnectionHandler
011: * - Handles HTTP by forwarding requests to gateway
012: * - Proxy chains with client proxy if one exists
013: * - Provides a secure channel for HTTP requests.
014: * - Tunnels SSL requests to end-server through gateway.
015: *
016: * For every client connection received, a new transferhandler thread gets created. This thread takes care
017: * of processing the incoming request based on the HTTP scheme, and sends the response back to the client.
018: */
019:
020: public class ConnectionHandler {
021: private ClientListener parent = null;
022: private Socket connection = null;
023:
024: public ConnectionHandler(Socket clntSocket, ClientListener parent) {
025: this .connection = clntSocket;
026: this .parent = parent;
027:
028: handleConnection();
029: }
030:
031: /**
032: * This function hands over the request to the appropriate handler based on the scheme
033: */
034: private void handleConnection() {
035: try {
036: //
037: // Determine the type of request
038: //
039: HTTPRequestStream clientIn = new HTTPRequestStream(
040: connection.getInputStream());
041: OutputStream clientOut = connection.getOutputStream();
042: HTTPFields reqFields = new HTTPFields();
043: reqFields.read(clientIn);
044:
045: //
046: // Handle special cases here
047: // Determine if request is for PAC file
048: //
049: String firstLine = reqFields.getFirstLine();
050: System.out.println("ConnectionHandler firstLine "
051: + firstLine);
052: int index = firstLine.lastIndexOf(" ");
053: reqFields.setHttpVersion(firstLine.substring(index,
054: firstLine.length()).trim());
055: if (firstLine.indexOf("/startNetlet") != -1) {
056: handleNetletRequest(reqFields, clientOut);
057: clientIn.close();
058: clientOut.close();
059: return;
060: } else if (firstLine.indexOf("/startProxylet") != -1) {
061: //TODO : BY PROXYLET
062: handleProxyletRequest(reqFields, clientOut);
063: clientIn.close();
064: clientOut.close();
065: return;
066: }
067: //
068: // Close the applet window, if user has opted to Logout
069: //
070: else if (firstLine.indexOf("/UI/Logout") != -1) {
071: /*
072: Log.message("Logout Message. Close the applet ");
073:
074: JSObject win = ((ProxyletApplet)grandParent).getWindow();
075: win.call("resetProxysetting", null);
076:
077: Thread.sleep(2000);
078: sendRedirectResponse(reqFields, clientOut);
079: clientIn.close();
080: clientOut.close();
081:
082: parent.close();
083: grandParent.stop();
084: win.call("closeAppletWindow", null);
085: grandParent.destroy();
086: if (GlobalConfigMgr.getBrowserType().equals(BrowserType.IE) &&
087: (!System.getProperty("java.version").startsWith("1.3"))) {
088: PluginProxyManager.reset();
089: }
090: */
091:
092: return;
093: }
094:
095: //
096: // Parse the incoming request
097: //
098: //reqFields.parseRequest();
099: //
100: // Hand over the request to appropriate handler
101: //
102: //ProtocolHandlerI handler = ConnectionFactory.getHandler(reqFields.getScheme(), reqFields.getHttpVersion());
103: //handler.handleRequest(reqFields, clientIn, clientOut);
104:
105: } catch (MalformedURLException me) {
106: //Log.error( "Malformed Exception ME001" + me.getMessage());
107: } catch (IOException e) {
108: e.printStackTrace();
109: } catch (Exception ignore) {
110: } finally {
111: try {
112: connection.close();
113: } catch (Exception e) {
114: }
115:
116: }
117: } // run method
118:
119: private void handleNetletRequest(HTTPFields reqFields,
120: OutputStream clientOut) throws IOException {
121: ClientManager.getNetletHandler().startClient();
122: sendRedirectResponse(reqFields, clientOut);
123: }
124:
125: private void handleProxyletRequest(HTTPFields reqFields,
126: OutputStream clientOut) throws IOException {
127: byte[] header = getHeader(0, reqFields.getHttpVersion());
128: clientOut.write(header);
129: clientOut.flush();
130: }
131:
132: private byte[] getHeader(int contentLength, String httpVersion) {
133: String dummyHeader = httpVersion + Constants.SP + 200
134: + Constants.SP + "OK" + "\n"
135: + "Content-Type: text/html" + "\n"
136: + "Connection: close" + "\n" + "Content-Length: "
137: + contentLength + "\n\r\n\r";
138: return dummyHeader.getBytes();
139: }
140:
141: private void sendRedirectResponse(HTTPFields reqFields,
142: OutputStream clientOut) throws IOException {
143: String redirectURL = reqFields.getRedirectURL();
144: redirectURL = URLDecoder.decode(redirectURL);
145: System.out.println("redirectURL " + redirectURL);
146:
147: String gwURL = ClientManager.getParam("gwURL");
148: System.out.println("GWURL " + gwURL);
149:
150: if (redirectURL.startsWith("/")) {
151: String codebaseURL = ClientManager.getCodeBaseUrl();
152: if (codebaseURL != null && codebaseURL.length() > 0) {
153: int hostIndex = codebaseURL.indexOf("/");
154: int portIndex = codebaseURL.indexOf("/", hostIndex + 2);
155: redirectURL = gwURL
156: + codebaseURL.substring(0, portIndex)
157: + redirectURL;
158: }
159: }
160: System.out
161: .println("ConnectionHandler.sendRedirectResponse redirectURL "
162: + redirectURL);
163: String content = "File Moved temporarily";
164: String respHeader_302 = reqFields.getHttpVersion()
165: + Constants.SP + 302 + Constants.SP
166: + "Moved Temporarily" + "\n"
167: + "Content-Type: text/html; charset=UTF-8" + "\n"
168: + "Location: " + redirectURL + "\n"
169: + "Connection: close" + "\n" + "Content-Length: "
170: + content.length() + "\n\r\n\r";
171: clientOut.write(respHeader_302.getBytes());
172: clientOut.write(content.getBytes());
173: clientOut.flush();
174: }
175: }
|