001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: HttpSerialized.java,v 1.2 2006-06-15 13:44:07 sinisa Exp $
022: */
023:
024: package com.lutris.appserver.server.httpPresentation;
025:
026: import java.io.ByteArrayOutputStream;
027: import java.io.IOException;
028: import java.io.InvalidClassException;
029: import java.io.NotSerializableException;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectOutputStream;
032: import java.io.OptionalDataException;
033: import java.io.Serializable;
034: import java.io.StreamCorruptedException;
035:
036: /**
037: * This class provides two utility methods for presentation objects
038: * that want to read and/or write Java serialized objects. <P>
039: *
040: * Reading is accomplished by having the client issue a POST request,
041: * with the serialized data as the body of the request. <P>
042: *
043: * Writing is accomplished by simply sending out the serialized bytes.
044: *
045: * @see java.io.Serializable
046: */
047: public class HttpSerialized {
048:
049: /**
050: * The mime type used to send and recieve Java serialized objects.
051: */
052: public static final String serializedMimeType = "application/java-serialized";
053:
054: /**
055: * Read in a Java object from a POST request. The object is returned.
056: *
057: * @param request
058: * HTTP communication request.
059: * @return
060: * The java object sent in the POST request.
061: * @exception HttpPresentationException
062: * If the request is not of type POST, or the content type is not
063: * correct, or there is an IO or serialization error.
064: */
065: public static Object readSerializedObject(
066: HttpPresentationRequest request)
067: throws HttpPresentationException {
068: /*
069: * Is this a POST request?
070: */
071: String method = request.getMethod();
072: if ((method == null) || !method.equalsIgnoreCase("POST"))
073: throw new HttpPresentationException(
074: "Request method is not POST.");
075: /*
076: * Is the content type correct?
077: */
078: String type = request.getContentType();
079: if ((type == null)
080: || !type.equalsIgnoreCase(serializedMimeType))
081: throw new HttpPresentationException(
082: "POSTed data is not of type " + serializedMimeType
083: + ".");
084: /*
085: * Try to read in the object.
086: */
087: Object result = null;
088: try {
089: HttpPresentationInputStream hpis = request.getInputStream();
090: ObjectInputStream ois = new ObjectInputStream(hpis);
091: result = ois.readObject();
092: } catch (InvalidClassException e) {
093: throw new HttpPresentationException("Invalid class sent: "
094: + e.getMessage(), e);
095: } catch (StreamCorruptedException e) {
096: throw new HttpPresentationException("Invalid data sent: "
097: + e.getMessage(), e);
098: } catch (OptionalDataException e) {
099: throw new HttpPresentationException("Invalid data sent: "
100: + e.getMessage(), e);
101: } catch (ClassNotFoundException e) {
102: throw new HttpPresentationException("Invalid class sent: "
103: + e.getMessage(), e);
104: } catch (IOException e) {
105: throw new HttpPresentationException(
106: "IO error reading object: " + e.getMessage(), e);
107: }
108: /*
109: * Success!
110: */
111: return result;
112: }
113:
114: /**
115: * Read in a Java object from a POST request. The object is returned.
116: *
117: * @param comms
118: * HTTP communications object. Contains objects and interfaces to read
119: * the request and send a response.
120: * @return
121: * The java object sent in the POST request.
122: * @exception HttpPresentationException
123: * If the request is not of type POST, or the content type is not
124: * correct, or there is an IO or serialization error.
125: */
126: public static Object readSerializedObject(
127: HttpPresentationComms comms)
128: throws HttpPresentationException {
129: return readSerializedObject(comms.request);
130: }
131:
132: /**
133: * Return a serialized Java object to the client.
134: *
135: * @param comms
136: * HTTP Response object. interface to send a response.
137: * @param object
138: * The object to return to the client.
139: * @exception HttpPresentationException
140: * If there is an error serializing the object, or an error in the
141: * underlying calls to response.
142: */
143: public static void writeSerializedObject(
144: HttpPresentationResponse response, Serializable object)
145: throws HttpPresentationException {
146: /*
147: * First try to serialize the object, so we can catch any exceptions
148: * up front, and so we know the length.
149: */
150: byte[] objectData = null;
151: try {
152: ByteArrayOutputStream baos = new ByteArrayOutputStream();
153: ObjectOutputStream oos = new ObjectOutputStream(baos);
154: oos.writeObject(object);
155: objectData = baos.toByteArray();
156: } catch (InvalidClassException e) {
157: throw new HttpPresentationException("Invalid class: "
158: + e.getMessage(), e);
159: } catch (NotSerializableException e) {
160: throw new HttpPresentationException("Invalid class: "
161: + e.getMessage(), e);
162: } catch (IOException e) {
163: throw new HttpPresentationException(
164: "Error serializing object: " + e.getMessage(), e);
165: }
166: if (objectData == null)
167: throw new HttpPresentationException(
168: "Error serializing object: no data generated.");
169: /*
170: * Set up the response.
171: */
172: response.setContentType(serializedMimeType);
173: response.setContentLength(objectData.length);
174: /*
175: * Transmit the object data.
176: */
177: HttpPresentationOutputStream hpos = response.getOutputStream();
178: try {
179: hpos.write(objectData);
180: } catch (IOException e) {
181: throw new HttpPresentationException(
182: "Error sending object: " + e, e);
183: }
184: /*
185: * Success!
186: */
187: }
188:
189: /**
190: * Return a serialized Java object to the client.
191: *
192: * @param comms
193: * HTTP communications object. Contains objects and interfaces to read
194: * the request and send a response.
195: * @param object
196: * The object to return to the client.
197: * @exception HttpPresentationException
198: * If there is an error serializing the object, or an error in the
199: * underlying calls to response.
200: */
201: public static void writeSerializedObject(
202: HttpPresentationComms comms, Serializable object)
203: throws HttpPresentationException {
204: writeSerializedObject(comms.response, object);
205: }
206:
207: }
|