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 INVALID_FILE_MESSAGE = "error.inputFile.invalidFile"; 047 048 public static final String INVALID_WITH_AJAX_MESSAGE = "error.inputFile.ajax"; 049 050 protected Object choice; 051 052 protected Blob blob; 053 054 protected Object filename; 055 056 protected Object mimeType; 057 058 // empty constructor needed by JSF restore method 059 public InputFileInfo() { 060 super(); 061 } 062 063 public InputFileInfo(Object choice, Blob blob, Object filename, Object mimeType) { 064 this.choice = choice; 065 this.blob = blob; 066 this.filename = filename; 067 this.mimeType = mimeType; 068 } 069 070 public Blob getBlob() { 071 return blob; 072 } 073 074 public void setBlob(Blob blob) { 075 this.blob = blob; 076 } 077 078 public Object getMimeType() { 079 return mimeType; 080 } 081 082 public void setMimeType(Object mimeType) { 083 this.mimeType = mimeType; 084 } 085 086 public String getConvertedMimeType() throws ConverterException { 087 if (mimeType instanceof String) { 088 return (String) mimeType; 089 } else if (mimeType != null) { 090 log.error("Invalid mimetype detected: " + mimeType); 091 } 092 return null; 093 } 094 095 public Blob getConvertedBlob() throws ConverterException { 096 Blob convertedBlob = null; 097 if (blob instanceof Blob) { 098 convertedBlob = (Blob) blob; 099 } else if (blob instanceof InputStream) { 100 InputStream upFile = (InputStream) blob; 101 try { 102 if (upFile.available() == 0) { 103 throw new ConverterException(INVALID_FILE_MESSAGE); 104 } 105 convertedBlob = FileUtils.createSerializableBlob(upFile, getConvertedFilename(), getConvertedMimeType()); 106 } catch (ConverterException e) { 107 throw e; 108 } catch (IOException e) { 109 throw new ConverterException(INVALID_FILE_MESSAGE); 110 } 111 } else if (blob != null) { 112 throw new ConverterException(INVALID_FILE_MESSAGE); 113 } 114 return convertedBlob; 115 } 116 117 public Object getChoice() { 118 return choice; 119 } 120 121 public void setChoice(Object choice) { 122 this.choice = choice; 123 } 124 125 public String getConvertedChoice() throws ConverterException { 126 if (choice == null || choice instanceof String) { 127 return (String) choice; 128 } else { 129 throw new ConverterException("error.inputFile.invalidChoice"); 130 } 131 } 132 133 public Object getFilename() { 134 return filename; 135 } 136 137 public void setFilename(Object filename) { 138 this.filename = filename; 139 } 140 141 public String getConvertedFilename() throws ConverterException { 142 String convertedFilename = null; 143 if (filename instanceof String) { 144 convertedFilename = FileUtils.getCleanFileName((String) filename); 145 } else if (filename != null) { 146 throw new ConverterException("error.inputFile.invalidFilename"); 147 } 148 return convertedFilename; 149 } 150 151 protected static boolean equalValues(Object first, Object second) { 152 if (first == null) { 153 return second == null; 154 } else { 155 return first.equals(second); 156 } 157 } 158 159 @Override 160 public boolean equals(Object obj) { 161 if (this == obj) { 162 return true; 163 } 164 if (!(obj instanceof InputFileInfo)) { 165 return false; 166 } 167 InputFileInfo other = (InputFileInfo) obj; 168 if (!equalValues(choice, other.choice)) { 169 return false; 170 } 171 if (!equalValues(filename, other.filename)) { 172 return false; 173 } 174 if (!equalValues(blob, other.blob)) { 175 return false; 176 } 177 if (!equalValues(mimeType, other.mimeType)) { 178 return false; 179 } 180 return true; 181 } 182 183}