1,5 → 1,6 |
package pt.estgp.es.exemplos.hibernate.web.rest; |
|
import org.json.JSONArray; |
import org.json.JSONException; |
import org.json.JSONObject; |
import pt.estgp.es.exemplos.hibernate.utils.StreamsUtils; |
10,13 → 11,23 |
import javax.servlet.http.HttpServletResponse; |
import java.io.IOException; |
import java.io.InputStream; |
import java.io.OutputStream; |
import java.io.PrintWriter; |
import java.lang.reflect.InvocationTargetException; |
import java.lang.reflect.Method; |
import java.util.ArrayList; |
import java.util.List; |
|
public abstract class AbstractRestServlet extends HttpServlet |
{ |
|
|
|
public void addMessage(String mensagem, HttpServletRequest request) |
{ |
List<String> messages = (List<String>) request.getAttribute("Messages"); |
messages.add(mensagem); |
} |
|
@Override |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
process(req,resp); |
29,54 → 40,111 |
|
protected void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException |
{ |
JSONObject requestObj; |
|
if(req.getContentType().equalsIgnoreCase("application/json")) |
|
req.setAttribute("Messages",new ArrayList<String>()); |
if(req.getContentType() != null && req.getContentType().equalsIgnoreCase("application/json")) |
{ |
InputStream content = req.getInputStream(); |
if(content != null) |
{ |
String json = StreamsUtils.readString(content); |
try { |
requestObj = new JSONObject(json); |
System.out.println("REQUEST JSON:"); |
System.out.println(requestObj.toString()); |
ParseJsonRequestResult parseJsonRequestResult = new ParseJsonRequestResult(content).invoke(); |
invokeService(req, resp, parseJsonRequestResult.getService(), parseJsonRequestResult.getData()); |
} catch (Throwable e) { |
sendErrorResponse(resp, e); |
} |
} |
} |
else |
{ |
JSONObject response = new JSONObject(); |
try { |
response.put("service","error"); |
response.put("response","REST REQUEST REQUIRED"); |
resp.setContentType("application/json"); |
PrintWriter pw = resp.getWriter(); |
pw.write(response.toString()); |
} catch (JSONException e) { |
e.printStackTrace(); |
} |
|
String service = requestObj.getString("service"); |
String data = requestObj.has("data") ? requestObj.getString("data") : null; |
|
Method innerMethod = this.getClass().getMethod(service,new Class[]{ |
JSONObject.class, |
HttpServletRequest.class, |
HttpServletResponse.class}); |
JSONObject obj = (JSONObject) innerMethod.invoke(this,new Object[]{data,req,resp}); |
|
JSONObject response = new JSONObject(); |
response.put("service","ok"); |
response.put("response",obj); |
} |
|
resp.setContentType("application/json"); |
PrintWriter pw = resp.getWriter(); |
pw.write(obj.toString()); |
|
} catch (Throwable e) { |
e.printStackTrace(); |
JSONObject response = new JSONObject(); |
try { |
response.put("service","error"); |
response.put("cause",e.toString()); |
resp.setContentType("application/json"); |
PrintWriter pw = resp.getWriter(); |
pw.write(response.toString()); |
} catch (JSONException e1) { |
e1.printStackTrace(); |
} |
} |
|
private void invokeService(HttpServletRequest req, HttpServletResponse resp, String service, JSONObject data) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, JSONException, IOException { |
Method innerMethod = this.getClass().getMethod(service,new Class[]{ |
JSONObject.class, |
HttpServletRequest.class, |
HttpServletResponse.class}); |
JSONObject obj = (JSONObject) innerMethod.invoke(this,new Object[]{data,req,resp}); |
|
} |
JSONObject response = new JSONObject(); |
response.put("service","ok"); |
response.put("response",obj); |
|
List<String> messages = (List<String>) req.getAttribute("Messages"); |
|
if(messages.size() > 0) |
{ |
JSONArray msgs = new JSONArray(); |
for(String msgAdded: messages) |
{ |
msgs.put(msgAdded); |
} |
response.put("messages",msgs); |
} |
|
resp.setContentType("application/json"); |
PrintWriter pw = resp.getWriter(); |
pw.write(response.toString()); |
} |
|
private void sendErrorResponse(HttpServletResponse resp, Throwable e) throws IOException { |
e.printStackTrace(); |
JSONObject response = new JSONObject(); |
try { |
response.put("service","error"); |
response.put("exception",e.toString()); |
resp.setContentType("application/json"); |
PrintWriter pw = resp.getWriter(); |
pw.write(response.toString()); |
} catch (JSONException e1) { |
e1.printStackTrace(); |
} |
} |
|
private class ParseJsonRequestResult { |
private InputStream content; |
private String service; |
private JSONObject data; |
|
public ParseJsonRequestResult(InputStream content) { |
this.content = content; |
} |
|
public String getService() { |
return service; |
} |
|
public JSONObject getData() { |
return data; |
} |
|
public ParseJsonRequestResult invoke() throws IOException, JSONException { |
JSONObject requestObj; |
String json = StreamsUtils.readString(content); |
requestObj = new JSONObject(json); |
System.out.println("REQUEST JSON:"); |
System.out.println(requestObj.toString()); |
|
service = requestObj.getString("service"); |
data = requestObj.has("data") ? requestObj.getJSONObject("data") : null; |
return this; |
} |
} |
} |