001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.api.model.impl.primitives;
016
017import java.io.ByteArrayInputStream;
018import java.io.IOException;
019import java.io.InputStream;
020import java.io.Serializable;
021
022import org.nuxeo.common.utils.FileUtils;
023import org.nuxeo.ecm.core.api.model.InvalidPropertyValueException;
024import org.nuxeo.ecm.core.api.model.Property;
025import org.nuxeo.ecm.core.api.model.PropertyConversionException;
026import org.nuxeo.ecm.core.api.model.impl.ScalarProperty;
027import org.nuxeo.ecm.core.schema.types.Field;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032public class BinaryProperty extends ScalarProperty {
033
034    private static final long serialVersionUID = 1L;
035
036    public BinaryProperty(Property parent, Field field, int flags) {
037        super(parent, field, flags);
038    }
039
040    @Override
041    public boolean isNormalized(Object value) {
042        return value == null || value instanceof InputStream;
043    }
044
045    @Override
046    public Serializable normalize(Object value) throws PropertyConversionException {
047        if (isNormalized(value)) {
048            // TODO if input stream is not serializable? do we convert to a serializable input stream?
049            return (Serializable) value;
050        }
051        throw new PropertyConversionException(value.getClass(), InputStream.class);
052        // TODO byte array is not serializable
053        // if (value.getClass() == String.class) {
054        // return new ByteArrayInputStream(((String)value).getBytes());
055        // }
056        // if (value.getClass() == byte[].class) {
057        // return new ByteArrayInputStream((byte[])value.);
058        // }
059    }
060
061    @SuppressWarnings("unchecked")
062    @Override
063    public <T> T convertTo(Serializable value, Class<T> toType) throws PropertyConversionException {
064        if (value == null) {
065            return null;
066        }
067        if (InputStream.class.isAssignableFrom(toType)) {
068            return (T) value;
069        }
070        if (toType == String.class && value instanceof InputStream) {
071            try {
072                return (T) FileUtils.read((InputStream) value);
073            } catch (IOException e) {
074                throw new InvalidPropertyValueException("Failed to read given input stream", e);
075            }
076        }
077        if (toType == byte[].class && value instanceof InputStream) {
078            try {
079                return (T) FileUtils.readBytes((InputStream) value);
080            } catch (IOException e) {
081                throw new InvalidPropertyValueException("Failed to read given input stream", e);
082            }
083        }
084        throw new PropertyConversionException(value.getClass(), toType);
085    }
086
087    @Override
088    public Object newInstance() {
089        return new ByteArrayInputStream("".getBytes()); // TODO not serializable
090    }
091
092}