Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.collect.ImmutableList;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Map.Entry;

Expand Down Expand Up @@ -61,19 +62,33 @@ public void init(ServletConfig config) throws ServletException {

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
if ("OPTIONS".equals(request.getMethod())) {
String method = getRequestMethod(request);
if ("OPTIONS".equals(method)) {
corsHandler.handle(request, response);
} else {
String path = Strings.stripSlash(
request.getRequestURI().substring(request.getServletPath().length()));
EndpointsContext context = new EndpointsContext(request.getMethod(), path, request, response);
if (!dispatcher.dispatch(request.getMethod(), path, context)) {
EndpointsContext context = new EndpointsContext(method, path, request, response);
if (!dispatcher.dispatch(method, path, context)) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
response.getWriter().append("Not Found");
}
}
}

private String getRequestMethod(HttpServletRequest request) {
Enumeration headerNames = request.getHeaderNames();
String methodOverride = null;
while (headerNames.hasMoreElements()) {
String headerName = (String) headerNames.nextElement();
if (headerName.toLowerCase().equals("x-http-method-override")) {
methodOverride = request.getHeader(headerName);
break;
}
}
return methodOverride != null ? methodOverride.toUpperCase() : request.getMethod();
}

private PathDispatcher<EndpointsContext> createDispatcher() {
PathDispatcher.Builder<EndpointsContext> builder = PathDispatcher.builder();
List<EndpointNode> endpoints = systemService.getEndpoints();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ public void echo() throws IOException {
assertThat(actual.get("x").asInt()).isEqualTo(1);
}

@Test
public void methodOverride() throws IOException {
req.setRequestURI("/_ah/api/test/v2/increment");
req.setMethod("POST");
req.addHeader("X-HTTP-Method-Override", "PATCH");
req.setParameter("x", "1");

servlet.service(req, resp);

assertThat(resp.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
ObjectMapper mapper = ObjectMapperUtil.createStandardObjectMapper();
ObjectNode actual = mapper.readValue(resp.getContentAsString(), ObjectNode.class);
assertThat(actual.size()).isEqualTo(1);
assertThat(actual.get("x").asInt()).isEqualTo(2);
}

@Test
public void proxy() throws IOException {
req.setRequestURI("/_ah/api/static/proxy.html");
Expand Down Expand Up @@ -157,5 +173,11 @@ public void empty() {}
public TestResource echo(TestResource r) {
return r;
}

@ApiMethod(httpMethod = "PATCH")
public TestResource increment(TestResource r) {
r.x = r.x + 1;
return r;
}
}
}