KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > transport > servlet > ServletServerInvoker


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.transport.servlet;
8
9 import java.io.IOException;
10 import java.util.Enumeration;
11 import java.util.HashMap;
12 import java.util.Map;
13 import javax.servlet.ServletException;
14 import javax.servlet.ServletInputStream;
15 import javax.servlet.ServletOutputStream;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18 import org.jboss.remoting.InvocationRequest;
19 import org.jboss.remoting.InvokerLocator;
20 import org.jboss.remoting.marshal.MarshalFactory;
21 import org.jboss.remoting.marshal.Marshaller;
22 import org.jboss.remoting.marshal.UnMarshaller;
23 import org.jboss.remoting.marshal.http.HTTPMarshaller;
24 import org.jboss.remoting.marshal.http.HTTPUnMarshaller;
25 import org.jboss.remoting.transport.web.WebServerInvoker;
26
27 /**
28  * The servlet based server invoker that receives the original http request
29  * from the ServerInvokerServlet.
30  *
31  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
32  */

33 public class ServletServerInvoker extends WebServerInvoker implements ServletServerInvokerMBean
34 {
35    public ServletServerInvoker(InvokerLocator locator)
36    {
37       super(locator);
38    }
39
40    public ServletServerInvoker(InvokerLocator locator, Map configuration)
41    {
42       super(locator, configuration);
43    }
44
45    protected String getDefaultDataType()
46    {
47       return HTTPMarshaller.DATATYPE;
48    }
49
50    public String getMBeanObjectName()
51    {
52       return "jboss.remoting:service=invoker,transport=servlet";
53    }
54
55    public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
56    {
57       Map metadata = new HashMap();
58
59       Enumeration enum = request.getHeaderNames();
60       while(enum.hasMoreElements())
61       {
62          Object obj = enum.nextElement();
63          String headerKey = (String) obj;
64          String headerValue = request.getHeader(headerKey);
65          metadata.put(headerKey, headerValue);
66       }
67
68       Map urlParams = request.getParameterMap();
69       metadata.putAll(urlParams);
70
71       String requestContentType = request.getContentType();
72
73
74       try
75       {
76          Object invocationResponse = null;
77
78          ServletInputStream inputStream = request.getInputStream();
79          UnMarshaller unmarshaller = MarshalFactory.getUnMarshaller(HTTPUnMarshaller.DATATYPE);
80          Object obj = unmarshaller.read(inputStream, metadata);
81          inputStream.close();
82
83          InvocationRequest invocationRequest = null;
84
85          if(obj instanceof InvocationRequest)
86          {
87             invocationRequest = (InvocationRequest) obj;
88          }
89          else
90          {
91             if(isBinary(requestContentType))
92             {
93                invocationRequest = getInvocationRequest(metadata, obj);
94             }
95             else
96             {
97                invocationRequest = createNewInvocationRequest(metadata, obj);
98             }
99          }
100
101          try
102          {
103             // call transport on the subclass, get the result to handback
104
invocationResponse = invoke(invocationRequest);
105          }
106          catch(Throwable ex)
107          {
108             log.debug("Error thrown calling invoke on server invoker.", ex);
109             invocationResponse = null;
110             response.sendError(500, "Error processing invocation request. " + ex.getMessage());
111          }
112
113          if(invocationResponse != null)
114          {
115             response.setContentType(requestContentType);
116             int iContentLength = getContentLength(invocationResponse);
117             response.setContentLength(iContentLength);
118             ServletOutputStream outputStream = response.getOutputStream();
119             Marshaller marshaller = MarshalFactory.getMarshaller(HTTPMarshaller.DATATYPE);
120             marshaller.write(invocationResponse, outputStream);
121             outputStream.close();
122          }
123
124       }
125       catch(ClassNotFoundException e)
126       {
127          log.error("Error processing invocation request due to class not being found.", e);
128          response.sendError(500, "Error processing invocation request due to class not being found. " + e.getMessage());
129
130       }
131
132    }
133 }
Popular Tags