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.ext.spring;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: import org.restlet.resource.Representation;
025: import org.springframework.core.io.AbstractResource;
026:
027: /**
028: * Spring Resource based on a Restlet Representation. Do not get confused,
029: * Spring's notion of Resource is different from Restlet's one, actually it's
030: * closer to Restlet's Representations.
031: *
032: * @see <a href="http://www.springframework.org/">Spring home page</a>
033: * @author Jerome Louvel (contact@noelios.com)
034: */
035: public class SpringResource extends AbstractResource {
036: /** The wrapped representation. */
037: private final Representation representation;
038:
039: /** The description. */
040: private final String description;
041:
042: /** Indicates if the representation has already been read. */
043: private boolean read = false;
044:
045: /**
046: * Constructor.
047: *
048: * @param representation
049: * The description.
050: */
051: public SpringResource(Representation representation) {
052: this (representation, "Resource loaded through a Representation");
053: }
054:
055: /**
056: * Constructor.
057: *
058: * @param representation
059: * The description.
060: * @param description
061: * The description.
062: */
063: public SpringResource(Representation representation,
064: String description) {
065: if (representation == null) {
066: throw new IllegalArgumentException(
067: "Representation must not be null");
068: }
069:
070: this .representation = representation;
071: this .description = (description != null) ? description : "";
072: }
073:
074: /**
075: * This implementation always returns <code>true</code>.
076: */
077: @Override
078: public boolean exists() {
079: return true;
080: }
081:
082: /**
083: * This implementation always returns <code>true</code>.
084: */
085: @Override
086: public boolean isOpen() {
087: return true;
088: }
089:
090: /**
091: * This implementation throws IllegalStateException if attempting to read
092: * the underlying stream multiple times.
093: */
094: public InputStream getInputStream() throws IOException,
095: IllegalStateException {
096: if (this .read && this .representation.isTransient()) {
097: throw new IllegalStateException(
098: "Representation has already been read and is transient.");
099: }
100:
101: this .read = true;
102: return this .representation.getStream();
103: }
104:
105: /**
106: * Returns the description.
107: *
108: * @return The description.
109: */
110: @Override
111: public String getDescription() {
112: return this .description;
113: }
114:
115: /**
116: * This implementation compares the underlying InputStream.
117: */
118: @Override
119: public boolean equals(Object obj) {
120: return (obj == this || (obj instanceof SpringResource && ((SpringResource) obj).representation
121: .equals(this .representation)));
122: }
123:
124: /**
125: * This implementation returns the hash code of the underlying InputStream.
126: */
127: @Override
128: public int hashCode() {
129: return this.representation.hashCode();
130: }
131:
132: }
|