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 @@ -27,12 +27,14 @@
public class EndpointsContext extends DispatcherContext {
private final HttpServletRequest request;
private final HttpServletResponse response;
private final boolean prettyPrint;

public EndpointsContext(String httpMethod, String path, HttpServletRequest request,
HttpServletResponse response) {
HttpServletResponse response, boolean prettyPrint) {
super(httpMethod, path);
this.request = Preconditions.checkNotNull(request, "request");
this.response = Preconditions.checkNotNull(response, "response");
this.prettyPrint = prettyPrint;
}

public HttpServletRequest getRequest() {
Expand All @@ -42,4 +44,8 @@ public HttpServletRequest getRequest() {
public HttpServletResponse getResponse() {
return response;
}

public boolean isPrettyPrintEnabled() {
return prettyPrint;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public void service(HttpServletRequest request, HttpServletResponse response) th
} else {
String path = Strings.stripSlash(
request.getRequestURI().substring(request.getServletPath().length()));
EndpointsContext context = new EndpointsContext(method, path, request, response);
EndpointsContext context = new EndpointsContext(method, path, request, response,
initParameters.isPrettyPrintEnabled());
if (!dispatcher.dispatch(method, path, context)) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
response.getWriter().append("Not Found");
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.api.server.spi.config.model;

import com.google.api.server.spi.EndpointsContext;
import com.google.common.collect.ImmutableSet;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -45,8 +46,12 @@ public static boolean isStandardParamName(String paramName) {
return STANDARD_PARAM_NAMES.contains(paramName);
}

public static boolean shouldPrettyPrint(HttpServletRequest request) {
public static boolean shouldPrettyPrint(EndpointsContext context) {
HttpServletRequest request = context.getRequest();
String prettyPrintStr = request.getParameter("prettyPrint");
return prettyPrintStr == null || "true".equals(prettyPrintStr.toLowerCase());
if (prettyPrintStr == null) {
return context.isPrettyPrintEnabled();
}
return "true".equals(prettyPrintStr.toLowerCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class DispatcherContext {
private final String httpMethod;
private final String path;
private ImmutableMap<String, String> rawPathParameters;
private ImmutableMap<String, String> rawPathParameters = ImmutableMap.of();

public DispatcherContext(String httpMethod, String path) {
this.httpMethod = Preconditions.checkNotNull(httpMethod, "httpMethod").toUpperCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public DispatcherHandler<EndpointsContext> getRestHandler() {
@VisibleForTesting
protected ParamReader createRestParamReader(EndpointsContext context,
ApiSerializationConfig serializationConfig) {
return new RestServletRequestParamReader(endpointMethod, context.getRequest(),
servletContext, serializationConfig, methodConfig, context.getRawPathParameters());
return new RestServletRequestParamReader(endpointMethod, context,
servletContext, serializationConfig, methodConfig);
}

@VisibleForTesting
protected ResultWriter createResultWriter(EndpointsContext context,
ApiSerializationConfig serializationConfig) {
return new RestResponseResultWriter(context.getResponse(), serializationConfig,
StandardParameters.shouldPrettyPrint(context.getRequest()),
StandardParameters.shouldPrettyPrint(context),
initParameters.isExceptionCompatibilityEnabled());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.api.server.spi.request;

import com.google.api.server.spi.EndpointMethod;
import com.google.api.server.spi.EndpointsContext;
import com.google.api.server.spi.IoUtil;
import com.google.api.server.spi.ServiceException;
import com.google.api.server.spi.Strings;
Expand Down Expand Up @@ -59,11 +60,10 @@ public class RestServletRequestParamReader extends ServletRequestParamReader {
private final Map<String, ApiParameterConfig> parameterConfigMap;

public RestServletRequestParamReader(EndpointMethod method,
HttpServletRequest servletRequest, ServletContext servletContext,
ApiSerializationConfig serializationConfig, ApiMethodConfig methodConfig,
Map<String, String> rawPathParameters) {
super(method, servletRequest, servletContext, serializationConfig, methodConfig);
this.rawPathParameters = rawPathParameters;
EndpointsContext endpointsContext, ServletContext servletContext,
ApiSerializationConfig serializationConfig, ApiMethodConfig methodConfig) {
super(method, endpointsContext, servletContext, serializationConfig, methodConfig);
this.rawPathParameters = endpointsContext.getRawPathParameters();
ImmutableMap.Builder<String, ApiParameterConfig> builder = ImmutableMap.builder();
for (ApiParameterConfig config : methodConfig.getParameterConfigs()) {
if (config.getName() != null) {
Expand All @@ -82,6 +82,7 @@ public Object[] read() throws ServiceException {
if (method.getParameterClasses().length == 0) {
return new Object[0];
}
HttpServletRequest servletRequest = endpointsContext.getRequest();
String requestBody = IoUtil.readRequestBody(servletRequest);
logger.log(Level.FINE, "requestBody=" + requestBody);
// Unlike the Lily protocol, which essentially always requires a JSON body to exist (due to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.api.server.spi.ConfiguredObjectMapper;
import com.google.api.server.spi.EndpointMethod;
import com.google.api.server.spi.EndpointsContext;
import com.google.api.server.spi.IoUtil;
import com.google.api.server.spi.ServiceException;
import com.google.api.server.spi.auth.common.User;
Expand Down Expand Up @@ -162,7 +163,7 @@ protected Object[] deserializeParams(JsonNode node) throws IOException, IllegalA
logger.log(Level.FINE, "deserialize: App Engine User injected into param[{0}]", i);
} else if (clazz == HttpServletRequest.class) {
// HttpServletRequest type parameter requires no Named annotation (ignored if present)
params[i] = servletRequest;
params[i] = endpointsContext.getRequest();
logger.log(Level.FINE, "deserialize: HttpServletRequest injected into param[{0}]", i);
} else if (clazz == ServletContext.class) {
// ServletContext type parameter requires no Named annotation (ignored if present)
Expand Down Expand Up @@ -201,21 +202,21 @@ protected Object[] deserializeParams(JsonNode node) throws IOException, IllegalA

@VisibleForTesting
User getUser() throws ServiceException {
return Auth.from(servletRequest).authenticate();
return Auth.from(endpointsContext.getRequest()).authenticate();
}

@VisibleForTesting
com.google.appengine.api.users.User getAppEngineUser() throws ServiceException {
return Auth.from(servletRequest).authenticateAppEngineUser();
return Auth.from(endpointsContext.getRequest()).authenticateAppEngineUser();
}

private Object getStandardParamValue(JsonNode body, String paramName) {
if (!StandardParameters.isStandardParamName(paramName)) {
throw new IllegalArgumentException("paramName");
} else if (StandardParameters.USER_IP.equals(paramName)) {
return servletRequest.getRemoteAddr();
return endpointsContext.getRequest().getRemoteAddr();
} else if (StandardParameters.PRETTY_PRINT.equals(paramName)) {
return StandardParameters.shouldPrettyPrint(servletRequest);
return StandardParameters.shouldPrettyPrint(endpointsContext);
}
JsonNode value = body.get(paramName);
if (value == null && StandardParameters.ALT.equals(paramName)) {
Expand Down Expand Up @@ -288,21 +289,21 @@ public Blob deserialize(JsonParser jsonParser, DeserializationContext context)
}
}

protected final HttpServletRequest servletRequest;
protected final EndpointsContext endpointsContext;
private final ServletContext servletContext;
protected final ObjectReader objectReader;
protected final ApiMethodConfig methodConfig;

public ServletRequestParamReader(
EndpointMethod method,
HttpServletRequest servletRequest,
EndpointsContext endpointsContext,
ServletContext servletContext,
ApiSerializationConfig serializationConfig,
ApiMethodConfig methodConfig) {
super(method);

this.methodConfig = methodConfig;
this.servletRequest = servletRequest;
this.endpointsContext = endpointsContext;
this.servletContext = servletContext;

LinkedHashSet<SimpleModule> modules = new LinkedHashSet<>();
Expand All @@ -320,7 +321,7 @@ public Object[] read() throws ServiceException {
// Assumes input stream to be encoded in UTF-8
// TODO: Take charset from content-type as encoding
try {
String requestBody = IoUtil.readStream(servletRequest.getInputStream());
String requestBody = IoUtil.readStream(endpointsContext.getRequest().getInputStream());
logger.log(Level.FINE, "requestBody=" + requestBody);
if (requestBody == null || requestBody.trim().isEmpty()) {
return new Object[0];
Expand Down
Loading