KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jnlp > sample > servlet > DownloadRequest


1 /*
2  * @(#)DownloadRequest.java 1.7 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 package jnlp.sample.servlet;
38 import java.io.File JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import javax.servlet.*;
41 import javax.servlet.http.*;
42
43 /**
44  * The DownloadRequest incapsulates all the data in a request
45  * SQE: We need to address query string
46  */

47 public class DownloadRequest {
48     // Arguments
49
private static final String JavaDoc ARG_ARCH = "arch";
50     private static final String JavaDoc ARG_OS = "os";
51     private static final String JavaDoc ARG_LOCALE = "locale";
52     private static final String JavaDoc ARG_VERSION_ID = "version-id";
53     private static final String JavaDoc ARG_CURRENT_VERSION_ID = "current-version-id";
54     private static final String JavaDoc ARG_PLATFORM_VERSION_ID = "platform-version-id";
55     private static final String JavaDoc ARG_KNOWN_PLATFORMS = "known-platforms";
56     private static final String JavaDoc TEST_JRE = "TestJRE";
57     
58     private String JavaDoc _path = null;
59     private String JavaDoc _version = null;
60     private String JavaDoc _currentVersionId = null;
61     private String JavaDoc[] _os = null;
62     private String JavaDoc[] _arch = null;
63     private String JavaDoc[] _locale = null;
64     private String JavaDoc[] _knownPlatforms = null;
65     private String JavaDoc _query = null;
66     private String JavaDoc _testJRE = null;
67     private boolean _isPlatformRequest = false;
68     private ServletContext _context = null;
69     private String JavaDoc _encoding = null;
70     
71     private HttpServletRequest _httpRequest = null;
72
73     // HTTP Compression RFC 2616 : Standard headers
74
public static final String JavaDoc ACCEPT_ENCODING = "accept-encoding";
75     
76     // Contruct Request object based on HTTP request
77
public DownloadRequest(HttpServletRequest request) {
78     this((ServletContext)null, request);
79     }
80
81     public DownloadRequest(ServletContext context, HttpServletRequest request) {
82     _context = context;
83     _httpRequest = request;
84     _path = request.getRequestURI();
85     _encoding = request.getHeader(ACCEPT_ENCODING);
86     String JavaDoc context_path = request.getContextPath();
87     if (context_path != null) _path = _path.substring(context_path.length());
88     if (_path == null) _path = request.getServletPath(); // This works for *.<ext> invocations
89
if (_path == null) _path = "/"; // No path given
90
_path = _path.trim();
91     if (_context != null && !_path.endsWith("/")) {
92         String JavaDoc realPath = _context.getRealPath(_path);
93         // fix for 4474021 - getRealPath might returns NULL
94
if (realPath != null) {
95         File JavaDoc f = new File JavaDoc(realPath);
96         if (f != null && f.exists() && f.isDirectory()) {
97             _path += "/";
98         }
99         }
100     }
101         // Append default file for a directory
102
if (_path.endsWith("/")) _path += "launch.jnlp";
103     _version = getParameter(request, ARG_VERSION_ID);
104     _currentVersionId = getParameter(request, ARG_CURRENT_VERSION_ID);
105     _os = getParameterList(request, ARG_OS);
106     _arch = getParameterList(request, ARG_ARCH);
107     _locale = getParameterList(request, ARG_LOCALE);
108     _knownPlatforms = getParameterList(request, ARG_KNOWN_PLATFORMS);
109     String JavaDoc platformVersion = getParameter(request, ARG_PLATFORM_VERSION_ID);
110     _isPlatformRequest = (platformVersion != null);
111     if (_isPlatformRequest) _version = platformVersion;
112     _query = request.getQueryString();
113     _testJRE = getParameter(request, TEST_JRE);
114     }
115     
116     /** Returns a DownloadRequest for the currentVersionId, that can be used
117      * to lookup the existing cached version
118      */

119     private DownloadRequest(DownloadRequest dreq) {
120     _encoding = dreq._encoding;
121     _context = dreq._context;
122     _httpRequest = dreq._httpRequest;
123     _path = dreq._path;
124     _version = dreq._currentVersionId;
125     _currentVersionId = null;
126     _os = dreq._os;
127     _arch = dreq._arch;
128     _locale = dreq._locale;
129     _knownPlatforms = dreq._knownPlatforms;
130     _isPlatformRequest = dreq._isPlatformRequest;
131     _query = dreq._query;
132     _testJRE = dreq._testJRE;
133     }
134     
135     
136     private String JavaDoc getParameter(HttpServletRequest req, String JavaDoc key) {
137     String JavaDoc res = req.getParameter(key);
138     return (res == null) ? null : res.trim();
139     }
140
141      /** Converts a space delimitered string to a list of strings */
142     static private String JavaDoc[] getStringList(String JavaDoc str) {
143         if (str == null) return null;
144         ArrayList JavaDoc list = new ArrayList JavaDoc();
145         int i = 0;
146         int length = str.length();
147         StringBuffer JavaDoc sb = null;
148         while(i < length) {
149             char ch = str.charAt(i);
150             if (ch == ' ') {
151                 // A space was hit. Add string to list
152
if (sb != null) {
153                     list.add(sb.toString());
154                     sb = null;
155                 }
156             } else if (ch == '\\') {
157                 // It is a delimiter. Add next character
158
if (i + 1 < length) {
159                     ch = str.charAt(++i);
160                     if (sb == null) sb = new StringBuffer JavaDoc();
161                     sb.append(ch);
162                 }
163             } else {
164                 if (sb == null) sb = new StringBuffer JavaDoc();
165                 sb.append(ch);
166             }
167             i++; // Next character
168
}
169         // Make sure to add the last part to the list too
170
if (sb != null) {
171             list.add(sb.toString());
172         }
173     if (list.size() == 0) return null;
174         String JavaDoc[] results = new String JavaDoc[list.size()];
175         return (String JavaDoc[])list.toArray(results);
176     }
177     
178     /* Split parameter at spaces. Convert '\ ' insto a space */
179     private String JavaDoc[] getParameterList(HttpServletRequest req, String JavaDoc key) {
180     String JavaDoc res = req.getParameter(key);
181     return (res == null) ? null : getStringList(res.trim());
182     }
183     
184     // Query
185
public String JavaDoc getPath() { return _path; }
186     public String JavaDoc getVersion() { return _version; }
187     public String JavaDoc getCurrentVersionId() { return _currentVersionId; }
188     public String JavaDoc getQuery() { return _query; }
189     public String JavaDoc getTestJRE() { return _testJRE; }
190     public String JavaDoc getEncoding() { return _encoding; }
191     public String JavaDoc[] getOS() { return _os; }
192     public String JavaDoc[] getArch() { return _arch; }
193     public String JavaDoc[] getLocale() { return _locale; }
194     public String JavaDoc[] getKnownPlatforms() { return _knownPlatforms; }
195     public boolean isPlatformRequest() { return _isPlatformRequest; }
196     public HttpServletRequest getHttpRequest() { return _httpRequest; }
197     
198     /** Returns a DownloadRequest for the currentVersionId, that can be used
199      * to lookup the existing cached version
200      */

201     DownloadRequest getFromDownloadRequest() {
202     return new DownloadRequest(this);
203     }
204     
205     // Debug
206
public String JavaDoc toString() {
207     return "DownloadRequest[path=" + _path +
208         showEntry(" encoding=", _encoding) +
209         showEntry(" query=", _query) +
210         showEntry(" TestJRE=", _testJRE) +
211         showEntry(" version=", _version) +
212         showEntry(" currentVersionId=", _currentVersionId) +
213         showEntry(" os=", _os) +
214         showEntry(" arch=", _arch) +
215         showEntry(" locale=", _locale) +
216         showEntry(" knownPlatforms=", _knownPlatforms)
217         + " isPlatformRequest=" + _isPlatformRequest + "]";
218     }
219     
220     private String JavaDoc showEntry(String JavaDoc msg, String JavaDoc value) {
221     if (value == null) return "";
222     return msg + value;
223     }
224     
225     private String JavaDoc showEntry(String JavaDoc msg, String JavaDoc[] value) {
226     if (value == null) return "";
227     return msg + java.util.Arrays.asList(value).toString();
228     }
229 }
230
231
232
Popular Tags