KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.struts.action.Action;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32
33 import com.sslexplorer.navigation.actions.FileDownloadAction;
34 import com.sslexplorer.navigation.actions.ShowFileDownloadAction;
35
36 /**
37  * Implementation of a of {@link com.sslexplorer.core.PageInterceptListener}
38  * that may be used to force a file download when it becomes available.
39  *
40  *
41  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
42  */

43
44 public class FileDownloadManager implements PageInterceptListener {
45     
46     /**
47      * Intercept listener id
48      */

49     public final static String JavaDoc INTERCEPT_ID = "fileDownload";
50
51     // Protected instance variables
52
protected List JavaDoc downloads;
53
54     // Private statics
55
static int id = 0;
56
57     /**
58      * Constructor.
59      */

60     public FileDownloadManager() {
61         downloads = new ArrayList JavaDoc();
62     }
63
64     /*
65      * (non-Javadoc)
66      *
67      * @see com.sslexplorer.core.PageInterceptListener#getId()
68      */

69     public String JavaDoc getId() {
70         return INTERCEPT_ID;
71     }
72
73     /*
74      * (non-Javadoc)
75      *
76      * @see com.sslexplorer.core.PageInterceptListener#checkForForward(org.apache.struts.action.Action,
77      * org.apache.struts.action.ActionMapping,
78      * javax.servlet.http.HttpServletRequest,
79      * javax.servlet.http.HttpServletResponse)
80      */

81     public ActionForward checkForForward(Action action, ActionMapping mapping, HttpServletRequest JavaDoc request,
82                                          HttpServletResponse JavaDoc response) throws PageInterceptException {
83         if (downloads.size() > 0 && !(action instanceof ShowFileDownloadAction) && !(action instanceof FileDownloadAction)) {
84             DownloadContent download = ((DownloadContent) downloads.get(0));
85             return CoreUtil.addParameterToForward(download.getMessageForward(), "id", String.valueOf(download.getId()));
86         }
87         return null;
88     }
89
90     /**
91      * Add a new download.
92      *
93      * @param downloadContent download
94      * @return assigned id
95      */

96     public int addDownload(DownloadContent downloadContent) {
97         id++;
98         downloadContent.setId(id);
99         downloads.add(downloadContent);
100         return id;
101     }
102
103     /**
104      * Get a download given its id.
105      *
106      * @param id id
107      * @return download
108      */

109     public DownloadContent getDownload(int id) {
110         for (Iterator JavaDoc i = downloads.iterator(); i.hasNext();) {
111             DownloadContent l = (DownloadContent) i.next();
112             if (l.getId() == id) {
113                 return l;
114             }
115         }
116         return null;
117     }
118
119     /**
120      * Get the number of downloads available.
121      *
122      * @return number of downloads
123      */

124     public int size() {
125         return downloads.size();
126     }
127
128     /**
129      * Remove a download given its id.
130      *
131      * @param id id of download to remove
132      */

133     public void removeDownload(int id) {
134         DownloadContent d = getDownload(id);
135         if (d != null) {
136             downloads.remove(d);
137         }
138
139     }
140
141     /*
142      * (non-Javadoc)
143      *
144      * @see com.sslexplorer.core.PageInterceptListener#isRedirect()
145      */

146     public boolean isRedirect() {
147         return false;
148     }
149 }
Popular Tags