|
@@ -2,18 +2,13 @@ package cz.hsrs.servlet.provider;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
-import java.io.PrintWriter;
|
|
|
|
|
|
|
+import java.sql.SQLException;
|
|
|
|
|
|
|
|
-import javax.naming.AuthenticationException;
|
|
|
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.ServletException;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.ws.rs.core.HttpHeaders;
|
|
import javax.ws.rs.core.HttpHeaders;
|
|
|
|
|
|
|
|
-import cz.hsrs.db.model.insert.UnitInsert;
|
|
|
|
|
-import cz.hsrs.db.util.UtilFactory;
|
|
|
|
|
-import cz.hsrs.servlet.feeder.ServiceParameters;
|
|
|
|
|
-import cz.hsrs.servlet.security.LoginUser;
|
|
|
|
|
import net.sf.json.JSON;
|
|
import net.sf.json.JSON;
|
|
|
import net.sf.json.JSONException;
|
|
import net.sf.json.JSONException;
|
|
|
import net.sf.json.JSONObject;
|
|
import net.sf.json.JSONObject;
|
|
@@ -21,6 +16,12 @@ import net.sf.json.JSONSerializer;
|
|
|
import net.sf.json.JsonConfig;
|
|
import net.sf.json.JsonConfig;
|
|
|
import net.sf.json.util.CycleDetectionStrategy;
|
|
import net.sf.json.util.CycleDetectionStrategy;
|
|
|
|
|
|
|
|
|
|
+import cz.hsrs.db.model.NoItemFoundException;
|
|
|
|
|
+import cz.hsrs.db.model.insert.UnitInsert;
|
|
|
|
|
+import cz.hsrs.db.util.UtilFactory;
|
|
|
|
|
+import cz.hsrs.servlet.feeder.ServiceParameters;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* @author mkepka
|
|
* @author mkepka
|
|
|
*
|
|
*
|
|
@@ -28,16 +29,32 @@ import net.sf.json.util.CycleDetectionStrategy;
|
|
|
public class ManagementService extends DBServlet {
|
|
public class ManagementService extends DBServlet {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
private static final long serialVersionUID = 1L;
|
|
|
- private static final String jsonContent = "application/json";
|
|
|
|
|
|
|
+ private static final String JSON_CONTENT = "application/json";
|
|
|
|
|
+ private static final String TEXT_CONTENT = "plain/text";
|
|
|
|
|
|
|
|
- public static final String INS_UNIT = "InsertUnit";
|
|
|
|
|
- public static final String INS_SENSOR = "InsertSensor";
|
|
|
|
|
-
|
|
|
|
|
- private UtilFactory db;
|
|
|
|
|
-
|
|
|
|
|
- public ManagementService(){
|
|
|
|
|
- super();
|
|
|
|
|
|
|
+ // POST
|
|
|
|
|
+ private static final String INSERT_UNIT = "InsertUnit";
|
|
|
|
|
+ private static final String INSERT_SENSOR = "InsertSensor";
|
|
|
|
|
+
|
|
|
|
|
+ // PUT
|
|
|
|
|
+ private static final String UPDATE_UNIT = "UpdateUnit";
|
|
|
|
|
+ private static final String UPDATE_SENSOR = "UpdateSensor";
|
|
|
|
|
+
|
|
|
|
|
+ // DELETE
|
|
|
|
|
+ private static final String DELETE_UNIT = "DeleteUnit";
|
|
|
|
|
+ private static final String DELETE_SENSOR = "DeleteSensor";
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private static final JsonConfig DEFAULT_JSON_CONFIG;
|
|
|
|
|
+
|
|
|
|
|
+ static {
|
|
|
|
|
+ DEFAULT_JSON_CONFIG = new JsonConfig();
|
|
|
|
|
+ DEFAULT_JSON_CONFIG.setIgnoreTransientFields(true);
|
|
|
|
|
+ DEFAULT_JSON_CONFIG.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
|
|
|
|
|
+ DEFAULT_JSON_CONFIG.setJsonPropertyFilter((source, name, value) -> (value == null));
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private UtilFactory db;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void init() throws ServletException {
|
|
public void init() throws ServletException {
|
|
@@ -49,99 +66,212 @@ public class ManagementService extends DBServlet {
|
|
|
throw new ServletException(e);
|
|
throw new ServletException(e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
|
|
|
|
|
- RequestParameters params = new RequestParameters(request);
|
|
|
|
|
- /*
|
|
|
|
|
- if(user == null){
|
|
|
|
|
- throw new ServletException("Authentication failure, no user specified for request: "+ request.getQueryString());
|
|
|
|
|
|
|
+ private static String requestBodyAsString(HttpServletRequest request) {
|
|
|
|
|
+ if (request.getContentLength() <= 0) {
|
|
|
|
|
+ return "";
|
|
|
}
|
|
}
|
|
|
- else{
|
|
|
|
|
- try {
|
|
|
|
|
- String testLang = db.userUtil.getUserLanguage(user);
|
|
|
|
|
- if(testLang.isEmpty()){
|
|
|
|
|
- throw new ServletException("Authentication failure for request "+ request.getQueryString());
|
|
|
|
|
- }
|
|
|
|
|
- } catch (SQLException | NoItemFoundException e1) {
|
|
|
|
|
- throw new ServletException("Authentication failure for request "+ request.getQueryString());
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ String line;
|
|
|
|
|
+ StringBuilder jb = new StringBuilder();
|
|
|
|
|
+ BufferedReader reader = request.getReader();
|
|
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
|
|
+ jb.append(line);
|
|
|
}
|
|
}
|
|
|
- }*/
|
|
|
|
|
-
|
|
|
|
|
- /* session login method */
|
|
|
|
|
- LoginUser loggedUser;
|
|
|
|
|
|
|
+ return jb.toString();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static JSONObject stringToJson(String jsonString) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return JSONObject.fromObject(jsonString);
|
|
|
|
|
+ } catch (JSONException e){
|
|
|
|
|
+ throw new JSONException(e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean isNotAuthorized(String user) {
|
|
|
|
|
+ if(user == null || user.isEmpty()) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
try {
|
|
try {
|
|
|
- loggedUser = getAuthenticatedLoginUser(request);
|
|
|
|
|
- String userName = loggedUser.getUserName();
|
|
|
|
|
- params.setUser(userName);
|
|
|
|
|
- } catch (AuthenticationException e1) {
|
|
|
|
|
- throw new ServletException("Authentication failure for request "+ request.getQueryString());
|
|
|
|
|
- }
|
|
|
|
|
- String user = params.getUSER();
|
|
|
|
|
- /* Setting response headers */
|
|
|
|
|
|
|
+ String testLang = db.userUtil.getUserLanguage(user);
|
|
|
|
|
+ if (testLang.isEmpty()) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (SQLException | NoItemFoundException e) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
+ response.addHeader(HttpHeaders.CONTENT_TYPE, JSON_CONTENT);
|
|
|
response.addHeader("Access-Control-Allow-Origin", "*");
|
|
response.addHeader("Access-Control-Allow-Origin", "*");
|
|
|
-
|
|
|
|
|
- PrintWriter out = response.getWriter();
|
|
|
|
|
|
|
+
|
|
|
|
|
+ String user = request.getParameter("user");
|
|
|
|
|
+ if (isNotAuthorized(user)) {
|
|
|
|
|
+ throw new ServletException("Authentication failure for request: "+ request.getQueryString());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
|
|
|
|
|
+ if (contentType != null && !contentType.equalsIgnoreCase(JSON_CONTENT)) {
|
|
|
|
|
+ throw new ServletException("Received request does not contain JSON data. " +
|
|
|
|
|
+ "Only request with 'application/json' header could be accepter. " + request.getQueryString()
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String body = requestBodyAsString(request);
|
|
|
|
|
+ if (body.isEmpty()) {
|
|
|
|
|
+ throw new ServletException("Request does not contain any content.");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ JSON bodyResponse;
|
|
|
try {
|
|
try {
|
|
|
- if (request.getParameter(ServiceParameters.OPERATION).equals(INS_UNIT)) {
|
|
|
|
|
- if(request.getContentLength() > 0){
|
|
|
|
|
- String contentHeader = request.getHeader(HttpHeaders.CONTENT_TYPE);
|
|
|
|
|
- if(contentHeader != null && contentHeader.toLowerCase().contains(jsonContent)){
|
|
|
|
|
- response.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
|
|
|
|
- JSONObject payload = readPayload(request);
|
|
|
|
|
- UnitInsert responseO = db.manUtil.insertUnit(payload, user);
|
|
|
|
|
-
|
|
|
|
|
- JsonConfig cfg = new JsonConfig();
|
|
|
|
|
- cfg.setIgnoreTransientFields(true);
|
|
|
|
|
- cfg.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
|
|
|
|
|
- JSON json = JSONSerializer.toJSON(responseO, cfg);
|
|
|
|
|
-
|
|
|
|
|
- json.write(out);
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- throw new ServletException("Request doesn't contain any content!");
|
|
|
|
|
- }
|
|
|
|
|
- // } else if (request.getParameter(ServiceParameters.OPERATION).equals(INS_SENSOR)) {
|
|
|
|
|
- // DBJsonUtils.writeJSON(out, new UnitTrack(), db.userUtil.getTracksByUserName(params.getUSER(),params.LIMIT));
|
|
|
|
|
- } else {
|
|
|
|
|
- throw new ServletException("No operation specified!");
|
|
|
|
|
|
|
+ JSONObject bodyJson = stringToJson(body);
|
|
|
|
|
+ String operationMode = request.getParameter(ServiceParameters.OPERATION);
|
|
|
|
|
+ switch (operationMode) {
|
|
|
|
|
+ case INSERT_UNIT: bodyResponse = insertUnit(bodyJson, user); break;
|
|
|
|
|
+ case INSERT_SENSOR: bodyResponse = insertSensor(bodyJson, user); break;
|
|
|
|
|
+ default: throw new ServletException(
|
|
|
|
|
+ String.format("No operation specified! Allowed: [%s, %s].", INSERT_UNIT, INSERT_SENSOR)
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- response.addHeader(HttpHeaders.CONTENT_TYPE, "plain/text");
|
|
|
|
|
- solveGetException(e, out);
|
|
|
|
|
|
|
+ JSONObject msg = new JSONObject();
|
|
|
|
|
+ msg.put("message", e.getMessage());
|
|
|
|
|
+ bodyResponse = msg;
|
|
|
|
|
+ response.setStatus(400);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ bodyResponse.write(response.getWriter());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private JSONObject readPayload(HttpServletRequest request) throws Exception{
|
|
|
|
|
- StringBuilder jb = new StringBuilder();
|
|
|
|
|
- String line;
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
+ response.addHeader(HttpHeaders.CONTENT_TYPE, JSON_CONTENT);
|
|
|
|
|
+ response.addHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
|
+
|
|
|
|
|
+ String user = request.getParameter("user");
|
|
|
|
|
+ if (isNotAuthorized(user)) {
|
|
|
|
|
+ throw new ServletException("Authentication failure for request: "+ request.getQueryString());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
|
|
|
|
|
+ if (contentType != null && !contentType.equalsIgnoreCase(JSON_CONTENT)) {
|
|
|
|
|
+ throw new ServletException("Received request does not contain JSON data. " +
|
|
|
|
|
+ "Only request with 'application/json' header could be accepter. " + request.getQueryString()
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String body = requestBodyAsString(request);
|
|
|
|
|
+ if (body.isEmpty()) {
|
|
|
|
|
+ throw new ServletException("Request does not contain any content.");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ JSON bodyResponse;
|
|
|
try {
|
|
try {
|
|
|
- BufferedReader reader = request.getReader();
|
|
|
|
|
- while ((line = reader.readLine()) != null) {
|
|
|
|
|
- jb.append(line);
|
|
|
|
|
|
|
+ JSONObject bodyJson = stringToJson(body);
|
|
|
|
|
+ String operationMode = request.getParameter(ServiceParameters.OPERATION);
|
|
|
|
|
+ switch (operationMode) {
|
|
|
|
|
+ case UPDATE_UNIT: bodyResponse = updateUnit(bodyJson); break;
|
|
|
|
|
+ case UPDATE_SENSOR: bodyResponse = updateSensor(bodyJson); break;
|
|
|
|
|
+ default: throw new ServletException(
|
|
|
|
|
+ String.format("No operation specified! Allowed: [%s, %s].", UPDATE_UNIT, UPDATE_SENSOR)
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
- return JSONObject.fromObject(jb.toString());
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- throw new Exception(e.getMessage());
|
|
|
|
|
- } catch (JSONException e){
|
|
|
|
|
- throw new JSONException(e.getMessage());
|
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ JSONObject msg = new JSONObject();
|
|
|
|
|
+ msg.put("message", e.getMessage());
|
|
|
|
|
+ bodyResponse = msg;
|
|
|
|
|
+ response.setStatus(400);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ bodyResponse.write(response.getWriter());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
+ response.addHeader(HttpHeaders.CONTENT_TYPE, JSON_CONTENT);
|
|
|
|
|
+ response.addHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
|
+
|
|
|
|
|
+ String user = request.getParameter("user");
|
|
|
|
|
+ if (isNotAuthorized(user)) {
|
|
|
|
|
+ throw new ServletException("Authentication failure for request: "+ request.getQueryString());
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- static class RequestParameters {
|
|
|
|
|
- private String user;
|
|
|
|
|
-
|
|
|
|
|
- RequestParameters(HttpServletRequest request) throws NullPointerException{
|
|
|
|
|
- Object userO = request.getParameter("user");
|
|
|
|
|
- this.user = userO != null ? userO.toString() : "";
|
|
|
|
|
|
|
+ String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
|
|
|
|
|
+ if (contentType != null && !contentType.equalsIgnoreCase(JSON_CONTENT)) {
|
|
|
|
|
+ throw new ServletException("Received request does not contain JSON data. " +
|
|
|
|
|
+ "Only request with 'application/json' header could be accepter. " + request.getQueryString()
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- public String getUSER() {
|
|
|
|
|
- return user;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ String body = requestBodyAsString(request);
|
|
|
|
|
+ if (body.isEmpty()) {
|
|
|
|
|
+ throw new ServletException("Request does not contain any content.");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ JSON bodyResponse;
|
|
|
|
|
+ try {
|
|
|
|
|
+ JSONObject bodyJson = stringToJson(body);
|
|
|
|
|
+ String operationMode = request.getParameter(ServiceParameters.OPERATION);
|
|
|
|
|
+ switch (operationMode) {
|
|
|
|
|
+ case DELETE_UNIT: bodyResponse = deleteUnit(bodyJson); break;
|
|
|
|
|
+ case DELETE_SENSOR: bodyResponse = deleteSensor(bodyJson); break;
|
|
|
|
|
+ default: {
|
|
|
|
|
+ String msg = String.format("No operation specified! Allowed: [%s, %s].", DELETE_UNIT, DELETE_SENSOR);
|
|
|
|
|
+ throw new ServletException(msg);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ JSONObject msg = new JSONObject();
|
|
|
|
|
+ msg.put("message", e.getMessage());
|
|
|
|
|
+ bodyResponse = msg;
|
|
|
|
|
+ response.setStatus(400);
|
|
|
}
|
|
}
|
|
|
- public void setUser(String user) {
|
|
|
|
|
- this.user = user;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ bodyResponse.write(response.getWriter());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private JSON insertUnit(JSONObject unitJson, String user) throws SQLException, NoItemFoundException {
|
|
|
|
|
+ UnitInsert newUnit = db.manUtil.insertUnit(unitJson, user);
|
|
|
|
|
+ return JSONSerializer.toJSON(newUnit, DEFAULT_JSON_CONFIG);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private JSON insertSensor(JSONObject sensorJson, String user) throws SQLException, NoItemFoundException {
|
|
|
|
|
+ UnitInsert sensorsToUnit = db.manUtil.insertSensor(sensorJson, user);
|
|
|
|
|
+ return JSONSerializer.toJSON(sensorsToUnit, DEFAULT_JSON_CONFIG);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private JSON updateUnit(JSONObject unitJson) throws SQLException, NoItemFoundException {
|
|
|
|
|
+ UnitInsert updatedUnit = db.manUtil.updateUnit(unitJson);
|
|
|
|
|
+ return JSONSerializer.toJSON(updatedUnit, DEFAULT_JSON_CONFIG);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private JSON updateSensor(JSONObject sensorJson) throws SQLException, NoItemFoundException {
|
|
|
|
|
+ UnitInsert updatedSensors = db.manUtil.updateSensor(sensorJson);
|
|
|
|
|
+ return JSONSerializer.toJSON(updatedSensors, DEFAULT_JSON_CONFIG);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private JSON deleteUnit(JSONObject unitJson) throws SQLException, NoItemFoundException {
|
|
|
|
|
+ Long deletedUnit = db.manUtil.deleteUnit(unitJson);
|
|
|
|
|
+ String message;
|
|
|
|
|
+ if (deletedUnit != null) {
|
|
|
|
|
+ message = String.format("Unit '%s' was successfully deleted.", deletedUnit);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ message = "Unit can not be deleted.";
|
|
|
}
|
|
}
|
|
|
|
|
+ JSONObject resultJson = new JSONObject();
|
|
|
|
|
+ resultJson.put("message", message);
|
|
|
|
|
+ return resultJson;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private JSON deleteSensor(JSONObject unitJson) throws SQLException, NoItemFoundException {
|
|
|
|
|
+ UnitInsert deletedSensors = db.manUtil.deleteSensor(unitJson);
|
|
|
|
|
+ return JSONSerializer.toJSON(deletedSensors, DEFAULT_JSON_CONFIG);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|