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