001package biweekly.property; 002 003import java.io.File; 004import java.io.FileInputStream; 005import java.io.IOException; 006import java.util.List; 007 008import biweekly.Warning; 009import biweekly.component.ICalComponent; 010import biweekly.util.IOUtils; 011 012/* 013 Copyright (c) 2013, Michael Angstadt 014 All rights reserved. 015 016 Redistribution and use in source and binary forms, with or without 017 modification, are permitted provided that the following conditions are met: 018 019 1. Redistributions of source code must retain the above copyright notice, this 020 list of conditions and the following disclaimer. 021 2. Redistributions in binary form must reproduce the above copyright notice, 022 this list of conditions and the following disclaimer in the documentation 023 and/or other materials provided with the distribution. 024 025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 026 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 027 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 028 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 029 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 030 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 031 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 032 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 033 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 034 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 035 */ 036 037/** 038 * <p> 039 * Defines a binary resource that is associated with the component to which it 040 * belongs (such as an image or document). 041 * </p> 042 * 043 * <p> 044 * <b>Code sample:</b> 045 * 046 * <pre class="brush:java"> 047 * VEvent event = new VEvent(); 048 * 049 * //from a byte array 050 * byte[] data = ... 051 * Attachment attach = new Attachment("image/png", data); 052 * event.addAttachment(attach); 053 * 054 * //from a file 055 * File file = new File("image.png"); 056 * attach = new Attachment("image/png", file); 057 * event.addAttachment(attach); 058 * 059 * //referencing a URL 060 * attach = new Attachment("image/png", "http://example.com/image.png"); 061 * event.addAttachment(attach); 062 * </pre> 063 * 064 * </p> 065 * @author Michael Angstadt 066 * @see <a href="http://tools.ietf.org/html/rfc5545#page-80">RFC 5545 p.80-1</a> 067 */ 068public class Attachment extends ICalProperty { 069 private byte[] data; 070 private String uri; 071 072 /** 073 * Creates a new attachment. 074 * @param formatType the content-type of the data (e.g. "image/png") 075 * @param file the file to attach 076 * @throws IOException if there's a problem reading from the file 077 */ 078 public Attachment(String formatType, File file) throws IOException { 079 this.data = IOUtils.toByteArray(new FileInputStream(file), true); 080 setFormatType(formatType); 081 } 082 083 /** 084 * Creates a new attachment. 085 * @param formatType the content-type of the data (e.g. "image/png") 086 * @param data the binary data 087 */ 088 public Attachment(String formatType, byte[] data) { 089 this.data = data; 090 setFormatType(formatType); 091 } 092 093 /** 094 * Creates a new attachment. 095 * @param formatType the content-type of the data (e.g. "image/png") 096 * @param uri a URL pointing to the resource (e.g. 097 * "http://example.com/image.png") 098 */ 099 public Attachment(String formatType, String uri) { 100 this.uri = uri; 101 setFormatType(formatType); 102 } 103 104 /** 105 * Gets the attachment's binary data. 106 * @return the binary data or null if not set 107 */ 108 public byte[] getData() { 109 return data; 110 } 111 112 /** 113 * Sets the attachment's binary data. If the attachment has a URI associated 114 * with it, the URI will be set to null. 115 * @param data the binary data 116 */ 117 public void setData(byte[] data) { 118 this.data = data; 119 uri = null; 120 } 121 122 /** 123 * Gets the attachment's URI. 124 * @return the URI (e.g. "http://example.com/image.png") or null if not set 125 */ 126 public String getUri() { 127 return uri; 128 } 129 130 /** 131 * Sets the attachment's URI. If the attachment has binary data associated 132 * with it, the binary data will be set to null. 133 * @param uri the URI (e.g. "http://example.com/image.png") 134 */ 135 public void setUri(String uri) { 136 this.uri = uri; 137 data = null; 138 } 139 140 @Override 141 public String getFormatType() { 142 return super.getFormatType(); 143 } 144 145 @Override 146 public void setFormatType(String formatType) { 147 super.setFormatType(formatType); 148 } 149 150 @Override 151 protected void validate(List<ICalComponent> components, List<Warning> warnings) { 152 if (uri == null && data == null) { 153 warnings.add(Warning.validate(26)); 154 } 155 } 156}