Subversion Repositories bacoAlunos

Rev

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