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