KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > strutsutil > file > filter > ImageSessionFilter


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.strutsutil.file.filter;
17
18 import com.jdon.strutsutil.file.*;
19 import javax.servlet.http.*;
20 import java.util.*;
21 import com.jdon.util.Debug;
22 import com.jdon.util.UtilValidate;
23
24 public class ImageSessionFilter implements ImageFilter {
25
26   public final static String JavaDoc module = ImageSessionFilter.class.getName();
27
28   private final static String JavaDoc PIC_NAME_PACKAGE = "PicturesPakage";
29
30   private ImageFilter imageFilter = null;
31
32   public ImageSessionFilter(ImageFilter imageFilter) {
33     this.imageFilter = imageFilter;
34
35   }
36
37   /**
38    * 有些功能直接æ“?作ImageSessionFilter()为止
39    */

40   public ImageSessionFilter() {
41   }
42
43   /**
44    * å°†HttpSession中的ID集å?ˆå’Œè€?çš„imageFilterID集å?ˆå?ˆå¹¶
45    *
46    * ä¿?è¯?æ¯?个IDå?ªæœ‰ä¸€ä¸ªã€‚
47    *
48    * @param request
49    * @return
50    */

51   public Collection getUploadFileID(HttpServletRequest request) {
52     Collection set = null;
53     Debug.logVerbose("[JdonFramework]--> enter session filter ", module);
54     try {
55        //ä¿?è¯?一个idå?ªæœ‰ä¸€ä¸ª
56

57       //从下一个Filterå?–出ID集å?ˆï¼Œæ²¡æœ‰é‡?å¤?
58
Collection list = imageFilter.getUploadFileID(request);
59       if (list == null)
60         set = new LinkedHashSet();
61       else
62         set = new LinkedHashSet(list);
63
64       HttpSession session = request.getSession();
65       Collection sessionList = (Collection) session.getAttribute(PIC_NAME_PACKAGE);
66       if (sessionList == null) return set;
67
68       //ç¡®ä¿?Session中的ID覆盖下一个Filter中ID
69
Iterator iter = sessionList.iterator();
70       while (iter.hasNext()) {
71         UploadFile uploadFile = (UploadFile) iter.next();
72         if ( (uploadFile.getId() != null) && (!uploadFile.getId().equals(""))) {
73           set.add(uploadFile.getId());
74         } else { //如果Session有多于下一个Filterçš„ID,以排列TempId加入
75
set.add(uploadFile.getTempId());
76         }
77       }
78     } catch (Exception JavaDoc ex) {
79       Debug.logError("[JdonFramework] getUploadFileID error" + ex, module);
80     }
81     return set;
82
83   }
84
85   /**
86    * æ ¹æ?®UploadFileçš„id获得UploadFile实例
87    * @param request
88    * @param id
89    * @return
90    */

91   public UploadFile getUploadFile(HttpServletRequest request, String JavaDoc id) {
92     UploadFile uploadFile = null;
93     Debug.logVerbose("[JdonFramework]--> getUploadFile session filter id=" + id, module);
94     try {
95       uploadFile = getUploadFileFromSession(request, id);
96       if (uploadFile == null) {
97         uploadFile = imageFilter.getUploadFile(request, id);
98       }
99     } catch (Exception JavaDoc ex) {
100       Debug.logWarning(" getUploadFile error" + ex, module);
101
102     }
103     return uploadFile;
104   }
105
106   /**
107    * æ ¹æ?®UploadFileçš„id删除UploadFile实例
108    * @param request
109    * @param id
110    */

111   public void deleteUploadFile(HttpServletRequest request,
112                                UploadFile uploadFile) {
113     try {
114       String JavaDoc imageId = null;
115       //首先先删除Session
116
if (!UtilValidate.isEmpty(uploadFile.getTempId())) {
117         imageId = uploadFile.getTempId();
118         deleteUploadFileFromSession(request, imageId);
119       }else if (!UtilValidate.isEmpty(uploadFile.getId())) {
120        imageId = uploadFile.getId();
121        deleteUploadFileFromSession(request, imageId);
122        imageFilter.deleteUploadFile(request, uploadFile);
123       } else
124         throw new Exception JavaDoc("parameter id and tempId all is null");
125
126     } catch (Exception JavaDoc ex) {
127       Debug.logError("[JdonFramework] deleteUploadFile error:" + ex, module);
128     }
129
130   }
131
132   /**
133    * é€?个UploadFile新增ä¿?存到Http Session
134    * @param request
135    * @param uploadFile
136    */

137   public void addUploadFile(HttpServletRequest request,
138                             UploadFile uploadFile) {
139     if (uploadFile == null)
140       return;
141     try{
142       HttpSession session = request.getSession(true);
143       Collection uploadList = (Collection)session.getAttribute(PIC_NAME_PACKAGE);
144       if (uploadList == null) {
145         uploadList = new ArrayList();
146         session.setAttribute(PIC_NAME_PACKAGE, uploadList);
147       }
148       uploadList.add(uploadFile);
149       uploadFile.setTempId(Integer.toString(uploadList.size() - 1)); //临时给予id
150
}catch(Exception JavaDoc ex){
151       Debug.logError("[JdonFramework]addUploadFile error:" + ex, module);
152     }
153
154   }
155
156   /**
157    * 一次性将之å‰?ä¿?存的所有UploadFile集å?ˆå?–出,然å?Žä»ŽSession中删除
158    *
159    * 获得å‰?一个步骤上传的数æ?®
160    *
161    * 文件上传是在专门页é?¢æ“?作,æ“?作æˆ?功å?Žï¼Œå…¶å®ƒé¡µé?¢é€šè¿‡æœ¬æ–¹æ³•èŽ·å¾—æ•°æ?®ã€‚
162    *
163    * @param request
164    * @return UploadFile 集å?ˆ
165    */

166   public Collection loadAllUploadFile(HttpServletRequest request) {
167     Debug.logVerbose("[JdonFramework] load all upload files from session and remove them", module);
168     Collection uploadList = null;
169     try {
170       HttpSession session = request.getSession();
171       if (session != null) {
172         uploadList = (Collection) session.getAttribute(PIC_NAME_PACKAGE);
173         session.removeAttribute(PIC_NAME_PACKAGE);
174       }
175     } catch (Exception JavaDoc ex) {
176       Debug.logError("[JdonFramework] not found the upload files in session" + ex, module);
177     }
178     return uploadList;
179   }
180
181   /**
182    * å?–出ä¿?存的所有UploadFile集å?ˆï¼Œä½†ä¸?从Session删除这些集å?ˆ
183    * @param request
184    * @return
185    */

186   public Collection getAllUploadFile(HttpServletRequest request) {
187
188     Collection uploadList = null;
189     try {
190       HttpSession session = request.getSession();
191       if (session != null) {
192         uploadList = (Collection) session.getAttribute(PIC_NAME_PACKAGE);
193       }
194     } catch (Exception JavaDoc ex) {
195       Debug.logError("[JdonFramework] not found the upload files in session" + ex, module);
196     }
197     return uploadList;
198   }
199
200
201   /**
202    * æ ¹æ?®UploadFileçš„id获得UploadFile实例
203    * @param request
204    * @param id
205    * @return
206    */

207   private UploadFile getUploadFileFromSession(HttpServletRequest request,
208                                               String JavaDoc id) {
209     UploadFile uploadFile = null;
210     boolean found = false;
211     try {
212       HttpSession session = request.getSession(true);
213       Collection uploadList = (Collection) session.getAttribute(PIC_NAME_PACKAGE);
214       if (uploadList == null)
215         return null;
216       Iterator iter = uploadList.iterator();
217       while (iter.hasNext()) {
218         uploadFile = (UploadFile) iter.next();
219         if (uploadFile.getId().equals(id)){
220           found = true;
221           break;
222         }else if (uploadFile.getTempId().equals(id)){
223           found = true;
224           break;
225         }
226       }
227       if (!found ) uploadFile = null;
228     } catch (Exception JavaDoc ex) {
229       Debug.logError("[JdonFramework] getUploadFileSession error" + ex, module);
230
231     }
232     return uploadFile;
233
234   }
235
236   /**
237    * 删除图片
238    * @param request
239    * @param uploadFile
240    */

241   private void deleteUploadFileFromSession(HttpServletRequest request,
242                                            String JavaDoc id) {
243     try {
244
245       HttpSession session = request.getSession(true);
246       Collection uploadList = (Collection) session.getAttribute(PIC_NAME_PACKAGE);
247       if ( (uploadList == null) || (id == null))
248         return;
249
250       UploadFile uploadFile = null;
251       boolean found = false;
252       Iterator iter = uploadList.iterator();
253       while (iter.hasNext()) {
254         Object JavaDoc o = iter.next();
255         Debug.logError("[JdonFramework] object: " + o.getClass().getName(), module);
256         uploadFile = (UploadFile) o;
257         if (uploadFile.getId().equals(id)){
258           found = true;
259           break;
260         }else if (uploadFile.getTempId().equals(id)){
261           found = true;
262           break;
263         }
264       }
265       if (found )
266         uploadList.remove(uploadFile);
267     } catch (Exception JavaDoc ex) {
268       Debug.logError("[JdonFramework] deleteUploadFileFromSession error" + ex, module);
269
270     }
271
272   }
273
274 }
275
Popular Tags