Sfoglia il codice sorgente

Change http code for endpoint of sensor update

Lukas Cerny 4 anni fa
parent
commit
bc201fc849

+ 20 - 0
src/main/java/cz/hsrs/core/WebServiceException.java

@@ -0,0 +1,20 @@
+package cz.hsrs.core;
+
+public class WebServiceException extends RuntimeException {
+
+    private final int code;
+
+    public WebServiceException(int code, String message) {
+        super(message);
+        this.code = code;
+    }
+
+    public WebServiceException(int code, Throwable cause) {
+        super(cause);
+        this.code = code;
+    }
+
+    public int getCode() {
+        return code;
+    }
+}

+ 5 - 0
src/main/java/cz/hsrs/db/util/ManagementUtil.java

@@ -5,6 +5,7 @@ import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 
+import cz.hsrs.core.WebServiceException;
 import net.sf.json.JSONArray;
 import net.sf.json.JSONObject;
 import cz.hsrs.db.model.NoItemFoundException;
@@ -116,6 +117,8 @@ public class ManagementUtil extends DBUtil {
                 throw new NoItemFoundException("Attribute 'sensors' is required.");
             }
 
+            throw new WebServiceException(425, "Update of the sensor is not supported yet.");
+            /*
             for (Sensor sensor : sensors) {
                 long sensorId = sensor.getSensorId();
                 // 2. iterace
@@ -124,6 +127,8 @@ public class ManagementUtil extends DBUtil {
             }
             return new UnitInsert(unitId, null, sensors);
 
+             */
+
         } else {
             throw new NoItemFoundException("Attribute 'unit' is required.");
         }

+ 6 - 0
src/main/java/cz/hsrs/servlet/provider/ManagementService.java

@@ -10,6 +10,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.core.HttpHeaders;
 
+import cz.hsrs.core.WebServiceException;
 import net.sf.json.JSON;
 import net.sf.json.JSONException;
 import net.sf.json.JSONObject;
@@ -229,6 +230,11 @@ public class ManagementService extends DBServlet {
                     throw new ServletException(msg);
                 }
             }
+        } catch (WebServiceException e) {
+            JSONObject msg = new JSONObject();
+            msg.put("message", e.getMessage());
+            bodyResponse = msg;
+            response.setStatus(e.getCode());
         } catch (Exception e) {
             JSONObject msg = new JSONObject();
             msg.put("message", e.getMessage());