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