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