--- /dev/null
+/**
+ * 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();
+ }
+}
+
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;
}
} );
- singletons.add(new JacksonJaxbJsonProvider());
+ singletons.add(getJsonProvider());
+ singletons.add(new JacksonJsonProcessingExceptionMapper());
return singletons;
}
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;
// 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