All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.evasion.common.inputfile.UploadRenderer Maven / Gradle / Ivy

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evasion.common.inputfile;

import java.io.File;
import javax.el.ValueExpression;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.behavior.ClientBehaviorHolder;
import javax.faces.context.ExternalContext;
import javax.faces.context.ResponseWriter;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.faces.FacesException;
import javax.faces.component.behavior.ClientBehavior;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author sebastien.glon
 */
@FacesComponent(value = "fileUpload")
public class UploadRenderer extends UIInput implements ClientBehaviorHolder {

    int i;

    @Override
    public String getFamily() {
        return "fileUpload";
    }

    @Override
    public void encodeEnd(FacesContext context) throws IOException {

        ResponseWriter responseWriter = context.getResponseWriter();
        String clientId = getClientId(context);

        responseWriter.startElement("input", null);
        responseWriter.writeAttribute("id", clientId, "id");
        responseWriter.writeAttribute("name", clientId, "clientId");
        responseWriter.writeAttribute("type", "file", "file");

        //add a style class if the styleClass attribute exist
        UIComponent component = getCurrentComponent(context);
        String styleClass = (String) component.getAttributes().get("styleClass");
        if (styleClass != null) {
            responseWriter.writeAttribute("class", styleClass, "styleClass");
        }

        //add a style if the style attribute exist
        String style = (String) component.getAttributes().get("style");
        if (style != null) {
            responseWriter.writeAttribute("style", style, "style");
        }

        responseWriter.endElement("input");
        responseWriter.flush();
    }

    @Override
    public void decode(FacesContext context) {
        final ExternalContext external = context.getExternalContext();
        final HttpServletRequest request =
                (HttpServletRequest) external.getRequest();
        final String clientId = getClientId(context);
        final FileItem item = (FileItem) request.getAttribute(clientId);

        final ValueExpression valueExpr = getValueExpression("value");
        Object target = getAttributes().get("target");
        File file;
        if (target != null) {
            String realPath = getServletRealPath(external, target.toString());
            String fileName = StringUtils.replace(item.getName(), " ", "_");
            file = new File(getLongFileName(realPath, fileName));
            try {
                if (item.getSize() > 0 && item.getName().length() > 0) {
                    new File(realPath).mkdirs();
                    item.write(file);
                }
            } catch (Exception ex) {
                throw new FacesException(ex);
            }
            // Recuperation de la valeur pour passage au managed bean.
            if (valueExpr != null) {

                final UIComponent component = getCurrentComponent(context);
                if (item.getSize() > 0) {
                    ((EditableValueHolder) component).setSubmittedValue(fileName);
                } else {
                    ((EditableValueHolder) component).setSubmittedValue(null);
                }
                ((EditableValueHolder) component).setValid(true);
            }
        }

    }

    private String getLongFileName(String path, String fileName) {
        return path + fileName;
    }

    @Override
    public Collection getEventNames() {
        return null;
    }

    @Override
    public String getDefaultEventName() {
        return "";
    }

    @Override
    public void addClientBehavior(String s, ClientBehavior c) {
    }

    @Override
    public Map> getClientBehaviors() {
        return null;
    }

    /**
     *
     * @TODO A modifier pour utilisation d'un context param si present.
     *
     * @param external
     * @param target
     * @return
     */
    public static String getServletRealPath(ExternalContext external, String target) {
        ServletContext servletContext = (ServletContext) external.getContext();
        String realPath = servletContext.getInitParameter(
                "com.evasion.EXTERNAL_DIRECTORY");
        if (!realPath.endsWith("/") && !target.startsWith("/")) {
            realPath += "/" + target;
        } else {
            realPath += target;
        }
        if (!realPath.endsWith("/")) {
            realPath += "/";
        }
        return realPath;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy