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 *     Bogdan Stefanescu
018 *     Florent Guillaume
019 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
020 */
021package org.nuxeo.ecm.core.schema.types.primitives;
022
023import java.io.ByteArrayInputStream;
024import java.io.IOException;
025import java.io.InputStream;
026
027import org.apache.commons.io.Charsets;
028import org.apache.commons.io.IOUtils;
029import org.apache.commons.lang.StringUtils;
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.ecm.core.schema.types.PrimitiveType;
033import org.nuxeo.ecm.core.schema.types.constraints.Constraint;
034import org.nuxeo.ecm.core.schema.types.constraints.NotNullConstraint;
035
036/**
037 * The binary type handles values of type InputStream.
038 */
039public final class BinaryType extends PrimitiveType {
040
041    private static final long serialVersionUID = 1L;
042
043    private static final Log log = LogFactory.getLog(BinaryType.class);
044
045    public static final String ID = "binary";
046
047    public static final BinaryType INSTANCE = new BinaryType();
048
049    private BinaryType() {
050        super(ID);
051    }
052
053    @Override
054    public boolean validate(Object object) {
055        return true;
056    }
057
058    @Override
059    public Object convert(Object value) {
060        if (value instanceof CharSequence) {
061            return new ByteArrayInputStream(value.toString().getBytes());
062        } else if (value instanceof byte[]) {
063            return new ByteArrayInputStream((byte[]) value);
064        } else if (value instanceof InputStream) {
065            return value;
066        }
067        return null;
068    }
069
070    public static Object parseString(String str) {
071        return new ByteArrayInputStream(str.getBytes());
072    }
073
074    protected Object readResolve() {
075        return INSTANCE;
076    }
077
078    @Override
079    public Object decode(String str) {
080        if (StringUtils.isEmpty(str)) {
081            return null;
082        }
083        return new ByteArrayInputStream(str.getBytes());
084    }
085
086    @Override
087    public String encode(Object object) {
088        if (object instanceof InputStream) {
089            try (InputStream in = (InputStream) object){
090                return IOUtils.toString(in, Charsets.UTF_8);
091            } catch (IOException e) {
092                log.error(e, e);
093                return null;
094            }
095        }
096        return object.toString();
097    }
098
099    @Override
100    public boolean support(Class<? extends Constraint> constraint) {
101        if (NotNullConstraint.class.equals(constraint)) {
102            return true;
103        }
104        return false;
105    }
106
107}