Restructuring the form of REST API deployment to provide
for security of app's REST APIs and for consistency of
exception mappers and JSON writer.

Change-Id: Id318372bf62f82ed974355c05e7fe64e0fbfc0c5
diff --git a/web/api/src/main/java/org/onosproject/rest/exceptions/AbstractMapper.java b/web/api/src/main/java/org/onosproject/rest/exceptions/AbstractMapper.java
deleted file mode 100644
index 0d9d94d..0000000
--- a/web/api/src/main/java/org/onosproject/rest/exceptions/AbstractMapper.java
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Copyright 2014-2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.rest.exceptions;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-
-import javax.ws.rs.core.Response;
-import javax.ws.rs.ext.ExceptionMapper;
-
-import static com.google.common.base.Strings.isNullOrEmpty;
-
-/**
- * Base exception mapper implementation.
- */
-public abstract class AbstractMapper<E extends Throwable> implements ExceptionMapper<E> {
-
-    /**
-     * Holds the current exception for use in subclasses.
-     */
-    protected Throwable error;
-
-    /**
-     * Returns the response status to be given when the exception occurs.
-     *
-     * @return response status
-     */
-    protected abstract Response.Status responseStatus();
-
-    @Override
-    public Response toResponse(E exception) {
-        error = exception;
-        return response(responseStatus(), exception).build();
-    }
-
-    /**
-     * Produces a response builder primed with the supplied status code
-     * and JSON entity with the status code and exception message.
-     *
-     * @param status    response status
-     * @param exception exception to encode
-     * @return response builder
-     */
-    protected Response.ResponseBuilder response(Response.Status status,
-                                                Throwable exception) {
-        error = exception;
-        ObjectMapper mapper = new ObjectMapper();
-        String message = messageFrom(exception);
-        ObjectNode result = mapper.createObjectNode()
-                .put("code", status.getStatusCode())
-                .put("message", message);
-        return Response.status(status).entity(result.toString());
-    }
-
-    /**
-     * Produces a response message from the supplied exception. Either it will
-     * use the exception message, if there is one, or it will use the top
-     * stack-frame message.
-     *
-     * @param exception exception from which to produce a message
-     * @return response message
-     */
-    protected String messageFrom(Throwable exception) {
-        if (isNullOrEmpty(exception.getMessage())) {
-            StackTraceElement[] trace = exception.getStackTrace();
-            return trace.length == 0 ? "Unknown error" : trace[0].toString();
-        }
-        return exception.getMessage();
-    }
-
-}
diff --git a/web/api/src/main/java/org/onosproject/rest/exceptions/BadRequestMapper.java b/web/api/src/main/java/org/onosproject/rest/exceptions/BadRequestMapper.java
deleted file mode 100644
index 89b1368..0000000
--- a/web/api/src/main/java/org/onosproject/rest/exceptions/BadRequestMapper.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright 2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.rest.exceptions;
-
-import javax.ws.rs.core.Response;
-import javax.ws.rs.ext.Provider;
-
-import java.io.IOException;
-
-/**
- * Mapper for IO exceptions to the BAD_REQUEST response code.
- */
-@Provider
-public class BadRequestMapper extends AbstractMapper<IOException> {
-    @Override
-    protected Response.Status responseStatus() {
-        return Response.Status.BAD_REQUEST;
-    }
-}
diff --git a/web/api/src/main/java/org/onosproject/rest/exceptions/EntityNotFoundMapper.java b/web/api/src/main/java/org/onosproject/rest/exceptions/EntityNotFoundMapper.java
deleted file mode 100644
index 9e42a56..0000000
--- a/web/api/src/main/java/org/onosproject/rest/exceptions/EntityNotFoundMapper.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright 2014-2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.rest.exceptions;
-
-import org.onlab.util.ItemNotFoundException;
-
-import javax.ws.rs.core.Response;
-import javax.ws.rs.ext.Provider;
-
-/**
- * Mapper for service not found exceptions to the NOT_FOUND response code.
- */
-@Provider
-public class EntityNotFoundMapper extends AbstractMapper<ItemNotFoundException> {
-    @Override
-    protected Response.Status responseStatus() {
-        return Response.Status.NOT_FOUND;
-    }
-}
diff --git a/web/api/src/main/java/org/onosproject/rest/exceptions/IllegalArgumentExceptionMapper.java b/web/api/src/main/java/org/onosproject/rest/exceptions/IllegalArgumentExceptionMapper.java
deleted file mode 100644
index 2d7a1ea..0000000
--- a/web/api/src/main/java/org/onosproject/rest/exceptions/IllegalArgumentExceptionMapper.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright 2014-2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.rest.exceptions;
-
-import javax.ws.rs.core.Response;
-import javax.ws.rs.ext.Provider;
-
-/**
- * Mapper for illegal argument exceptions to the BAD_REQUEST response code.
- */
-@Provider
-public class IllegalArgumentExceptionMapper extends AbstractMapper<IllegalArgumentException> {
-    @Override
-    protected Response.Status responseStatus() {
-        return Response.Status.BAD_REQUEST;
-    }
-}
-
diff --git a/web/api/src/main/java/org/onosproject/rest/exceptions/IllegalStateExceptionMapper.java b/web/api/src/main/java/org/onosproject/rest/exceptions/IllegalStateExceptionMapper.java
deleted file mode 100644
index a9f977c..0000000
--- a/web/api/src/main/java/org/onosproject/rest/exceptions/IllegalStateExceptionMapper.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright 2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.rest.exceptions;
-
-import javax.ws.rs.core.Response;
-import javax.ws.rs.ext.Provider;
-
-/**
- * Mapper for illegal state exceptions to the BAD_REQUEST response code.
- */
-@Provider
-public class IllegalStateExceptionMapper extends AbstractMapper<IllegalStateException> {
-    @Override
-    protected Response.Status responseStatus() {
-        return Response.Status.CONFLICT;
-    }
-}
-
diff --git a/web/api/src/main/java/org/onosproject/rest/exceptions/NotFoundMapper.java b/web/api/src/main/java/org/onosproject/rest/exceptions/NotFoundMapper.java
deleted file mode 100644
index 2bf3614..0000000
--- a/web/api/src/main/java/org/onosproject/rest/exceptions/NotFoundMapper.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright 2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.rest.exceptions;
-
-import javax.ws.rs.core.Response;
-import javax.ws.rs.ext.Provider;
-
-import com.sun.jersey.api.NotFoundException;
-
-/**
- * Mapper for api not found exceptions to the NOT_FOUND response code.
- */
-@Provider
-public class NotFoundMapper extends AbstractMapper<NotFoundException> {
-
-    @Override
-    protected Response.Status responseStatus() {
-        return Response.Status.NOT_FOUND;
-    }
-
-}
diff --git a/web/api/src/main/java/org/onosproject/rest/exceptions/ServerErrorMapper.java b/web/api/src/main/java/org/onosproject/rest/exceptions/ServerErrorMapper.java
deleted file mode 100644
index 778750e..0000000
--- a/web/api/src/main/java/org/onosproject/rest/exceptions/ServerErrorMapper.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright 2014-2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.rest.exceptions;
-
-import javax.ws.rs.core.Response;
-import javax.ws.rs.ext.Provider;
-
-import org.slf4j.Logger;
-
-import static org.slf4j.LoggerFactory.getLogger;
-
-/**
- * Mapper for service not found exceptions to the INTERNAL_SERVER_ERROR response code.
- */
-@Provider
-public class ServerErrorMapper extends AbstractMapper<RuntimeException> {
-    private static final Logger log = getLogger(ServerErrorMapper.class);
-    @Override
-    protected Response.Status responseStatus() {
-        log.warn("Unhandled REST exception", error);
-        return Response.Status.INTERNAL_SERVER_ERROR;
-    }
-}
diff --git a/web/api/src/main/java/org/onosproject/rest/exceptions/ServiceNotFoundMapper.java b/web/api/src/main/java/org/onosproject/rest/exceptions/ServiceNotFoundMapper.java
deleted file mode 100644
index 69e5508..0000000
--- a/web/api/src/main/java/org/onosproject/rest/exceptions/ServiceNotFoundMapper.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright 2014-2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.rest.exceptions;
-
-import org.onlab.osgi.ServiceNotFoundException;
-
-import javax.ws.rs.core.Response;
-import javax.ws.rs.ext.Provider;
-
-/**
- * Mapper for service not found exceptions to the SERVICE_UNAVAILABLE response code.
- */
-@Provider
-public class ServiceNotFoundMapper extends AbstractMapper<ServiceNotFoundException> {
-    @Override
-    protected Response.Status responseStatus() {
-        return Response.Status.SERVICE_UNAVAILABLE;
-    }
-}
diff --git a/web/api/src/main/java/org/onosproject/rest/exceptions/WebApplicationExceptionMapper.java b/web/api/src/main/java/org/onosproject/rest/exceptions/WebApplicationExceptionMapper.java
deleted file mode 100644
index 86d8434..0000000
--- a/web/api/src/main/java/org/onosproject/rest/exceptions/WebApplicationExceptionMapper.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright 2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.rest.exceptions;
-
-import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.ext.Provider;
-
-/**
- * Exception mapper for WebApplicationExceptions.
- */
-@Provider
-public class WebApplicationExceptionMapper extends AbstractMapper<WebApplicationException> {
-
-    /**
-     * Extracts and returns the response from a WebApplicationException.
-     *
-     * @param e WebApplicationException that was thrown
-     * @return precomputed Response from the exception
-     */
-    @Override
-    public Response toResponse(WebApplicationException e) {
-        return e.getResponse();
-    }
-
-    @Override
-    public Response.Status responseStatus() {
-        // This should never be called because this class overrides toResponse()
-        throw new UnsupportedOperationException(
-                "responseStatus() for a WebApplicationException should never be called");
-    }
-}
diff --git a/web/api/src/main/java/org/onosproject/rest/exceptions/package-info.java b/web/api/src/main/java/org/onosproject/rest/exceptions/package-info.java
deleted file mode 100644
index 6b581bc..0000000
--- a/web/api/src/main/java/org/onosproject/rest/exceptions/package-info.java
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright 2014 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * Various exception mappers to map errors to proper response status codes.
- */
-package org.onosproject.rest.exceptions;
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java b/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
new file mode 100644
index 0000000..44ad52a
--- /dev/null
+++ b/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.rest.resources;
+
+import org.onlab.rest.AbstractWebApplication;
+
+import java.util.Set;
+
+/**
+ * Core REST APIs web application.
+ */
+public class CoreWebApplication extends AbstractWebApplication {
+
+    @Override
+    public Set<Class<?>> getClasses() {
+        return getClasses(ApiDocResource.class,
+                          ApplicationsWebResource.class,
+                          ComponentConfigWebResource.class,
+                          NetworkConfigWebResource.class,
+                          ClusterWebResource.class,
+                          DevicesWebResource.class,
+                          LinksWebResource.class,
+                          HostsWebResource.class,
+                          IntentsWebResource.class,
+                          FlowsWebResource.class,
+                          TopologyWebResource.class,
+                          ConfigWebResource.class,
+                          PathsWebResource.class,
+                          StatisticsWebResource.class
+        );
+    }
+}