Merge "Support raw input for POST requests"

This commit is contained in:
Shawn Pearce
2013-06-04 09:30:28 +00:00
committed by Gerrit Code Review
3 changed files with 13 additions and 13 deletions

View File

@@ -17,9 +17,8 @@ package com.google.gerrit.extensions.restapi;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
/** Raw data stream supplied by the body of a PUT or POST. */
/** Raw data stream supplied by the body of a PUT. */ public interface RawInput {
public interface PutInput {
String getContentType(); String getContentType();
long getContentLength(); long getContentLength();
InputStream getInputStream() throws IOException; InputStream getInputStream() throws IOException;

View File

@@ -56,7 +56,7 @@ import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.restapi.IdString; import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException; import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.PreconditionFailedException; import com.google.gerrit.extensions.restapi.PreconditionFailedException;
import com.google.gerrit.extensions.restapi.PutInput; import com.google.gerrit.extensions.restapi.RawInput;
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;
import com.google.gerrit.extensions.restapi.Response; import com.google.gerrit.extensions.restapi.Response;
@@ -439,8 +439,9 @@ public class RestApiServlet extends HttpServlet {
} finally { } finally {
br.close(); br.close();
} }
} else if ("PUT".equals(req.getMethod()) && acceptsPutInput(type)) { } else if (("PUT".equals(req.getMethod()) || "POST".equals(req.getMethod()))
return parsePutInput(req, type); && acceptsRawInput(type)) {
return parseRawInput(req, type);
} else if ("DELETE".equals(req.getMethod()) && hasNoBody(req)) { } else if ("DELETE".equals(req.getMethod()) && hasNoBody(req)) {
return null; return null;
} else if (hasNoBody(req)) { } else if (hasNoBody(req)) {
@@ -476,10 +477,10 @@ public class RestApiServlet extends HttpServlet {
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private static boolean acceptsPutInput(Type type) { private static boolean acceptsRawInput(Type type) {
if (type instanceof Class) { if (type instanceof Class) {
for (Field f : ((Class) type).getDeclaredFields()) { for (Field f : ((Class) type).getDeclaredFields()) {
if (f.getType() == PutInput.class) { if (f.getType() == RawInput.class) {
return true; return true;
} }
} }
@@ -487,15 +488,15 @@ public class RestApiServlet extends HttpServlet {
return false; return false;
} }
private Object parsePutInput(final HttpServletRequest req, Type type) private Object parseRawInput(final HttpServletRequest req, Type type)
throws SecurityException, NoSuchMethodException, throws SecurityException, NoSuchMethodException,
IllegalArgumentException, InstantiationException, IllegalAccessException, IllegalArgumentException, InstantiationException, IllegalAccessException,
InvocationTargetException, MethodNotAllowedException { InvocationTargetException, MethodNotAllowedException {
Object obj = createInstance(type); Object obj = createInstance(type);
for (Field f : obj.getClass().getDeclaredFields()) { for (Field f : obj.getClass().getDeclaredFields()) {
if (f.getType() == PutInput.class) { if (f.getType() == RawInput.class) {
f.setAccessible(true); f.setAccessible(true);
f.set(obj, new PutInput() { f.set(obj, new RawInput() {
@Override @Override
public String getContentType() { public String getContentType() {
return req.getContentType(); return req.getContentType();

View File

@@ -18,7 +18,7 @@ import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.extensions.annotations.RequiresCapability; import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.extensions.restapi.BadRequestException; import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.DefaultInput; import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.restapi.PutInput; import com.google.gerrit.extensions.restapi.RawInput;
import com.google.gerrit.extensions.restapi.Response; import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestModifyView; import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.restapi.TopLevelResource; import com.google.gerrit.extensions.restapi.TopLevelResource;
@@ -38,7 +38,7 @@ class InstallPlugin implements RestModifyView<TopLevelResource, Input> {
static class Input { static class Input {
@DefaultInput @DefaultInput
String url; String url;
PutInput raw; RawInput raw;
} }
private final PluginLoader loader; private final PluginLoader loader;