001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: LBSConnection.java,v 1.2 2006-06-15 13:40:47 sinisa Exp $
022: */
023:
024: package com.lutris.applet;
025:
026: import java.applet.Applet;
027: import java.io.IOException;
028: import java.net.MalformedURLException;
029: import java.net.URL;
030: import java.net.URLConnection;
031:
032: //import java.util.*;
033:
034: /**
035: * This file is the counterpart to
036: * com.lutris.appserver.server.httpPresentation.AppletUtils. It is meant
037: * to be added to the applet's jar file or classes directory. The methods
038: * are split into two files so that the applet does not need to include
039: * any com.lutris.appserver.server.* classes. <P>
040: *
041: * A dynamicly generated page contained the <APPLET...> tag that lead to
042: * the applet calling this function. That <APPLET...> tag was created by
043: * com.lutris.appserver.server.httpPresentation.AppletUtils.createAppletTag.
044: * When the applet wants to establish a connection back to the
045: * Multiserver, simply call the contactServer() and the extra
046: * initialization parameters added by createAppletTag() will be used to
047: * contact the server. Specifically, the request will match the user's
048: * current session.
049: *
050: * @see com.lutris.appserver.server.httpPresentation.AppletUtils
051: */
052: public class LBSConnection {
053:
054: public static final String nameParamName = "cookieName";
055: public static final String valueParamName = "cookieValue";
056: public static final String targetParamName = "targetURL";
057:
058: /**
059: * Do not create instances; use the static method.
060: */
061: private LBSConnection() {
062: }
063:
064: /**
065: * Use the extra initialization parameters added to the APPLET tag
066: * by AppletUtils.createAppletTag() to open a connection back to
067: * the Multiserver, using the same session as the one used
068: * to get the HTML page that contained the APPLET tag. <P>
069: *
070: * The resulting URLConnection is ready to have
071: * <CODE>connect()</CODE> called on it. You may initialize more
072: * options if you wish, for example <CODE>setDoOutput()</CODE> or
073: * <CODE>setRequestMethod()</CODE> before calling <CODE>connect()</CODE>.
074: *
075: * The resulting URLConnection may also be a HttpURLConnection, but
076: * this is not guarenteed. In the JDK on Windows and Solaris it is,
077: * but when running in Netscape Communicator as an applet, it is not.
078: * So to be safe, we return the more generic class. The main
079: * functionality missing is the ability to set the request method.
080: * However, if you call <CODE>setDoOutput(true)</CODE> in Communicator,
081: * it will issue a POST request.
082: *
083: * @param applet
084: * The applet that is calling this method.
085: * @return
086: * A connection back to the server, or null if an error occured.
087: */
088: public static URLConnection contactServer(Applet applet) {
089: /*
090: * Get the extra parameters. Cookie is optional.
091: */
092: String targetURL = applet.getParameter(targetParamName);
093: System.out.println("target = " + targetURL);
094: if (targetURL == null)
095: return null;
096: String cookieName = applet.getParameter(nameParamName);
097: System.out.println("name = " + cookieName);
098: String cookieValue = applet.getParameter(valueParamName);
099: System.out.println("value = " + cookieValue);
100: /*
101: * Get ready to contact the server.
102: */
103: URL url = null;
104: try {
105: url = new URL(targetURL);
106: } catch (MalformedURLException e) {
107: System.out.println("bad url!");
108: return null;
109: }
110: URLConnection conn = null;
111: try {
112: conn = url.openConnection();
113: System.out.println("conn = " + conn);
114: } catch (IOException e) {
115: System.out.println("io err");
116: return null;
117: } catch (ClassCastException e) {
118: System.out.println("class cast err");
119: return null;
120: } catch (Throwable e) {
121: System.out.println("threw " + e);
122: return null;
123: }
124:
125: /*
126: * Optionally set a cookie.
127: * This is used to simulate the session cookie.,
128: */
129: if ((cookieName != null) && (cookieValue != null))
130: conn.setRequestProperty("Cookie", cookieName + "="
131: + cookieValue);
132: /*
133: * That's it. Don't initiate the connection, so the caller may set
134: * other parameters, for example method or more headers.
135: */
136: return conn;
137: }
138:
139: }
|