001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.isis.core.progmodel.facets.value.blobs; 021 022import javax.activation.MimeType; 023import javax.activation.MimeTypeParseException; 024 025import org.apache.commons.codec.binary.Base64; 026 027import org.apache.isis.applib.adapters.DefaultsProvider; 028import org.apache.isis.applib.adapters.EncoderDecoder; 029import org.apache.isis.applib.adapters.Parser; 030import org.apache.isis.applib.profiles.Localization; 031import org.apache.isis.applib.value.Blob; 032import org.apache.isis.core.commons.config.IsisConfiguration; 033import org.apache.isis.core.metamodel.facetapi.Facet; 034import org.apache.isis.core.metamodel.facetapi.FacetHolder; 035import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract; 036import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext; 037 038public class BlobValueSemanticsProvider extends ValueSemanticsProviderAndFacetAbstract<Blob> implements BlobValueFacet { 039 040 private static final int TYPICAL_LENGTH = 0; 041 042 private static Class<? extends Facet> type() { 043 return BlobValueFacet.class; 044 } 045 046 private static final Blob DEFAULT_VALUE = null; 047 048 /** 049 * Required because implementation of {@link Parser} and 050 * {@link EncoderDecoder}. 051 */ 052 public BlobValueSemanticsProvider() { 053 this(null, null, null); 054 } 055 056 public BlobValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) { 057 super(type(), holder, Blob.class, TYPICAL_LENGTH, Immutability.IMMUTABLE, EqualByContent.NOT_HONOURED, DEFAULT_VALUE, configuration, context); 058 } 059 060 @Override 061 public String titleString(final Object object, final Localization localization) { 062 return object != null? ((Blob)object).getName(): "[null]"; 063 } 064 065 @Override 066 public String titleStringWithMask(final Object value, final String usingMask) { 067 return titleString(value, null); 068 } 069 070 071 // ////////////////////////////////////////////////////////////////// 072 // Parser 073 // ////////////////////////////////////////////////////////////////// 074 075 @Override 076 public Parser<Blob> getParser() { 077 return null; 078 } 079 080 // ////////////////////////////////////////////////////////////////// 081 // DefaultsProvider 082 // ////////////////////////////////////////////////////////////////// 083 084 @Override 085 public DefaultsProvider<Blob> getDefaultsProvider() { 086 return null; 087 } 088 089 // ////////////////////////////////////////////////////////////////// 090 // EncoderDecoder 091 // ////////////////////////////////////////////////////////////////// 092 093 @Override 094 protected String doEncode(final Object object) { 095 Blob blob = (Blob)object; 096 return blob.getName() + ":" + blob.getMimeType().getBaseType() + ":" + Base64.encodeBase64String((blob.getBytes())); 097 } 098 099 @Override 100 protected Blob doRestore(final String data) { 101 final int colonIdx = data.indexOf(':'); 102 final String name = data.substring(0, colonIdx); 103 final int colon2Idx = data.indexOf(":", colonIdx+1); 104 final String mimeTypeBase = data.substring(colonIdx+1, colon2Idx); 105 final byte[] bytes = Base64.decodeBase64(data.substring(colon2Idx+1)); 106 try { 107 return new Blob(name, new MimeType(mimeTypeBase), bytes); 108 } catch (MimeTypeParseException e) { 109 throw new RuntimeException(e); 110 } 111 } 112 113 // /////// toString /////// 114 115 @Override 116 public String toString() { 117 return "BlobValueSemanticsProvider"; 118 } 119 120}