KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > taglib > common > ParseMultipartTag


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.deliver.taglib.common;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.servlet.jsp.JspException JavaDoc;
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 /**
40  * This tag will get a cookie value
41  */

42
43 public class ParseMultipartTag extends TemplateControllerTag
44 {
45     public final static Logger logger = Logger.getLogger(ParseMultipartTag.class.getName());
46
47     /**
48      * The universal version identifier.
49      */

50     private static final long serialVersionUID = 8603406098980150888L;
51     
52     private Integer JavaDoc maxSize = new Integer JavaDoc(100000);
53     private String JavaDoc allowedContentTypes;
54     private String JavaDoc[] allowedContentTypeArray;
55     private boolean ignoreEmpty = false;
56     
57     /**
58      * Default constructor.
59      */

60     public ParseMultipartTag()
61     {
62         super();
63     }
64     
65     /**
66      * Process the end tag. Sets a cookie.
67      *
68      * @return indication of whether to continue evaluating the JSP page.
69      * @throws JspException if an error occurred while processing this tag.
70      */

71     public int doEndTag() throws JspException JavaDoc
72     {
73         Map JavaDoc parameters = new HashMap JavaDoc();
74         
75         try
76         {
77             //Create a factory for disk-based file items
78
DiskFileItemFactory factory = new DiskFileItemFactory();
79             
80             //Set factory constraints
81
factory.setSizeThreshold(maxSize.intValue());
82             //factory.setRepository(new File(CmsPropertyHandler.getDigitalAssetPath()));
83

84             //Create a new file upload handler
85
ServletFileUpload upload = new ServletFileUpload(factory);
86             
87             //Set overall request size constraint
88
upload.setSizeMax(this.maxSize.intValue());
89     
90             if(upload.isMultipartContent(this.getController().getHttpServletRequest()))
91             {
92                 //Parse the request
93
List JavaDoc items = upload.parseRequest(this.getController().getHttpServletRequest());
94                     
95                 List JavaDoc files = new ArrayList JavaDoc();
96                 
97                 //Process the uploaded items
98
Iterator JavaDoc iter = items.iterator();
99                 while (iter.hasNext())
100                 {
101                     FileItem item = (FileItem) iter.next();
102         
103                     if (!item.isFormField())
104                     {
105                         String JavaDoc fieldName = item.getFieldName();
106                         String JavaDoc fileName = item.getName();
107                         String JavaDoc 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                                 //throw new JspException("Not a valid content type");
127
}
128                         }
129                     }
130                     else
131                     {
132                         String JavaDoc name = item.getFieldName();
133                         String JavaDoc value = item.getString();
134                         //System.out.println(name + "=" + value);
135
String JavaDoc oldValue = (String JavaDoc)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 JavaDoc e)
154         {
155             logger.warn("Error doing an upload" + e.getMessage(), e);
156             //pageContext.setAttribute("fieldName_exception", "contentType_MAX");
157
//throw new JspException("File upload failed: " + e.getMessage());
158
pageContext.setAttribute("status", "nok");
159             pageContext.setAttribute("upload_error", "" + e.getMessage());
160         }
161         
162         return EVAL_PAGE;
163     }
164
165     private boolean isValidContentType(String JavaDoc 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 JavaDoc maxSize) throws JspException JavaDoc
177     {
178         this.maxSize = evaluateInteger("FileUploadTag", "maxSize", maxSize);
179     }
180
181     public void setAllowedContentTypes(String JavaDoc allowedContentTypes) throws JspException JavaDoc
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