Rev 2027 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2027 | Rev 2028 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | package pt.estgp.es.exemplos.hibernate.web.rest; |
1 | package pt.estgp.es.exemplos.hibernate.web.rest; |
2 | 2 | ||
- | 3 | import org.json.JSONException; |
|
- | 4 | import org.json.JSONObject; |
|
- | 5 | import pt.estgp.es.exemplos.hibernate.utils.StreamsUtils; |
|
- | 6 | ||
3 | import javax.servlet.ServletException; |
7 | import javax.servlet.ServletException; |
4 | import javax.servlet.http.HttpServlet; |
8 | import javax.servlet.http.HttpServlet; |
5 | import javax.servlet.http.HttpServletRequest; |
9 | import javax.servlet.http.HttpServletRequest; |
6 | import javax.servlet.http.HttpServletResponse; |
10 | import javax.servlet.http.HttpServletResponse; |
7 | import java.io.IOException; |
11 | import java.io.IOException; |
- | 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; |
|
8 | 17 | ||
9 | public class AbstractRestServlet extends HttpServlet |
18 | public abstract class AbstractRestServlet extends HttpServlet |
10 | { |
19 | { |
11 | @1.5.0/docs/api/java/lang/Override.html">Override |
20 | @1.5.0/docs/api/java/lang/Override.html">Override |
12 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
21 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
13 | process(req,resp); |
22 | process(req,resp); |
14 | } |
23 | } |
Line 18... | Line 27... | ||
18 | process(req,resp); |
27 | process(req,resp); |
19 | } |
28 | } |
20 | 29 | ||
21 | protected void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
30 | protected void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
22 | { |
31 | { |
23 | if(req.getContentType().equals("application/json")) |
32 | JSONObject requestObj; |
24 | { |
- | |
25 | 33 | ||
26 | } |
- | |
27 | else |
34 | if(req.getContentType().equalsIgnoreCase("application/json")) |
28 | { |
35 | { |
- | 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()); |
|
- | 44 | ||
- | 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 | } |
|
29 | 74 | ||
- | 75 | ||
- | 76 | } |
|
- | 77 | } |
|
30 | } |
78 | } |
- | 79 | ||
- | 80 | ||
31 | } |
81 | } |
32 | } |
82 | } |