Source Code Cross Referenced for HttpSerialized.java in  » J2EE » Enhydra-Application-Framework » com » lutris » appserver » server » httpPresentation » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Enhydra Application Framework » com.lutris.appserver.server.httpPresentation 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.