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