1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/model/property/Attach.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,303 @@ 1.4 +/** 1.5 + * Copyright (c) 2012, Ben Fortuna 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1.12 + * o Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * o Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * o Neither the name of Ben Fortuna nor the names of any other contributors 1.20 + * may be used to endorse or promote products derived from this software 1.21 + * without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + */ 1.35 +package net.fortuna.ical4j.model.property; 1.36 + 1.37 +import java.io.IOException; 1.38 +import java.io.UnsupportedEncodingException; 1.39 +import java.net.URI; 1.40 +import java.net.URISyntaxException; 1.41 + 1.42 +import net.fortuna.ical4j.model.Parameter; 1.43 +import net.fortuna.ical4j.model.ParameterList; 1.44 +import net.fortuna.ical4j.model.Property; 1.45 +import net.fortuna.ical4j.model.PropertyFactoryImpl; 1.46 +import net.fortuna.ical4j.model.ValidationException; 1.47 +import net.fortuna.ical4j.model.parameter.Encoding; 1.48 +import net.fortuna.ical4j.model.parameter.Value; 1.49 +import net.fortuna.ical4j.util.DecoderFactory; 1.50 +import net.fortuna.ical4j.util.EncoderFactory; 1.51 +import net.fortuna.ical4j.util.ParameterValidator; 1.52 +import net.fortuna.ical4j.util.Strings; 1.53 +import net.fortuna.ical4j.util.Uris; 1.54 + 1.55 +import org.apache.commons.codec.BinaryDecoder; 1.56 +import org.apache.commons.codec.BinaryEncoder; 1.57 +import org.apache.commons.codec.DecoderException; 1.58 +import org.apache.commons.codec.EncoderException; 1.59 +import org.apache.commons.logging.Log; 1.60 +import org.apache.commons.logging.LogFactory; 1.61 + 1.62 +/** 1.63 + * $Id$ 1.64 + * 1.65 + * Created: [Apr 6, 2004] 1.66 + * 1.67 + * Defines an ATTACH iCalendar component property. 1.68 + * 1.69 + * <pre> 1.70 + * 4.8.1.1 Attachment 1.71 + * 1.72 + * Property Name: ATTACH 1.73 + * 1.74 + * Purpose: The property provides the capability to associate a document 1.75 + * object with a calendar component. 1.76 + * 1.77 + * Value Type: The default value type for this property is URI. The 1.78 + * value type can also be set to BINARY to indicate inline binary 1.79 + * encoded content information. 1.80 + * 1.81 + * Property Parameters: Non-standard, inline encoding, format type and 1.82 + * value data type property parameters can be specified on this 1.83 + * property. 1.84 + * 1.85 + * Conformance: The property can be specified in a "VEVENT", "VTODO", 1.86 + * "VJOURNAL" or "VALARM" calendar components. 1.87 + * 1.88 + * Description: The property can be specified within "VEVENT", "VTODO", 1.89 + * "VJOURNAL", or "VALARM" calendar components. This property can be 1.90 + * specified multiple times within an iCalendar object. 1.91 + * 1.92 + * Format Definition: The property is defined by the following notation: 1.93 + * 1.94 + * attach = "ATTACH" attparam ":" uri CRLF 1.95 + * 1.96 + * attach =/ "ATTACH" attparam ";" "ENCODING" "=" "BASE64" 1.97 + * ";" "VALUE" "=" "BINARY" ":" binary 1.98 + * 1.99 + * attparam = *( 1.100 + * 1.101 + * ; the following is optional, 1.102 + * ; but MUST NOT occur more than once 1.103 + * 1.104 + * (";" fmttypeparam) / 1.105 + * 1.106 + * ; the following is optional, 1.107 + * ; and MAY occur more than once 1.108 + * 1.109 + * (";" xparam) 1.110 + * 1.111 + * ) 1.112 + * </pre> 1.113 + * 1.114 + * @author benf 1.115 + */ 1.116 +public class Attach extends Property { 1.117 + 1.118 + private static final long serialVersionUID = 4439949507756383452L; 1.119 + 1.120 + private URI uri; 1.121 + 1.122 + private byte[] binary; 1.123 + 1.124 + /** 1.125 + * Default constructor. 1.126 + */ 1.127 + public Attach() { 1.128 + super(ATTACH, PropertyFactoryImpl.getInstance()); 1.129 + } 1.130 + 1.131 + /** 1.132 + * @param aList a list of parameters for this component 1.133 + * @param aValue a value string for this component 1.134 + * @throws IOException when there is an error reading the binary stream 1.135 + * @throws URISyntaxException where the specified string is not a valid uri 1.136 + */ 1.137 + public Attach(final ParameterList aList, final String aValue) 1.138 + throws IOException, URISyntaxException { 1.139 + super(ATTACH, aList, PropertyFactoryImpl.getInstance()); 1.140 + setValue(aValue); 1.141 + } 1.142 + 1.143 + /** 1.144 + * @param data binary data 1.145 + */ 1.146 + public Attach(final byte[] data) { 1.147 + super(ATTACH, PropertyFactoryImpl.getInstance()); 1.148 + // add required parameters.. 1.149 + getParameters().add(Encoding.BASE64); 1.150 + getParameters().add(Value.BINARY); 1.151 + this.binary = data; 1.152 + } 1.153 + 1.154 + /** 1.155 + * @param aList a list of parameters for this component 1.156 + * @param data binary data 1.157 + */ 1.158 + public Attach(final ParameterList aList, final byte[] data) { 1.159 + super(ATTACH, aList, PropertyFactoryImpl.getInstance()); 1.160 + this.binary = data; 1.161 + } 1.162 + 1.163 + /** 1.164 + * @param aUri a URI 1.165 + */ 1.166 + public Attach(final URI aUri) { 1.167 + super(ATTACH, PropertyFactoryImpl.getInstance()); 1.168 + this.uri = aUri; 1.169 + } 1.170 + 1.171 + /** 1.172 + * @param aList a list of parameters for this component 1.173 + * @param aUri a URI 1.174 + */ 1.175 + public Attach(final ParameterList aList, final URI aUri) { 1.176 + super(ATTACH, aList, PropertyFactoryImpl.getInstance()); 1.177 + this.uri = aUri; 1.178 + } 1.179 + 1.180 + /** 1.181 + * {@inheritDoc} 1.182 + */ 1.183 + public final void validate() throws ValidationException { 1.184 + 1.185 + /* 1.186 + * ; the following is optional, ; but MUST NOT occur more than once (";" fmttypeparam) / 1.187 + */ 1.188 + ParameterValidator.getInstance().assertOneOrLess(Parameter.FMTTYPE, 1.189 + getParameters()); 1.190 + 1.191 + /* 1.192 + * ; the following is optional, ; and MAY occur more than once (";" xparam) 1.193 + */ 1.194 + 1.195 + /* 1.196 + * If the value type parameter is ";VALUE=BINARY", then the inline encoding parameter MUST be specified with the 1.197 + * value ";ENCODING=BASE64". 1.198 + */ 1.199 + if (Value.BINARY.equals(getParameter(Parameter.VALUE))) { 1.200 + ParameterValidator.getInstance().assertOne(Parameter.ENCODING, 1.201 + getParameters()); 1.202 + if (!Encoding.BASE64.equals(getParameter(Parameter.ENCODING))) { 1.203 + throw new ValidationException( 1.204 + "If the value type parameter is [BINARY], the inline" 1.205 + + "encoding parameter MUST be specified with the value [BASE64]"); 1.206 + } 1.207 + } 1.208 + } 1.209 + 1.210 + /** 1.211 + * @return Returns the binary. 1.212 + */ 1.213 + public final byte[] getBinary() { 1.214 + return binary; 1.215 + } 1.216 + 1.217 + /** 1.218 + * @return Returns the uri. 1.219 + */ 1.220 + public final URI getUri() { 1.221 + return uri; 1.222 + } 1.223 + 1.224 + /** 1.225 + * Sets the current value of the Attach instance. If the specified 1.226 + * value is encoded binary data, the value is decoded and stored in 1.227 + * the binary field. Otherwise the value is assumed to be a URI 1.228 + * location to binary data and is stored as such. 1.229 + * 1.230 + * @param aValue a string encoded binary or URI value 1.231 + * @throws IOException where binary data cannot be decoded 1.232 + * @throws URISyntaxException where the specified value is not a valid URI 1.233 + */ 1.234 + public final void setValue(final String aValue) throws IOException, 1.235 + URISyntaxException { 1.236 + 1.237 + // determine if ATTACH is a URI or an embedded 1.238 + // binary.. 1.239 + if (getParameter(Parameter.ENCODING) != null) { 1.240 + // binary = Base64.decode(aValue); 1.241 + try { 1.242 + final BinaryDecoder decoder = DecoderFactory.getInstance() 1.243 + .createBinaryDecoder( 1.244 + (Encoding) getParameter(Parameter.ENCODING)); 1.245 + binary = decoder.decode(aValue.getBytes()); 1.246 + } 1.247 + catch (UnsupportedEncodingException uee) { 1.248 + Log log = LogFactory.getLog(Attach.class); 1.249 + log.error("Error encoding binary data", uee); 1.250 + } 1.251 + catch (DecoderException de) { 1.252 + Log log = LogFactory.getLog(Attach.class); 1.253 + log.error("Error decoding binary data", de); 1.254 + } 1.255 + } 1.256 + // assume URI.. 1.257 + else { 1.258 + uri = Uris.create(aValue); 1.259 + } 1.260 + } 1.261 + 1.262 + /** 1.263 + * {@inheritDoc} 1.264 + */ 1.265 + public final String getValue() { 1.266 + if (getUri() != null) { 1.267 + return Uris.decode(Strings.valueOf(getUri())); 1.268 + } 1.269 + else if (getBinary() != null) { 1.270 + // return Base64.encodeBytes(getBinary(), Base64.DONT_BREAK_LINES); 1.271 + try { 1.272 + final BinaryEncoder encoder = EncoderFactory.getInstance() 1.273 + .createBinaryEncoder( 1.274 + (Encoding) getParameter(Parameter.ENCODING)); 1.275 + return new String(encoder.encode(getBinary())); 1.276 + } 1.277 + catch (UnsupportedEncodingException uee) { 1.278 + Log log = LogFactory.getLog(Attach.class); 1.279 + log.error("Error encoding binary data", uee); 1.280 + } 1.281 + catch (EncoderException ee) { 1.282 + Log log = LogFactory.getLog(Attach.class); 1.283 + log.error("Error encoding binary data", ee); 1.284 + } 1.285 + } 1.286 + return null; 1.287 + } 1.288 + 1.289 + /** 1.290 + * @param binary The binary to set. 1.291 + */ 1.292 + public final void setBinary(final byte[] binary) { 1.293 + this.binary = binary; 1.294 + // unset uri.. 1.295 + this.uri = null; 1.296 + } 1.297 + 1.298 + /** 1.299 + * @param uri The uri to set. 1.300 + */ 1.301 + public final void setUri(final URI uri) { 1.302 + this.uri = uri; 1.303 + // unset binary.. 1.304 + this.binary = null; 1.305 + } 1.306 +}