01: /**
02: * Copyright 2004-2005 jManage.org
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */package org.jmanage.webui.servlets;
16:
17: import org.jmanage.core.remote.RemoteInvocation;
18: import org.jmanage.core.remote.InvocationResult;
19: import org.jmanage.core.remote.server.ServiceCallHandler;
20:
21: import javax.servlet.http.HttpServlet;
22: import javax.servlet.http.HttpServletRequest;
23: import javax.servlet.http.HttpServletResponse;
24: import javax.servlet.ServletException;
25: import javax.servlet.ServletInputStream;
26: import javax.servlet.ServletOutputStream;
27: import java.io.IOException;
28: import java.io.ObjectInputStream;
29: import java.io.ObjectOutputStream;
30:
31: /**
32: *
33: * date: Feb 24, 2005
34: * @author Rakesh Kalra
35: */
36: public class ServiceInvokerServlet extends HttpServlet {
37:
38: private static String RESPONSE_CONTENT_TYPE = "application/x-java-serialized-object; class=org.jmanage.core.remote.InvocationResult";
39:
40: /**
41: * Handles the HTTP <code>GET</code> method.
42: */
43: protected void doGet(HttpServletRequest request,
44: HttpServletResponse response) throws ServletException,
45: IOException {
46: processRequest(request, response);
47: }
48:
49: /**
50: * Handles the HTTP <code>POST</code> method.
51: */
52: protected void doPost(HttpServletRequest request,
53: HttpServletResponse response) throws ServletException,
54: IOException {
55: processRequest(request, response);
56: }
57:
58: public String getServletInfo() {
59: return "Servlet to remotely invoke service methods";
60: }
61:
62: protected void processRequest(HttpServletRequest request,
63: HttpServletResponse response) throws ServletException,
64: IOException {
65:
66: response.setContentType(RESPONSE_CONTENT_TYPE);
67: try {
68: /* get the RemoteInvocation object */
69: ServletInputStream sis = request.getInputStream();
70: ObjectInputStream ois = new ObjectInputStream(sis);
71: RemoteInvocation invocation = (RemoteInvocation) ois
72: .readObject();
73: ois.close();
74: /* execute the method */
75: InvocationResult result = ServiceCallHandler
76: .execute(invocation);
77: /* write the result */
78: ServletOutputStream sos = response.getOutputStream();
79: ObjectOutputStream oos = new ObjectOutputStream(sos);
80: oos.writeObject(result);
81: oos.close();
82: } catch (Throwable t) {
83: /* write the exception */
84: ServletOutputStream sos = response.getOutputStream();
85: ObjectOutputStream oos = new ObjectOutputStream(sos);
86: oos.writeObject(new InvocationResult(t));
87: oos.close();
88: }
89: }
90: }
|