1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 package jnlp.sample.servlet;
38
39 import javax.servlet.ServletContext;
40 import javax.servlet.http.HttpServletRequest;
41 import java.io.File;
42 import java.util.ArrayList;
43 import java.util.List;
44
45
46
47
48
49 public class DownloadRequest
50 {
51
52 private static final String ARG_ARCH = "arch";
53
54 private static final String ARG_OS = "os";
55
56 private static final String ARG_LOCALE = "locale";
57
58 private static final String ARG_VERSION_ID = "version-id";
59
60 private static final String ARG_CURRENT_VERSION_ID = "current-version-id";
61
62 private static final String ARG_PLATFORM_VERSION_ID = "platform-version-id";
63
64 private static final String ARG_KNOWN_PLATFORMS = "known-platforms";
65
66 private static final String TEST_JRE = "TestJRE";
67
68 private String _path = null;
69
70 private String _version = null;
71
72 private String _currentVersionId = null;
73
74 private String[] _os = null;
75
76 private String[] _arch = null;
77
78 private String[] _locale = null;
79
80 private String[] _knownPlatforms = null;
81
82 private String _query = null;
83
84 private String _testJRE = null;
85
86 private boolean _isPlatformRequest = false;
87
88 private ServletContext _context = null;
89
90 private String _encoding = null;
91
92 private HttpServletRequest _httpRequest = null;
93
94
95 public static final String ACCEPT_ENCODING = "accept-encoding";
96
97
98 public DownloadRequest( HttpServletRequest request )
99 {
100 this( null, request );
101 }
102
103 public DownloadRequest( ServletContext context, HttpServletRequest request )
104 {
105 _context = context;
106 _httpRequest = request;
107 _path = request.getRequestURI();
108 _encoding = request.getHeader( ACCEPT_ENCODING );
109 String context_path = request.getContextPath();
110 if ( context_path != null )
111 {
112 _path = _path.substring( context_path.length() );
113 }
114 if ( _path == null )
115 {
116 _path = request.getServletPath();
117 }
118 if ( _path == null )
119 {
120 _path = "/";
121 }
122 _path = _path.trim();
123 if ( _context != null && !_path.endsWith( "/" ) )
124 {
125 String realPath = _context.getRealPath( _path );
126
127 if ( realPath != null )
128 {
129 File f = new File( realPath );
130 if ( f.exists() && f.isDirectory() )
131 {
132 _path += "/";
133 }
134 }
135 }
136
137 if ( _path.endsWith( "/" ) )
138 {
139 _path += "launch.jnlp";
140 }
141 _version = getParameter( request, ARG_VERSION_ID );
142 _currentVersionId = getParameter( request, ARG_CURRENT_VERSION_ID );
143 _os = getParameterList( request, ARG_OS );
144 _arch = getParameterList( request, ARG_ARCH );
145 _locale = getParameterList( request, ARG_LOCALE );
146 _knownPlatforms = getParameterList( request, ARG_KNOWN_PLATFORMS );
147 String platformVersion = getParameter( request, ARG_PLATFORM_VERSION_ID );
148 _isPlatformRequest = ( platformVersion != null );
149 if ( _isPlatformRequest )
150 {
151 _version = platformVersion;
152 }
153 _query = request.getQueryString();
154 _testJRE = getParameter( request, TEST_JRE );
155 }
156
157
158
159
160
161 private DownloadRequest( DownloadRequest dreq )
162 {
163 _encoding = dreq._encoding;
164 _context = dreq._context;
165 _httpRequest = dreq._httpRequest;
166 _path = dreq._path;
167 _version = dreq._currentVersionId;
168 _currentVersionId = null;
169 _os = dreq._os;
170 _arch = dreq._arch;
171 _locale = dreq._locale;
172 _knownPlatforms = dreq._knownPlatforms;
173 _isPlatformRequest = dreq._isPlatformRequest;
174 _query = dreq._query;
175 _testJRE = dreq._testJRE;
176 }
177
178
179 private String getParameter( HttpServletRequest req, String key )
180 {
181 String res = req.getParameter( key );
182 return ( res == null ) ? null : res.trim();
183 }
184
185
186
187
188 static private String[] getStringList( String str )
189 {
190 if ( str == null )
191 {
192 return null;
193 }
194 List<String> list = new ArrayList<String>();
195 int i = 0;
196 int length = str.length();
197 StringBuffer sb = null;
198 while ( i < length )
199 {
200 char ch = str.charAt( i );
201 if ( ch == ' ' )
202 {
203
204 if ( sb != null )
205 {
206 list.add( sb.toString() );
207 sb = null;
208 }
209 }
210 else if ( ch == '\\' )
211 {
212
213 if ( i + 1 < length )
214 {
215 ch = str.charAt( ++i );
216 if ( sb == null )
217 {
218 sb = new StringBuffer();
219 }
220 sb.append( ch );
221 }
222 }
223 else
224 {
225 if ( sb == null )
226 {
227 sb = new StringBuffer();
228 }
229 sb.append( ch );
230 }
231 i++;
232 }
233
234 if ( sb != null )
235 {
236 list.add( sb.toString() );
237 }
238 if ( list.size() == 0 )
239 {
240 return null;
241 }
242 String[] results = new String[list.size()];
243 return list.toArray( results );
244 }
245
246
247 private String[] getParameterList( HttpServletRequest req, String key )
248 {
249 String res = req.getParameter( key );
250 return ( res == null ) ? null : getStringList( res.trim() );
251 }
252
253
254 public String getPath()
255 {
256 return _path;
257 }
258
259 public String getVersion()
260 {
261 return _version;
262 }
263
264 public String getCurrentVersionId()
265 {
266 return _currentVersionId;
267 }
268
269 public String getQuery()
270 {
271 return _query;
272 }
273
274 public String getTestJRE()
275 {
276 return _testJRE;
277 }
278
279 public String getEncoding()
280 {
281 return _encoding;
282 }
283
284 public String[] getOS()
285 {
286 return _os;
287 }
288
289 public String[] getArch()
290 {
291 return _arch;
292 }
293
294 public String[] getLocale()
295 {
296 return _locale;
297 }
298
299 public String[] getKnownPlatforms()
300 {
301 return _knownPlatforms;
302 }
303
304 public boolean isPlatformRequest()
305 {
306 return _isPlatformRequest;
307 }
308
309 public HttpServletRequest getHttpRequest()
310 {
311 return _httpRequest;
312 }
313
314
315
316
317
318 DownloadRequest getFromDownloadRequest()
319 {
320 return new DownloadRequest( this );
321 }
322
323
324 public String toString()
325 {
326 return "DownloadRequest[path=" + _path + showEntry( " encoding=", _encoding ) + showEntry( " query=", _query ) +
327 showEntry( " TestJRE=", _testJRE ) + showEntry( " version=", _version ) +
328 showEntry( " currentVersionId=", _currentVersionId ) + showEntry( " os=", _os ) +
329 showEntry( " arch=", _arch ) + showEntry( " locale=", _locale ) +
330 showEntry( " knownPlatforms=", _knownPlatforms ) + " isPlatformRequest=" + _isPlatformRequest + "]";
331 }
332
333 private String showEntry( String msg, String value )
334 {
335 if ( value == null )
336 {
337 return "";
338 }
339 return msg + value;
340 }
341
342 private String showEntry( String msg, String[] value )
343 {
344 if ( value == null )
345 {
346 return "";
347 }
348 return msg + java.util.Arrays.asList( value ).toString();
349 }
350 }
351
352