Rev 2027 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2027 | es | 1 | package pt.estgp.es.exemplos.hibernate.web.rest; |
2 | |||
2028 | es | 3 | import org.json.JSONException; |
4 | import org.json.JSONObject; |
||
5 | import pt.estgp.es.exemplos.hibernate.utils.StreamsUtils; |
||
6 | |||
2027 | es | 7 | import javax.servlet.ServletException; |
8 | import javax.servlet.http.HttpServlet; |
||
9 | import javax.servlet.http.HttpServletRequest; |
||
10 | import javax.servlet.http.HttpServletResponse; |
||
11 | import java.io.IOException; |
||
2028 | es | 12 | import java.io.InputStream; |
13 | import java.io.OutputStream; |
||
14 | import java.io.PrintWriter; |
||
15 | import java.lang.reflect.InvocationTargetException; |
||
16 | import java.lang.reflect.Method; |
||
2027 | es | 17 | |
2028 | es | 18 | public abstract class AbstractRestServlet extends HttpServlet |
2027 | es | 19 | { |
20 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
21 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
22 | process(req,resp); |
||
23 | } |
||
24 | |||
25 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
26 | protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
27 | process(req,resp); |
||
28 | } |
||
29 | |||
30 | protected void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
31 | { |
||
2028 | es | 32 | JSONObject requestObj; |
33 | |||
34 | if(req.getContentType().equalsIgnoreCase("application/json")) |
||
2027 | es | 35 | { |
2028 | es | 36 | 5+0%2Fdocs%2Fapi+InputStream">InputStream content = req.getInputStream(); |
37 | if(content != null) |
||
38 | { |
||
39 | 1.5.0/docs/api/java/lang/String.html">String json = StreamsUtils.readString(content); |
||
40 | try { |
||
41 | requestObj = new JSONObject(json); |
||
42 | 1.5.0/docs/api/java/lang/System.html">System.out.println("REQUEST JSON:"); |
||
43 | 1.5.0/docs/api/java/lang/System.html">System.out.println(requestObj.toString()); |
||
2027 | es | 44 | |
2028 | es | 45 | 1.5.0/docs/api/java/lang/String.html">String service = requestObj.getString("service"); |
46 | 1.5.0/docs/api/java/lang/String.html">String data = requestObj.has("data") ? requestObj.getString("data") : null; |
||
47 | |||
48 | 1.5.0/docs/api/java/lang/reflect/Method.html">Method innerMethod = this.getClass().getMethod(service,new 1.5.0/docs/api/java/lang/Class.html">Class[]{ |
||
49 | JSONObject.class, |
||
50 | HttpServletRequest.class, |
||
51 | HttpServletResponse.class}); |
||
52 | JSONObject obj = (JSONObject) innerMethod.invoke(this,new 5+0%2Fdocs%2Fapi+Object">Object[]{data,req,resp}); |
||
53 | |||
54 | JSONObject response = new JSONObject(); |
||
55 | response.put("service","ok"); |
||
56 | response.put("response",obj); |
||
57 | |||
58 | resp.setContentType("application/json"); |
||
59 | 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter pw = resp.getWriter(); |
||
60 | pw.write(obj.toString()); |
||
61 | |||
62 | } catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) { |
||
63 | e.printStackTrace(); |
||
64 | JSONObject response = new JSONObject(); |
||
65 | try { |
||
66 | response.put("service","error"); |
||
67 | response.put("cause",e.toString()); |
||
68 | resp.setContentType("application/json"); |
||
69 | 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter pw = resp.getWriter(); |
||
70 | pw.write(response.toString()); |
||
71 | } catch (JSONException e1) { |
||
72 | e1.printStackTrace(); |
||
73 | } |
||
74 | |||
75 | |||
76 | } |
||
77 | } |
||
2027 | es | 78 | } |
79 | |||
2028 | es | 80 | |
2027 | es | 81 | } |
82 | } |