KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > FileDownload


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.core;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31 import org.apache.struts.action.ActionForward;
32
33 import com.sslexplorer.boot.Util;
34 import com.sslexplorer.security.SessionInfo;
35
36
37 /**
38  * Implementation of a {@link com.sslexplorer.core.AbstractDownloadContent}
39  * that allows a locat file to be downloaded to the user.
40  *
41  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
42  */

43 public class FileDownload extends AbstractDownloadContent {
44     
45     // Private instance variables
46
private File JavaDoc file;
47     private String JavaDoc filename;
48     private int downloadCount;
49
50     /**
51      * Constructor.
52      *
53      * @param file file to download
54      * @param filename filename to present file as
55      * @param mimeType mime type
56      * @param forward forward to direct to when complete
57      * @param messageKey message key for message
58      * @param messageResourcesKey bundle id
59      */

60     public FileDownload(File JavaDoc file, String JavaDoc filename, String JavaDoc mimeType, ActionForward forward, String JavaDoc messageKey,
61                         String JavaDoc messageResourcesKey) {
62         this(file, filename, mimeType, forward, messageKey, messageResourcesKey, null);
63     }
64
65     /**
66      * Constructor.
67      *
68      * @param file file to download
69      * @param filename filename to present file as
70      * @param mimeType mime type
71      * @param forward forward to direct to when complete
72      * @param messageKey message key for message
73      * @param messageResourcesKey bundle id
74      * @param messageArg0 first message argument
75      */

76     public FileDownload(File JavaDoc file, String JavaDoc filename, String JavaDoc mimeType, ActionForward forward, String JavaDoc messageKey,
77                     String JavaDoc messageResourcesKey, String JavaDoc messageArg0) {
78         this(file, filename, mimeType, forward, messageKey, messageResourcesKey, messageArg0, null, null, null, null);
79     }
80
81
82     /**
83      * Constructor.
84      *
85      * @param file file to download
86      * @param filename filename to present file as
87      * @param mimeType mime type
88      * @param forward forward to direct to when complete
89      * @param messageKey message key for message
90      * @param messageResourcesKey bundle id
91      * @param messageArg0 first message argument
92      * @param messageArg1 second message argument
93      * @param messageArg2 third message argument
94      * @param messageArg3 fourth message argument
95      * @param messageArg4 fifth message argument
96      */

97     public FileDownload(File JavaDoc file, String JavaDoc filename, String JavaDoc mimeType, ActionForward forward, String JavaDoc messageKey,
98                         String JavaDoc messageResourcesKey, String JavaDoc messageArg0,
99                         String JavaDoc messageArg1, String JavaDoc messageArg2, String JavaDoc messageArg3, String JavaDoc messageArg4) {
100         super(mimeType, forward, messageKey, messageResourcesKey, messageArg0, messageArg1, messageArg2, messageArg3, messageArg4);
101         this.file = file;
102         this.filename = filename;
103     }
104
105     /* (non-Javadoc)
106      * @see com.sslexplorer.core.DownloadContent#getFilename()
107      */

108     public String JavaDoc getFilename() {
109         return filename;
110     }
111
112     /**
113      * Set the filename to download as
114      *
115      * @param filename filename to download as
116      */

117     public void setFilename(String JavaDoc filename) {
118         this.filename = filename;
119     }
120
121     /**
122      * Get the local file to make available for download
123      *
124      * @return local file
125      */

126     public File JavaDoc getFile() {
127         return file;
128     }
129
130     /**
131      * Set the local file to make available for download
132      *
133      * @param file local file
134      */

135     public void setFile(File JavaDoc file) {
136         this.file = file;
137     }
138     
139     /* (non-Javadoc)
140      * @see com.sslexplorer.core.DownloadContent#sendDownload(javax.servlet.http.HttpServletResponse, javax.servlet.http.HttpServletRequest)
141      */

142     public void sendDownload(HttpServletResponse JavaDoc response, HttpServletRequest JavaDoc request) throws Exception JavaDoc{
143         InputStream JavaDoc in = null;
144         try {
145             in = new FileInputStream JavaDoc(file);
146             response.setContentLength((int) file.length());
147             Util.copy(in, response.getOutputStream());
148             response.getOutputStream().flush();
149         } finally {
150             downloadCount++;
151             Util.closeStream(in);
152         }
153     }
154     
155     /* (non-Javadoc)
156      * @see com.sslexplorer.core.AbstractDownloadContent#completeDownload(com.sslexplorer.security.SessionInfo)
157      */

158     public void completeDownload(SessionInfo session) {
159     }
160
161     /* (non-Javadoc)
162      * @see com.sslexplorer.core.DownloadContent#getDownloadCount()
163      */

164     public int getDownloadCount() {
165         return downloadCount;
166     }
167 }
Popular Tags