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.resource;
020:
021: import java.io.IOException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.io.OutputStream;
025: import java.io.Serializable;
026:
027: import org.restlet.data.MediaType;
028:
029: /**
030: * Representation based on a serializable Java object.
031: *
032: * @author Jerome Louvel (contact@noelios.com)
033: */
034: public class ObjectRepresentation extends OutputRepresentation {
035: /** The serializable object. */
036: private Object object;
037:
038: /**
039: * Constructor reading the object from a serialized representation. This
040: * representation must have the proper media type:
041: * "application/x-java-serialized-object".
042: *
043: * @param serializedRepresentation
044: * The serialized representation.
045: * @throws IOException
046: * @throws ClassNotFoundException
047: * @throws IllegalArgumentException
048: */
049: public ObjectRepresentation(Representation serializedRepresentation)
050: throws IOException, ClassNotFoundException,
051: IllegalArgumentException {
052: super (MediaType.APPLICATION_JAVA_OBJECT);
053: if (serializedRepresentation.getMediaType().equals(
054: MediaType.APPLICATION_JAVA_OBJECT)) {
055: ObjectInputStream ois = new ObjectInputStream(
056: serializedRepresentation.getStream());
057: this .object = ois.readObject();
058: ois.close();
059: } else {
060: throw new IllegalArgumentException(
061: "The serialized representation must have this media type: "
062: + MediaType.APPLICATION_JAVA_OBJECT
063: .toString());
064: }
065: }
066:
067: /**
068: * Constructor
069: *
070: * @param object
071: * The serializable object.
072: */
073: public ObjectRepresentation(Serializable object) {
074: super (MediaType.APPLICATION_JAVA_OBJECT);
075: this .object = object;
076: }
077:
078: /**
079: * Returns the represented object.
080: *
081: * @return The represented object.
082: * @throws IOException
083: */
084: public Object getObject() throws IOException {
085: return this .object;
086: }
087:
088: /**
089: * Writes the datum as a stream of bytes.
090: *
091: * @param outputStream
092: * The stream to use when writing.
093: */
094: public void write(OutputStream outputStream) throws IOException {
095: ObjectOutputStream oos = new ObjectOutputStream(outputStream);
096: oos.writeObject(getObject());
097: oos.close();
098: }
099:
100: }
|