Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update echo endpoint to support upcoming tutorial #436

Merged
merged 6 commits into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions appengine/endpoints-frameworks-v2/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ To add the project ID:
0. For `<endpoints.project.id>`, 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
Expand Down
4 changes: 2 additions & 2 deletions appengine/endpoints-frameworks-v2/backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<endpoints.framework.version>2.0.0-beta.8</endpoints.framework.version>
<endpoints.management.version>1.0.0-beta.10</endpoints.management.version>
<endpoints.framework.version>2.0.0-beta.9</endpoints.framework.version>
<endpoints.management.version>1.0.0-beta.11</endpoints.management.version>

<endpoints.project.id>YOUR_PROJECT_ID</endpoints.project.id>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -32,20 +38,80 @@
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".
*
* 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;
}

Expand All @@ -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]
}

This file was deleted.