View Javadoc
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  
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   * The DownloadRequest incapsulates all the data in a request
47   * SQE: We need to address query string
48   */
49  public class DownloadRequest
50  {
51      // Arguments
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      // HTTP Compression RFC 2616 : Standard headers
95      public static final String ACCEPT_ENCODING = "accept-encoding";
96  
97      // Contruct Request object based on HTTP request
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(); // This works for *.<ext> invocations
117         }
118         if ( _path == null )
119         {
120             _path = "/"; // No path given
121         }
122         _path = _path.trim();
123         if ( _context != null && !_path.endsWith( "/" ) )
124         {
125             String realPath = _context.getRealPath( _path );
126             // fix for 4474021 - getRealPath might returns NULL
127             if ( realPath != null )
128             {
129                 File f = new File( realPath );
130                 if ( f.exists() && f.isDirectory() )
131                 {
132                     _path += "/";
133                 }
134             }
135         }
136         // Append default file for a directory
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      * Returns a DownloadRequest for the currentVersionId, that can be used
159      * to lookup the existing cached version
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      * Converts a space delimitered string to a list of strings
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                 // A space was hit. Add string to list
204                 if ( sb != null )
205                 {
206                     list.add( sb.toString() );
207                     sb = null;
208                 }
209             }
210             else if ( ch == '\\' )
211             {
212                 // It is a delimiter. Add next character
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++; // Next character
232         }
233         // Make sure to add the last part to the list too
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     /* Split parameter at spaces. Convert '\ ' insto a space */
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     // Query
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      * Returns a DownloadRequest for the currentVersionId, that can be used
316      * to lookup the existing cached version
317      */
318     DownloadRequest getFromDownloadRequest()
319     {
320         return new DownloadRequest( this );
321     }
322 
323     // Debug
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