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;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
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
(1.5.0/docs/api/java/lang/String.html">String mensagem, HttpServletRequest request
)
{
List
<String
> messages =
(List
<String
>) request.
getAttribute("Messages");
messages.
add(mensagem
);
}
@
1.5.0/docs/api/java/lang/Override.html">Override
protected void doGet
(HttpServletRequest req, HttpServletResponse resp
) throws ServletException,
1.5.0/docs/api/java/io/IOException.html">IOException {
process
(req,resp
);
}
@
1.5.0/docs/api/java/lang/Override.html">Override
protected void doPost
(HttpServletRequest req, HttpServletResponse resp
) throws ServletException,
1.5.0/docs/api/java/io/IOException.html">IOException {
process
(req,resp
);
}
protected void process
(HttpServletRequest req, HttpServletResponse resp
) throws ServletException,
1.5.0/docs/api/java/io/IOException.html">IOException
{
req.
setAttribute("Messages",
new ArrayList
<String
>());
if(req.
getContentType() !=
null && req.
getContentType().
equalsIgnoreCase("application/json"))
{
5+0%2Fdocs%2Fapi+InputStream">InputStream content = req.
getInputStream();
if(content
!=
null)
{
try {
ParseJsonRequestResult parseJsonRequestResult =
new ParseJsonRequestResult
(content
).
invoke();
invokeService
(req, resp, parseJsonRequestResult.
getService(), parseJsonRequestResult.
getData());
} catch (1.5.0/docs/api/java/lang/Throwable.html">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");
1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter pw = resp.
getWriter();
pw.
write(response.
toString());
} catch (JSONException e
) {
e.
printStackTrace();
}
}
}
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 {
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[]{
JSONObject.
class,
HttpServletRequest.
class,
HttpServletResponse.
class});
JSONObject obj =
(JSONObject
) innerMethod.
invoke(this,
new 5+0%2Fdocs%2Fapi+Object">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(1.5.0/docs/api/java/lang/String.html">String msgAdded: messages
)
{
msgs.
put(msgAdded
);
}
response.
put("messages",msgs
);
}
resp.
setContentType("application/json");
1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter pw = resp.
getWriter();
pw.
write(response.
toString());
}
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 {
e.
printStackTrace();
JSONObject response =
new JSONObject
();
try {
if(e
instanceof 1.5.0/docs/api/java/lang/reflect/InvocationTargetException.html">InvocationTargetException)
{
if(((1.5.0/docs/api/java/lang/reflect/InvocationTargetException.html">InvocationTargetException) e
).
getTargetException() !=
null)
{
e =
((1.5.0/docs/api/java/lang/reflect/InvocationTargetException.html">InvocationTargetException) e
).
getTargetException();
}
}
response.
put("service",
"error");
response.
put("exception",e.
toString());
resp.
setContentType("application/json");
1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter pw = resp.
getWriter();
pw.
write(response.
toString());
} catch (JSONException e1
) {
e1.
printStackTrace();
}
}
private class ParseJsonRequestResult
{
private 5+0%2Fdocs%2Fapi+InputStream">InputStream content
;
private 1.5.0/docs/api/java/lang/String.html">String service
;
private JSONObject data
;
public ParseJsonRequestResult
(5+0%2Fdocs%2Fapi+InputStream">InputStream content
) {
this.
content = content
;
}
public 1.5.0/docs/api/java/lang/String.html">String getService
() {
return service
;
}
public JSONObject getData
() {
return data
;
}
public ParseJsonRequestResult invoke
() throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException
{
JSONObject requestObj
;
1.5.0/docs/api/java/lang/String.html">String json = StreamsUtils.
readString(content
);
requestObj =
new JSONObject
(json
);
1.5.0/docs/api/java/lang/System.html">System.
out.
println("REQUEST JSON:");
1.5.0/docs/api/java/lang/System.html">System.
out.
println(requestObj.
toString());
service = requestObj.
getString("service");
data = requestObj.
has("data") ? requestObj.
getJSONObject("data") :
null;
return this;
}
}
}