use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.
the class ApiAnnotationConfigTest method testSetAuthLevelIfSpecified_unspecified.
@Test
public void testSetAuthLevelIfSpecified_unspecified() throws Exception {
EndpointMethod method = getResultNoParamsMethod();
annotationConfig.setAuthLevelIfSpecified(AuthLevel.UNSPECIFIED);
ApiMethodConfig methodConfig = config.getApiClassConfig().getMethods().getOrCreate(method);
assertEquals(AuthLevel.NONE, methodConfig.getAuthLevel());
annotationConfig.setAuthLevelIfSpecified(AuthLevel.REQUIRED);
assertEquals(AuthLevel.REQUIRED, config.getAuthLevel());
annotationConfig.setAuthLevelIfSpecified(AuthLevel.UNSPECIFIED);
assertEquals(AuthLevel.REQUIRED, config.getAuthLevel());
}
use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.
the class ApiAnnotationConfigTest method testSetAudiencesIfSpecified_unspecified.
@Test
public void testSetAudiencesIfSpecified_unspecified() throws Exception {
String[] unspecified = { Api.UNSPECIFIED_STRING_FOR_LIST };
EndpointMethod method = getResultNoParamsMethod();
annotationConfig.setScopesIfSpecified(unspecified);
ApiMethodConfig methodConfig = config.getApiClassConfig().getMethods().getOrCreate(method);
assertEquals(Collections.emptyList(), methodConfig.getAudiences());
String[] audiences = { "bleh", "more bleh" };
annotationConfig.setAudiencesIfSpecified(audiences);
annotationConfig.setAudiencesIfSpecified(null);
assertEquals(Arrays.asList(audiences), config.getAudiences());
annotationConfig.setAudiencesIfSpecified(audiences);
annotationConfig.setAudiencesIfSpecified(unspecified);
assertEquals(Arrays.asList(audiences), config.getAudiences());
}
use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.
the class ApiAnnotationConfigTest method testSetScopesIfSpecified_unspecified.
@Test
public void testSetScopesIfSpecified_unspecified() throws Exception {
String[] unspecified = { Api.UNSPECIFIED_STRING_FOR_LIST };
EndpointMethod method = getResultNoParamsMethod();
annotationConfig.setScopesIfSpecified(unspecified);
ApiMethodConfig methodConfig = config.getApiClassConfig().getMethods().getOrCreate(method);
assertEquals(toScopeExpression(Constant.API_EMAIL_SCOPE), methodConfig.getScopeExpression());
String[] scopes = { "bleh", "more bleh" };
annotationConfig.setScopesIfSpecified(scopes);
annotationConfig.setScopesIfSpecified(null);
assertEquals(toScopeExpression(scopes), config.getScopeExpression());
annotationConfig.setScopesIfSpecified(scopes);
annotationConfig.setScopesIfSpecified(unspecified);
assertEquals(toScopeExpression(scopes), config.getScopeExpression());
}
use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.
the class ApiAnnotationConfigTest method testSetClientIdsIfSpecified_unspecified.
@Test
public void testSetClientIdsIfSpecified_unspecified() throws Exception {
String[] unspecified = { Api.UNSPECIFIED_STRING_FOR_LIST };
EndpointMethod method = getResultNoParamsMethod();
annotationConfig.setScopesIfSpecified(unspecified);
ApiMethodConfig methodConfig = config.getApiClassConfig().getMethods().getOrCreate(method);
assertEquals(ImmutableList.of(Constant.API_EXPLORER_CLIENT_ID), methodConfig.getClientIds());
String[] clientIds = { "bleh", "more bleh" };
annotationConfig.setClientIdsIfSpecified(clientIds);
annotationConfig.setClientIdsIfSpecified(null);
assertEquals(Arrays.asList(clientIds), config.getClientIds());
annotationConfig.setClientIdsIfSpecified(clientIds);
annotationConfig.setClientIdsIfSpecified(unspecified);
assertEquals(Arrays.asList(clientIds), config.getClientIds());
}
use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.
the class RestServletRequestParamReader method read.
@Override
public Object[] read() throws ServiceException {
// TODO: Take charset from content-type as encoding
try {
EndpointMethod method = getMethod();
if (method.getParameterClasses().length == 0) {
return new Object[0];
}
HttpServletRequest servletRequest = endpointsContext.getRequest();
JsonNode node;
// this case, each part represents a named parameter instead.
if (ServletFileUpload.isMultipartContent(servletRequest)) {
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(servletRequest);
ObjectNode obj = (ObjectNode) objectReader.createObjectNode();
while (iter.hasNext()) {
FileItemStream item = iter.next();
if (item.isFormField()) {
obj.put(item.getFieldName(), IoUtil.readStream(item.openStream()));
} else {
throw new BadRequestException("unable to parse multipart form field");
}
}
node = obj;
} catch (FileUploadException e) {
throw new BadRequestException("unable to parse multipart request", e);
}
} else {
String requestBody = IoUtil.readRequestBody(servletRequest);
logger.atFine().log("requestBody=%s", requestBody);
// Unlike the Lily protocol, which essentially always requires a JSON body to exist (due to
// path and query parameters being injected into the body), bodies are optional here, so we
// create an empty body and inject named parameters to make deserialize work.
node = Strings.isEmptyOrWhitespace(requestBody) ? objectReader.createObjectNode() : objectReader.readTree(requestBody);
}
if (!node.isObject()) {
throw new BadRequestException("expected a JSON object body");
}
ObjectNode body = (ObjectNode) node;
Map<String, Class<?>> parameterMap = getParameterMap(method);
// the order of precedence is resource field > query parameter > path parameter.
for (Enumeration<?> e = servletRequest.getParameterNames(); e.hasMoreElements(); ) {
String parameterName = (String) e.nextElement();
if (!body.has(parameterName)) {
Class<?> parameterClass = parameterMap.get(parameterName);
ApiParameterConfig parameterConfig = parameterConfigMap.get(parameterName);
if (parameterClass != null && parameterConfig.isRepeated()) {
ArrayNode values = body.putArray(parameterName);
for (String value : servletRequest.getParameterValues(parameterName)) {
values.add(value);
}
} else {
body.put(parameterName, servletRequest.getParameterValues(parameterName)[0]);
}
}
}
for (Entry<String, String> entry : rawPathParameters.entrySet()) {
String parameterName = entry.getKey();
Class<?> parameterClass = parameterMap.get(parameterName);
if (parameterClass != null && !body.has(parameterName)) {
if (parameterConfigMap.get(parameterName).isRepeated()) {
ArrayNode values = body.putArray(parameterName);
for (String value : COMPOSITE_PATH_SPLITTER.split(entry.getValue())) {
values.add(value);
}
} else {
body.put(parameterName, entry.getValue());
}
}
}
for (Entry<String, ApiParameterConfig> entry : parameterConfigMap.entrySet()) {
if (!body.has(entry.getKey()) && entry.getValue().getDefaultValue() != null) {
body.put(entry.getKey(), entry.getValue().getDefaultValue());
}
}
return deserializeParams(body);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | IOException e) {
logger.atInfo().withCause(e).log("Unable to read request parameter(s)");
throw new BadRequestException(e);
}
}
Aggregations