Skip to content

Commit

Permalink
Adds the Endpoints Frameworks v2 Echo Sample (#284)
Browse files Browse the repository at this point in the history
* Adds the Endpoints Frameworks v2 Echo Sample
* Remove unneeded properties and use appengine.sdk.version.
* Use Maven properties to specify project id and replace hardcoded oauth client id.
* Add comments about the different parts of web.xml.
* Add more comments to web.xml.
* Update root pom.xml with the correct path for endpoints-frameworks-v2.
* Update versions and remove manual-scaling.
* Add README to Endpoints Frameworks v2 sample.
* Changes from auto-scaling to basic-scaling.
* Aligns Endpoints Frameworks v2 sample with other App Engine Standard
samples.
Moves Endpoints Frameworks v2 sample under appengine.
Updates pom.xml.
Updates root pom.xml.
Updates README to mention App Engine Standard.
* Remove <vm> from appengine-web.xml.
* Version bump and use framework only.
  • Loading branch information
wmwong authored and lesv committed Aug 25, 2016
1 parent 4780ef8 commit 7cec9ea
Show file tree
Hide file tree
Showing 11 changed files with 433 additions and 0 deletions.
1 change: 1 addition & 0 deletions appengine/endpoints-frameworks-v2/backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
swagger.json
77 changes: 77 additions & 0 deletions appengine/endpoints-frameworks-v2/backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# App Engine Standard & Google Cloud Endpoints Frameworks & Java

This sample demonstrates how to use Google Cloud Endpoints Frameworks using
Java on App Engine Standard.

## Adding the project ID to the sample API code

You must add the project ID obtained when you created your project to the
sample's `pom.xml` before you can deploy the code.

To add the project ID:

0. Edit the file `pom.xml`.

0. For `<endpoints.project.id>`, replace the value `YOUR_PROJECT_ID` with
your project ID.

0. Save your changes.

## Building the sample project

To build the project:

mvn clean package

## Generating the swagger.json file

To generate the required configuration file `swagger.json`:

0. Download and unzip the [Endpoints Framework tools
package](http://search.maven.org/remotecontent?filepath=com/google/endpoints/endpoints-framework-tools/2.0.0-beta.7/endpoints-framework-tools-2.0.0-beta.7.zip).

0. Invoke the Endpoints Tool using this command:

path/to/endpoints-framework-tools-2.0.0-beta.7/bin/endpoints-framework-tools get-swagger-doc \
-h <PROJECT_ID>.appspot.com \
-w target/echo-1.0-SNAPSHOT com.example.echo.Echo

Replace`<PROJECT_ID>` with your project ID.

## Deploying the sample API to App Engine

To deploy the sample API:

0. Invoke the `gcloud` command to deploy the API configuration file:

gcloud beta service-management deploy swagger.json

0. Deploy the API implementation code by invoking:

mvn appengine:update

The first time you upload a sample app, you may be prompted to authorize the
deployment. Follow the prompts: when you are presented with a browser window
containing a code, copy it to the terminal window.

0. Wait for the upload to finish.

## Sending a request to the sample API

After you deploy the API and its configuration file, you can send requests
to the API.

To send a request to the API, from a command line, invoke the following `cURL`
command:

curl \
-H "Content-Type: application/json" \
-X POST \
-d '{"message":"echo"}' \
https://$PROJECT_ID.appspot.com/_ah/api/echo/v1/echo

You will get a 200 response with the following data:

{
"message": "echo"
}
104 changes: 104 additions & 0 deletions appengine/endpoints-frameworks-v2/backend/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<!--
Copyright 2016 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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

<groupId>com.example.echo</groupId>
<artifactId>echo</artifactId>

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

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<endpoints.project.id>YOUR_PROJECT_ID</endpoints.project.id>
</properties>

<dependencies>
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.endpoints</groupId>
<artifactId>endpoints-framework</artifactId>
<version>2.0.0-beta.7</version>
</dependency>
</dependencies>

<build>
<!-- for hot reload of the web application-->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>display-dependency-updates</goal>
<goal>display-plugin-updates</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>

<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.sdk.version}</version>
<configuration>
<enableJarClasses>false</enableJarClasses>
<!-- Comment in the below snippet to bind to all IPs instead of just localhost -->
<!-- address>0.0.0.0</address>
<port>8080</port -->
<!-- Comment in the below snippet to enable local debugging with a remove debugger
like those included with Eclipse or IntelliJ -->
<!-- jvmFlags>
<jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>
</jvmFlags -->
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.auth.common.User;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;

/** The Echo API which Endpoints will be exposing. */
@Api(
name = "echo",
version = "v1",
namespace =
@ApiNamespace(
ownerDomain = "echo.example.com",
ownerName = "echo.example.com",
packagePath = ""
)
)
public class Echo {
/**
* Echoes the received message back.
*
* 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.
*/
@ApiMethod(name = "echo")
public Message echo(Message message) {
return message;
}

/**
* 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.
*/
@ApiMethod(httpMethod = ApiMethod.HttpMethod.GET)
public Email getUserEmail(User user) throws UnauthorizedException {
if (user == null) {
throw new UnauthorizedException();
}

Email response = new Email();
response.setEmail(user.getEmail());
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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;

/** The email bean that will be used in the getUserEmail response. */
public class Email {
private String email;

public String getEmail() {
return this.email;
}

public void setEmail(String email) {
this.email = email;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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;

/** The message bean that will be used in the echo request and response. */
public class Message {

private String message;

public String getMessage() {
return this.message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2016 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.
-->
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>${endpoints.project.id}</application>
<version>1</version>
<threadsafe>true</threadsafe>

<basic-scaling>
<max-instances>2</max-instances>
</basic-scaling>

<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
</appengine-web-app>
Loading

0 comments on commit 7cec9ea

Please sign in to comment.