001/*
002 * (C) Copyright 2006-2007 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 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
018 *
019 * $Id: InputFileInfo.java 28476 2008-01-04 09:52:52Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.ui.web.component.file;
023
024import java.io.IOException;
025import java.io.InputStream;
026import java.io.Serializable;
027
028import javax.faces.convert.ConverterException;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.platform.ui.web.util.files.FileUtils;
034
035/**
036 * File information used to manage a file adding/removal.
037 *
038 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
039 */
040public class InputFileInfo implements Serializable {
041
042    private static final long serialVersionUID = 1L;
043
044    private static final Log log = LogFactory.getLog(InputFileInfo.class);
045
046    public static final String EMPTY_FILE_MESSAGE = "error.inputFile.emptyFile";
047
048    public static final String INVALID_FILE_MESSAGE = "error.inputFile.invalidFile";
049
050    public static final String INVALID_WITH_AJAX_MESSAGE = "error.inputFile.ajax";
051
052    protected Object choice;
053
054    protected Blob blob;
055
056    protected Object filename;
057
058    protected Object mimeType;
059
060    // empty constructor needed by JSF restore method
061    public InputFileInfo() {
062        super();
063    }
064
065    public InputFileInfo(Object choice, Blob blob, Object filename, Object mimeType) {
066        this.choice = choice;
067        this.blob = blob;
068        this.filename = filename;
069        this.mimeType = mimeType;
070    }
071
072    public Blob getBlob() {
073        return blob;
074    }
075
076    public void setBlob(Blob blob) {
077        this.blob = blob;
078    }
079
080    public Object getMimeType() {
081        return mimeType;
082    }
083
084    public void setMimeType(Object mimeType) {
085        this.mimeType = mimeType;
086    }
087
088    public String getConvertedMimeType() throws ConverterException {
089        if (mimeType instanceof String) {
090            return (String) mimeType;
091        } else if (mimeType != null) {
092            log.error("Invalid mimetype detected: " + mimeType);
093        }
094        return null;
095    }
096
097    public Blob getConvertedBlob() throws ConverterException {
098        Blob convertedBlob = null;
099        if (blob instanceof Blob) {
100            convertedBlob = (Blob) blob;
101        } else if (blob instanceof InputStream) {
102            InputStream upFile = (InputStream) blob;
103            try {
104                if (upFile.available() == 0) {
105                    throw new ConverterException(INVALID_FILE_MESSAGE);
106                }
107                convertedBlob = FileUtils.createSerializableBlob(upFile, getConvertedFilename(),
108                        getConvertedMimeType());
109            } catch (ConverterException e) {
110                throw e;
111            } catch (IOException e) {
112                throw new ConverterException(INVALID_FILE_MESSAGE);
113            }
114        } else if (blob != null) {
115            throw new ConverterException(INVALID_FILE_MESSAGE);
116        }
117        return convertedBlob;
118    }
119
120    public Object getChoice() {
121        return choice;
122    }
123
124    public void setChoice(Object choice) {
125        this.choice = choice;
126    }
127
128    public String getConvertedChoice() throws ConverterException {
129        if (choice == null || choice instanceof String) {
130            return (String) choice;
131        } else {
132            throw new ConverterException("error.inputFile.invalidChoice");
133        }
134    }
135
136    public Object getFilename() {
137        return filename;
138    }
139
140    public void setFilename(Object filename) {
141        this.filename = filename;
142    }
143
144    public String getConvertedFilename() throws ConverterException {
145        String convertedFilename = null;
146        if (filename instanceof String) {
147            convertedFilename = FileUtils.getCleanFileName((String) filename);
148        } else if (filename != null) {
149            throw new ConverterException("error.inputFile.invalidFilename");
150        }
151        return convertedFilename;
152    }
153
154    protected static boolean equalValues(Object first, Object second) {
155        if (first == null) {
156            return second == null;
157        } else {
158            return first.equals(second);
159        }
160    }
161
162    /**
163     * Restores information on blob and filename from given {@link InputFileInfo}.
164     *
165     * @since 10.1
166     */
167    public void setInfo(InputFileInfo previous) {
168        if (previous == null) {
169            setBlob(null);
170            setFilename(null);
171            return;
172        }
173        setBlob(previous.getConvertedBlob());
174        setFilename(previous.getConvertedFilename());
175    }
176
177    @Override
178    public boolean equals(Object obj) {
179        if (this == obj) {
180            return true;
181        }
182        if (!(obj instanceof InputFileInfo)) {
183            return false;
184        }
185        InputFileInfo other = (InputFileInfo) obj;
186        if (!equalValues(choice, other.choice)) {
187            return false;
188        }
189        if (!equalValues(filename, other.filename)) {
190            return false;
191        }
192        if (!equalValues(blob, other.blob)) {
193            return false;
194        }
195        if (!equalValues(mimeType, other.mimeType)) {
196            return false;
197        }
198        return true;
199    }
200
201}