Skip to content

Commit

Permalink
Merge pull request #395 from bradfriedman/java-quickstart
Browse files Browse the repository at this point in the history
Add Java Endpoints quickstart
  • Loading branch information
lesv authored Nov 17, 2016
2 parents 7e81ef5 + 7017ddf commit ec44add
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 0 deletions.
21 changes: 21 additions & 0 deletions endpoints/getting-started/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Google Cloud Endpoints & Java
This sample demonstrates how to use Google Cloud Endpoints using a Java backend.

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).

## Deploying to Production

See the [Google Cloud Endpoints Quickstarts](https://cloud.google.com/endpoints/docs/quickstarts).

## Calling your API

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.

## Viewing the Endpoints graphs

By using Endpoints, you get access to several metrics that are displayed graphically in the Cloud Console.

To view the Endpoints graphs:

1. Go to the [Endpoints section in Cloud Console](https://console.cloud.google.com/endpoints) of the project you deployed your API to.
2. Click on your API to view more detailed information about the metrics collected.
56 changes: 56 additions & 0 deletions endpoints/getting-started/container-engine.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2015 Google Inc. All Rights Reserved.
#
# 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.

apiVersion: v1
kind: Service
metadata:
name: esp-echo
spec:
ports:
- port: 80
targetPort: 8081
protocol: TCP
name: http
selector:
app: esp-echo
type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: esp-echo
spec:
replicas: 1
template:
metadata:
labels:
app: esp-echo
spec:
containers:
# [START esp]
- name: esp
image: b.gcr.io/endpoints/endpoints-runtime:0.3
args: [
"-p", "8081",
"-a", "127.0.0.1:8080",
"-s", "SERVICE_NAME",
"-v", "SERVICE_VERSION",
]
# [END esp]
ports:
- containerPort: 8081
- name: echo
image: gcr.io/google-samples/echo-java:1.0
ports:
- containerPort: 8080
81 changes: 81 additions & 0 deletions endpoints/getting-started/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.endpoints</groupId>
<artifactId>endpoints</artifactId>

<parent>
<artifactId>doc-samples</artifactId>
<groupId>com.google.cloud</groupId>
<version>1.0.0</version>
<relativePath>../..</relativePath>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>

<maven.war.plugin>2.6</maven.war.plugin>

<appengine.maven.plugin>1.0.0</appengine.maven.plugin>
<jetty.maven.plugin>9.3.8.v20160314</jetty.maven.plugin>

<failOnMissingWebXml>false</failOnMissingWebXml> <!-- REQUIRED -->
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<!-- for hot reload of the web application -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin> <!-- TEMPORARY -->
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.121.v20160815</version>
<configuration>
<gcloud_app_prefix>beta</gcloud_app_prefix>
</configuration>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.maven.plugin}</version>
<configuration>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven.war.plugin}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.maven.plugin}</version>
</plugin>
</plugins>
</build>
</project>
4 changes: 4 additions & 0 deletions endpoints/getting-started/src/main/appengine/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM gcr.io/google_appengine/jetty9

ADD endpoints-1.0-SNAPSHOT.war $JETTY_BASE/webapps/root.war
ADD . /app
13 changes: 13 additions & 0 deletions endpoints/getting-started/src/main/appengine/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
runtime: custom
env: flex

handlers:
- url: /.*
script: this field is required, but ignored
secure: always

beta_settings:
# Enable Google Cloud Endpoints API management.
use_endpoints_api_management: true
# Specify the Swagger API specification.
endpoints_swagger_spec_file: swagger.yaml
106 changes: 106 additions & 0 deletions endpoints/getting-started/src/main/appengine/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
swagger: "2.0"
info:
description: "A simple Google Cloud Endpoints API example."
title: "Endpoints Example"
version: "1.0.0"
host: "YOUR-PROJECT-ID.appspot.com"
basePath: "/"
consumes:
- "application/json"
produces:
- "application/json"
schemes:
- "https"
paths:
"/echo":
post:
description: "Echo back a given message."
operationId: "echo"
produces:
- "application/json"
responses:
200:
description: "Echo"
schema:
$ref: "#/definitions/echoMessage"
parameters:
- description: "Message to echo"
in: body
name: message
required: true
schema:
$ref: "#/definitions/echoMessage"
"/auth/info/googlejwt":
get:
description: "Returns the requests' authentication information."
operationId: "auth_info_google_jwt"
produces:
- "application/json"
responses:
200:
description: "Authenication info."
schema:
$ref: "#/definitions/authInfoResponse"
x-security:
- google_jwt:
audiences:
# This must match the "aud" field in the JWT. You can add multiple
# audiences to accept JWTs from multiple clients.
- "echo.endpoints.sample.google.com"
"/auth/info/googleidtoken":
get:
description: "Returns the requests' authentication information."
operationId: "authInfoGoogleIdToken"
produces:
- "application/json"
responses:
200:
description: "Authenication info."
schema:
$ref: "#/definitions/authInfoResponse"
x-security:
- google_id_token:
audiences:
# Your OAuth2 client's Client ID must be added here. You can add
# multiple client IDs to accept tokens from multiple clients.
- "YOUR-CLIENT-ID"
definitions:
echoMessage:
properties:
message:
type: "string"
authInfoResponse:
properties:
id:
type: "string"
email:
type: "string"
# This section requires all requests to any path to require an API key.
security:
- api_key: []
securityDefinitions:
# This section configures basic authentication with an API key.
api_key:
type: "apiKey"
name: "key"
in: "query"
# This section configures authentication using Google API Service Accounts
# to sign a json web token. This is mostly used for server-to-server
# communication.
google_jwt:
authorizationUrl: ""
flow: "implicit"
type: "oauth2"
# This must match the 'iss' field in the JWT.
x-issuer: "jwt-client.endpoints.sample.google.com"
# Update this with your service account's email address.
x-jwks_uri: "https://www.googleapis.com/service_accounts/v1/jwk/YOUR-SERVICE-ACCOUNT-EMAIL"
# This section configures authentication using Google OAuth2 ID Tokens.
# ID Tokens can be obtained using OAuth2 clients, and can be used to access
# your API on behalf of a particular user.
google_id_token:
authorizationUrl: ""
flow: "implicit"
type: "oauth2"
x-issuer: "accounts.google.com"
x-jwks_uri: "https://www.googleapis.com/oauth2/v1/certs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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.endpoints;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.util.Base64;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* A servlet that returns authentication information.
* See swagger.yaml for authentication mechanisms (e.g. JWT tokens, Google ID token).
*/
@WebServlet("/auth/info/*")
public class AuthInfoServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String encodedInfo = req.getHeader("X-Endpoint-API-UserInfo");
if (encodedInfo == null || encodedInfo == "") {
JsonObject anon = new JsonObject();
anon.addProperty("id", "anonymous");
new Gson().toJson(anon, resp.getWriter());
return;
}

try {
byte[] authInfo = Base64.getDecoder().decode(encodedInfo);
resp.getOutputStream().write(authInfo);
} catch (IllegalArgumentException iae) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
JsonObject error = new JsonObject();
error.addProperty("code", HttpServletResponse.SC_BAD_REQUEST);
error.addProperty("message", "Could not decode auth info.");
new Gson().toJson(error, resp.getWriter());
}
}
}
Loading

0 comments on commit ec44add

Please sign in to comment.