1 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 ; 34 import javax.servlet.http.HttpServletRequestWrapper ; 35 import java.io.IOException ; 36 37 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 93 { 94 super.encodeEnd(facesContext, uiComponent); 96 ResponseWriter writer = facesContext.getResponseWriter(); 97 writer.startElement(HTML.INPUT_ELEM, uiComponent); 98 writer.writeAttribute(HTML.TYPE_ATTR, "file", null); 99 String 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); 143 ServletRequest multipartRequest = (ServletRequest )facesContext.getExternalContext().getRequest(); 148 while (multipartRequest != null && 149 !(multipartRequest instanceof MultipartRequestWrapper)) 150 { 151 if (multipartRequest instanceof HttpServletRequestWrapper ) 152 { 153 multipartRequest = ((HttpServletRequestWrapper )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 paramName = uiComponent.getClientId(facesContext); 166 FileItem fileItem = mpReq.getFileItem(paramName); 167 if (fileItem != null) 168 { 169 try{ 170 UploadedFile upFile; 171 String 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 ioe){ 179 log.error(ioe); 180 } 181 } 182 } 183 } 184 185 public Object getConvertedValue(FacesContext context, UIComponent component, Object 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 |