diff --git a/appengine/endpoints-frameworks-v2/backend/README.md b/appengine/endpoints-frameworks-v2/backend/README.md index d7b23ec6bdd..bb53854ae2b 100644 --- a/appengine/endpoints-frameworks-v2/backend/README.md +++ b/appengine/endpoints-frameworks-v2/backend/README.md @@ -15,6 +15,10 @@ To add the project ID: 0. For ``, replace the value `YOUR_PROJECT_ID` with your project ID. +0. Edit the file `src/main/java/com/example/echo/Echo.java`. + +0. Replace the value `YOUR-PROJECT-ID` with your project ID. + 0. Save your changes. ## Building the sample project diff --git a/appengine/endpoints-frameworks-v2/backend/pom.xml b/appengine/endpoints-frameworks-v2/backend/pom.xml index 30ee0c1e3d5..bffc894ab3f 100644 --- a/appengine/endpoints-frameworks-v2/backend/pom.xml +++ b/appengine/endpoints-frameworks-v2/backend/pom.xml @@ -31,8 +31,8 @@ UTF-8 - 2.0.0-beta.8 - 1.0.0-beta.10 + 2.0.0-beta.9 + 1.0.0-beta.11 YOUR_PROJECT_ID diff --git a/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Echo.java b/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Echo.java index f8bc0590748..5804e86dfca 100644 --- a/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Echo.java +++ b/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Echo.java @@ -18,12 +18,18 @@ import com.google.api.server.spi.auth.EspAuthenticator; import com.google.api.server.spi.auth.common.User; +import com.google.api.server.spi.config.AnnotationBoolean; import com.google.api.server.spi.config.Api; +import com.google.api.server.spi.config.ApiIssuer; +import com.google.api.server.spi.config.ApiIssuerAudience; import com.google.api.server.spi.config.ApiMethod; import com.google.api.server.spi.config.ApiNamespace; -import com.google.api.server.spi.config.AuthLevel; +import com.google.api.server.spi.config.Named; +import com.google.api.server.spi.config.Nullable; +import com.google.api.server.spi.response.UnauthorizedException; /** The Echo API which Endpoints will be exposing. */ +// [START echo_api_annotation] @Api( name = "echo", version = "v1", @@ -32,11 +38,21 @@ ownerDomain = "echo.example.com", ownerName = "echo.example.com", packagePath = "" - ) + ), + // [START_EXCLUDE] + issuers = { + @ApiIssuer( + name = "firebase", + issuer = "https://securetoken.google.com/YOUR-PROJECT-ID", + jwksUri = "https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com") + } + // [END_EXCLUDE] ) +// [END echo_api_annotation] public class Echo { /** - * Echoes the received message back. + * Echoes the received message back. If n is a non-negative integer, the message is copied that + * many times in the returned message. * * Note that name is specified and will override the default name of "{class name}.{method * name}". For example, the default is "echo.echo". @@ -44,8 +60,58 @@ public class Echo { * Note that httpMethod is not specified. This will default to a reasonable HTTP method * depending on the API method name. In this case, the HTTP method will default to POST. */ + // [START echo_method] @ApiMethod(name = "echo") - public Message echo(Message message) { + public Message echo(Message message, @Named("n") @Nullable Integer n) { + return doEcho(message, n); + } + // [END echo_method] + + /** + * Echoes the received message back. If n is a non-negative integer, the message is copied that + * many times in the returned message. + * + * Note that name is specified and will override the default name of "{class name}.{method + * name}". For example, the default is "echo.echo". + * + * Note that httpMethod is not specified. This will default to a reasonable HTTP method + * depending on the API method name. In this case, the HTTP method will default to POST. + */ + // [START echo_path] + @ApiMethod(name = "echo_path_parameter", path = "echo/{n}") + public Message echoPathParameter(Message message, @Named("n") int n) { + return doEcho(message, n); + } + // [END echo_path] + + /** + * Echoes the received message back. If n is a non-negative integer, the message is copied that + * many times in the returned message. + * + * Note that name is specified and will override the default name of "{class name}.{method + * name}". For example, the default is "echo.echo". + * + * Note that httpMethod is not specified. This will default to a reasonable HTTP method + * depending on the API method name. In this case, the HTTP method will default to POST. + */ + // [START echo_api_key] + @ApiMethod(name = "echo_api_key", path = "echo_api_key", apiKeyRequired = AnnotationBoolean.TRUE) + public Message echoApiKey(Message message, @Named("n") @Nullable Integer n) { + return doEcho(message, n); + } + // [END echo_api_key] + + private Message doEcho(Message message, Integer n) { + if (n != null && n >= 0) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < n; i++) { + if (i > 0) { + sb.append(" "); + } + sb.append(message.getMessage()); + } + message.setMessage(sb.toString()); + } return message; } @@ -59,19 +125,49 @@ public Message echo(Message message) { * Note that httpMethod is not required here. Without httpMethod, this will default to GET due * to the API method name. httpMethod is added here for example purposes. */ + // [START google_id_token_auth] @ApiMethod( httpMethod = ApiMethod.HttpMethod.GET, authenticators = {EspAuthenticator.class}, audiences = {"YOUR_OAUTH_CLIENT_ID"}, - authLevel = AuthLevel.REQUIRED + clientIds = {"YOUR_OAUTH_CLIENT_ID"} ) public Email getUserEmail(User user) throws UnauthorizedException { if (user == null) { - throw new UnauthorizedException(); + throw new UnauthorizedException("Invalid credentials"); + } + + Email response = new Email(); + response.setEmail(user.getEmail()); + return response; + } + // [END google_id_token_auth] + + /** + * Gets the authenticated user's email. If the user is not authenticated, this will return an HTTP + * 401. + * + * Note that name is not specified. This will default to "{class name}.{method name}". For + * example, the default is "echo.getUserEmail". + * + * Note that httpMethod is not required here. Without httpMethod, this will default to GET due + * to the API method name. httpMethod is added here for example purposes. + */ + // [START firebase_auth] + @ApiMethod( + path = "firebase_user", + httpMethod = ApiMethod.HttpMethod.GET, + authenticators = {EspAuthenticator.class}, + issuerAudiences = {@ApiIssuerAudience(name = "firebase", audiences = {"YOUR-PROJECT-ID"})} + ) + public Email getUserEmailFirebase(User user) throws UnauthorizedException { + if (user == null) { + throw new UnauthorizedException("Invalid credentials"); } Email response = new Email(); response.setEmail(user.getEmail()); return response; } + // [END firebase_auth] } diff --git a/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/UnauthorizedException.java b/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/UnauthorizedException.java deleted file mode 100644 index 9d73e09ca06..00000000000 --- a/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/UnauthorizedException.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2016 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.example.echo; - -import com.google.api.server.spi.ServiceException; - -/** - * Throw this when the user is unauthorized. - * - * Note that this must inherit from ServiceException. - */ -public class UnauthorizedException extends ServiceException { - public UnauthorizedException() { - super(401, "Unauthorized"); - } -}