Extract MethodNotAllowed as an API exception
This allows views to throw with a proper HTTP error code if a specific method is not a valid call on a resource. Change-Id: Iad15024edd66ab3908c5b4e7319b8956b935b8a2
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
// Copyright (C) 2012 The Android Open Source Project
|
||||||
|
//
|
||||||
|
// 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 com.google.gerrit.extensions.restapi;
|
||||||
|
|
||||||
|
/** Method is not acceptable on the resource (HTTP 405 Method Not Allowed). */
|
||||||
|
public class MethodNotAllowedException extends RestApiException {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
}
|
||||||
@@ -41,6 +41,7 @@ import com.google.gerrit.extensions.restapi.AuthException;
|
|||||||
import com.google.gerrit.extensions.restapi.BadRequestException;
|
import com.google.gerrit.extensions.restapi.BadRequestException;
|
||||||
import com.google.gerrit.extensions.restapi.BinaryResult;
|
import com.google.gerrit.extensions.restapi.BinaryResult;
|
||||||
import com.google.gerrit.extensions.restapi.DefaultInput;
|
import com.google.gerrit.extensions.restapi.DefaultInput;
|
||||||
|
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
|
||||||
import com.google.gerrit.extensions.restapi.PutInput;
|
import com.google.gerrit.extensions.restapi.PutInput;
|
||||||
import com.google.gerrit.extensions.restapi.ResourceConflictException;
|
import com.google.gerrit.extensions.restapi.ResourceConflictException;
|
||||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||||
@@ -247,7 +248,7 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
replyError(res, SC_FORBIDDEN, e.getMessage());
|
replyError(res, SC_FORBIDDEN, e.getMessage());
|
||||||
} catch (BadRequestException e) {
|
} catch (BadRequestException e) {
|
||||||
replyError(res, SC_BAD_REQUEST, e.getMessage());
|
replyError(res, SC_BAD_REQUEST, e.getMessage());
|
||||||
} catch (InvalidMethodException e) {
|
} catch (MethodNotAllowedException e) {
|
||||||
replyError(res, SC_METHOD_NOT_ALLOWED, "Method not allowed");
|
replyError(res, SC_METHOD_NOT_ALLOWED, "Method not allowed");
|
||||||
} catch (ResourceConflictException e) {
|
} catch (ResourceConflictException e) {
|
||||||
replyError(res, SC_CONFLICT, e.getMessage());
|
replyError(res, SC_CONFLICT, e.getMessage());
|
||||||
@@ -265,7 +266,7 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
private Object parseRequest(HttpServletRequest req, Class<Object> type)
|
private Object parseRequest(HttpServletRequest req, Class<Object> type)
|
||||||
throws IOException, BadRequestException, SecurityException,
|
throws IOException, BadRequestException, SecurityException,
|
||||||
IllegalArgumentException, NoSuchMethodException, IllegalAccessException,
|
IllegalArgumentException, NoSuchMethodException, IllegalAccessException,
|
||||||
InstantiationException, InvocationTargetException, InvalidMethodException {
|
InstantiationException, InvocationTargetException, MethodNotAllowedException {
|
||||||
if (isType(JSON_TYPE, req.getContentType())) {
|
if (isType(JSON_TYPE, req.getContentType())) {
|
||||||
BufferedReader br = req.getReader();
|
BufferedReader br = req.getReader();
|
||||||
try {
|
try {
|
||||||
@@ -326,7 +327,7 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
private Object parsePutInput(final HttpServletRequest req, Class<Object> type)
|
private Object parsePutInput(final HttpServletRequest req, Class<Object> type)
|
||||||
throws SecurityException, NoSuchMethodException,
|
throws SecurityException, NoSuchMethodException,
|
||||||
IllegalArgumentException, InstantiationException, IllegalAccessException,
|
IllegalArgumentException, InstantiationException, IllegalAccessException,
|
||||||
InvocationTargetException, InvalidMethodException {
|
InvocationTargetException, MethodNotAllowedException {
|
||||||
Object obj = createInstance(type);
|
Object obj = createInstance(type);
|
||||||
for (Field f : type.getDeclaredFields()) {
|
for (Field f : type.getDeclaredFields()) {
|
||||||
if (f.getType() == PutInput.class) {
|
if (f.getType() == PutInput.class) {
|
||||||
@@ -350,7 +351,7 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new InvalidMethodException();
|
throw new MethodNotAllowedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object parseString(String value, Class<Object> type)
|
private Object parseString(String value, Class<Object> type)
|
||||||
@@ -513,7 +514,7 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
private RestView<RestResource> view(
|
private RestView<RestResource> view(
|
||||||
RestCollection<RestResource, RestResource> rc,
|
RestCollection<RestResource, RestResource> rc,
|
||||||
String method, List<String> path) throws ResourceNotFoundException,
|
String method, List<String> path) throws ResourceNotFoundException,
|
||||||
InvalidMethodException, AmbiguousViewException {
|
MethodNotAllowedException, AmbiguousViewException {
|
||||||
DynamicMap<RestView<RestResource>> views = rc.views();
|
DynamicMap<RestView<RestResource>> views = rc.views();
|
||||||
final String projection = path.isEmpty() ? "/" : path.remove(0);
|
final String projection = path.isEmpty() ? "/" : path.remove(0);
|
||||||
if (!path.isEmpty()) {
|
if (!path.isEmpty()) {
|
||||||
@@ -680,10 +681,6 @@ public class RestApiServlet extends HttpServlet {
|
|||||||
return new TemporaryBuffer.Heap(max);
|
return new TemporaryBuffer.Heap(max);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
|
||||||
private static class InvalidMethodException extends Exception {
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
private static class AmbiguousViewException extends Exception {
|
private static class AmbiguousViewException extends Exception {
|
||||||
AmbiguousViewException(String message) {
|
AmbiguousViewException(String message) {
|
||||||
|
|||||||
Reference in New Issue
Block a user