001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.ui.web.component.file;
020
021import java.io.IOException;
022import java.util.Collection;
023import java.util.Map;
024
025import javax.faces.FacesException;
026import javax.faces.component.UIComponent;
027import javax.faces.context.ExternalContext;
028import javax.faces.context.FacesContext;
029import javax.servlet.ServletException;
030import javax.servlet.http.HttpServletRequest;
031import javax.servlet.http.Part;
032
033import org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException;
034import org.nuxeo.ecm.core.api.Blobs;
035import org.nuxeo.ecm.platform.ui.web.util.files.FileUtils;
036
037import com.sun.faces.renderkit.html_basic.FileRenderer;
038
039/**
040 * Renderer for file input.
041 * <p>
042 * Overrides the base JSF renderer for files to ignore error when submitting file inside a non multipart form.
043 * <p>
044 * Component {@link UIInputFile} will handle error message management in UI, if validation phase occurs.
045 *
046 * @since 7.1
047 */
048public class NXFileRenderer extends FileRenderer {
049
050    public static final String RENDERER_TYPE = "javax.faces.NXFile";
051
052    @Override
053    public void decode(FacesContext context, UIComponent component) {
054
055        rendererParamsNotNull(context, component);
056
057        if (!shouldDecode(component)) {
058            return;
059        }
060
061        String clientId = decodeBehaviors(context, component);
062
063        if (clientId == null) {
064            clientId = component.getClientId(context);
065        }
066
067        assert (clientId != null);
068        ExternalContext externalContext = context.getExternalContext();
069        Map<String, String> requestMap = externalContext.getRequestParameterMap();
070
071        if (requestMap.containsKey(clientId)) {
072            setSubmittedValue(component, requestMap.get(clientId));
073        }
074
075        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
076        try {
077            Collection<Part> parts = request.getParts();
078            for (Part cur : parts) {
079                if (clientId.equals(cur.getName())) {
080                    // Nuxeo patch: transform into serializable blob right away, and do not set component transient
081                    // component.setTransient(true);
082                    // setSubmittedValue(component, cur);
083                    setSubmittedValue(component, FileUtils.createBlob(cur));
084                }
085            }
086        } catch (IOException ioe) {
087            throw new FacesException(ioe);
088        } catch (ServletException se) {
089            Throwable cause = se.getCause();
090            // Nuxeo specific error management
091            if ((cause instanceof InvalidContentTypeException)
092                    || (cause != null && cause.getClass().getName().contains("InvalidContentTypeException"))) {
093                setSubmittedValue(component, Blobs.createBlob(""));
094            } else {
095                throw new FacesException(se);
096            }
097        }
098    }
099}