Search in sources :

Example 16 with EndpointMethod

use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.

the class ServletRequestParamReader method deserializeParams.

protected Object[] deserializeParams(JsonNode node) throws IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ServiceException {
    EndpointMethod method = getMethod();
    Class<?>[] paramClasses = method.getParameterClasses();
    TypeToken<?>[] paramTypes = method.getParameterTypes();
    Object[] params = new Object[paramClasses.length];
    List<String> parameterNames = getParameterNames(method);
    for (int i = 0; i < paramClasses.length; i++) {
        TypeToken<?> type = paramTypes[i];
        Class<?> clazz = paramClasses[i];
        if (User.class.isAssignableFrom(clazz)) {
            // User type parameter requires no Named annotation (ignored if present)
            User user = getUser();
            if (user == null && methodConfig != null && methodConfig.getAuthLevel() == AuthLevel.REQUIRED) {
                throw new UnauthorizedException("Valid user credentials are required.");
            }
            if (user == null || clazz.isAssignableFrom(user.getClass())) {
                params[i] = user;
                logger.atFine().log("deserialize: User injected into param[%d]", i);
            } else {
                logger.atWarning().log("deserialize: User object of type %s is not assignable to %s. User will be null.", user.getClass().getName(), clazz.getName());
            }
        } else if (APPENGINE_USER_CLASS_NAME.equals(clazz.getName())) {
            // User type parameter requires no Named annotation (ignored if present)
            com.google.appengine.api.users.User appEngineUser = getAppEngineUser();
            if (appEngineUser == null && methodConfig != null && methodConfig.getAuthLevel() == AuthLevel.REQUIRED) {
                throw new UnauthorizedException("Valid user credentials are required.");
            }
            params[i] = appEngineUser;
            logger.atFine().log("deserialize: App Engine User injected into param[%d]", i);
        } else if (clazz == HttpServletRequest.class) {
            // HttpServletRequest type parameter requires no Named annotation (ignored if present)
            params[i] = endpointsContext.getRequest();
            logger.atFine().log("deserialize: HttpServletRequest injected into param[%d]", i);
        } else if (clazz == ServletContext.class) {
            // ServletContext type parameter requires no Named annotation (ignored if present)
            params[i] = servletContext;
            logger.atFine().log("deserialize: ServletContext %s injected into param[%d]", params[i], i);
        } else {
            String name = parameterNames.get(i);
            if (Strings.isNullOrEmpty(name)) {
                params[i] = (node == null) ? null : objectReader.forType(clazz).readValue(node);
                logger.atFine().log("deserialize: %s %s injected into unnamed param[%d]", clazz, params[i], i);
            } else if (StandardParameters.isStandardParamName(name)) {
                params[i] = getStandardParamValue(node, name);
            } else {
                JsonNode nodeValue = node.get(name);
                if (nodeValue == null) {
                    params[i] = null;
                } else {
                    // Check for collection type
                    if (Collection.class.isAssignableFrom(clazz) && type.getType() instanceof ParameterizedType) {
                        params[i] = deserializeCollection(clazz, (ParameterizedType) type.getType(), nodeValue);
                    } else {
                        params[i] = objectReader.forType(clazz).readValue(nodeValue);
                    }
                }
                if (params[i] == null && isRequiredParameter(method, i)) {
                    throw new BadRequestException("null value for parameter '" + name + "' not allowed");
                }
                logger.atFine().log("deserialize: %s %s injected into param[%d] named {%s}", clazz, params[i], i, name);
            }
        }
    }
    return params;
}
Also used : User(com.google.api.server.spi.auth.common.User) JsonNode(com.fasterxml.jackson.databind.JsonNode) ParameterizedType(java.lang.reflect.ParameterizedType) TypeToken(com.google.common.reflect.TypeToken) UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException) EndpointMethod(com.google.api.server.spi.EndpointMethod) ServletContext(javax.servlet.ServletContext) Collection(java.util.Collection) BadRequestException(com.google.api.server.spi.response.BadRequestException)

Example 17 with EndpointMethod

use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.

the class ServletRequestParamReader method getParameterNames.

protected static List<String> getParameterNames(EndpointMethod endpointMethod) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    List<String> parameterNames = endpointMethod.getParameterNames();
    if (parameterNames == null) {
        Method method = endpointMethod.getMethod();
        parameterNames = new ArrayList<>();
        for (int parameter = 0; parameter < method.getParameterTypes().length; parameter++) {
            Annotation annotation = AnnotationUtil.getNamedParameter(method, parameter, Named.class);
            if (annotation == null) {
                parameterNames.add(null);
            } else {
                parameterNames.add((String) annotation.getClass().getMethod("value").invoke(annotation));
            }
        }
        endpointMethod.setParameterNames(parameterNames);
    }
    return parameterNames;
}
Also used : Method(java.lang.reflect.Method) EndpointMethod(com.google.api.server.spi.EndpointMethod) Annotation(java.lang.annotation.Annotation)

Example 18 with EndpointMethod

use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.

the class SwaggerGenerator method writeApiClass.

private void writeApiClass(ApiConfig apiConfig, Swagger swagger, GenerationContext genCtx) throws ApiConfigException {
    Map<EndpointMethod, ApiMethodConfig> methodConfigs = apiConfig.getApiClassConfig().getMethods();
    for (Map.Entry<EndpointMethod, ApiMethodConfig> methodConfig : methodConfigs.entrySet()) {
        if (!methodConfig.getValue().isIgnored()) {
            ApiMethodConfig config = methodConfig.getValue();
            writeApiMethod(config, apiConfig, swagger, genCtx);
        }
    }
}
Also used : ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) EndpointMethod(com.google.api.server.spi.EndpointMethod) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 19 with EndpointMethod

use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.

the class JsonConfigWriter method convertApiMethods.

private void convertApiMethods(ObjectNode methodsNode, ObjectNode descriptorSchemasNode, ObjectNode descriptorMethodsNode, ApiConfig apiConfig) throws IllegalArgumentException, SecurityException, ApiConfigException {
    Map<EndpointMethod, ApiMethodConfig> methodConfigs = apiConfig.getApiClassConfig().getMethods();
    for (Map.Entry<EndpointMethod, ApiMethodConfig> methodConfig : methodConfigs.entrySet()) {
        if (!methodConfig.getValue().isIgnored()) {
            EndpointMethod endpointMethod = methodConfig.getKey();
            ApiMethodConfig config = methodConfig.getValue();
            convertApiMethod(methodsNode, descriptorSchemasNode, descriptorMethodsNode, endpointMethod, config, apiConfig);
        }
    }
}
Also used : ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) EndpointMethod(com.google.api.server.spi.EndpointMethod) Map(java.util.Map)

Example 20 with EndpointMethod

use of com.google.api.server.spi.EndpointMethod in project endpoints-java by cloudendpoints.

the class ApiMethodConfig method setDefaults.

/**
 * Sets all fields to their default value to be used if not set otherwise.  Override to change the
 * default configuration.
 */
protected void setDefaults(EndpointMethod endpointMethod, TypeLoader typeLoader, String apiDefaultResource) {
    Method method = endpointMethod.getMethod();
    RestMethod restMethod = getRestMethod(method);
    String resourceTypeName;
    if (apiDefaultResource != null) {
        resourceTypeName = apiDefaultResource.toLowerCase();
    } else {
        resourceTypeName = restMethod.guessResourceName(apiClassConfig.getApiConfig(), endpointMethod, typeLoader.getClassTypes());
    }
    name = null;
    httpMethod = Preconditions.checkNotNull(restMethod.getHttpMethod(), "httpMethod");
    setPath(Preconditions.checkNotNull(resourceTypeName == null ? method.getName() : resourceTypeName.toLowerCase(), "path"));
    authLevel = AuthLevel.UNSPECIFIED;
    scopeExpression = null;
    audiences = null;
    issuerAudiences = ApiIssuerAudienceConfig.UNSPECIFIED;
    clientIds = null;
    authenticators = null;
    peerAuthenticators = null;
    ignored = false;
    apiKeyRequired = null;
    returnType = endpointMethod.getReturnType();
    metricCosts = ImmutableList.of();
}
Also used : EndpointMethod(com.google.api.server.spi.EndpointMethod) Method(java.lang.reflect.Method)

Aggregations

EndpointMethod (com.google.api.server.spi.EndpointMethod)26 ApiMethodConfig (com.google.api.server.spi.config.model.ApiMethodConfig)14 Test (org.junit.Test)14 Method (java.lang.reflect.Method)6 Annotation (java.lang.annotation.Annotation)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Named (com.google.api.server.spi.config.Named)2 Nullable (com.google.api.server.spi.config.Nullable)2 ApiParameterConfig (com.google.api.server.spi.config.model.ApiParameterConfig)2 BadRequestException (com.google.api.server.spi.response.BadRequestException)2 TypeToken (com.google.common.reflect.TypeToken)2 Map (java.util.Map)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 MethodHierarchyReader (com.google.api.server.spi.MethodHierarchyReader)1 TypeLoader (com.google.api.server.spi.TypeLoader)1 EndpointsAuthenticator (com.google.api.server.spi.auth.EndpointsAuthenticator)1 EndpointsPeerAuthenticator (com.google.api.server.spi.auth.EndpointsPeerAuthenticator)1 User (com.google.api.server.spi.auth.common.User)1 Authenticator (com.google.api.server.spi.config.Authenticator)1