KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > fileupload > HtmlFileUploadRenderer


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

16 package org.apache.myfaces.custom.fileupload;
17
18 import org.apache.myfaces.component.UserRoleUtils;
19 import org.apache.myfaces.component.html.util.MultipartRequestWrapper;
20 import org.apache.myfaces.renderkit.RendererUtils;
21 import org.apache.myfaces.renderkit.html.HTML;
22 import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
23
24 import org.apache.commons.fileupload.FileItem;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import javax.faces.component.UIComponent;
29 import javax.faces.context.FacesContext;
30 import javax.faces.context.ResponseWriter;
31 import javax.faces.render.Renderer;
32 import javax.faces.convert.ConverterException;
33 import javax.servlet.ServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 /**
38  * @author Manfred Geiler (latest modification by $Author: svieujot $)
39  * @version $Revision: 1.15 $ $Date: 2004/12/01 16:32:03 $
40  * $Log: HtmlFileUploadRenderer.java,v $
41  * Revision 1.15 2004/12/01 16:32:03 svieujot
42  * Convert the Multipart filter in an ExtensionsFilter that provides an additional facility to include resources in a page.
43  * Tested only with javascript resources right now, but should work fine with images too.
44  * Some work to do to include css resources.
45  * The popup component has been converted to use this new Filter.
46  *
47  * Revision 1.14 2004/10/13 11:50:57 matze
48  * renamed packages to org.apache
49  *
50  * Revision 1.13 2004/09/03 12:32:05 tinytoony
51  * file upload
52  *
53  * Revision 1.12 2004/07/14 06:02:48 svieujot
54  * FileUpload : split file based and memory based implementation.
55  * Use the storage="memory|file" attribute.
56  * Default is memory because file based implementation fails to serialize.
57  *
58  * Revision 1.11 2004/07/12 03:06:36 svieujot
59  * Restore error handling due to UploadedFileDefaultImpl changes
60  *
61  * Revision 1.10 2004/07/01 21:53:05 mwessendorf
62  * ASF switch
63  *
64  * Revision 1.9 2004/05/24 22:48:10 svieujot
65  * Making UploadedFile an interface, and adjusting the renderer.
66  *
67  * Revision 1.8 2004/05/18 14:26:49 manolito
68  * added UserRoleAware support
69  *
70  * Revision 1.7 2004/05/10 22:17:24 o_rossmueller
71  * max file size configurable by filter init parameter 'maxFileSize'
72  * removed default creation of file contents byte array
73  *
74  * Revision 1.6 2004/04/26 13:16:32 manolito
75  * Log was missing
76  *
77  * Revision 1.4 Sylvain Vieujot
78  * Don't change the file bean if no file is uploaded.
79  *
80  * Revision 1.3 Sylvain Vieujot
81  * Fix a null pointer exception in encodeEnd if the file name is null.
82  *
83  * Revision 1.2 Sylvain Vieujot
84  * Upgraded to use commons fileUpload
85  */

86 public class HtmlFileUploadRenderer
87     extends Renderer
88 {
89     private static final Log log = LogFactory.getLog(HtmlFileUploadRenderer.class);
90
91     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
92         throws IOException JavaDoc
93     {
94         super.encodeEnd(facesContext, uiComponent); //check for NP
95

96         ResponseWriter writer = facesContext.getResponseWriter();
97         writer.startElement(HTML.INPUT_ELEM, uiComponent);
98         writer.writeAttribute(HTML.TYPE_ATTR, "file", null);
99         String JavaDoc clientId = uiComponent.getClientId(facesContext);
100         writer.writeAttribute(HTML.ID_ATTR, clientId, null);
101         writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
102         UploadedFile value = (UploadedFile)((HtmlInputFileUpload)uiComponent).getValue();
103         if (value != null)
104         {
105             if( value.getName() != null )
106             {
107                 writer.writeAttribute(HTML.VALUE_ATTR, value.getName(), null);
108             }
109         }
110         HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.INPUT_FILE_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
111         if (isDisabled(facesContext, uiComponent))
112         {
113             writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null);
114         }
115
116         writer.endElement(HTML.INPUT_ELEM);
117     }
118
119     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
120     {
121         if (!UserRoleUtils.isEnabledOnUserRole(uiComponent))
122         {
123             return false;
124         }
125         else
126         {
127             if (uiComponent instanceof HtmlInputFileUpload)
128             {
129                 return ((HtmlInputFileUpload)uiComponent).isDisabled();
130             }
131             else
132             {
133                 return RendererUtils.getBooleanAttribute(uiComponent, HTML.DISABLED_ATTR, false);
134             }
135         }
136     }
137
138
139     public void decode(FacesContext facesContext, UIComponent uiComponent)
140     {
141         super.decode(facesContext, uiComponent); //check for NP
142

143         //MultipartWrapper might have been wrapped again by one or more additional
144
//Filters. We try to find the MultipartWrapper, but if a filter has wrapped
145
//the ServletRequest with a class other than HttpServletRequestWrapper
146
//this will fail.
147
ServletRequest JavaDoc multipartRequest = (ServletRequest JavaDoc)facesContext.getExternalContext().getRequest();
148         while (multipartRequest != null &&
149                !(multipartRequest instanceof MultipartRequestWrapper))
150         {
151             if (multipartRequest instanceof HttpServletRequestWrapper JavaDoc)
152             {
153                 multipartRequest = ((HttpServletRequestWrapper JavaDoc)multipartRequest).getRequest();
154             }
155             else
156             {
157                 multipartRequest = null;
158             }
159         }
160
161         if (multipartRequest != null)
162         {
163             MultipartRequestWrapper mpReq = (MultipartRequestWrapper)multipartRequest;
164
165             String JavaDoc paramName = uiComponent.getClientId(facesContext);
166             FileItem fileItem = mpReq.getFileItem(paramName);
167             if (fileItem != null)
168             {
169                 try{
170                     UploadedFile upFile;
171                     String JavaDoc implementation = ((HtmlInputFileUpload) uiComponent).getStorage();
172                     if( implementation == null || ("memory").equals( implementation ) )
173                         upFile = new UploadedFileDefaultMemoryImpl( fileItem );
174                     else
175                         upFile = new UploadedFileDefaultFileImpl( fileItem );
176                     ((HtmlInputFileUpload)uiComponent).setSubmittedValue(upFile);
177                     ((HtmlInputFileUpload)uiComponent).setValid(true);
178                 }catch(IOException JavaDoc ioe){
179                     log.error(ioe);
180                 }
181             }
182         }
183     }
184
185     public Object JavaDoc getConvertedValue(FacesContext context, UIComponent component, Object JavaDoc submittedValue) throws ConverterException
186     {
187         if(submittedValue instanceof UploadedFile)
188         {
189             UploadedFile file = (UploadedFile) submittedValue;
190
191             if(file.getName()!=null && file.getName().length()>0)
192             {
193                 return file;
194             }
195         }
196
197         return null;
198     }
199 }
200
Popular Tags