Rev 2028 | 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 | |||
2046 | es | 3 | import org.json.JSONArray; |
2028 | es | 4 | import org.json.JSONException; |
5 | import org.json.JSONObject; |
||
6 | import pt.estgp.es.exemplos.hibernate.utils.StreamsUtils; |
||
7 | |||
2027 | es | 8 | import javax.servlet.ServletException; |
9 | import javax.servlet.http.HttpServlet; |
||
10 | import javax.servlet.http.HttpServletRequest; |
||
11 | import javax.servlet.http.HttpServletResponse; |
||
12 | import java.io.IOException; |
||
2028 | es | 13 | import java.io.InputStream; |
14 | import java.io.PrintWriter; |
||
15 | import java.lang.reflect.InvocationTargetException; |
||
16 | import java.lang.reflect.Method; |
||
2046 | es | 17 | import java.util.ArrayList; |
18 | import java.util.List; |
||
2027 | es | 19 | |
2028 | es | 20 | public abstract class AbstractRestServlet extends HttpServlet |
2027 | es | 21 | { |
2046 | es | 22 | |
23 | |||
24 | |||
25 | public void addMessage(1.5.0/docs/api/java/lang/String.html">String mensagem, HttpServletRequest request) |
||
26 | { |
||
27 | List<String> messages = (List<String>) request.getAttribute("Messages"); |
||
28 | messages.add(mensagem); |
||
29 | } |
||
30 | |||
2027 | es | 31 | @1.5.0/docs/api/java/lang/Override.html">Override |
32 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
33 | process(req,resp); |
||
34 | } |
||
35 | |||
36 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
37 | protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
38 | process(req,resp); |
||
39 | } |
||
40 | |||
41 | protected void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
42 | { |
||
2028 | es | 43 | |
2046 | es | 44 | |
45 | req.setAttribute("Messages",new ArrayList<String>()); |
||
46 | if(req.getContentType() != null && req.getContentType().equalsIgnoreCase("application/json")) |
||
2027 | es | 47 | { |
2028 | es | 48 | 5+0%2Fdocs%2Fapi+InputStream">InputStream content = req.getInputStream(); |
49 | if(content != null) |
||
50 | { |
||
51 | try { |
||
2046 | es | 52 | ParseJsonRequestResult parseJsonRequestResult = new ParseJsonRequestResult(content).invoke(); |
53 | invokeService(req, resp, parseJsonRequestResult.getService(), parseJsonRequestResult.getData()); |
||
54 | } catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) { |
||
55 | sendErrorResponse(resp, e); |
||
56 | } |
||
57 | } |
||
58 | } |
||
59 | else |
||
60 | { |
||
61 | JSONObject response = new JSONObject(); |
||
62 | try { |
||
63 | response.put("service","error"); |
||
64 | response.put("response","REST REQUEST REQUIRED"); |
||
65 | resp.setContentType("application/json"); |
||
66 | 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter pw = resp.getWriter(); |
||
67 | pw.write(response.toString()); |
||
68 | } catch (JSONException e) { |
||
69 | e.printStackTrace(); |
||
70 | } |
||
2027 | es | 71 | |
2028 | es | 72 | |
73 | |||
2046 | es | 74 | } |
2028 | es | 75 | |
76 | |||
2046 | es | 77 | } |
2028 | es | 78 | |
2046 | es | 79 | private void invokeService(HttpServletRequest req, HttpServletResponse resp, 1.5.0/docs/api/java/lang/String.html">String service, JSONObject data) throws 1.5.0/docs/api/java/lang/NoSuchMethodException.html">NoSuchMethodException, 1.5.0/docs/api/java/lang/IllegalAccessException.html">IllegalAccessException, 1.5.0/docs/api/java/lang/reflect/InvocationTargetException.html">InvocationTargetException, JSONException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
80 | 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[]{ |
||
81 | JSONObject.class, |
||
82 | HttpServletRequest.class, |
||
83 | HttpServletResponse.class}); |
||
84 | JSONObject obj = (JSONObject) innerMethod.invoke(this,new 5+0%2Fdocs%2Fapi+Object">Object[]{data,req,resp}); |
||
2028 | es | 85 | |
2046 | es | 86 | JSONObject response = new JSONObject(); |
87 | response.put("service","ok"); |
||
88 | response.put("response",obj); |
||
89 | |||
90 | List<String> messages = (List<String>) req.getAttribute("Messages"); |
||
91 | |||
92 | if(messages.size() > 0) |
||
93 | { |
||
94 | JSONArray msgs = new JSONArray(); |
||
95 | for(1.5.0/docs/api/java/lang/String.html">String msgAdded: messages) |
||
96 | { |
||
97 | msgs.put(msgAdded); |
||
2028 | es | 98 | } |
2046 | es | 99 | response.put("messages",msgs); |
2027 | es | 100 | } |
101 | |||
2046 | es | 102 | resp.setContentType("application/json"); |
103 | 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter pw = resp.getWriter(); |
||
104 | pw.write(response.toString()); |
||
105 | } |
||
2028 | es | 106 | |
2046 | es | 107 | private void sendErrorResponse(HttpServletResponse resp, 1.5.0/docs/api/java/lang/Throwable.html">Throwable e) throws 1.5.0/docs/api/java/io/IOException.html">IOException { |
108 | e.printStackTrace(); |
||
109 | JSONObject response = new JSONObject(); |
||
110 | try { |
||
111 | response.put("service","error"); |
||
112 | response.put("exception",e.toString()); |
||
113 | resp.setContentType("application/json"); |
||
114 | 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter pw = resp.getWriter(); |
||
115 | pw.write(response.toString()); |
||
116 | } catch (JSONException e1) { |
||
117 | e1.printStackTrace(); |
||
118 | } |
||
2027 | es | 119 | } |
2046 | es | 120 | |
121 | private class ParseJsonRequestResult { |
||
122 | private 5+0%2Fdocs%2Fapi+InputStream">InputStream content; |
||
123 | private 1.5.0/docs/api/java/lang/String.html">String service; |
||
124 | private JSONObject data; |
||
125 | |||
126 | public ParseJsonRequestResult(5+0%2Fdocs%2Fapi+InputStream">InputStream content) { |
||
127 | this.content = content; |
||
128 | } |
||
129 | |||
130 | public 1.5.0/docs/api/java/lang/String.html">String getService() { |
||
131 | return service; |
||
132 | } |
||
133 | |||
134 | public JSONObject getData() { |
||
135 | return data; |
||
136 | } |
||
137 | |||
138 | public ParseJsonRequestResult invoke() throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException { |
||
139 | JSONObject requestObj; |
||
140 | 1.5.0/docs/api/java/lang/String.html">String json = StreamsUtils.readString(content); |
||
141 | requestObj = new JSONObject(json); |
||
142 | 1.5.0/docs/api/java/lang/System.html">System.out.println("REQUEST JSON:"); |
||
143 | 1.5.0/docs/api/java/lang/System.html">System.out.println(requestObj.toString()); |
||
144 | |||
145 | service = requestObj.getString("service"); |
||
146 | data = requestObj.has("data") ? requestObj.getJSONObject("data") : null; |
||
147 | return this; |
||
148 | } |
||
149 | } |
||
2027 | es | 150 | } |