1 36 37 package jnlp.sample.servlet; 38 import java.io.File ; 39 import java.util.ArrayList ; 40 import javax.servlet.*; 41 import javax.servlet.http.*; 42 43 47 public class DownloadRequest { 48 private static final String ARG_ARCH = "arch"; 50 private static final String ARG_OS = "os"; 51 private static final String ARG_LOCALE = "locale"; 52 private static final String ARG_VERSION_ID = "version-id"; 53 private static final String ARG_CURRENT_VERSION_ID = "current-version-id"; 54 private static final String ARG_PLATFORM_VERSION_ID = "platform-version-id"; 55 private static final String ARG_KNOWN_PLATFORMS = "known-platforms"; 56 private static final String TEST_JRE = "TestJRE"; 57 58 private String _path = null; 59 private String _version = null; 60 private String _currentVersionId = null; 61 private String [] _os = null; 62 private String [] _arch = null; 63 private String [] _locale = null; 64 private String [] _knownPlatforms = null; 65 private String _query = null; 66 private String _testJRE = null; 67 private boolean _isPlatformRequest = false; 68 private ServletContext _context = null; 69 private String _encoding = null; 70 71 private HttpServletRequest _httpRequest = null; 72 73 public static final String ACCEPT_ENCODING = "accept-encoding"; 75 76 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 context_path = request.getContextPath(); 87 if (context_path != null) _path = _path.substring(context_path.length()); 88 if (_path == null) _path = request.getServletPath(); if (_path == null) _path = "/"; _path = _path.trim(); 91 if (_context != null && !_path.endsWith("/")) { 92 String realPath = _context.getRealPath(_path); 93 if (realPath != null) { 95 File f = new File (realPath); 96 if (f != null && f.exists() && f.isDirectory()) { 97 _path += "/"; 98 } 99 } 100 } 101 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 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 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 getParameter(HttpServletRequest req, String key) { 137 String res = req.getParameter(key); 138 return (res == null) ? null : res.trim(); 139 } 140 141 142 static private String [] getStringList(String str) { 143 if (str == null) return null; 144 ArrayList list = new ArrayList (); 145 int i = 0; 146 int length = str.length(); 147 StringBuffer sb = null; 148 while(i < length) { 149 char ch = str.charAt(i); 150 if (ch == ' ') { 151 if (sb != null) { 153 list.add(sb.toString()); 154 sb = null; 155 } 156 } else if (ch == '\\') { 157 if (i + 1 < length) { 159 ch = str.charAt(++i); 160 if (sb == null) sb = new StringBuffer (); 161 sb.append(ch); 162 } 163 } else { 164 if (sb == null) sb = new StringBuffer (); 165 sb.append(ch); 166 } 167 i++; } 169 if (sb != null) { 171 list.add(sb.toString()); 172 } 173 if (list.size() == 0) return null; 174 String [] results = new String [list.size()]; 175 return (String [])list.toArray(results); 176 } 177 178 179 private String [] getParameterList(HttpServletRequest req, String key) { 180 String res = req.getParameter(key); 181 return (res == null) ? null : getStringList(res.trim()); 182 } 183 184 public String getPath() { return _path; } 186 public String getVersion() { return _version; } 187 public String getCurrentVersionId() { return _currentVersionId; } 188 public String getQuery() { return _query; } 189 public String getTestJRE() { return _testJRE; } 190 public String getEncoding() { return _encoding; } 191 public String [] getOS() { return _os; } 192 public String [] getArch() { return _arch; } 193 public String [] getLocale() { return _locale; } 194 public String [] getKnownPlatforms() { return _knownPlatforms; } 195 public boolean isPlatformRequest() { return _isPlatformRequest; } 196 public HttpServletRequest getHttpRequest() { return _httpRequest; } 197 198 201 DownloadRequest getFromDownloadRequest() { 202 return new DownloadRequest(this); 203 } 204 205 public String 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 showEntry(String msg, String value) { 221 if (value == null) return ""; 222 return msg + value; 223 } 224 225 private String showEntry(String msg, String [] value) { 226 if (value == null) return ""; 227 return msg + java.util.Arrays.asList(value).toString(); 228 } 229 } 230 231 232 | Popular Tags |