Search in sources :

Example 11 with EndpointMethod

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());
}
Also used : ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) EndpointMethod(com.google.api.server.spi.EndpointMethod) Test(org.junit.Test)

Example 12 with EndpointMethod

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());
}
Also used : ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) EndpointMethod(com.google.api.server.spi.EndpointMethod) Test(org.junit.Test)

Example 13 with EndpointMethod

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());
}
Also used : ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) EndpointMethod(com.google.api.server.spi.EndpointMethod) Test(org.junit.Test)

Example 14 with EndpointMethod

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());
}
Also used : ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) EndpointMethod(com.google.api.server.spi.EndpointMethod) Test(org.junit.Test)

Example 15 with EndpointMethod

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);
    }
}
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) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) EndpointMethod(com.google.api.server.spi.EndpointMethod) BadRequestException(com.google.api.server.spi.response.BadRequestException) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

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