1 36 37 package jnlp.sample.servlet; 38 import java.io.*; 39 import java.util.*; 40 import java.net.URL ; 41 import java.net.URLConnection ; 42 import javax.servlet.http.HttpServletResponse ; 43 44 48 abstract public class DownloadResponse { 49 private static final String HEADER_LASTMOD = "Last-Modified"; 50 private static final String HEADER_JNLP_VERSION = "x-java-jnlp-version-id"; 51 private static final String JNLP_ERROR_MIMETYPE = "application/x-java-jnlp-error"; 52 53 public static final int STS_00_OK = 0; 54 public static final int ERR_10_NO_RESOURCE = 10; 55 public static final int ERR_11_NO_VERSION = 11; 56 public static final int ERR_20_UNSUP_OS = 20; 57 public static final int ERR_21_UNSUP_ARCH = 21; 58 public static final int ERR_22_UNSUP_LOCALE = 22; 59 public static final int ERR_23_UNSUP_JRE = 23; 60 public static final int ERR_99_UNKNOWN = 99; 61 62 public static final String CONTENT_ENCODING = "content-encoding"; 64 public static final String GZIP_ENCODING = "gzip"; 66 public static final String PACK200_GZIP_ENCODING = "pack200-gzip"; 67 68 public DownloadResponse() { } 69 70 public String toString() { return getClass().getName(); } 71 72 73 abstract void sendRespond(HttpServletResponse response) throws IOException; 74 75 76 static DownloadResponse getNotFoundResponse() { return new NotFoundResponse(); } 77 static DownloadResponse getNoContentResponse() { return new NotFoundResponse(); } 78 static DownloadResponse getJnlpErrorResponse(int jnlpErrorCode) { return new JnlpErrorResponse(jnlpErrorCode); } 79 80 81 82 static DownloadResponse getNotModifiedResponse() { 83 return new NotModifiedResponse(); 84 } 85 86 static DownloadResponse getHeadRequestResponse(String mimeType, 87 String versionId, long lastModified, int contentLength) { 88 return new HeadRequestResponse(mimeType, versionId, lastModified, 89 contentLength); 90 } 91 92 static DownloadResponse getFileDownloadResponse(byte[] content, String mimeType, long timestamp, String versionId) { 93 return new ByteArrayFileDownloadResponse(content, mimeType, versionId, timestamp); 94 } 95 96 static DownloadResponse getFileDownloadResponse(URL resource, String mimeType, long timestamp, String versionId) { 97 return new ResourceFileDownloadResponse(resource, mimeType, versionId, timestamp); 98 } 99 100 static DownloadResponse getFileDownloadResponse(File file, String mimeType, long timestamp, String versionId) { 101 return new DiskFileDownloadResponse(file, mimeType, versionId, timestamp); 102 } 103 104 108 static private class NotModifiedResponse extends DownloadResponse { 109 public void sendRespond(HttpServletResponse response) throws 110 IOException { 111 response.sendError(HttpServletResponse.SC_NOT_MODIFIED); 112 } 113 } 114 115 static private class NotFoundResponse extends DownloadResponse { 116 public void sendRespond(HttpServletResponse response) throws IOException { 117 response.sendError(HttpServletResponse.SC_NOT_FOUND); 118 } 119 } 120 121 static private class NoContentResponse extends DownloadResponse { 122 public void sendRespond(HttpServletResponse response) throws IOException { 123 response.sendError(HttpServletResponse.SC_NO_CONTENT); 124 } 125 } 126 127 static private class HeadRequestResponse extends DownloadResponse { 128 private String _mimeType; 129 private String _versionId; 130 private long _lastModified; 131 private int _contentLength; 132 133 HeadRequestResponse(String mimeType, String versionId, 134 long lastModified, int contentLength) { 135 _mimeType = mimeType; 136 _versionId = versionId; 137 _lastModified = lastModified; 138 _contentLength = contentLength; 139 } 140 141 142 public void sendRespond(HttpServletResponse response) throws 143 IOException { 144 response.setContentType(_mimeType); 146 response.setContentLength(_contentLength); 147 if (_versionId != null) { 148 response.setHeader(HEADER_JNLP_VERSION, _versionId); 149 } 150 if (_lastModified != 0) { 151 response.setDateHeader(HEADER_LASTMOD, _lastModified); 152 } 153 response.sendError(HttpServletResponse.SC_OK); 154 } 155 } 156 157 static public class JnlpErrorResponse extends DownloadResponse { 158 private String _message; 159 160 public JnlpErrorResponse(int jnlpErrorCode) { 161 String msg = Integer.toString(jnlpErrorCode); 162 String dsc = "No description"; 163 try { 164 dsc = JnlpDownloadServlet.getResourceBundle().getString("servlet.jnlp.err." + msg); 165 } catch (MissingResourceException mre) { } 166 _message = msg + " " + dsc; 167 } 168 169 public void sendRespond(HttpServletResponse response) throws IOException { 170 response.setContentType(JNLP_ERROR_MIMETYPE); 171 PrintWriter pw = response.getWriter(); 172 pw.println(_message); 173 }; 174 175 public String toString() { return super.toString() + "[" + _message + "]"; } 176 } 177 178 static private abstract class FileDownloadResponse extends DownloadResponse { 179 private String _mimeType; 180 private String _versionId; 181 private long _lastModified; 182 private String _fileName; 183 184 FileDownloadResponse(String mimeType, String versionId, long lastModified) { 185 _mimeType = mimeType; 186 _versionId = versionId; 187 _lastModified = lastModified; 188 _fileName = null; 189 } 190 191 FileDownloadResponse(String mimeType, String versionId, long lastModified, String fileName) { 192 _mimeType = mimeType; 193 _versionId = versionId; 194 _lastModified = lastModified; 195 _fileName = fileName; 196 } 197 198 199 200 String getMimeType() { return _mimeType; } 201 String getVersionId() { return _versionId; } 202 long getLastModified() { return _lastModified; } 203 abstract int getContentLength() throws IOException; 204 abstract InputStream getContent() throws IOException; 205 206 207 public void sendRespond(HttpServletResponse response) throws IOException { 208 response.setContentType(getMimeType()); 210 response.setContentLength(getContentLength()); 211 if (getVersionId() != null) response.setHeader(HEADER_JNLP_VERSION, getVersionId()); 212 if (getLastModified() != 0) response.setDateHeader(HEADER_LASTMOD, getLastModified()); 213 if (_fileName != null) { 214 215 if (_fileName.endsWith(".pack.gz")) { 216 response.setHeader(CONTENT_ENCODING, PACK200_GZIP_ENCODING ); 217 } else if (_fileName.endsWith(".gz")) { 218 response.setHeader(CONTENT_ENCODING, GZIP_ENCODING ); 219 } else { 220 response.setHeader(CONTENT_ENCODING, null); 221 } 222 } 223 224 InputStream in = getContent(); 226 OutputStream out = response.getOutputStream(); 227 try { 228 byte[] bytes = new byte[32 * 1024]; 229 int read; 230 while ((read = in.read(bytes)) != -1) { 231 out.write(bytes, 0, read); 232 } 233 } finally { 234 if (in != null) in.close(); 235 } 236 } 237 238 protected String getArgString() { 239 long length = 0; 240 try { 241 length = getContentLength(); 242 } catch(IOException ioe) { } 243 return "Mimetype=" + getMimeType() + 244 " VersionId=" + getVersionId() + 245 " Timestamp=" + new Date(getLastModified()) + 246 " Length=" + length; 247 } 248 } 249 250 static private class ByteArrayFileDownloadResponse extends FileDownloadResponse { 251 private byte[] _content; 252 253 ByteArrayFileDownloadResponse(byte[] content, String mimeType, String versionId, long lastModified) { 254 super(mimeType, versionId, lastModified); 255 _content = content; 256 } 257 258 int getContentLength() { return _content.length; } 259 InputStream getContent() { return new ByteArrayInputStream(_content); } 260 public String toString() { return super.toString() + "[ " + getArgString() + "]"; } 261 } 262 263 static private class ResourceFileDownloadResponse extends FileDownloadResponse { 264 URL _url; 265 266 ResourceFileDownloadResponse(URL url, String mimeType, String versionId, long lastModified) { 267 super(mimeType, versionId, lastModified, url.toString()); 268 _url= url; 269 } 270 271 int getContentLength() throws IOException { 272 return _url.openConnection().getContentLength(); 273 } 274 InputStream getContent() throws IOException { 275 return _url.openConnection().getInputStream(); 276 } 277 public String toString() { return super.toString() + "[ " + getArgString() + "]"; } 278 } 279 280 static private class DiskFileDownloadResponse extends FileDownloadResponse { 281 private File _file; 282 283 DiskFileDownloadResponse(File file, String mimeType, String versionId, long lastModified) { 284 super(mimeType, versionId, lastModified, file.getName()); 285 _file = file; 286 } 287 288 int getContentLength() throws IOException { 289 return (int)_file.length(); 290 } 291 292 InputStream getContent() throws IOException { 293 return new BufferedInputStream(new FileInputStream(_file)); 294 } 295 296 public String toString() { return super.toString() + "[ " + getArgString() + "]"; } 297 } 298 } 299 300 301 302 | Popular Tags |