use of com.google.api.server.spi.config.model.ApiParameterConfig 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();
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
// 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.
JsonNode 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) {
e.printStackTrace();
throw new BadRequestException(e);
}
}
use of com.google.api.server.spi.config.model.ApiParameterConfig in project endpoints-java by cloudendpoints.
the class ApiConfigAnnotationReaderTest method testParameterAnnotations_javax.
@Test
public void testParameterAnnotations_javax() throws Exception {
@Api
class Endpoint {
@SuppressWarnings("unused")
public void method(@javax.inject.Named("foo") @javax.annotation.Nullable int foo) {
}
}
ApiConfig config = createConfig(Endpoint.class);
annotationReader.loadEndpointClass(serviceContext, Endpoint.class, config);
annotationReader.loadEndpointMethods(serviceContext, Endpoint.class, config.getApiClassConfig().getMethods());
ApiMethodConfig methodConfig = Iterables.getOnlyElement(config.getApiClassConfig().getMethods().values());
ApiParameterConfig parameterConfig = Iterables.getOnlyElement(methodConfig.getParameterConfigs());
validateParameter(parameterConfig, "foo", true, null, int.class, null, int.class);
}
use of com.google.api.server.spi.config.model.ApiParameterConfig in project endpoints-java by cloudendpoints.
the class ApiConfigAnnotationReaderTest method testParameterDescription.
@Test
public void testParameterDescription() throws Exception {
@Api
final class TestParameterDescription {
public void foo(@Description("desc") String param) {
}
}
ApiConfig config = createConfig(TestParameterDescription.class);
annotationReader.loadEndpointMethods(serviceContext, TestParameterDescription.class, config.getApiClassConfig().getMethods());
ApiMethodConfig methodConfig = Iterables.getOnlyElement(config.getApiClassConfig().getMethods().values());
ApiParameterConfig parameterConfig = Iterables.getOnlyElement(methodConfig.getParameterConfigs());
assertEquals("desc", parameterConfig.getDescription());
}
use of com.google.api.server.spi.config.model.ApiParameterConfig in project endpoints-java by cloudendpoints.
the class ApiConfigAnnotationReaderTest method genericParameterTypeTestImpl.
private <T> void genericParameterTypeTestImpl() throws Exception {
@Api
class Bar<T1> {
@SuppressWarnings("unused")
public void bar(T1 t1) {
}
}
class Foo extends Bar<T> {
}
ApiConfig config = createConfig(Foo.class);
annotationReader.loadEndpointMethods(serviceContext, Foo.class, config.getApiClassConfig().getMethods());
ApiParameterConfig parameter = config.getApiClassConfig().getMethods().get(methodToEndpointMethod(Foo.class.getSuperclass().getDeclaredMethod("bar", Object.class))).getParameterConfigs().get(0);
assertEquals(ApiParameterConfig.Classification.UNKNOWN, parameter.getClassification());
}
use of com.google.api.server.spi.config.model.ApiParameterConfig in project endpoints-java by cloudendpoints.
the class JsonConfigWriter method convertMethodRequestParameters.
private void convertMethodRequestParameters(EndpointMethod endpointMethod, ObjectNode requestNode, ObjectNode descriptorSchemasNode, ObjectNode descriptorMethodNode, ApiMethodConfig config, ApiConfig apiConfig) throws IllegalArgumentException, SecurityException, ApiConfigException {
ObjectNode parametersNode = objectMapper.createObjectNode();
Method method = endpointMethod.getMethod();
List<ApiParameterConfig> parameterConfigs = config.getParameterConfigs();
for (ApiParameterConfig parameterConfig : parameterConfigs) {
switch(parameterConfig.getClassification()) {
case INJECTED:
// Do nothing.
break;
case API_PARAMETER:
convertSimpleParameter(parameterConfig, parametersNode);
break;
case RESOURCE:
// Inserts resource in.
convertComplexParameter(parameterConfig, method, descriptorSchemasNode, descriptorMethodNode, apiConfig, parameterConfigs);
break;
case UNKNOWN:
throw new IllegalArgumentException("Unclassifiable parameter type found.");
}
}
// Set API parameter types if needed.
if (parametersNode.size() != 0) {
requestNode.set("parameters", parametersNode);
}
// Sets request body to auto-template if Lily request portion is set..
if (descriptorMethodNode.get("request") != null) {
requestNode.put("body", "autoTemplate(backendRequest)");
requestNode.put("bodyName", "resource");
} else {
requestNode.put("body", "empty");
}
}
Aggregations