Ray Milkey

ONOS-887 - Return a 404 status when a URL specifies a bad resource

Change-Id: I145ae65076d54ec50f7627a50307c975df8f2c0a
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.rest.exceptions;
17 +
18 +import javax.ws.rs.core.Response;
19 +
20 +import com.sun.jersey.api.NotFoundException;
21 +
22 +/**
23 + * Mapper for api not found exceptions to the NOT_FOUND response code.
24 + */
25 +public class NotFoundMapper extends AbstractMapper<NotFoundException> {
26 +
27 + @Override
28 + protected Response.Status responseStatus() {
29 + return Response.Status.NOT_FOUND;
30 + }
31 +
32 +}
...@@ -18,7 +18,7 @@ package org.onosproject.rest.exceptions; ...@@ -18,7 +18,7 @@ package org.onosproject.rest.exceptions;
18 import javax.ws.rs.core.Response; 18 import javax.ws.rs.core.Response;
19 19
20 /** 20 /**
21 - * Mapper for service not found exceptions to the NOT_FOUND response code. 21 + * Mapper for service not found exceptions to the INTERNAL_SERVER_ERROR response code.
22 */ 22 */
23 public class ServerErrorMapper extends AbstractMapper<RuntimeException> { 23 public class ServerErrorMapper extends AbstractMapper<RuntimeException> {
24 @Override 24 @Override
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
32 <param-value> 32 <param-value>
33 org.onosproject.rest.exceptions.EntityNotFoundMapper, 33 org.onosproject.rest.exceptions.EntityNotFoundMapper,
34 org.onosproject.rest.exceptions.ServiceNotFoundMapper, 34 org.onosproject.rest.exceptions.ServiceNotFoundMapper,
35 + org.onosproject.rest.exceptions.NotFoundMapper,
35 org.onosproject.rest.exceptions.ServerErrorMapper, 36 org.onosproject.rest.exceptions.ServerErrorMapper,
36 org.onosproject.rest.JsonBodyWriter, 37 org.onosproject.rest.JsonBodyWriter,
37 38
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.rest;
17 +
18 +import org.junit.Test;
19 +
20 +import com.sun.jersey.api.client.UniformInterfaceException;
21 +import com.sun.jersey.api.client.WebResource;
22 +
23 +import static org.hamcrest.Matchers.containsString;
24 +import static org.junit.Assert.assertThat;
25 +import static org.junit.Assert.fail;
26 +
27 +/**
28 + * Unit tests for bad REST requests.
29 + */
30 +public class BadRequestTest extends ResourceTest {
31 + @Test
32 + public void badUrl() {
33 + WebResource rs = resource();
34 + try {
35 + rs.path("ThisIsABadURL").get(String.class);
36 + fail("Fetch of non-existent URL did not throw an exception");
37 + } catch (UniformInterfaceException ex) {
38 + assertThat(ex.getMessage(),
39 + containsString("returned a response status of 404 Not Found"));
40 + }
41 + }
42 +}
...@@ -122,7 +122,7 @@ ...@@ -122,7 +122,7 @@
122 org.slf4j, 122 org.slf4j,
123 org.osgi.framework, 123 org.osgi.framework,
124 javax.ws.rs,javax.ws.rs.core,javax.ws.rs.ext, 124 javax.ws.rs,javax.ws.rs.core,javax.ws.rs.ext,
125 - com.sun.jersey.api.core, 125 + com.sun.jersey.api,
126 com.sun.jersey.spi.container.servlet, 126 com.sun.jersey.spi.container.servlet,
127 com.sun.jersey.server.impl.container.servlet, 127 com.sun.jersey.server.impl.container.servlet,
128 com.fasterxml.jackson.databind, 128 com.fasterxml.jackson.databind,
......