View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.model.meta;
17  
18  
19  import org.fourthline.cling.model.Validatable;
20  import org.fourthline.cling.model.ValidationError;
21  import org.fourthline.cling.model.types.BinHexDatatype;
22  import org.seamless.util.io.IO;
23  import org.seamless.util.MimeType;
24  import org.seamless.util.URIUtil;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.net.MalformedURLException;
30  import java.net.URI;
31  import java.net.URL;
32  import java.util.ArrayList;
33  import java.util.List;
34  import java.util.logging.Logger;
35  
36  /**
37   * The metadata of a device icon, might include the actual image data of a local icon.
38   *
39   * <p>
40   * Note that validation of icons is lax on purpose, a valid <code>Icon</code> might still
41   * return <code>null</code> from {@link #getMimeType()}, {@link #getWidth()},
42   * {@link #getHeight()}, and {@link #getDepth()}. However, {@link #getUri()} will return
43   * a valid URI for a valid <code>Icon</code>.
44   * </p>
45   *
46   * @author Christian Bauer
47   */
48  public class Icon implements Validatable {
49  
50      final private static Logger log = Logger.getLogger(StateVariable.class.getName());
51  
52      final private MimeType mimeType;
53      final private int width;
54      final private int height;
55      final private int depth;
56      final private URI uri;
57      final private byte[] data;
58  
59      // Package mutable state
60      private Device device;
61  
62      /**
63       * Used internally by Cling when {@link RemoteDevice} is discovered, you shouldn't have to call this.
64       */
65      public Icon(String mimeType, int width, int height, int depth, URI uri) {
66          this(mimeType != null && mimeType.length() > 0 ? MimeType.valueOf(mimeType) : null, width, height, depth, uri, null);
67      }
68  
69      /**
70       * Use this constructor if your local icon data can be resolved on the classpath, for
71       * example: <code>MyClass.class.getResource("/my/icon.png)</code>
72       *
73       * @param url A URL of the icon data that can be read with <code>new File(url.toURI())</code>.
74       */
75      public Icon(String mimeType, int width, int height, int depth, URL url) throws IOException{
76          this(mimeType, width, height, depth, new File(URIUtil.toURI(url)));
77      }
78  
79      /**
80       * Use this constructor if your local icon data can be resolved with a <code>File</code>, the file's
81       * name must be unique within the scope of a device.
82       */
83      public Icon(String mimeType, int width, int height, int depth, File file) throws IOException {
84          this(mimeType, width, height, depth, file.getName(), IO.readBytes(file));
85      }
86  
87      /**
88       * Use this constructor if your local icon data is an <code>InputStream</code>.
89       *
90       * @param uniqueName Must be a valid URI path segment and unique within the scope of a device.
91       */
92      public Icon(String mimeType, int width, int height, int depth, String uniqueName, InputStream is) throws IOException {
93          this(mimeType, width, height, depth, uniqueName, IO.readBytes(is));
94      }
95  
96      /**
97       * Use this constructor if your local icon data is in a <code>byte[]</code>.
98       *
99       * @param uniqueName Must be a valid URI path segment and unique within the scope of a device.
100      */
101     public Icon(String mimeType, int width, int height, int depth, String uniqueName, byte[] data) {
102         this(mimeType != null && mimeType.length() > 0 ? MimeType.valueOf(mimeType) : null, width, height, depth, URI.create(uniqueName), data);
103     }
104 
105     /**
106      * Use this constructor if your local icon is binary data encoded with <em>BinHex</em>.
107 
108      * @param uniqueName Must be a valid URI path segment and unique within the scope of a device.
109      * @param binHexEncoded The icon bytes encoded as BinHex.
110      */
111     public Icon(String mimeType, int width, int height, int depth, String uniqueName, String binHexEncoded) {
112         this(
113                 mimeType, width, height, depth, uniqueName,
114                 binHexEncoded != null && !binHexEncoded.equals("") ? new BinHexDatatype().valueOf(binHexEncoded) : null
115         );
116     }
117 
118     protected Icon(MimeType mimeType, int width, int height, int depth, URI uri, byte[] data) {
119         this.mimeType = mimeType;
120         this.width = width;
121         this.height = height;
122         this.depth = depth;
123         this.uri = uri;
124         this.data = data;
125     }
126 
127     public MimeType getMimeType() {
128         return mimeType;
129     }
130 
131     public int getWidth() {
132         return width;
133     }
134 
135     public int getHeight() {
136         return height;
137     }
138 
139     public int getDepth() {
140         return depth;
141     }
142 
143     public URI getUri() {
144         return uri;
145     }
146 
147     public byte[] getData() {
148         return data;
149     }
150 
151     public Device getDevice() {
152         return device;
153     }
154 
155     void setDevice(Device device) {
156         if (this.device != null)
157             throw new IllegalStateException("Final value has been set already, model is immutable");
158         this.device = device;
159     }
160 
161     public List<ValidationError> validate() {
162         List<ValidationError> errors = new ArrayList<>();
163 
164         if (getMimeType() == null) {
165             log.warning("UPnP specification violation of: " + getDevice());
166             log.warning("Invalid icon, missing mime type: " + this);
167         }
168         if (getWidth() == 0) {
169             log.warning("UPnP specification violation of: " + getDevice());
170             log.warning("Invalid icon, missing width: " + this);
171         }
172         if (getHeight() == 0) {
173             log.warning("UPnP specification violation of: " + getDevice());
174             log.warning("Invalid icon, missing height: " + this);
175         }
176         if (getDepth() == 0) {
177             log.warning("UPnP specification violation of: " + getDevice());
178             log.warning("Invalid icon, missing bitmap depth: " + this);
179         }
180 
181         if (getUri() == null) {
182             errors.add(new ValidationError(
183                     getClass(),
184                     "uri",
185                     "URL is required"
186             ));
187         } else {
188         	try {
189         		URL testURI = getUri().toURL();
190         		if (testURI == null)
191         			throw new MalformedURLException();
192         	} catch (MalformedURLException ex) {
193         		errors.add(new ValidationError(
194         				getClass(),
195         				"uri",
196         				"URL must be valid: " + ex.getMessage())
197         				);
198         	} catch (IllegalArgumentException ex) {
199         		// Relative URI is fine here!
200         	}
201         }
202 
203         return errors;
204     }
205 
206     public Icon deepCopy() {
207         return new Icon(
208                 getMimeType(),
209                 getWidth(),
210                 getHeight(),
211                 getDepth(),
212                 getUri(),
213                 getData()
214         );
215     }
216 
217     @Override
218     public String toString() {
219         return "Icon(" + getWidth() + "x" + getHeight() + ", MIME: " + getMimeType() + ") " + getUri();
220     }
221 }