View Javadoc

1   /***
2    Copyright (C) 2005 The Java Community
3   
4    This program is free software; you can redistribute it and/or modify  it under
5    the terms of the GNU General Public License as published by  the Free Software
6    Foundation; either version 2 of the License, or  (at your option) any later
7    version.
8   
9    This program is distributed in the hope that it will be useful,  but WITHOUT
10   ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.
12  
13   You should have received a copy of the GNU General Public License  along with
14   this program; if not, write to the Free Software  Foundation, Inc., 59 Temple
15   Place, Suite 330, Boston, MA 02111-1307 USA.
16  
17   */
18  
19  package org.bejug.javacareers.common.view.jsf.upload;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.UnsupportedEncodingException;
25  
26  import javax.faces.FacesException;
27  import javax.faces.component.EditableValueHolder;
28  import javax.faces.component.UIComponent;
29  import javax.faces.context.ExternalContext;
30  import javax.faces.context.FacesContext;
31  import javax.faces.context.ResponseWriter;
32  import javax.faces.el.ValueBinding;
33  import javax.faces.render.Renderer;
34  import javax.servlet.ServletContext;
35  import javax.servlet.http.HttpServletRequest;
36  
37  import org.apache.commons.fileupload.FileItem;
38  
39  /***
40   * This class renders the upload component.
41   *
42   * @author Peter Symoens (Last modified by $Author: shally $)
43   * @version $Revision: 1.8 $ - $Date: 2005/12/21 11:38:42 $
44   */
45  public class UploadRenderer extends Renderer {
46  
47      /***
48       * @param component UIComponent
49       * @param context   FacesContext
50       * @throws IOException in case of error
51       */
52      public void encodeBegin(FacesContext context, UIComponent component)
53              throws IOException {
54          if (!component.isRendered()) {
55              return;
56          }
57  
58          ResponseWriter writer = context.getResponseWriter();
59  
60          String clientId = component.getClientId(context);
61  
62          writer.startElement("input", component);
63          writer.writeAttribute("type", "file", "type");
64          writer.writeAttribute("name", clientId, "clientId");
65          writer.endElement("input");
66          writer.flush();
67      }
68  
69      /***
70       * @param component UIComponent
71       * @param context   FacesContext
72       */
73      public void decode(FacesContext context, UIComponent component) {
74          ExternalContext external = context.getExternalContext();
75          HttpServletRequest request = (HttpServletRequest) external.getRequest();
76          String clientId = component.getClientId(context);
77          FileItem item = (FileItem) request.getAttribute(clientId);
78  
79          Object newValue;
80          ValueBinding binding = component.getValueBinding("value");
81          if (binding != null) {
82              if (binding.getType(context) == byte[].class) {
83                  newValue = item.get();
84              }
85              if (binding.getType(context) == InputStream.class) {
86                  try {
87                      newValue = item.getInputStream();
88                  } catch (IOException ex) {
89                      throw new FacesException(ex);
90                  }
91              } else {
92                  String encoding = request.getCharacterEncoding();
93                  if (encoding != null) {
94                      try {
95                          newValue = item.getString(encoding);
96                      } catch (UnsupportedEncodingException ex) {
97                          newValue = item.getString();
98                      }
99                  } else {
100                     newValue = item.getString();
101                 }
102             }
103             ((EditableValueHolder) component).setSubmittedValue(newValue);
104         }
105 
106         Object target;
107         binding = component.getValueBinding("target");
108         if (binding != null) {
109             target = binding.getValue(context);
110         } else {
111             target = component.getAttributes().get("target");
112         }
113 
114         if (target != null) {
115             File file;
116             if (target instanceof File) {
117                 file = (File) target;
118             } else {
119                 ServletContext servletContext
120                         = (ServletContext) external.getContext();
121                 file = new File(servletContext.getRealPath("/"),
122                         target.toString());
123             }
124 
125             try { // ugh--write is declared with "throws Exception"
126                 item.write(file);
127             } catch (Exception ex) {
128                 throw new FacesException(ex);
129             }
130         }
131     }
132 }
133 
134 /***
135  * $Log: UploadRenderer.java,v $
136  * Revision 1.8  2005/12/21 11:38:42  shally
137  * *** empty log message ***
138  *
139  * Revision 1.7  2005/12/08 14:53:46  shally
140  * Opkuis voor checkstyle.
141  *
142  * Revision 1.6  2005/09/13 08:11:06  schauwvliege
143  * organize imports
144  *
145  * Revision 1.5  2005/08/10 09:04:47  bavo_jcs
146  * Optimized imports according to checkstyle
147  *
148  * Revision 1.4  2005/08/09 12:59:54  bavo_jcs
149  * Optimized imports
150  *
151  * Revision 1.3  2005/07/20 12:36:40  bavo_jcs
152  * Stream closed bugfix
153  *
154  * Revision 1.2  2005/06/09 08:18:42  bejug_cc
155  * Fix initial import
156  *
157  * Revision 1.6  2005/05/12 08:23:55  ssc
158  * Checkstyle errors
159  *
160  * Revision 1.5  2005/05/11 15:48:53  sja
161  * Closed the writer in the encodeBegin method.
162  *
163  * Revision 1.4  2005/05/11 14:36:04  ssc
164  * Checstyle errors
165  *
166  * Revision 1.3  2005/05/11 13:47:18  PSONG09
167  * update upload functionality, added javadoc
168  * 
169  **/