Enhancements to common.northbound 29/929/2
authorAlessandro Boch <aboch@cisco.com>
Tue, 20 Aug 2013 23:36:45 +0000 (16:36 -0700)
committerAlessandro Boch <aboch@cisco.com>
Tue, 20 Aug 2013 23:40:21 +0000 (16:40 -0700)
- Added BadRequest and NotImplemented exceptions

Change-Id: I9a021ddd103c16122c39626e9cca044bd8a33ea6
Signed-off-by: Alessandro Boch <aboch@cisco.com>
opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/RestMessages.java
opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/BadRequestException.java [new file with mode: 0644]
opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/NotImplemented.java [new file with mode: 0644]
opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/NotImplementedException.java [new file with mode: 0644]

index ebe36a9..668efbc 100644 (file)
@@ -9,17 +9,13 @@
 package org.opendaylight.controller.northbound.commons;
 
 public enum RestMessages {
-    SUCCESS("Success"), NOCONTAINER("Container does not exist"), NOFLOWSPEC(
-            "Flow Spec does not exist"), NOSUBNET("Subnet does not exist"), NOSTATICROUTE(
-            "Static Route does not exist"), NOHOST("Host does not exist"), NOFLOW(
-            "Flow does not exist"), NONODE("Node does not exist"), NOPOLICY(
-            "Policy does not exist"), NORESOURCE("Resource does not exist"), RESOURCECONFLICT(
-            "Operation failed due to Resource Conflict"), NODEFAULT(
-            "Container default is not a custom container"), DEFAULTDISABLED(
+    SUCCESS("Success"), NOCONTAINER("Container does not exist"), NOSUBNET("Subnet does not exist"), NOSTATICROUTE(
+            "Static Route does not exist"), NOHOST("Host does not exist"), NOFLOW("Flow does not exist"), NONODE(
+            "Node does not exist"), NOPOLICY("Policy does not exist"), NORESOURCE("Resource does not exist"), RESOURCECONFLICT(
+            "Operation failed due to Resource Conflict"), NODEFAULT("Container default is not a custom container"), DEFAULTDISABLED(
             "Container(s) are configured. Container default is not operational"), NOTALLOWEDONDEFAULT(
-            "Container default is a static resource, no modification allowed on it"), UNKNOWNACTION(
-            "Unknown action"), INVALIDJSON("JSON message is invalid"), INVALIDADDRESS(
-            "invalid InetAddress"), AVAILABLESOON(
+            "Container default is a static resource, no modification allowed on it"), UNKNOWNACTION("Unknown action"), INVALIDJSON(
+            "JSON message is invalid"), INVALIDADDRESS("invalid InetAddress"), AVAILABLESOON(
             "Resource is not implemented yet"), INTERNALERROR("Internal Error"), SERVICEUNAVAILABLE(
             "Service is not available. Could be down for maintanence"), INVALIDDATA(
             "Data is invalid or conflicts with URI");
@@ -30,6 +26,7 @@ public enum RestMessages {
         message = msg;
     }
 
+    @Override
     public String toString() {
         return message;
     }
diff --git a/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/BadRequestException.java b/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/BadRequestException.java
new file mode 100644 (file)
index 0000000..c1b832d
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.northbound.commons.exception;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+/**
+ * Status Code 400 (Bad Request)
+ *
+ * The request could not be understood by the server due to malformed syntax.
+ * The client SHOULD NOT repeat the request without modifications.
+ */
+public class BadRequestException extends WebApplicationException {
+    private static final long serialVersionUID = 1L;
+
+    public BadRequestException(String string) {
+        super(Response.status(Response.Status.BAD_REQUEST).entity(string).type(MediaType.TEXT_PLAIN).build());
+    }
+}
diff --git a/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/NotImplemented.java b/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/NotImplemented.java
new file mode 100644 (file)
index 0000000..16d0cd6
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.northbound.commons.exception;
+
+import javax.ws.rs.core.Response;
+
+/**
+ * Implementation of StatusType for error 501 as in:
+ * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6
+ */
+public class NotImplemented implements Response.StatusType {
+    @Override
+    public int getStatusCode() {
+        return 501;
+    }
+
+    @Override
+    public String getReasonPhrase() {
+        return "Not Implemented";
+    }
+
+    @Override
+    public Response.Status.Family getFamily() {
+        return Response.Status.Family.SERVER_ERROR;
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/NotImplementedException.java b/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/exception/NotImplementedException.java
new file mode 100644 (file)
index 0000000..5c931d2
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.northbound.commons.exception;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+/**
+ * Status Code 501 (Not Implemented)
+ *
+ * The server does not support the functionality required to fulfill the
+ * request. This is the appropriate response when the server does not recognize
+ * the request method and is not capable of supporting it for any resource.
+ */
+public class NotImplementedException extends WebApplicationException {
+    private static final long serialVersionUID = 1L;
+
+    public NotImplementedException(String string) {
+        super(Response.status(new NotImplemented()).entity(string).type(MediaType.TEXT_PLAIN).build());
+    }
+}