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