001/* 002 * (C) Copyright 2006-2016 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 * bstefanescu 018 */ 019package org.nuxeo.ecm.core.api.model.impl.primitives; 020 021import java.io.ByteArrayInputStream; 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.Serializable; 025 026import org.apache.commons.io.Charsets; 027import org.apache.commons.io.IOUtils; 028import org.nuxeo.ecm.core.api.model.InvalidPropertyValueException; 029import org.nuxeo.ecm.core.api.model.Property; 030import org.nuxeo.ecm.core.api.model.PropertyConversionException; 031import org.nuxeo.ecm.core.api.model.impl.ScalarProperty; 032import org.nuxeo.ecm.core.schema.types.Field; 033 034/** 035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 036 */ 037public class BinaryProperty extends ScalarProperty { 038 039 private static final long serialVersionUID = 1L; 040 041 public BinaryProperty(Property parent, Field field, int flags) { 042 super(parent, field, flags); 043 } 044 045 @Override 046 public boolean isNormalized(Object value) { 047 return value == null || value instanceof InputStream; 048 } 049 050 @Override 051 public Serializable normalize(Object value) throws PropertyConversionException { 052 if (isNormalized(value)) { 053 // TODO if input stream is not serializable? do we convert to a serializable input stream? 054 return (Serializable) value; 055 } 056 throw new PropertyConversionException(value.getClass(), InputStream.class); 057 // TODO byte array is not serializable 058 // if (value.getClass() == String.class) { 059 // return new ByteArrayInputStream(((String)value).getBytes()); 060 // } 061 // if (value.getClass() == byte[].class) { 062 // return new ByteArrayInputStream((byte[])value.); 063 // } 064 } 065 066 @SuppressWarnings("unchecked") 067 @Override 068 public <T> T convertTo(Serializable value, Class<T> toType) throws PropertyConversionException { 069 if (value == null) { 070 return null; 071 } 072 if (InputStream.class.isAssignableFrom(toType)) { 073 return (T) value; 074 } 075 if (toType == String.class && value instanceof InputStream) { 076 try (InputStream in = (InputStream) value){ 077 return (T) IOUtils.toString(in, Charsets.UTF_8); 078 } catch (IOException e) { 079 throw new InvalidPropertyValueException("Failed to read given input stream", e); 080 } 081 } 082 if (toType == byte[].class && value instanceof InputStream) { 083 try { 084 return (T) IOUtils.toByteArray((InputStream) value); 085 } catch (IOException e) { 086 throw new InvalidPropertyValueException("Failed to read given input stream", e); 087 } 088 } 089 throw new PropertyConversionException(value.getClass(), toType); 090 } 091 092 @Override 093 public Object newInstance() { 094 return new ByteArrayInputStream("".getBytes()); // TODO not serializable 095 } 096 097}