1 package net.sf.crispy.impl.rest;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.lang.reflect.Method;
6 import java.util.Hashtable;
7 import java.util.Map;
8 import java.util.Vector;
9
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import net.sf.crispy.impl.http.Constant;
15 import net.sf.crispy.impl.http.HttpServlet;
16 import net.sf.crispy.util.Converter;
17 import net.sf.crispy.util.Invoker;
18
19 /**
20 *
21 *
22 * @author Linke
23 *
24 */
25 public class RestServlet extends HttpServlet implements Constant {
26
27 private static final long serialVersionUID = 89089242427878787L;
28
29 public RestServlet () {
30 super();
31 }
32
33
34 /**
35 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
36 */
37 protected void doPost(HttpServletRequest pvRequest, HttpServletResponse pvResponse) throws ServletException, IOException {
38
39 Object lvResult = null;
40 String lvServiceClassName = "NoServiceClass-available";
41 String lvMethodName = "NoMethod-available";
42
43 try {
44 Object lvService = getService(pvRequest, pvResponse);
45 String lvServiceAndMethodName[] = getServiceAndMethodName(pvRequest.getParameter(PARAM_CLASS), pvRequest.getParameter(PARAM_METHOD));
46 lvServiceClassName = lvServiceAndMethodName[0];
47 lvMethodName = lvServiceAndMethodName[1];
48
49 Method lvMethod = getMethod(lvService, pvRequest.getParameterMap(), pvRequest.getParameter(PARAM_CLASS), pvRequest.getParameter(PARAM_METHOD));
50 Object lvArgs[] = getArgs(pvRequest.getParameterMap(), lvMethod);
51 lvResult = doInvoke(lvService, lvMethod, lvArgs, createNewInterceptorHandlerInstance());
52 } catch (Exception e) {
53
54 lvResult = createResponseFail(lvServiceClassName, lvMethodName, e);
55 }
56
57 if (log.isDebugEnabled()) { log.debug("Result from method " + pvRequest.getParameter(PARAM_METHOD) + " is: " + lvResult); }
58 String output = makeXmlStream(lvResult);
59
60 PrintWriter pw = pvResponse.getWriter();
61 pw.print(output);
62 pw.flush();
63 pw.close();
64 }
65
66 /** Convert the result value to xml representation. */
67 public String makeXmlStream (Object pvResult) {
68 String xmlStr = null;
69 try {
70 xmlStr = Serializer.serialize(pvResult);
71 } catch (Exception e) {
72 ResponseFail lvFail = new ResponseFail ();
73 lvFail.setMessage(e.getMessage());
74 lvFail.setException(e.getClass().getName());
75 try {
76 xmlStr = Serializer.serialize(pvResult);
77 } catch (Exception e2) {
78 log.debug("Error by Serializer: " + pvResult, e);
79 }
80 e.printStackTrace();
81 }
82 if (log.isDebugEnabled()) { log.debug(xmlStr); }
83 return xmlStr;
84 }
85
86 protected Object[] getArgs(Map pvParameterMap, Method pvMethod) throws Exception {
87 Object lvParameters = new ParameterDeserializer().deserialize(prepareParameter(pvParameterMap));
88
89 Vector lvParamVector = null;
90 if (lvParameters instanceof Vector) {
91 lvParamVector = (Vector) lvParameters;
92 } else {
93 lvParamVector = new Vector ();
94 lvParamVector.add(lvParameters);
95 }
96 Object lvParams[] = Converter.convertStringParams2MethodParams(pvMethod, lvParamVector);
97 return lvParams;
98 }
99
100 public Method getMethod (Object pvService, Map pvParameterMap, String pvServiceClassName, String pvMethodName) throws Exception {
101
102 String lvServiceAndMethodName[] = getServiceAndMethodName(pvServiceClassName, pvMethodName);
103
104 String lvMethodName = lvServiceAndMethodName[1];
105
106 String lvParamTypes[] = (String[]) pvParameterMap.get(PARAM_TYPES);
107 Vector findParamVector = convertInParameterTypes(lvParamTypes);
108
109
110 Object lvParameters = new ParameterDeserializer().deserialize(prepareParameter(pvParameterMap));
111
112 Vector lvParamVector = null;
113 if (lvParameters instanceof Vector) {
114 lvParamVector = (Vector) lvParameters;
115 } else {
116 lvParamVector = new Vector ();
117 lvParamVector.add(lvParameters);
118 }
119
120 if (findParamVector == null) { findParamVector = lvParamVector; }
121
122
123
124 Method lvMethod = Invoker.findMethod (pvService.getClass(), lvMethodName, findParamVector);
125 return lvMethod;
126 }
127
128 private ResponseFail createResponseFail(String pvServiceClassName, String pvMethodName, Throwable pvThrowable) {
129 ResponseFail lvFail = new ResponseFail ();
130 lvFail.setService(pvServiceClassName);
131 lvFail.setMethod(pvMethodName);
132 lvFail.setMessage(pvThrowable.getMessage());
133 lvFail.setException(pvThrowable.getClass().getName());
134 return lvFail;
135 }
136
137 public Object call (Map pvParameterMap, String pvServiceClassName, String pvMethodName) {
138 String lvServiceClassName = "NoServiceClass-available";
139 String lvMethodName = "NoMethod-available";
140 try {
141 String lvServiceAndMethodName[] = getServiceAndMethodName(pvServiceClassName, pvMethodName);
142 lvServiceClassName = lvServiceAndMethodName[0];
143 lvMethodName = lvServiceAndMethodName[1];
144
145 Object lvService = getService(lvServiceClassName);
146
147 Method lvMethod = getMethod(lvService, pvParameterMap, pvServiceClassName, pvMethodName);
148 Object lvArgs[] = getArgs(pvParameterMap, lvMethod);
149
150 Object lvResult = doInvoke(lvService, lvMethod, lvArgs, createNewInterceptorHandlerInstance());
151 return lvResult;
152 } catch (Exception e) {
153
154 return createResponseFail(lvServiceClassName, lvMethodName, e);
155 }
156 }
157
158
159 protected Map prepareParameter(Map pvParameterMap) {
160
161 Map lvParameterMap = new Hashtable(pvParameterMap);
162 lvParameterMap.remove(Constant.PARAM_METHOD);
163 lvParameterMap.remove(Constant.PARAM_CLASS);
164 return lvParameterMap;
165 }
166
167
168
169 }