Skip to content

Commit 7ea9632

Browse files
broadylesv
authored andcommitted
Add endpoints sample. (GoogleCloudPlatform#248)
* endpoints: copy helloworld sample * endpoints: add README, config files, rename source file * endpoints: add echo endpoint * endpoints: add auth info handler. * endpoints: add class-level javadoc for servlets * endpoints: update to new gcloud mvn plugin * endpoints: use custom runtime to copy endpoints/service.json file
1 parent 7986276 commit 7ea9632

File tree

7 files changed

+315
-0
lines changed

7 files changed

+315
-0
lines changed

managed_vms/endpoints/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Google Cloud Endpoints on App Engine flexible environment
2+
This sample demonstrates how to use Google Cloud Endpoints on Google App Engine Flexible Environment using Java.
3+
4+
## Running locally
5+
$ mvn jetty:run
6+
7+
## Deploying
8+
$ mvn gcloud:deploy

managed_vms/endpoints/pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.managedvms</groupId>
9+
<artifactId>managed-vms-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+
<dependencies>
19+
<dependency>
20+
<groupId>javax.servlet</groupId>
21+
<artifactId>javax.servlet-api</artifactId>
22+
<version>3.1.0</version>
23+
<type>jar</type>
24+
<scope>provided</scope>
25+
</dependency>
26+
<!-- Gson: Java to Json conversion -->
27+
<dependency>
28+
<groupId>com.google.code.gson</groupId>
29+
<artifactId>gson</artifactId>
30+
<version>2.6.2</version>
31+
<scope>compile</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<!-- for hot reload of the web application -->
37+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
38+
<plugins>
39+
<plugin>
40+
<groupId>com.google.appengine</groupId>
41+
<artifactId>gcloud-maven-plugin</artifactId>
42+
<version>2.0.9.111.v20160527</version>
43+
</plugin>
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-war-plugin</artifactId>
47+
<version>2.6</version>
48+
<configuration>
49+
<failOnMissingWebXml>false</failOnMissingWebXml>
50+
</configuration>
51+
</plugin>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<version>3.3</version>
55+
<artifactId>maven-compiler-plugin</artifactId>
56+
<configuration>
57+
<source>1.7</source>
58+
<target>1.7</target>
59+
</configuration>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.eclipse.jetty</groupId>
63+
<artifactId>jetty-maven-plugin</artifactId>
64+
<version>9.3.8.v20160314</version>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM gcr.io/google_appengine/jetty9
2+
3+
ADD . /app
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
runtime: custom
2+
vm: true
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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.managedvms.endpoints;
18+
19+
import java.io.IOException;
20+
import java.io.PrintWriter;
21+
import java.util.Base64;
22+
23+
import javax.servlet.annotation.WebServlet;
24+
import javax.servlet.http.HttpServlet;
25+
import javax.servlet.http.HttpServletRequest;
26+
import javax.servlet.http.HttpServletResponse;
27+
28+
import com.google.gson.Gson;
29+
import com.google.gson.JsonObject;
30+
31+
/**
32+
* A servlet that returns authentication information.
33+
* See swagger.yaml for authentication mechanisms (e.g. JWT tokens, Google ID token).
34+
*/
35+
@WebServlet("/auth/info/*")
36+
public class AuthInfoServlet extends HttpServlet {
37+
38+
@Override
39+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
40+
String encodedInfo = req.getHeader("X-Endpoint-API-UserInfo");
41+
if (encodedInfo == null || encodedInfo == "") {
42+
JsonObject anon = new JsonObject();
43+
anon.addProperty("id", "anonymous");
44+
new Gson().toJson(anon, resp.getWriter());
45+
return;
46+
}
47+
48+
try {
49+
byte[] authInfo = Base64.getDecoder().decode(encodedInfo);
50+
resp.getOutputStream().write(authInfo);
51+
} catch (IllegalArgumentException iae) {
52+
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
53+
JsonObject error = new JsonObject();
54+
error.addProperty("code", HttpServletResponse.SC_BAD_REQUEST);
55+
error.addProperty("message", "Could not decode auth info.");
56+
new Gson().toJson(error, resp.getWriter());
57+
}
58+
}
59+
}
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.managedvms.endpoints;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.io.OutputStream;
22+
import java.util.Map;
23+
24+
import javax.servlet.annotation.WebServlet;
25+
import javax.servlet.http.HttpServlet;
26+
import javax.servlet.http.HttpServletRequest;
27+
import javax.servlet.http.HttpServletResponse;
28+
29+
import com.google.gson.Gson;
30+
import com.google.gson.JsonObject;
31+
import com.google.gson.JsonParseException;
32+
import com.google.gson.stream.JsonReader;
33+
34+
/**
35+
* A servlet that echoes JSON message bodies.
36+
*/
37+
@WebServlet("/echo")
38+
public class EchoServlet extends HttpServlet {
39+
40+
@Override
41+
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
42+
resp.addHeader("Content-Encoding", "application/json");
43+
44+
Object responseBody;
45+
try {
46+
JsonReader jsonReader = new JsonReader(req.getReader());
47+
responseBody = new Gson().fromJson(jsonReader, Map.class);
48+
} catch (JsonParseException je) {
49+
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
50+
JsonObject error = new JsonObject();
51+
error.addProperty("code", HttpServletResponse.SC_BAD_REQUEST);
52+
error.addProperty("message", "Body was not valid JSON.");
53+
responseBody = error;
54+
}
55+
56+
new Gson().toJson(responseBody, resp.getWriter());
57+
}
58+
}

0 commit comments

Comments
 (0)