Subversion Repositories bacoAlunos

Rev

Rev 2049 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2049 es 1
package pt.estgp.es.exemplos.hibernate.web.rest;
2
 
3
import org.json.JSONArray;
4
import org.json.JSONException;
5
import org.json.JSONObject;
6
import pt.estgp.es.exemplos.hibernate.utils.StreamsUtils;
7
 
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;
13
import java.io.InputStream;
14
import java.io.PrintWriter;
15
import java.lang.reflect.InvocationTargetException;
16
import java.lang.reflect.Method;
17
import java.util.ArrayList;
18
import java.util.List;
19
 
20
public abstract class AbstractRestServlet extends HttpServlet
21
{
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
 
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
    {
43
 
44
 
45
        req.setAttribute("Messages",new ArrayList<String>());
46
        if(req.getContentType() != null && req.getContentType().equalsIgnoreCase("application/json"))
47
        {
48
            5+0%2Fdocs%2Fapi+InputStream">InputStream content = req.getInputStream();
49
            if(content != null)
50
            {
51
                try {
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
            }
71
 
72
 
73
 
74
        }
75
 
76
 
77
    }
78
 
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});
85
 
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);
98
            }
99
            response.put("messages",msgs);
100
        }
101
 
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
    }
106
 
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 {
2069 es 111
            if(e instanceof 1.5.0/docs/api/java/lang/reflect/InvocationTargetException.html">InvocationTargetException)
112
            {
113
                if(((1.5.0/docs/api/java/lang/reflect/InvocationTargetException.html">InvocationTargetException) e).getTargetException() != null)
114
                {
115
                    e = ((1.5.0/docs/api/java/lang/reflect/InvocationTargetException.html">InvocationTargetException) e).getTargetException();
116
                }
117
            }
2049 es 118
            response.put("service","error");
119
            response.put("exception",e.toString());
120
            resp.setContentType("application/json");
121
            1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter pw = resp.getWriter();
122
            pw.write(response.toString());
123
        } catch (JSONException e1) {
124
            e1.printStackTrace();
125
        }
126
    }
127
 
128
    private class ParseJsonRequestResult {
129
        private 5+0%2Fdocs%2Fapi+InputStream">InputStream content;
130
        private 1.5.0/docs/api/java/lang/String.html">String service;
131
        private JSONObject data;
132
 
133
        public ParseJsonRequestResult(5+0%2Fdocs%2Fapi+InputStream">InputStream content) {
134
            this.content = content;
135
        }
136
 
137
        public 1.5.0/docs/api/java/lang/String.html">String getService() {
138
            return service;
139
        }
140
 
141
        public JSONObject getData() {
142
            return data;
143
        }
144
 
145
        public ParseJsonRequestResult invoke() throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {
146
            JSONObject requestObj;
147
            1.5.0/docs/api/java/lang/String.html">String json = StreamsUtils.readString(content);
148
            requestObj = new JSONObject(json);
149
            1.5.0/docs/api/java/lang/System.html">System.out.println("REQUEST JSON:");
150
            1.5.0/docs/api/java/lang/System.html">System.out.println(requestObj.toString());
151
 
152
            service = requestObj.getString("service");
153
            data = requestObj.has("data") ? requestObj.getJSONObject("data") : null;
154
            return this;
155
        }
156
    }
157
}