1 16 17 package org.springframework.remoting.httpinvoker; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.ObjectInputStream ; 22 import java.io.ObjectOutputStream ; 23 import java.io.OutputStream ; 24 import java.rmi.RemoteException ; 25 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.springframework.beans.factory.InitializingBean; 31 import org.springframework.remoting.rmi.CodebaseAwareObjectInputStream; 32 import org.springframework.remoting.support.RemoteInvocation; 33 import org.springframework.remoting.support.RemoteInvocationBasedExporter; 34 import org.springframework.remoting.support.RemoteInvocationResult; 35 import org.springframework.util.Assert; 36 import org.springframework.web.HttpRequestHandler; 37 import org.springframework.web.util.NestedServletException; 38 39 60 public class HttpInvokerServiceExporter extends RemoteInvocationBasedExporter 61 implements HttpRequestHandler, InitializingBean { 62 63 66 public static final String CONTENT_TYPE_SERIALIZED_OBJECT = "application/x-java-serialized-object"; 67 68 69 private String contentType = CONTENT_TYPE_SERIALIZED_OBJECT; 70 71 private Object proxy; 72 73 74 78 public void setContentType(String contentType) { 79 Assert.notNull(contentType, "'contentType' must not be null"); 80 this.contentType = contentType; 81 } 82 83 86 public String getContentType() { 87 return this.contentType; 88 } 89 90 91 public void afterPropertiesSet() { 92 prepare(); 93 } 94 95 98 public void prepare() { 99 this.proxy = getProxyForService(); 100 } 101 102 103 110 public void handleRequest(HttpServletRequest request, HttpServletResponse response) 111 throws ServletException , IOException { 112 113 Assert.notNull(this.proxy, "HttpInvokerServiceExporter has not been initialized"); 114 115 try { 116 RemoteInvocation invocation = readRemoteInvocation(request); 117 RemoteInvocationResult result = invokeAndCreateResult(invocation, this.proxy); 118 writeRemoteInvocationResult(request, response, result); 119 } 120 catch (ClassNotFoundException ex) { 121 throw new NestedServletException("Class not found during deserialization", ex); 122 } 123 } 124 125 126 137 protected RemoteInvocation readRemoteInvocation(HttpServletRequest request) 138 throws IOException , ClassNotFoundException { 139 140 return readRemoteInvocation(request, request.getInputStream()); 141 } 142 143 156 protected RemoteInvocation readRemoteInvocation(HttpServletRequest request, InputStream is) 157 throws IOException , ClassNotFoundException { 158 159 ObjectInputStream ois = createObjectInputStream(decorateInputStream(request, is)); 160 try { 161 return doReadRemoteInvocation(ois); 162 } 163 finally { 164 ois.close(); 165 } 166 } 167 168 178 protected InputStream decorateInputStream(HttpServletRequest request, InputStream is) throws IOException { 179 return is; 180 } 181 182 193 protected ObjectInputStream createObjectInputStream(InputStream is) throws IOException { 194 return new CodebaseAwareObjectInputStream(is, null); 195 } 196 197 210 protected RemoteInvocation doReadRemoteInvocation(ObjectInputStream ois) 211 throws IOException , ClassNotFoundException { 212 213 Object obj = ois.readObject(); 214 if (!(obj instanceof RemoteInvocation)) { 215 throw new RemoteException ("Deserialized object needs to be assignable to type [" + 216 RemoteInvocation.class.getName() + "]: " + obj); 217 } 218 return (RemoteInvocation) obj; 219 } 220 221 222 229 protected void writeRemoteInvocationResult( 230 HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result) 231 throws IOException { 232 233 response.setContentType(getContentType()); 234 writeRemoteInvocationResult(request, response, result, response.getOutputStream()); 235 } 236 237 252 protected void writeRemoteInvocationResult( 253 HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result, OutputStream os) 254 throws IOException { 255 256 ObjectOutputStream oos = createObjectOutputStream(decorateOutputStream(request, response, os)); 257 try { 258 doWriteRemoteInvocationResult(result, oos); 259 oos.flush(); 260 } 261 finally { 262 oos.close(); 263 } 264 } 265 266 277 protected OutputStream decorateOutputStream( 278 HttpServletRequest request, HttpServletResponse response, OutputStream os) throws IOException { 279 280 return os; 281 } 282 283 291 protected ObjectOutputStream createObjectOutputStream(OutputStream os) throws IOException { 292 return new ObjectOutputStream (os); 293 } 294 295 306 protected void doWriteRemoteInvocationResult(RemoteInvocationResult result, ObjectOutputStream oos) 307 throws IOException { 308 309 oos.writeObject(result); 310 } 311 312 } 313 | Popular Tags |