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(), getConvertedMimeType()); 108 } catch (ConverterException e) { 109 throw e; 110 } catch (IOException e) { 111 throw new ConverterException(INVALID_FILE_MESSAGE); 112 } 113 } else if (blob != null) { 114 throw new ConverterException(INVALID_FILE_MESSAGE); 115 } 116 return convertedBlob; 117 } 118 119 public Object getChoice() { 120 return choice; 121 } 122 123 public void setChoice(Object choice) { 124 this.choice = choice; 125 } 126 127 public String getConvertedChoice() throws ConverterException { 128 if (choice == null || choice instanceof String) { 129 return (String) choice; 130 } else { 131 throw new ConverterException("error.inputFile.invalidChoice"); 132 } 133 } 134 135 public Object getFilename() { 136 return filename; 137 } 138 139 public void setFilename(Object filename) { 140 this.filename = filename; 141 } 142 143 public String getConvertedFilename() throws ConverterException { 144 String convertedFilename = null; 145 if (filename instanceof String) { 146 convertedFilename = FileUtils.getCleanFileName((String) filename); 147 } else if (filename != null) { 148 throw new ConverterException("error.inputFile.invalidFilename"); 149 } 150 return convertedFilename; 151 } 152 153 protected static boolean equalValues(Object first, Object second) { 154 if (first == null) { 155 return second == null; 156 } else { 157 return first.equals(second); 158 } 159 } 160 161 @Override 162 public boolean equals(Object obj) { 163 if (this == obj) { 164 return true; 165 } 166 if (!(obj instanceof InputFileInfo)) { 167 return false; 168 } 169 InputFileInfo other = (InputFileInfo) obj; 170 if (!equalValues(choice, other.choice)) { 171 return false; 172 } 173 if (!equalValues(filename, other.filename)) { 174 return false; 175 } 176 if (!equalValues(blob, other.blob)) { 177 return false; 178 } 179 if (!equalValues(mimeType, other.mimeType)) { 180 return false; 181 } 182 return true; 183 } 184 185}