1 23 package org.infoglue.deliver.taglib.common; 24 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import javax.servlet.jsp.JspException ; 32 33 import org.apache.commons.fileupload.FileItem; 34 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 35 import org.apache.commons.fileupload.servlet.ServletFileUpload; 36 import org.apache.log4j.Logger; 37 import org.infoglue.deliver.taglib.TemplateControllerTag; 38 39 42 43 public class ParseMultipartTag extends TemplateControllerTag 44 { 45 public final static Logger logger = Logger.getLogger(ParseMultipartTag.class.getName()); 46 47 50 private static final long serialVersionUID = 8603406098980150888L; 51 52 private Integer maxSize = new Integer (100000); 53 private String allowedContentTypes; 54 private String [] allowedContentTypeArray; 55 private boolean ignoreEmpty = false; 56 57 60 public ParseMultipartTag() 61 { 62 super(); 63 } 64 65 71 public int doEndTag() throws JspException 72 { 73 Map parameters = new HashMap (); 74 75 try 76 { 77 DiskFileItemFactory factory = new DiskFileItemFactory(); 79 80 factory.setSizeThreshold(maxSize.intValue()); 82 84 ServletFileUpload upload = new ServletFileUpload(factory); 86 87 upload.setSizeMax(this.maxSize.intValue()); 89 90 if(upload.isMultipartContent(this.getController().getHttpServletRequest())) 91 { 92 List items = upload.parseRequest(this.getController().getHttpServletRequest()); 94 95 List files = new ArrayList (); 96 97 Iterator iter = items.iterator(); 99 while (iter.hasNext()) 100 { 101 FileItem item = (FileItem) iter.next(); 102 103 if (!item.isFormField()) 104 { 105 String fieldName = item.getFieldName(); 106 String fileName = item.getName(); 107 String contentType = item.getContentType(); 108 boolean isInMemory = item.isInMemory(); 109 long sizeInBytes = item.getSize(); 110 111 if(isValidContentType(contentType)) 112 { 113 files.add(item); 114 } 115 else 116 { 117 if((item.getName() == null || item.getName().equals("")) && this.ignoreEmpty) 118 { 119 logger.warn("Empty file but that was ok.."); 120 } 121 else 122 { 123 pageContext.setAttribute("status", "nok"); 124 pageContext.setAttribute("upload_error", "A field did not have a valid content type"); 125 pageContext.setAttribute(fieldName + "_error", "Not a valid content type"); 126 } 128 } 129 } 130 else 131 { 132 String name = item.getFieldName(); 133 String value = item.getString(); 134 String oldValue = (String )parameters.get(name); 136 if(oldValue != null) 137 value = oldValue + "," + value; 138 139 parameters.put(name, value); 140 } 141 142 } 143 144 parameters.put("files", files); 145 146 setResultAttribute(parameters); 147 } 148 else 149 { 150 setResultAttribute(null); 151 } 152 } 153 catch(Exception e) 154 { 155 logger.warn("Error doing an upload" + e.getMessage(), e); 156 pageContext.setAttribute("status", "nok"); 159 pageContext.setAttribute("upload_error", "" + e.getMessage()); 160 } 161 162 return EVAL_PAGE; 163 } 164 165 private boolean isValidContentType(String contentType) 166 { 167 boolean valid = false; 168 for(int i=0; i<this.allowedContentTypeArray.length; i++) 169 { 170 if(this.allowedContentTypeArray[i].equalsIgnoreCase(contentType)) 171 valid = true; 172 } 173 return valid; 174 } 175 176 public void setMaxSize(String maxSize) throws JspException 177 { 178 this.maxSize = evaluateInteger("FileUploadTag", "maxSize", maxSize); 179 } 180 181 public void setAllowedContentTypes(String allowedContentTypes) throws JspException 182 { 183 this.allowedContentTypes = evaluateString("FileUploadTag", "allowedContentTypes", allowedContentTypes); 184 this.allowedContentTypeArray = this.allowedContentTypes.split(","); 185 } 186 187 public void setIgnoreEmpty(boolean ignoreEmpty) 188 { 189 this.ignoreEmpty = ignoreEmpty; 190 } 191 } 192 | Popular Tags |