001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Anahide Tchertchian 016 */ 017package org.nuxeo.ecm.platform.ui.web.component.file; 018 019import java.io.IOException; 020import java.util.Collection; 021import java.util.Map; 022 023import javax.faces.FacesException; 024import javax.faces.component.UIComponent; 025import javax.faces.context.ExternalContext; 026import javax.faces.context.FacesContext; 027import javax.servlet.ServletException; 028import javax.servlet.http.HttpServletRequest; 029import javax.servlet.http.Part; 030 031import org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException; 032import org.nuxeo.ecm.core.api.Blobs; 033import org.nuxeo.ecm.platform.ui.web.util.files.FileUtils; 034 035import com.sun.faces.renderkit.html_basic.FileRenderer; 036 037/** 038 * Renderer for file input. 039 * <p> 040 * Overrides the base JSF renderer for files to ignore error when submitting file inside a non multipart form. 041 * <p> 042 * Component {@link UIInputFile} will handle error message management in UI, if validation phase occurs. 043 * 044 * @since 7.1 045 */ 046public class NXFileRenderer extends FileRenderer { 047 048 public static final String RENDERER_TYPE = "javax.faces.NXFile"; 049 050 @Override 051 public void decode(FacesContext context, UIComponent component) { 052 053 rendererParamsNotNull(context, component); 054 055 if (!shouldDecode(component)) { 056 return; 057 } 058 059 String clientId = decodeBehaviors(context, component); 060 061 if (clientId == null) { 062 clientId = component.getClientId(context); 063 } 064 065 assert (clientId != null); 066 ExternalContext externalContext = context.getExternalContext(); 067 Map<String, String> requestMap = externalContext.getRequestParameterMap(); 068 069 if (requestMap.containsKey(clientId)) { 070 setSubmittedValue(component, requestMap.get(clientId)); 071 } 072 073 HttpServletRequest request = (HttpServletRequest) externalContext.getRequest(); 074 try { 075 Collection<Part> parts = request.getParts(); 076 for (Part cur : parts) { 077 if (clientId.equals(cur.getName())) { 078 // Nuxeo patch: transform into serializable blob right away, and do not set component transient 079 // component.setTransient(true); 080 // setSubmittedValue(component, cur); 081 setSubmittedValue(component, FileUtils.createBlob(cur)); 082 } 083 } 084 } catch (IOException ioe) { 085 throw new FacesException(ioe); 086 } catch (ServletException se) { 087 Throwable cause = se.getCause(); 088 // Nuxeo specific error management 089 if ((cause instanceof InvalidContentTypeException) 090 || (cause != null && cause.getClass().getName().contains("InvalidContentTypeException"))) { 091 setSubmittedValue(component, Blobs.createBlob("")); 092 } else { 093 throw new FacesException(se); 094 } 095 } 096 } 097}