1 2 24 25 26 27 28 package com.lutris.appserver.server.httpPresentation; 29 30 import java.io.ByteArrayOutputStream ; 31 import java.io.IOException ; 32 import java.io.InvalidClassException ; 33 import java.io.NotSerializableException ; 34 import java.io.ObjectInputStream ; 35 import java.io.ObjectOutputStream ; 36 import java.io.OptionalDataException ; 37 import java.io.Serializable ; 38 import java.io.StreamCorruptedException ; 39 40 51 public class HttpSerialized { 52 53 56 public static final String serializedMimeType = 57 "application/java-serialized"; 58 59 70 public static Object readSerializedObject( 71 HttpPresentationRequest request) 72 throws HttpPresentationException { 73 76 String method = request.getMethod(); 77 if ((method == null) || 78 !method.equalsIgnoreCase("POST")) 79 throw new HttpPresentationException("Request method is not POST."); 80 83 String type = request.getContentType(); 84 if ((type == null) || 85 !type.equalsIgnoreCase(serializedMimeType)) 86 throw new HttpPresentationException("POSTed data is not of type " + 87 serializedMimeType + "."); 88 91 Object result = null; 92 try { 93 HttpPresentationInputStream hpis = request.getInputStream(); 94 ObjectInputStream ois = new ObjectInputStream (hpis); 95 result = ois.readObject(); 96 } catch (InvalidClassException e) { 97 throw new HttpPresentationException( 98 "Invalid class sent: " + e.getMessage(), e); 99 } catch (StreamCorruptedException e) { 100 throw new HttpPresentationException( 101 "Invalid data sent: " + e.getMessage(), e); 102 } catch (OptionalDataException e) { 103 throw new HttpPresentationException( 104 "Invalid data sent: " + e.getMessage(), e); 105 } catch (ClassNotFoundException e) { 106 throw new HttpPresentationException( 107 "Invalid class sent: " + e.getMessage(), e); 108 } catch (IOException e) { 109 throw new HttpPresentationException( 110 "IO error reading object: " + e.getMessage(), e); 111 } 112 115 return result; 116 } 117 118 130 public static Object readSerializedObject( 131 HttpPresentationComms comms) 132 throws HttpPresentationException { 133 return readSerializedObject(comms.request); 134 } 135 136 147 public static void writeSerializedObject( 148 HttpPresentationResponse response, 149 Serializable object) 150 throws HttpPresentationException { 151 155 byte[] objectData = null; 156 try { 157 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 158 ObjectOutputStream oos = new ObjectOutputStream (baos); 159 oos.writeObject(object); 160 objectData = baos.toByteArray(); 161 } catch (InvalidClassException e) { 162 throw new HttpPresentationException( 163 "Invalid class: " + e.getMessage(), e); 164 } catch (NotSerializableException e) { 165 throw new HttpPresentationException( 166 "Invalid class: " + e.getMessage(), e); 167 } catch (IOException 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 177 response.setContentType(serializedMimeType); 178 response.setContentLength(objectData.length); 179 182 HttpPresentationOutputStream hpos = response.getOutputStream(); 183 try { 184 hpos.write(objectData); 185 } catch (IOException e) { 186 throw new HttpPresentationException( 187 "Error sending object: " + e, e); 188 } 189 192 } 193 194 206 public static void writeSerializedObject( 207 HttpPresentationComms comms, 208 Serializable object) 209 throws HttpPresentationException { 210 writeSerializedObject(comms.response,object); 211 } 212 213 } 214 215 | Popular Tags |