Ray Milkey
Committed by Gerrit Code Review

Fix for ONOS-1595 - return proper status (405) when using an unsupported method

Change-Id: I66fe2d4783364d5a5b8cccba15115c818ed3a3b3
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.WebApplicationException;
19 +import javax.ws.rs.core.Response;
20 +
21 +/**
22 + * Exception mapper for WebApplicationExceptions.
23 + */
24 +public class WebApplicationExceptionMapper extends AbstractMapper<WebApplicationException> {
25 +
26 + /**
27 + * Extracts and returns the response from a WebApplicationException.
28 + *
29 + * @param e WebApplicationException that was thrown
30 + * @return precomputed Response from the exception
31 + */
32 + @Override
33 + public Response toResponse(WebApplicationException e) {
34 + return e.getResponse();
35 + }
36 +
37 + @Override
38 + public Response.Status responseStatus() {
39 + // This should never be called because this class overrides toResponse()
40 + throw new UnsupportedOperationException(
41 + "responseStatus() for a WebApplicationException should never be called");
42 + }
43 +}
...@@ -60,6 +60,7 @@ ...@@ -60,6 +60,7 @@
60 org.onosproject.rest.exceptions.NotFoundMapper, 60 org.onosproject.rest.exceptions.NotFoundMapper,
61 org.onosproject.rest.exceptions.ServerErrorMapper, 61 org.onosproject.rest.exceptions.ServerErrorMapper,
62 org.onosproject.rest.exceptions.BadRequestMapper, 62 org.onosproject.rest.exceptions.BadRequestMapper,
63 + org.onosproject.rest.exceptions.WebApplicationExceptionMapper,
63 org.onosproject.rest.JsonBodyWriter, 64 org.onosproject.rest.JsonBodyWriter,
64 65
65 org.onosproject.rest.ApplicationsWebResource, 66 org.onosproject.rest.ApplicationsWebResource,
......
...@@ -28,6 +28,10 @@ import static org.junit.Assert.fail; ...@@ -28,6 +28,10 @@ import static org.junit.Assert.fail;
28 * Unit tests for bad REST requests. 28 * Unit tests for bad REST requests.
29 */ 29 */
30 public class BadRequestTest extends ResourceTest { 30 public class BadRequestTest extends ResourceTest {
31 +
32 + /**
33 + * Tests the response for an invalid URL.
34 + */
31 @Test 35 @Test
32 public void badUrl() { 36 public void badUrl() {
33 WebResource rs = resource(); 37 WebResource rs = resource();
...@@ -39,4 +43,19 @@ public class BadRequestTest extends ResourceTest { ...@@ -39,4 +43,19 @@ public class BadRequestTest extends ResourceTest {
39 containsString("returned a response status of 404 Not Found")); 43 containsString("returned a response status of 404 Not Found"));
40 } 44 }
41 } 45 }
46 +
47 + /**
48 + * Tests the response for a request with a bad method.
49 + */
50 + @Test
51 + public void badMethod() {
52 + WebResource rs = resource();
53 + try {
54 + rs.path("hosts").delete();
55 + fail("Fetch of non-existent URL did not throw an exception");
56 + } catch (UniformInterfaceException ex) {
57 + assertThat(ex.getMessage(),
58 + containsString("returned a response status of 405 Method Not Allowed"));
59 + }
60 + }
42 } 61 }
......