Configure jackson to turn off FAIL_ON_UNKNOWN_PROPERTIES. This ensures that Jackson is 46/1246/1
authorPrasanth Pallamreddy <ppallamr@cisco.com>
Tue, 17 Sep 2013 21:18:02 +0000 (14:18 -0700)
committerPrasanth Pallamreddy <ppallamr@cisco.com>
Wed, 18 Sep 2013 03:15:08 +0000 (20:15 -0700)
more lenient when it sees extra attributes in the request payload. Add custom exception
mapper for handling jackson json exceptions during deserialization

Change-Id: Id2da67b4edfc92f1d11e41fd349814c2de05079c
Signed-off-by: Prasanth Pallamreddy <ppallamr@cisco.com>
opendaylight/northbound/commons/pom.xml
opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/JacksonJsonProcessingExceptionMapper.java [new file with mode: 0644]
opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/NorthboundApplication.java
opendaylight/northbound/integrationtest/src/test/java/org/opendaylight/controller/northbound/integrationtest/NorthboundIT.java

index 692b17a9a6c5409bdb38d1e729b504a5177a4f27..972bece91fd2ce9e0400bf2500397e76a2d19691 100644 (file)
@@ -48,7 +48,9 @@
               org.osgi.service.packageadmin,
               org.osgi.util.tracker,
               javax.servlet.http,
+              org.codehaus.jackson,
               org.codehaus.jackson.jaxrs,
+              org.codehaus.jackson.map,
               org.slf4j
             </Import-Package>
           </instructions>
diff --git a/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/JacksonJsonProcessingExceptionMapper.java b/opendaylight/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/JacksonJsonProcessingExceptionMapper.java
new file mode 100644 (file)
index 0000000..ca0d1b7
--- /dev/null
@@ -0,0 +1,37 @@
+/**
+ * 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;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.core.GenericEntity;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+import org.codehaus.jackson.JsonProcessingException;
+
+/**
+ * A custom exception mapper for handling Jackson JsonProcessingException types
+ */
+@Provider
+@Consumes({MediaType.APPLICATION_JSON, "text/json"})
+public class JacksonJsonProcessingExceptionMapper
+    implements ExceptionMapper<JsonProcessingException>
+{
+
+    @Override
+    public Response toResponse(JsonProcessingException exception) {
+        GenericEntity<String> entity =
+                new GenericEntity<String>(exception.getMessage()) {};
+        return Response.status(Response.Status.BAD_REQUEST).entity(entity).build();
+    }
+}
+
index 1d3919f4ea396739af2dfb89a953a24cf59dd5de..5b8219126b8abbb7c82124b22f502505c2791e8c 100644 (file)
@@ -20,6 +20,7 @@ import javax.xml.bind.JAXBException;
 import javax.xml.bind.annotation.XmlRootElement;
 
 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
+import org.codehaus.jackson.map.DeserializationConfig;
 import org.opendaylight.controller.northbound.bundlescanner.IBundleScanService;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -54,7 +55,8 @@ public class NorthboundApplication extends Application {
             }
 
         } );
-        singletons.add(new JacksonJaxbJsonProvider());
+        singletons.add(getJsonProvider());
+        singletons.add(new JacksonJsonProcessingExceptionMapper());
         return singletons;
     }
 
@@ -65,6 +67,13 @@ public class NorthboundApplication extends Application {
         return result;
     }
 
+    private static final JacksonJaxbJsonProvider getJsonProvider() {
+        JacksonJaxbJsonProvider jsonProvider = new JacksonJaxbJsonProvider();
+        jsonProvider.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
+                false);
+        return jsonProvider;
+    }
+
     private BundleContext getBundleContext() {
         ClassLoader tlcl = Thread.currentThread().getContextClassLoader();
         Bundle bundle = null;
index 4404951135391f632a968aae8345e472e8f413c0..e7ca7f57822a438e4243a29115fd38f854dce5ed 100644 (file)
@@ -428,6 +428,11 @@ public class NorthboundIT {
         // Test GET deleted subnet1
         result = getJsonResult(baseURL + "default/subnet/" + name1);
         Assert.assertEquals(404, httpResponseCode.intValue());
+
+        // TEST PUT bad subnet, expect 400, validate JSON exception mapper
+        JSONObject joBad = new JSONObject().put("foo", "bar");
+        result = getJsonResult(baseURL + "default/subnet/foo", "PUT", joBad.toString());
+        Assert.assertEquals(400, httpResponseCode.intValue());
   }
 
     @Test