Search in sources :

Example 1 with ApiParameterConfig

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);
    }
}
Also used : ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HttpServletRequest(javax.servlet.http.HttpServletRequest) EndpointMethod(com.google.api.server.spi.EndpointMethod) BadRequestException(com.google.api.server.spi.response.BadRequestException) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 2 with ApiParameterConfig

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);
}
Also used : ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) SimpleLevelOverridingApi(com.google.api.server.spi.testing.SimpleLevelOverridingApi) SimpleLevelOverridingInheritedApi(com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi) Api(com.google.api.server.spi.config.Api) Test(org.junit.Test)

Example 3 with ApiParameterConfig

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());
}
Also used : ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) Description(com.google.api.server.spi.config.Description) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) SimpleLevelOverridingApi(com.google.api.server.spi.testing.SimpleLevelOverridingApi) SimpleLevelOverridingInheritedApi(com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi) Api(com.google.api.server.spi.config.Api) Test(org.junit.Test)

Example 4 with ApiParameterConfig

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());
}
Also used : ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) Foo(com.google.api.server.spi.testing.Foo) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) SimpleLevelOverridingApi(com.google.api.server.spi.testing.SimpleLevelOverridingApi) SimpleLevelOverridingInheritedApi(com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi) Api(com.google.api.server.spi.config.Api)

Example 5 with ApiParameterConfig

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");
    }
}
Also used : ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Method(java.lang.reflect.Method) EndpointMethod(com.google.api.server.spi.EndpointMethod)

Aggregations

ApiParameterConfig (com.google.api.server.spi.config.model.ApiParameterConfig)16 Api (com.google.api.server.spi.config.Api)7 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)7 SimpleLevelOverridingApi (com.google.api.server.spi.testing.SimpleLevelOverridingApi)7 SimpleLevelOverridingInheritedApi (com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi)7 Test (org.junit.Test)6 ApiMethodConfig (com.google.api.server.spi.config.model.ApiMethodConfig)4 Schema (com.google.api.server.spi.config.model.Schema)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 EndpointMethod (com.google.api.server.spi.EndpointMethod)2 Foo (com.google.api.server.spi.testing.Foo)2 JsonSchema (com.google.api.services.discovery.model.JsonSchema)2 ArrayList (java.util.ArrayList)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 DefaultValue (com.google.api.server.spi.config.DefaultValue)1 Description (com.google.api.server.spi.config.Description)1 Named (com.google.api.server.spi.config.Named)1 Nullable (com.google.api.server.spi.config.Nullable)1 BadRequestException (com.google.api.server.spi.response.BadRequestException)1