001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.util;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.nio.channels.ReadableByteChannel;
025: import java.nio.channels.WritableByteChannel;
026: import java.util.Date;
027: import java.util.List;
028:
029: import org.restlet.data.CharacterSet;
030: import org.restlet.data.Encoding;
031: import org.restlet.data.Language;
032: import org.restlet.data.MediaType;
033: import org.restlet.data.Reference;
034: import org.restlet.data.Tag;
035: import org.restlet.resource.Representation;
036:
037: /**
038: * Representation wrapper. Useful for application developer who need to enrich
039: * the representation with application related properties and behavior.
040: *
041: * @see <a href="http://c2.com/cgi/wiki?DecoratorPattern">The decorator (aka
042: * wrapper) pattern</a>
043: * @author Jerome Louvel (contact@noelios.com)
044: */
045: public class WrapperRepresentation extends Representation {
046: /** The wrapped representation. */
047: private Representation wrappedRepresentation;
048:
049: /**
050: * Constructor.
051: *
052: * @param wrappedRepresentation
053: * The wrapped representation.
054: */
055: public WrapperRepresentation(Representation wrappedRepresentation) {
056: this .wrappedRepresentation = wrappedRepresentation;
057: }
058:
059: /**
060: * Returns a channel with the representation's content.<br/> If it is
061: * supported by a file, a read-only instance of FileChannel is returned.<br/>
062: * This method is ensured to return a fresh channel for each invocation
063: * unless it is a transient representation, in which case null is returned.
064: *
065: * @return A channel with the representation's content.
066: * @throws IOException
067: */
068: public ReadableByteChannel getChannel() throws IOException {
069: return getWrappedRepresentation().getChannel();
070: }
071:
072: /**
073: * Returns the character set or null if not applicable.
074: *
075: * @return The character set or null if not applicable.
076: */
077: public CharacterSet getCharacterSet() {
078: return getWrappedRepresentation().getCharacterSet();
079: }
080:
081: /**
082: * Returns the list of encodings.
083: *
084: * @return The list of encodings.
085: */
086: public List<Encoding> getEncodings() {
087: return getWrappedRepresentation().getEncodings();
088: }
089:
090: /**
091: * Returns the future date when this representation expire. If this
092: * information is not known, returns null.
093: *
094: * @return The expiration date.
095: */
096: public Date getExpirationDate() {
097: return getWrappedRepresentation().getExpirationDate();
098: }
099:
100: /**
101: * Returns the official identifier.
102: *
103: * @return The official identifier.
104: */
105: public Reference getIdentifier() {
106: return getWrappedRepresentation().getIdentifier();
107: }
108:
109: /**
110: * Returns the list of languages.
111: *
112: * @return The list of languages.
113: */
114: public List<Language> getLanguages() {
115: return getWrappedRepresentation().getLanguages();
116: }
117:
118: /**
119: * Returns the media type.
120: *
121: * @return The media type.
122: */
123: public MediaType getMediaType() {
124: return getWrappedRepresentation().getMediaType();
125: }
126:
127: /**
128: * Returns the last date when this representation was modified. If this
129: * information is not known, returns null.
130: *
131: * @return The modification date.
132: */
133: public Date getModificationDate() {
134: return getWrappedRepresentation().getModificationDate();
135: }
136:
137: /**
138: * Returns the size in bytes if known, UNKNOWN_SIZE (-1) otherwise.
139: *
140: * @return The size in bytes if known, UNKNOWN_SIZE (-1) otherwise.
141: */
142: public long getSize() {
143: return getWrappedRepresentation().getSize();
144: }
145:
146: // ----------------------
147: // Representation methods
148: // ----------------------
149:
150: /**
151: * Returns a stream with the representation's content. This method is
152: * ensured to return a fresh stream for each invocation unless it is a
153: * transient representation, in which case null is returned.
154: *
155: * @return A stream with the representation's content.
156: * @throws IOException
157: */
158: public InputStream getStream() throws IOException {
159: return getWrappedRepresentation().getStream();
160: }
161:
162: /**
163: * Returns the tag.
164: *
165: * @return The tag.
166: */
167: public Tag getTag() {
168: return getWrappedRepresentation().getTag();
169: }
170:
171: /**
172: * Converts the representation to a string value. Be careful when using this
173: * method as the conversion of large content to a string fully stored in
174: * memory can result in OutOfMemoryErrors being thrown.
175: *
176: * @return The representation as a string value.
177: */
178: public String getText() throws IOException {
179: return getWrappedRepresentation().getText();
180: }
181:
182: /**
183: * Returns the wrapped representation.
184: *
185: * @return The wrapped representation.
186: */
187: public Representation getWrappedRepresentation() {
188: return this .wrappedRepresentation;
189: }
190:
191: /**
192: * Indicates if some fresh content is available, without having to actually
193: * call one of the content manipulation method like getStream() that would
194: * actually consume it. This is especially useful for transient
195: * representation whose content can only be accessed once.
196: *
197: * @return True if some fresh content is available.
198: */
199: public boolean isAvailable() {
200: return getWrappedRepresentation().isAvailable();
201: }
202:
203: /**
204: * Indicates if the representation's content is transient, which means that
205: * it can be obtained only once. This is often the case with representations
206: * transmitted via network sockets for example. In such case, if you need to
207: * read the content several times, you need to cache it first, for example
208: * into memory or into a file.
209: *
210: * @return True if the representation's content is transient.
211: */
212: public boolean isTransient() {
213: return getWrappedRepresentation().isTransient();
214: }
215:
216: /**
217: * Indicates if some fresh content is available.
218: *
219: * @param isAvailable
220: * True if some fresh content is available.
221: */
222: public void setAvailable(boolean isAvailable) {
223: getWrappedRepresentation().setAvailable(isAvailable);
224: }
225:
226: /**
227: * Sets the character set or null if not applicable.
228: *
229: * @param characterSet
230: * The character set or null if not applicable.
231: */
232: public void setCharacterSet(CharacterSet characterSet) {
233: getWrappedRepresentation().setCharacterSet(characterSet);
234: }
235:
236: /**
237: * Sets the future date when this representation expire. If this information
238: * is not known, pass null.
239: *
240: * @param expirationDate
241: * The expiration date.
242: */
243: public void setExpirationDate(Date expirationDate) {
244: getWrappedRepresentation().setExpirationDate(expirationDate);
245: }
246:
247: /**
248: * Sets the official identifier.
249: *
250: * @param identifier
251: * The official identifier.
252: */
253: public void setIdentifier(Reference identifier) {
254: getWrappedRepresentation().setIdentifier(identifier);
255: }
256:
257: /**
258: * Sets the official identifier from a URI string.
259: *
260: * @param identifierUri
261: * The official identifier to parse.
262: */
263: public void setIdentifier(String identifierUri) {
264: getWrappedRepresentation().setIdentifier(identifierUri);
265: }
266:
267: /**
268: * Sets the media type.
269: *
270: * @param mediaType
271: * The media type.
272: */
273: public void setMediaType(MediaType mediaType) {
274: getWrappedRepresentation().setMediaType(mediaType);
275: }
276:
277: /**
278: * Sets the last date when this representation was modified. If this
279: * information is not known, pass null.
280: *
281: * @param modificationDate
282: * The modification date.
283: */
284: public void setModificationDate(Date modificationDate) {
285: getWrappedRepresentation()
286: .setModificationDate(modificationDate);
287: }
288:
289: /**
290: * Sets the expected size in bytes if known, -1 otherwise.
291: *
292: * @param expectedSize
293: * The expected size in bytes if known, -1 otherwise.
294: */
295: public void setSize(long expectedSize) {
296: getWrappedRepresentation().setSize(expectedSize);
297: }
298:
299: /**
300: * Sets the tag.
301: *
302: * @param tag
303: * The tag.
304: */
305: public void setTag(Tag tag) {
306: getWrappedRepresentation().setTag(tag);
307: }
308:
309: /**
310: * Indicates if the representation's content is transient.
311: *
312: * @param isTransient
313: * True if the representation's content is transient.
314: */
315: public void setTransient(boolean isTransient) {
316: getWrappedRepresentation().setTransient(isTransient);
317: }
318:
319: /**
320: * Writes the representation to a byte stream. This method is ensured to
321: * write the full content for each invocation unless it is a transient
322: * representation, in which case an exception is thrown.
323: *
324: * @param outputStream
325: * The output stream.
326: * @throws IOException
327: */
328: public void write(OutputStream outputStream) throws IOException {
329: getWrappedRepresentation().write(outputStream);
330: }
331:
332: /**
333: * Writes the representation to a byte channel. This method is ensured to
334: * write the full content for each invocation unless it is a transient
335: * representation, in which case an exception is thrown.
336: *
337: * @param writableChannel
338: * A writable byte channel.
339: * @throws IOException
340: */
341: public void write(WritableByteChannel writableChannel)
342: throws IOException {
343: getWrappedRepresentation().write(writableChannel);
344: }
345: }
|