KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > applet > LBSConnection


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: LBSConnection.java,v 1.2 2005/03/24 10:51:16 slobodan Exp $
23  */

24
25
26
27
28 package com.lutris.applet;
29
30 import java.applet.Applet JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.net.MalformedURLException JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.net.URLConnection JavaDoc;
35 //import java.util.*;
36

37 /**
38  * This file is the counterpart to
39  * com.lutris.appserver.server.httpPresentation.AppletUtils. It is meant
40  * to be added to the applet's jar file or classes directory. The methods
41  * are split into two files so that the applet does not need to include
42  * any com.lutris.appserver.server.* classes. <P>
43  *
44  * A dynamicly generated page contained the <APPLET...> tag that lead to
45  * the applet calling this function. That <APPLET...> tag was created by
46  * com.lutris.appserver.server.httpPresentation.AppletUtils.createAppletTag.
47  * When the applet wants to establish a connection back to the
48  * Multiserver, simply call the contactServer() and the extra
49  * initialization parameters added by createAppletTag() will be used to
50  * contact the server. Specifically, the request will match the user's
51  * current session.
52  *
53  * @see com.lutris.appserver.server.httpPresentation.AppletUtils
54  */

55 public class LBSConnection {
56
57     public static final String JavaDoc nameParamName = "cookieName";
58     public static final String JavaDoc valueParamName = "cookieValue";
59     public static final String JavaDoc targetParamName = "targetURL";
60
61     /**
62      * Do not create instances; use the static method.
63      */

64     private LBSConnection() {
65     }
66
67     /**
68      * Use the extra initialization parameters added to the APPLET tag
69      * by AppletUtils.createAppletTag() to open a connection back to
70      * the Multiserver, using the same session as the one used
71      * to get the HTML page that contained the APPLET tag. <P>
72      *
73      * The resulting URLConnection is ready to have
74      * <CODE>connect()</CODE> called on it. You may initialize more
75      * options if you wish, for example <CODE>setDoOutput()</CODE> or
76      * <CODE>setRequestMethod()</CODE> before calling <CODE>connect()</CODE>.
77      *
78      * The resulting URLConnection may also be a HttpURLConnection, but
79      * this is not guarenteed. In the JDK on Windows and Solaris it is,
80      * but when running in Netscape Communicator as an applet, it is not.
81      * So to be safe, we return the more generic class. The main
82      * functionality missing is the ability to set the request method.
83      * However, if you call <CODE>setDoOutput(true)</CODE> in Communicator,
84      * it will issue a POST request.
85      *
86      * @param applet
87      * The applet that is calling this method.
88      * @return
89      * A connection back to the server, or null if an error occured.
90      */

91     public static URLConnection JavaDoc contactServer(Applet JavaDoc applet) {
92         /*
93          * Get the extra parameters. Cookie is optional.
94          */

95         String JavaDoc targetURL = applet.getParameter(targetParamName);
96 System.out.println("target = " + targetURL);
97         if (targetURL == null)
98             return null;
99         String JavaDoc cookieName = applet.getParameter(nameParamName);
100 System.out.println("name = " + cookieName);
101         String JavaDoc cookieValue = applet.getParameter(valueParamName);
102 System.out.println("value = " + cookieValue);
103         /*
104          * Get ready to contact the server.
105          */

106         URL JavaDoc url = null;
107         try {
108             url = new URL JavaDoc(targetURL);
109         } catch (MalformedURLException JavaDoc e) {
110 System.out.println("bad url!");
111             return null;
112         }
113         URLConnection JavaDoc conn = null;
114         try {
115             conn = url.openConnection();
116 System.out.println("conn = " + conn);
117         } catch (IOException JavaDoc e) {
118 System.out.println("io err");
119             return null;
120         } catch (ClassCastException JavaDoc e) {
121 System.out.println("class cast err");
122             return null;
123         } catch (Throwable JavaDoc e) {
124 System.out.println("threw " + e);
125             return null;
126         }
127
128
129         /*
130          * Optionally set a cookie.
131          * This is used to simulate the session cookie.,
132          */

133         if ((cookieName != null) && (cookieValue != null))
134             conn.setRequestProperty("Cookie", cookieName + "=" + cookieValue);
135         /*
136          * That's it. Don't initiate the connection, so the caller may set
137          * other parameters, for example method or more headers.
138          */

139         return conn;
140     }
141
142 }
143
144
Popular Tags