1 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 module = ImageSessionFilter.class.getName(); 27 28 private final static String PIC_NAME_PACKAGE = "PicturesPakage"; 29 30 private ImageFilter imageFilter = null; 31 32 public ImageSessionFilter(ImageFilter imageFilter) { 33 this.imageFilter = imageFilter; 34 35 } 36 37 40 public ImageSessionFilter() { 41 } 42 43 51 public Collection getUploadFileID(HttpServletRequest request) { 52 Collection set = null; 53 Debug.logVerbose("[JdonFramework]--> enter session filter ", module); 54 try { 55 57 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 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 { set.add(uploadFile.getTempId()); 76 } 77 } 78 } catch (Exception ex) { 79 Debug.logError("[JdonFramework] getUploadFileID error" + ex, module); 80 } 81 return set; 82 83 } 84 85 91 public UploadFile getUploadFile(HttpServletRequest request, String 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 ex) { 100 Debug.logWarning(" getUploadFile error" + ex, module); 101 102 } 103 return uploadFile; 104 } 105 106 111 public void deleteUploadFile(HttpServletRequest request, 112 UploadFile uploadFile) { 113 try { 114 String imageId = null; 115 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 ("parameter id and tempId all is null"); 125 126 } catch (Exception ex) { 127 Debug.logError("[JdonFramework] deleteUploadFile error:" + ex, module); 128 } 129 130 } 131 132 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)); }catch(Exception ex){ 151 Debug.logError("[JdonFramework]addUploadFile error:" + ex, module); 152 } 153 154 } 155 156 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 ex) { 176 Debug.logError("[JdonFramework] not found the upload files in session" + ex, module); 177 } 178 return uploadList; 179 } 180 181 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 ex) { 195 Debug.logError("[JdonFramework] not found the upload files in session" + ex, module); 196 } 197 return uploadList; 198 } 199 200 201 207 private UploadFile getUploadFileFromSession(HttpServletRequest request, 208 String 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 ex) { 229 Debug.logError("[JdonFramework] getUploadFileSession error" + ex, module); 230 231 } 232 return uploadFile; 233 234 } 235 236 241 private void deleteUploadFileFromSession(HttpServletRequest request, 242 String 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 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 ex) { 268 Debug.logError("[JdonFramework] deleteUploadFileFromSession error" + ex, module); 269 270 } 271 272 } 273 274 } 275 | Popular Tags |