01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.directwebremoting.servlet;
17:
18: import java.io.IOException;
19: import java.io.OutputStream;
20:
21: import javax.servlet.http.HttpServletRequest;
22: import javax.servlet.http.HttpServletResponse;
23:
24: import org.directwebremoting.extend.DownloadManager;
25: import org.directwebremoting.extend.FileGenerator;
26: import org.directwebremoting.extend.Handler;
27:
28: /**
29: * A DownloadHandler is basically a FileServingServlet that integrates with
30: * a DownloadManager to purge files from the system that have been downloaded.
31: * @author Joe Walker [joe at getahead dot ltd dot uk]
32: */
33: public class DownloadHandler implements Handler {
34: /* (non-Javadoc)
35: * @see org.directwebremoting.extend.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
36: */
37: public void handle(HttpServletRequest request,
38: HttpServletResponse response) throws IOException {
39: String id = request.getPathInfo();
40:
41: if (!id.startsWith(downloadHandlerUrl)) {
42: response.sendError(HttpServletResponse.SC_NOT_FOUND);
43: }
44: id = id.substring(downloadHandlerUrl.length());
45:
46: FileGenerator generator = downloadManager.getFile(id);
47: if (generator == null) {
48: response.sendError(HttpServletResponse.SC_NOT_FOUND);
49: } else {
50: response.setHeader("Content-disposition",
51: "attachment; filename=" + generator.getFilename());
52: response.setContentType(generator.getMimeType());
53:
54: OutputStream out = response.getOutputStream();
55: generator.generateFile(out);
56: }
57: }
58:
59: /**
60: * @param downloadManager The new DownloadManager
61: */
62: public void setDownloadManager(DownloadManager downloadManager) {
63: this .downloadManager = downloadManager;
64: }
65:
66: /**
67: * The URL part which we attach to the downloads.
68: * @param downloadHandlerUrl The URL for this Handler.
69: */
70: public void setDownloadHandlerUrl(String downloadHandlerUrl) {
71: this .downloadHandlerUrl = downloadHandlerUrl;
72: }
73:
74: /**
75: * The place we store files for later download
76: */
77: private DownloadManager downloadManager;
78:
79: /**
80: * The URL part which we attach to the downloads.
81: */
82: protected String downloadHandlerUrl;
83: }
|