Skip to content

Commit ec44add

Browse files
authored
Merge pull request #395 from bradfriedman/java-quickstart
Add Java Endpoints quickstart
2 parents 7e81ef5 + 7017ddf commit ec44add

File tree

8 files changed

+395
-0
lines changed

8 files changed

+395
-0
lines changed

endpoints/getting-started/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Google Cloud Endpoints & Java
2+
This sample demonstrates how to use Google Cloud Endpoints using a Java backend.
3+
4+
For a complete walkthrough showing how to run this sample in different environments, see the [Google Cloud Endpoints Quickstarts](https://cloud.google.com/endpoints/docs/quickstarts).
5+
6+
## Deploying to Production
7+
8+
See the [Google Cloud Endpoints Quickstarts](https://cloud.google.com/endpoints/docs/quickstarts).
9+
10+
## Calling your API
11+
12+
Please refer to the Google Cloud Endpoints [documentation](https://cloud.google.com/endpoints/docs/app-engine/) for App Engine Flexible Environment to learn about creating an API Key and calling your API.
13+
14+
## Viewing the Endpoints graphs
15+
16+
By using Endpoints, you get access to several metrics that are displayed graphically in the Cloud Console.
17+
18+
To view the Endpoints graphs:
19+
20+
1. Go to the [Endpoints section in Cloud Console](https://console.cloud.google.com/endpoints) of the project you deployed your API to.
21+
2. Click on your API to view more detailed information about the metrics collected.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2015 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: v1
16+
kind: Service
17+
metadata:
18+
name: esp-echo
19+
spec:
20+
ports:
21+
- port: 80
22+
targetPort: 8081
23+
protocol: TCP
24+
name: http
25+
selector:
26+
app: esp-echo
27+
type: LoadBalancer
28+
---
29+
apiVersion: extensions/v1beta1
30+
kind: Deployment
31+
metadata:
32+
name: esp-echo
33+
spec:
34+
replicas: 1
35+
template:
36+
metadata:
37+
labels:
38+
app: esp-echo
39+
spec:
40+
containers:
41+
# [START esp]
42+
- name: esp
43+
image: b.gcr.io/endpoints/endpoints-runtime:0.3
44+
args: [
45+
"-p", "8081",
46+
"-a", "127.0.0.1:8080",
47+
"-s", "SERVICE_NAME",
48+
"-v", "SERVICE_VERSION",
49+
]
50+
# [END esp]
51+
ports:
52+
- containerPort: 8081
53+
- name: echo
54+
image: gcr.io/google-samples/echo-java:1.0
55+
ports:
56+
- containerPort: 8080

endpoints/getting-started/pom.xml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
<packaging>war</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<groupId>com.example.endpoints</groupId>
9+
<artifactId>endpoints</artifactId>
10+
11+
<parent>
12+
<artifactId>doc-samples</artifactId>
13+
<groupId>com.google.cloud</groupId>
14+
<version>1.0.0</version>
15+
<relativePath>../..</relativePath>
16+
</parent>
17+
18+
<properties>
19+
<maven.compiler.target>1.8</maven.compiler.target>
20+
<maven.compiler.source>1.8</maven.compiler.source>
21+
22+
<maven.war.plugin>2.6</maven.war.plugin>
23+
24+
<appengine.maven.plugin>1.0.0</appengine.maven.plugin>
25+
<jetty.maven.plugin>9.3.8.v20160314</jetty.maven.plugin>
26+
27+
<failOnMissingWebXml>false</failOnMissingWebXml> <!-- REQUIRED -->
28+
</properties>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>javax.servlet</groupId>
33+
<artifactId>javax.servlet-api</artifactId>
34+
<version>3.1.0</version>
35+
<type>jar</type>
36+
<scope>provided</scope>
37+
</dependency>
38+
<!-- Gson: Java to Json conversion -->
39+
<dependency>
40+
<groupId>com.google.code.gson</groupId>
41+
<artifactId>gson</artifactId>
42+
<version>2.6.2</version>
43+
<scope>compile</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<!-- for hot reload of the web application -->
49+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
50+
<plugins>
51+
<plugin> <!-- TEMPORARY -->
52+
<groupId>com.google.appengine</groupId>
53+
<artifactId>gcloud-maven-plugin</artifactId>
54+
<version>2.0.9.121.v20160815</version>
55+
<configuration>
56+
<gcloud_app_prefix>beta</gcloud_app_prefix>
57+
</configuration>
58+
</plugin>
59+
<plugin>
60+
<groupId>com.google.cloud.tools</groupId>
61+
<artifactId>appengine-maven-plugin</artifactId>
62+
<version>${appengine.maven.plugin}</version>
63+
<configuration>
64+
</configuration>
65+
</plugin>
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-war-plugin</artifactId>
69+
<version>${maven.war.plugin}</version>
70+
<configuration>
71+
<failOnMissingWebXml>false</failOnMissingWebXml>
72+
</configuration>
73+
</plugin>
74+
<plugin>
75+
<groupId>org.eclipse.jetty</groupId>
76+
<artifactId>jetty-maven-plugin</artifactId>
77+
<version>${jetty.maven.plugin}</version>
78+
</plugin>
79+
</plugins>
80+
</build>
81+
</project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM gcr.io/google_appengine/jetty9
2+
3+
ADD endpoints-1.0-SNAPSHOT.war $JETTY_BASE/webapps/root.war
4+
ADD . /app
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
runtime: custom
2+
env: flex
3+
4+
handlers:
5+
- url: /.*
6+
script: this field is required, but ignored
7+
secure: always
8+
9+
beta_settings:
10+
# Enable Google Cloud Endpoints API management.
11+
use_endpoints_api_management: true
12+
# Specify the Swagger API specification.
13+
endpoints_swagger_spec_file: swagger.yaml
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
swagger: "2.0"
2+
info:
3+
description: "A simple Google Cloud Endpoints API example."
4+
title: "Endpoints Example"
5+
version: "1.0.0"
6+
host: "YOUR-PROJECT-ID.appspot.com"
7+
basePath: "/"
8+
consumes:
9+
- "application/json"
10+
produces:
11+
- "application/json"
12+
schemes:
13+
- "https"
14+
paths:
15+
"/echo":
16+
post:
17+
description: "Echo back a given message."
18+
operationId: "echo"
19+
produces:
20+
- "application/json"
21+
responses:
22+
200:
23+
description: "Echo"
24+
schema:
25+
$ref: "#/definitions/echoMessage"
26+
parameters:
27+
- description: "Message to echo"
28+
in: body
29+
name: message
30+
required: true
31+
schema:
32+
$ref: "#/definitions/echoMessage"
33+
"/auth/info/googlejwt":
34+
get:
35+
description: "Returns the requests' authentication information."
36+
operationId: "auth_info_google_jwt"
37+
produces:
38+
- "application/json"
39+
responses:
40+
200:
41+
description: "Authenication info."
42+
schema:
43+
$ref: "#/definitions/authInfoResponse"
44+
x-security:
45+
- google_jwt:
46+
audiences:
47+
# This must match the "aud" field in the JWT. You can add multiple
48+
# audiences to accept JWTs from multiple clients.
49+
- "echo.endpoints.sample.google.com"
50+
"/auth/info/googleidtoken":
51+
get:
52+
description: "Returns the requests' authentication information."
53+
operationId: "authInfoGoogleIdToken"
54+
produces:
55+
- "application/json"
56+
responses:
57+
200:
58+
description: "Authenication info."
59+
schema:
60+
$ref: "#/definitions/authInfoResponse"
61+
x-security:
62+
- google_id_token:
63+
audiences:
64+
# Your OAuth2 client's Client ID must be added here. You can add
65+
# multiple client IDs to accept tokens from multiple clients.
66+
- "YOUR-CLIENT-ID"
67+
definitions:
68+
echoMessage:
69+
properties:
70+
message:
71+
type: "string"
72+
authInfoResponse:
73+
properties:
74+
id:
75+
type: "string"
76+
email:
77+
type: "string"
78+
# This section requires all requests to any path to require an API key.
79+
security:
80+
- api_key: []
81+
securityDefinitions:
82+
# This section configures basic authentication with an API key.
83+
api_key:
84+
type: "apiKey"
85+
name: "key"
86+
in: "query"
87+
# This section configures authentication using Google API Service Accounts
88+
# to sign a json web token. This is mostly used for server-to-server
89+
# communication.
90+
google_jwt:
91+
authorizationUrl: ""
92+
flow: "implicit"
93+
type: "oauth2"
94+
# This must match the 'iss' field in the JWT.
95+
x-issuer: "jwt-client.endpoints.sample.google.com"
96+
# Update this with your service account's email address.
97+
x-jwks_uri: "https://www.googleapis.com/service_accounts/v1/jwk/YOUR-SERVICE-ACCOUNT-EMAIL"
98+
# This section configures authentication using Google OAuth2 ID Tokens.
99+
# ID Tokens can be obtained using OAuth2 clients, and can be used to access
100+
# your API on behalf of a particular user.
101+
google_id_token:
102+
authorizationUrl: ""
103+
flow: "implicit"
104+
type: "oauth2"
105+
x-issuer: "accounts.google.com"
106+
x-jwks_uri: "https://www.googleapis.com/oauth2/v1/certs"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.endpoints;
18+
19+
import com.google.gson.Gson;
20+
import com.google.gson.JsonObject;
21+
22+
import java.io.IOException;
23+
import java.util.Base64;
24+
25+
import javax.servlet.annotation.WebServlet;
26+
import javax.servlet.http.HttpServlet;
27+
import javax.servlet.http.HttpServletRequest;
28+
import javax.servlet.http.HttpServletResponse;
29+
30+
/**
31+
* A servlet that returns authentication information.
32+
* See swagger.yaml for authentication mechanisms (e.g. JWT tokens, Google ID token).
33+
*/
34+
@WebServlet("/auth/info/*")
35+
public class AuthInfoServlet extends HttpServlet {
36+
37+
@Override
38+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
39+
String encodedInfo = req.getHeader("X-Endpoint-API-UserInfo");
40+
if (encodedInfo == null || encodedInfo == "") {
41+
JsonObject anon = new JsonObject();
42+
anon.addProperty("id", "anonymous");
43+
new Gson().toJson(anon, resp.getWriter());
44+
return;
45+
}
46+
47+
try {
48+
byte[] authInfo = Base64.getDecoder().decode(encodedInfo);
49+
resp.getOutputStream().write(authInfo);
50+
} catch (IllegalArgumentException iae) {
51+
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
52+
JsonObject error = new JsonObject();
53+
error.addProperty("code", HttpServletResponse.SC_BAD_REQUEST);
54+
error.addProperty("message", "Could not decode auth info.");
55+
new Gson().toJson(error, resp.getWriter());
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)