KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > httpPresentation > HttpSerialized


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: HttpSerialized.java,v 1.2 2005/03/24 10:51:16 slobodan Exp $
23  */

24
25
26
27
28 package com.lutris.appserver.server.httpPresentation;
29
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InvalidClassException JavaDoc;
33 import java.io.NotSerializableException JavaDoc;
34 import java.io.ObjectInputStream JavaDoc;
35 import java.io.ObjectOutputStream JavaDoc;
36 import java.io.OptionalDataException JavaDoc;
37 import java.io.Serializable JavaDoc;
38 import java.io.StreamCorruptedException JavaDoc;
39
40 /**
41  * This class provides two utility methods for presentation objects
42  * that want to read and/or write Java serialized objects. <P>
43  *
44  * Reading is accomplished by having the client issue a POST request,
45  * with the serialized data as the body of the request. <P>
46  *
47  * Writing is accomplished by simply sending out the serialized bytes.
48  *
49  * @see java.io.Serializable
50  */

51 public class HttpSerialized {
52
53     /**
54      * The mime type used to send and recieve Java serialized objects.
55      */

56     public static final String JavaDoc serializedMimeType =
57                                              "application/java-serialized";
58
59     /**
60      * Read in a Java object from a POST request. The object is returned.
61      *
62      * @param request
63      * HTTP communication request.
64      * @return
65      * The java object sent in the POST request.
66      * @exception HttpPresentationException
67      * If the request is not of type POST, or the content type is not
68      * correct, or there is an IO or serialization error.
69      */

70     public static Object JavaDoc readSerializedObject(
71                              HttpPresentationRequest request)
72                              throws HttpPresentationException {
73         /*
74          * Is this a POST request?
75          */

76         String JavaDoc method = request.getMethod();
77         if ((method == null) ||
78             !method.equalsIgnoreCase("POST"))
79             throw new HttpPresentationException("Request method is not POST.");
80         /*
81          * Is the content type correct?
82          */

83         String JavaDoc type = request.getContentType();
84         if ((type == null) ||
85            !type.equalsIgnoreCase(serializedMimeType))
86             throw new HttpPresentationException("POSTed data is not of type " +
87                                                 serializedMimeType + ".");
88         /*
89          * Try to read in the object.
90          */

91         Object JavaDoc result = null;
92         try {
93             HttpPresentationInputStream hpis = request.getInputStream();
94             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(hpis);
95             result = ois.readObject();
96         } catch (InvalidClassException JavaDoc e) {
97             throw new HttpPresentationException(
98                           "Invalid class sent: " + e.getMessage(), e);
99         } catch (StreamCorruptedException JavaDoc e) {
100             throw new HttpPresentationException(
101                           "Invalid data sent: " + e.getMessage(), e);
102         } catch (OptionalDataException JavaDoc e) {
103             throw new HttpPresentationException(
104                           "Invalid data sent: " + e.getMessage(), e);
105         } catch (ClassNotFoundException JavaDoc e) {
106             throw new HttpPresentationException(
107                           "Invalid class sent: " + e.getMessage(), e);
108         } catch (IOException JavaDoc e) {
109             throw new HttpPresentationException(
110                           "IO error reading object: " + e.getMessage(), e);
111         }
112         /*
113          * Success!
114          */

115         return result;
116     }
117
118     /**
119      * Read in a Java object from a POST request. The object is returned.
120      *
121      * @param comms
122      * HTTP communications object. Contains objects and interfaces to read
123      * the request and send a response.
124      * @return
125      * The java object sent in the POST request.
126      * @exception HttpPresentationException
127      * If the request is not of type POST, or the content type is not
128      * correct, or there is an IO or serialization error.
129      */

130     public static Object JavaDoc readSerializedObject(
131                              HttpPresentationComms comms)
132                              throws HttpPresentationException {
133         return readSerializedObject(comms.request);
134     }
135
136     /**
137      * Return a serialized Java object to the client.
138      *
139      * @param comms
140      * HTTP Response object. interface to send a response.
141      * @param object
142      * The object to return to the client.
143      * @exception HttpPresentationException
144      * If there is an error serializing the object, or an error in the
145      * underlying calls to response.
146      */

147     public static void writeSerializedObject(
148                            HttpPresentationResponse response,
149                            Serializable JavaDoc object)
150                            throws HttpPresentationException {
151         /*
152          * First try to serialize the object, so we can catch any exceptions
153          * up front, and so we know the length.
154          */

155         byte[] objectData = null;
156         try {
157             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
158             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
159             oos.writeObject(object);
160             objectData = baos.toByteArray();
161         } catch (InvalidClassException JavaDoc e) {
162             throw new HttpPresentationException(
163                           "Invalid class: " + e.getMessage(), e);
164         } catch (NotSerializableException JavaDoc e) {
165             throw new HttpPresentationException(
166                           "Invalid class: " + e.getMessage(), e);
167         } catch (IOException JavaDoc e) {
168             throw new HttpPresentationException(
169                           "Error serializing object: " + e.getMessage(), e);
170         }
171         if (objectData == null)
172             throw new HttpPresentationException(
173                          "Error serializing object: no data generated.");
174         /*
175          * Set up the response.
176          */

177         response.setContentType(serializedMimeType);
178         response.setContentLength(objectData.length);
179         /*
180          * Transmit the object data.
181          */

182         HttpPresentationOutputStream hpos = response.getOutputStream();
183         try {
184             hpos.write(objectData);
185         } catch (IOException JavaDoc e) {
186             throw new HttpPresentationException(
187                          "Error sending object: " + e, e);
188         }
189         /*
190          * Success!
191          */

192     }
193
194     /**
195      * Return a serialized Java object to the client.
196      *
197      * @param comms
198      * HTTP communications object. Contains objects and interfaces to read
199      * the request and send a response.
200      * @param object
201      * The object to return to the client.
202      * @exception HttpPresentationException
203      * If there is an error serializing the object, or an error in the
204      * underlying calls to response.
205      */

206     public static void writeSerializedObject(
207                            HttpPresentationComms comms,
208                            Serializable JavaDoc object)
209                            throws HttpPresentationException {
210         writeSerializedObject(comms.response,object);
211     }
212     
213 }
214
215
Popular Tags