KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)JnlpResource.java 1.8 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 javax.servlet.ServletContext JavaDoc;
39 import java.net.URL JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.net.URLConnection JavaDoc;
43 import java.util.*;
44
45 /**
46  * A JnlpResource encapsulate the information about a resource that is
47  * needed to process a JNLP Download Request.
48  *
49  * The pattern matching arguments are: name, version-id, os, arch, and locale.
50  *
51  * The outgoing arguments are:
52  * - path to resource in (WAR File)
53  * - product version-id (Version-id to return or null. Typically same as version-id above)
54  * - mime-type for content
55  * - lastModified date of WAR file resource
56  *
57  */

58 public class JnlpResource {
59     private static final String JavaDoc JNLP_MIME_TYPE = "application/x-java-jnlp-file";
60     private static final String JavaDoc JAR_MIME_TYPE = "application/x-java-archive";
61
62     private static final String JavaDoc JAR_MIME_TYPE_NEW = "application/java-archive";
63     
64     // Default extension for the JNLP file
65
private static final String JavaDoc JNLP_EXTENSION = ".jnlp";
66     private static final String JavaDoc JAR_EXTENSION = ".jar";
67     
68     private static String JavaDoc _jnlpExtension = JNLP_EXTENSION;
69     private static String JavaDoc _jarExtension = JAR_EXTENSION;
70     
71     public static void setDefaultExtensions(String JavaDoc jnlpExtension, String JavaDoc jarExtension) {
72     if (jnlpExtension != null && jnlpExtension.length() > 0) {
73         if (!jnlpExtension.startsWith(".")) jnlpExtension = "." + jnlpExtension;
74         _jnlpExtension = jnlpExtension;
75     }
76     if (jarExtension != null && jarExtension.length() > 0) {
77         if (!jarExtension .startsWith(".")) jarExtension = "." + jarExtension ;
78         _jarExtension = jarExtension;
79     }
80     }
81     
82     /* Pattern matching arguments */
83     private String JavaDoc _name; // Name of resource with path (this is the same as path for non-version based)
84
private String JavaDoc _versionId; // Version-id for resource, or null if none
85
private String JavaDoc[] _osList; // List of OSes for which resource should be returned
86
private String JavaDoc[] _archList; // List of architectures for which the resource should be returned
87
private String JavaDoc[] _localeList; // List of locales for which the resource should be returned
88
/* Information used for reply */
89     private String JavaDoc _path; // Path to resource in WAR file (unique)
90
private URL JavaDoc _resource; // URL to resource in WAR file (unique - same as above really)
91
private long _lastModified; // Last modified in WAR file
92
private String JavaDoc _mimeType; // Mime-type for resource
93
private String JavaDoc _returnVersionId; // Version Id to return
94
private String JavaDoc _encoding; // Accept encoding
95

96     public JnlpResource(ServletContext JavaDoc context, String JavaDoc path) {
97     this(context, null, null, null, null, null, path, null);
98     }
99
100     public JnlpResource(ServletContext JavaDoc context,
101             String JavaDoc name,
102             String JavaDoc versionId,
103             String JavaDoc[] osList,
104             String JavaDoc[] archList,
105             String JavaDoc[] localeList,
106             String JavaDoc path,
107             String JavaDoc returnVersionId) {
108     this(context, name, versionId, osList, archList, localeList, path,
109          returnVersionId, null);
110     }
111     
112     public JnlpResource(ServletContext JavaDoc context,
113             String JavaDoc name,
114             String JavaDoc versionId,
115             String JavaDoc[] osList,
116             String JavaDoc[] archList,
117             String JavaDoc[] localeList,
118             String JavaDoc path,
119             String JavaDoc returnVersionId,
120             String JavaDoc encoding) {
121     // Matching arguments
122
_encoding = encoding;
123     _name = name;
124     _versionId = versionId;
125     _osList = osList;
126     _archList = archList;
127     _localeList = localeList;
128
129     _returnVersionId = returnVersionId;
130
131     /* Check for existance and get last modified timestamp */
132     try {
133         String JavaDoc orig_path = path.trim();
134         String JavaDoc search_path = orig_path;
135         _resource = context.getResource(orig_path);
136         _mimeType = getMimeType(context, orig_path);
137         if (_resource != null) {
138     
139         boolean found = false;
140         // pack200 compression
141
if (encoding != null && _mimeType != null &&
142             (_mimeType.compareTo(JAR_MIME_TYPE) == 0 || _mimeType.compareTo(JAR_MIME_TYPE_NEW) == 0) &&
143             encoding.toLowerCase().indexOf(DownloadResponse.PACK200_GZIP_ENCODING) > -1){
144             search_path = orig_path + ".pack.gz";
145             _resource = context.getResource(search_path);
146             // Get last modified time
147
if (_resource != null) {
148             _lastModified = getLastModified(context, _resource, search_path);
149             if (_lastModified != 0) {
150                 _path = search_path;
151                 found = true;
152             } else {
153                 _resource = null;
154             }
155             }
156         }
157
158         // gzip compression
159
if (found == false && encoding != null &&
160             encoding.toLowerCase().indexOf(DownloadResponse.GZIP_ENCODING) > -1){
161             search_path = orig_path + ".gz";
162             _resource = context.getResource(search_path);
163             // Get last modified time
164
if (_resource != null) {
165             _lastModified = getLastModified(context, _resource, search_path);
166             if (_lastModified != 0) {
167                 _path = search_path;
168                 found = true;
169             } else {
170                 _resource = null;
171             }
172             }
173         }
174         
175         if (found == false) {
176             // no compression
177
search_path = orig_path;
178             _resource = context.getResource(search_path);
179             // Get last modified time
180
if (_resource != null) {
181             _lastModified = getLastModified(context, _resource, search_path);
182             if (_lastModified != 0) {
183                 _path = search_path;
184                 found = true;
185             } else {
186                 _resource = null;
187             }
188             }
189         }
190         }
191     } catch(IOException JavaDoc ioe) {
192         _resource = null;
193     }
194     }
195
196     long getLastModified(ServletContext JavaDoc context, URL JavaDoc resource, String JavaDoc path) {
197     long lastModified = 0;
198     URLConnection JavaDoc conn;
199     try {
200         // Get last modified time
201
conn = resource.openConnection();
202         lastModified = conn.getLastModified();
203     } catch (Exception JavaDoc e) {
204         // do nothing
205
}
206     
207     if (lastModified == 0) {
208         // Arguably a bug in the JRE will not set the lastModified for file URLs, and
209
// always return 0. This is a workaround for that problem.
210
String JavaDoc filepath = context.getRealPath(path);
211         if (filepath != null) {
212         File JavaDoc f = new File JavaDoc(filepath);
213         if (f.exists()) {
214             lastModified = f.lastModified();
215         }
216         }
217     }
218     return lastModified;
219     }
220
221     /* Get resource specific attributes */
222     public String JavaDoc getPath() { return _path; }
223     public URL JavaDoc getResource() { return _resource; }
224     public String JavaDoc getMimeType() { return _mimeType; }
225     public long getLastModified() { return _lastModified; }
226     public boolean exists() { return _resource != null; }
227     public boolean isJnlpFile() { return _path.endsWith(_jnlpExtension); }
228     public boolean isJarFile() { return _path.endsWith(_jarExtension); }
229     
230     /* Get JNLP version specific attributes */
231     public String JavaDoc getName() { return _name; }
232     public String JavaDoc getVersionId() { return _versionId; }
233     public String JavaDoc[] getOSList() { return _osList; }
234     public String JavaDoc[] getArchList() { return _archList; }
235     public String JavaDoc[] getLocaleList() { return _localeList; }
236     public String JavaDoc getReturnVersionId() { return _returnVersionId; }
237
238     private String JavaDoc getMimeType(ServletContext JavaDoc context, String JavaDoc path) {
239     String JavaDoc mimeType = context.getMimeType(path);
240     if (mimeType != null) return mimeType;
241     if (path.endsWith(_jnlpExtension)) return JNLP_MIME_TYPE;
242     if (path.endsWith(_jarExtension)) return JAR_MIME_TYPE;
243     return "application/unknown";
244     }
245     
246     /** Print info about an entry */
247     public String JavaDoc toString() {
248     return "JnlpResource[WAR Path: " + _path +
249         showEntry(" versionId=",_versionId) +
250         showEntry(" name=", _name) +
251         " lastModified=" + new Date(_lastModified) +
252         showEntry(" osList=", _osList) +
253         showEntry(" archList=", _archList) +
254         showEntry(" localeList=", _localeList) + "]" +
255         showEntry(" returnVersionId=", _returnVersionId) + "]";
256     
257     }
258     
259     private String JavaDoc showEntry(String JavaDoc msg, String JavaDoc value) {
260     if (value == null) return "";
261     return msg + value;
262     }
263     
264     private String JavaDoc showEntry(String JavaDoc msg, String JavaDoc[] value) {
265     if (value == null) return "";
266     return msg + java.util.Arrays.asList(value).toString();
267     }
268 }
269
270
271
272
273
Popular Tags