KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > webui > servlets > ServiceInvokerServlet


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  */

16 package org.jmanage.webui.servlets;
17
18 import org.jmanage.core.remote.RemoteInvocation;
19 import org.jmanage.core.remote.InvocationResult;
20 import org.jmanage.core.remote.server.ServiceCallHandler;
21
22 import javax.servlet.http.HttpServlet JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.ServletInputStream JavaDoc;
27 import javax.servlet.ServletOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.io.ObjectOutputStream JavaDoc;
31
32 /**
33  *
34  * date: Feb 24, 2005
35  * @author Rakesh Kalra
36  */

37 public class ServiceInvokerServlet extends HttpServlet JavaDoc {
38
39     private static String JavaDoc RESPONSE_CONTENT_TYPE =
40        "application/x-java-serialized-object; class=org.jmanage.core.remote.InvocationResult";
41
42     /**
43      * Handles the HTTP <code>GET</code> method.
44      */

45     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
46             throws ServletException JavaDoc, IOException JavaDoc {
47         processRequest(request, response);
48     }
49
50     /**
51      * Handles the HTTP <code>POST</code> method.
52      */

53     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
54             throws ServletException JavaDoc, IOException JavaDoc {
55         processRequest(request, response);
56     }
57
58     public String JavaDoc getServletInfo() {
59         return "Servlet to remotely invoke service methods";
60     }
61
62
63     protected void processRequest(HttpServletRequest JavaDoc request,
64                                   HttpServletResponse JavaDoc response)
65             throws ServletException JavaDoc, IOException JavaDoc {
66
67         response.setContentType(RESPONSE_CONTENT_TYPE);
68         try {
69             /* get the RemoteInvocation object */
70             ServletInputStream JavaDoc sis = request.getInputStream();
71             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(sis);
72             RemoteInvocation invocation = (RemoteInvocation) ois.readObject();
73             ois.close();
74             /* execute the method */
75             InvocationResult result = ServiceCallHandler.execute(invocation);
76             /* write the result */
77             ServletOutputStream JavaDoc sos = response.getOutputStream();
78             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(sos);
79             oos.writeObject(result);
80             oos.close();
81         } catch (Throwable JavaDoc t) {
82             /* write the exception */
83             ServletOutputStream JavaDoc sos = response.getOutputStream();
84             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(sos);
85             oos.writeObject(new InvocationResult(t));
86             oos.close();
87         }
88     }
89 }
90
Popular Tags