KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)DownloadResponse.java 1.8 07/03/15
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 java.io.*;
39 import java.util.*;
40 import java.net.URL JavaDoc;
41 import java.net.URLConnection JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43
44 /**
45  * A class used to encapsulate a file response, and
46  * factory methods to create some common types.
47  */

48 abstract public class DownloadResponse {
49     private static final String JavaDoc HEADER_LASTMOD = "Last-Modified";
50     private static final String JavaDoc HEADER_JNLP_VERSION = "x-java-jnlp-version-id";
51     private static final String JavaDoc 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     // HTTP Compression RFC 2616 : Standard headers
63
public static final String JavaDoc CONTENT_ENCODING = "content-encoding";
64     // HTTP Compression RFC 2616 : Standard header for HTTP/Pack200 Compression
65
public static final String JavaDoc GZIP_ENCODING = "gzip";
66     public static final String JavaDoc PACK200_GZIP_ENCODING = "pack200-gzip";
67
68     public DownloadResponse() { /* do nothing */ }
69     
70     public String JavaDoc toString() { return getClass().getName(); }
71     
72     /** Post information to an HttpResponse */
73     abstract void sendRespond(HttpServletResponse JavaDoc response) throws IOException;
74     
75     /** Factory methods for error responses */
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     /** Factory method for file download responses */
81    
82     static DownloadResponse getNotModifiedResponse() {
83         return new NotModifiedResponse();
84     }
85     
86     static DownloadResponse getHeadRequestResponse(String JavaDoc mimeType,
87             String JavaDoc versionId, long lastModified, int contentLength) {
88         return new HeadRequestResponse(mimeType, versionId, lastModified,
89                 contentLength);
90     }
91     
92     static DownloadResponse getFileDownloadResponse(byte[] content, String JavaDoc mimeType, long timestamp, String JavaDoc versionId) {
93     return new ByteArrayFileDownloadResponse(content, mimeType, versionId, timestamp);
94     }
95     
96     static DownloadResponse getFileDownloadResponse(URL JavaDoc resource, String JavaDoc mimeType, long timestamp, String JavaDoc versionId) {
97     return new ResourceFileDownloadResponse(resource, mimeType, versionId, timestamp);
98     }
99     
100     static DownloadResponse getFileDownloadResponse(File file, String JavaDoc mimeType, long timestamp, String JavaDoc versionId) {
101     return new DiskFileDownloadResponse(file, mimeType, versionId, timestamp);
102     }
103     
104     //
105
// Private classes implementing the various types
106
//
107

108     static private class NotModifiedResponse extends DownloadResponse {
109         public void sendRespond(HttpServletResponse JavaDoc 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 JavaDoc 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 JavaDoc response) throws IOException {
123         response.sendError(HttpServletResponse.SC_NO_CONTENT);
124     }
125     }
126     
127     static private class HeadRequestResponse extends DownloadResponse {
128         private String JavaDoc _mimeType;
129     private String JavaDoc _versionId;
130     private long _lastModified;
131     private int _contentLength;
132         
133         HeadRequestResponse(String JavaDoc mimeType, String JavaDoc versionId,
134                 long lastModified, int contentLength) {
135         _mimeType = mimeType;
136         _versionId = versionId;
137         _lastModified = lastModified;
138         _contentLength = contentLength;
139     }
140         
141         /** Post information to an HttpResponse */
142     public void sendRespond(HttpServletResponse JavaDoc response) throws
143                 IOException {
144         // Set header information
145
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 JavaDoc _message;
159     
160     public JnlpErrorResponse(int jnlpErrorCode) {
161         String JavaDoc msg = Integer.toString(jnlpErrorCode);
162         String JavaDoc dsc = "No description";
163         try {
164         dsc = JnlpDownloadServlet.getResourceBundle().getString("servlet.jnlp.err." + msg);
165         } catch (MissingResourceException mre) { /* ignore */}
166         _message = msg + " " + dsc;
167     }
168     
169     public void sendRespond(HttpServletResponse JavaDoc response) throws IOException {
170         response.setContentType(JNLP_ERROR_MIMETYPE);
171         PrintWriter pw = response.getWriter();
172         pw.println(_message);
173     };
174     
175     public String JavaDoc toString() { return super.toString() + "[" + _message + "]"; }
176     }
177     
178     static private abstract class FileDownloadResponse extends DownloadResponse {
179     private String JavaDoc _mimeType;
180     private String JavaDoc _versionId;
181     private long _lastModified;
182     private String JavaDoc _fileName;
183     
184     FileDownloadResponse(String JavaDoc mimeType, String JavaDoc versionId, long lastModified) {
185         _mimeType = mimeType;
186         _versionId = versionId;
187         _lastModified = lastModified;
188         _fileName = null;
189     }
190     
191     FileDownloadResponse(String JavaDoc mimeType, String JavaDoc versionId, long lastModified, String JavaDoc fileName) {
192         _mimeType = mimeType;
193         _versionId = versionId;
194         _lastModified = lastModified;
195         _fileName = fileName;
196     }
197     
198     
199     /** Information about response */
200     String JavaDoc getMimeType() { return _mimeType; }
201     String JavaDoc getVersionId() { return _versionId; }
202     long getLastModified() { return _lastModified; }
203     abstract int getContentLength() throws IOException;
204     abstract InputStream getContent() throws IOException;
205     
206     /** Post information to an HttpResponse */
207     public void sendRespond(HttpServletResponse JavaDoc response) throws IOException {
208         // Set header information
209
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         // Send contents
225
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 JavaDoc getArgString() {
239         long length = 0;
240         try {
241         length = getContentLength();
242         } catch(IOException ioe) { /* ignore */ }
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 JavaDoc mimeType, String JavaDoc 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 JavaDoc toString() { return super.toString() + "[ " + getArgString() + "]"; }
261     }
262     
263     static private class ResourceFileDownloadResponse extends FileDownloadResponse {
264     URL JavaDoc _url;
265     
266     ResourceFileDownloadResponse(URL JavaDoc url, String JavaDoc mimeType, String JavaDoc 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 JavaDoc toString() { return super.toString() + "[ " + getArgString() + "]"; }
278     }
279         
280     static private class DiskFileDownloadResponse extends FileDownloadResponse {
281     private File _file;
282     
283     DiskFileDownloadResponse(File file, String JavaDoc mimeType, String JavaDoc 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 JavaDoc toString() { return super.toString() + "[ " + getArgString() + "]"; }
297     }
298 }
299
300
301
302
Popular Tags