1 36 37 package jnlp.sample.servlet; 38 import javax.servlet.ServletContext ; 39 import java.net.URL ; 40 import java.io.File ; 41 import java.io.IOException ; 42 import java.net.URLConnection ; 43 import java.util.*; 44 45 58 public class JnlpResource { 59 private static final String JNLP_MIME_TYPE = "application/x-java-jnlp-file"; 60 private static final String JAR_MIME_TYPE = "application/x-java-archive"; 61 62 private static final String JAR_MIME_TYPE_NEW = "application/java-archive"; 63 64 private static final String JNLP_EXTENSION = ".jnlp"; 66 private static final String JAR_EXTENSION = ".jar"; 67 68 private static String _jnlpExtension = JNLP_EXTENSION; 69 private static String _jarExtension = JAR_EXTENSION; 70 71 public static void setDefaultExtensions(String jnlpExtension, String 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 83 private String _name; private String _versionId; private String [] _osList; private String [] _archList; private String [] _localeList; 89 private String _path; private URL _resource; private long _lastModified; private String _mimeType; private String _returnVersionId; private String _encoding; 96 public JnlpResource(ServletContext context, String path) { 97 this(context, null, null, null, null, null, path, null); 98 } 99 100 public JnlpResource(ServletContext context, 101 String name, 102 String versionId, 103 String [] osList, 104 String [] archList, 105 String [] localeList, 106 String path, 107 String returnVersionId) { 108 this(context, name, versionId, osList, archList, localeList, path, 109 returnVersionId, null); 110 } 111 112 public JnlpResource(ServletContext context, 113 String name, 114 String versionId, 115 String [] osList, 116 String [] archList, 117 String [] localeList, 118 String path, 119 String returnVersionId, 120 String encoding) { 121 _encoding = encoding; 123 _name = name; 124 _versionId = versionId; 125 _osList = osList; 126 _archList = archList; 127 _localeList = localeList; 128 129 _returnVersionId = returnVersionId; 130 131 132 try { 133 String orig_path = path.trim(); 134 String 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 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 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 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 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 search_path = orig_path; 178 _resource = context.getResource(search_path); 179 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 ioe) { 192 _resource = null; 193 } 194 } 195 196 long getLastModified(ServletContext context, URL resource, String path) { 197 long lastModified = 0; 198 URLConnection conn; 199 try { 200 conn = resource.openConnection(); 202 lastModified = conn.getLastModified(); 203 } catch (Exception e) { 204 } 206 207 if (lastModified == 0) { 208 String filepath = context.getRealPath(path); 211 if (filepath != null) { 212 File f = new File (filepath); 213 if (f.exists()) { 214 lastModified = f.lastModified(); 215 } 216 } 217 } 218 return lastModified; 219 } 220 221 222 public String getPath() { return _path; } 223 public URL getResource() { return _resource; } 224 public String 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 231 public String getName() { return _name; } 232 public String getVersionId() { return _versionId; } 233 public String [] getOSList() { return _osList; } 234 public String [] getArchList() { return _archList; } 235 public String [] getLocaleList() { return _localeList; } 236 public String getReturnVersionId() { return _returnVersionId; } 237 238 private String getMimeType(ServletContext context, String path) { 239 String 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 247 public String 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 showEntry(String msg, String value) { 260 if (value == null) return ""; 261 return msg + value; 262 } 263 264 private String showEntry(String msg, String [] value) { 265 if (value == null) return ""; 266 return msg + java.util.Arrays.asList(value).toString(); 267 } 268 } 269 270 271 272 273 | Popular Tags |