Skip to content

Commit 7cec9ea

Browse files
wmwonglesv
authored andcommitted
Adds the Endpoints Frameworks v2 Echo Sample (GoogleCloudPlatform#284)
* 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.
1 parent 4780ef8 commit 7cec9ea

File tree

11 files changed

+433
-0
lines changed

11 files changed

+433
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
swagger.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# App Engine Standard & Google Cloud Endpoints Frameworks & Java
2+
3+
This sample demonstrates how to use Google Cloud Endpoints Frameworks using
4+
Java on App Engine Standard.
5+
6+
## Adding the project ID to the sample API code
7+
8+
You must add the project ID obtained when you created your project to the
9+
sample's `pom.xml` before you can deploy the code.
10+
11+
To add the project ID:
12+
13+
0. Edit the file `pom.xml`.
14+
15+
0. For `<endpoints.project.id>`, replace the value `YOUR_PROJECT_ID` with
16+
your project ID.
17+
18+
0. Save your changes.
19+
20+
## Building the sample project
21+
22+
To build the project:
23+
24+
mvn clean package
25+
26+
## Generating the swagger.json file
27+
28+
To generate the required configuration file `swagger.json`:
29+
30+
0. Download and unzip the [Endpoints Framework tools
31+
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).
32+
33+
0. Invoke the Endpoints Tool using this command:
34+
35+
path/to/endpoints-framework-tools-2.0.0-beta.7/bin/endpoints-framework-tools get-swagger-doc \
36+
-h <PROJECT_ID>.appspot.com \
37+
-w target/echo-1.0-SNAPSHOT com.example.echo.Echo
38+
39+
Replace`<PROJECT_ID>` with your project ID.
40+
41+
## Deploying the sample API to App Engine
42+
43+
To deploy the sample API:
44+
45+
0. Invoke the `gcloud` command to deploy the API configuration file:
46+
47+
gcloud beta service-management deploy swagger.json
48+
49+
0. Deploy the API implementation code by invoking:
50+
51+
mvn appengine:update
52+
53+
The first time you upload a sample app, you may be prompted to authorize the
54+
deployment. Follow the prompts: when you are presented with a browser window
55+
containing a code, copy it to the terminal window.
56+
57+
0. Wait for the upload to finish.
58+
59+
## Sending a request to the sample API
60+
61+
After you deploy the API and its configuration file, you can send requests
62+
to the API.
63+
64+
To send a request to the API, from a command line, invoke the following `cURL`
65+
command:
66+
67+
curl \
68+
-H "Content-Type: application/json" \
69+
-X POST \
70+
-d '{"message":"echo"}' \
71+
https://$PROJECT_ID.appspot.com/_ah/api/echo/v1/echo
72+
73+
You will get a 200 response with the following data:
74+
75+
{
76+
"message": "echo"
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<!--
2+
Copyright 2016 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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>war</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
21+
<groupId>com.example.echo</groupId>
22+
<artifactId>echo</artifactId>
23+
24+
<parent>
25+
<artifactId>doc-samples</artifactId>
26+
<groupId>com.google.cloud</groupId>
27+
<version>1.0.0</version>
28+
<relativePath>../../..</relativePath>
29+
</parent>
30+
31+
<properties>
32+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
33+
34+
<endpoints.project.id>YOUR_PROJECT_ID</endpoints.project.id>
35+
</properties>
36+
37+
<dependencies>
38+
<!-- Compile/runtime dependencies -->
39+
<dependency>
40+
<groupId>javax.servlet</groupId>
41+
<artifactId>servlet-api</artifactId>
42+
<version>2.5</version>
43+
<scope>provided</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.google.endpoints</groupId>
47+
<artifactId>endpoints-framework</artifactId>
48+
<version>2.0.0-beta.7</version>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<!-- for hot reload of the web application-->
54+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.codehaus.mojo</groupId>
58+
<artifactId>versions-maven-plugin</artifactId>
59+
<version>2.2</version>
60+
<executions>
61+
<execution>
62+
<phase>compile</phase>
63+
<goals>
64+
<goal>display-dependency-updates</goal>
65+
<goal>display-plugin-updates</goal>
66+
</goals>
67+
</execution>
68+
</executions>
69+
</plugin>
70+
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-war-plugin</artifactId>
74+
<version>2.6</version>
75+
<configuration>
76+
<webResources>
77+
<resource>
78+
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
79+
<filtering>true</filtering>
80+
<targetPath>WEB-INF</targetPath>
81+
</resource>
82+
</webResources>
83+
</configuration>
84+
</plugin>
85+
86+
<plugin>
87+
<groupId>com.google.appengine</groupId>
88+
<artifactId>appengine-maven-plugin</artifactId>
89+
<version>${appengine.sdk.version}</version>
90+
<configuration>
91+
<enableJarClasses>false</enableJarClasses>
92+
<!-- Comment in the below snippet to bind to all IPs instead of just localhost -->
93+
<!-- address>0.0.0.0</address>
94+
<port>8080</port -->
95+
<!-- Comment in the below snippet to enable local debugging with a remove debugger
96+
like those included with Eclipse or IntelliJ -->
97+
<!-- jvmFlags>
98+
<jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>
99+
</jvmFlags -->
100+
</configuration>
101+
</plugin>
102+
</plugins>
103+
</build>
104+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2016 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain a
6+
* 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.example.echo;
18+
19+
import com.google.api.server.spi.auth.common.User;
20+
import com.google.api.server.spi.config.Api;
21+
import com.google.api.server.spi.config.ApiMethod;
22+
import com.google.api.server.spi.config.ApiNamespace;
23+
24+
/** The Echo API which Endpoints will be exposing. */
25+
@Api(
26+
name = "echo",
27+
version = "v1",
28+
namespace =
29+
@ApiNamespace(
30+
ownerDomain = "echo.example.com",
31+
ownerName = "echo.example.com",
32+
packagePath = ""
33+
)
34+
)
35+
public class Echo {
36+
/**
37+
* Echoes the received message back.
38+
*
39+
* Note that name is specified and will override the default name of "{class name}.{method
40+
* name}". For example, the default is "echo.echo".
41+
*
42+
* Note that httpMethod is not specified. This will default to a reasonable HTTP method
43+
* depending on the API method name. In this case, the HTTP method will default to POST.
44+
*/
45+
@ApiMethod(name = "echo")
46+
public Message echo(Message message) {
47+
return message;
48+
}
49+
50+
/**
51+
* Gets the authenticated user's email. If the user is not authenticated, this will return an HTTP
52+
* 401.
53+
*
54+
* Note that name is not specified. This will default to "{class name}.{method name}". For
55+
* example, the default is "echo.getUserEmail".
56+
*
57+
* Note that httpMethod is not required here. Without httpMethod, this will default to GET due
58+
* to the API method name. httpMethod is added here for example purposes.
59+
*/
60+
@ApiMethod(httpMethod = ApiMethod.HttpMethod.GET)
61+
public Email getUserEmail(User user) throws UnauthorizedException {
62+
if (user == null) {
63+
throw new UnauthorizedException();
64+
}
65+
66+
Email response = new Email();
67+
response.setEmail(user.getEmail());
68+
return response;
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2016 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain a
6+
* 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.example.echo;
18+
19+
/** The email bean that will be used in the getUserEmail response. */
20+
public class Email {
21+
private String email;
22+
23+
public String getEmail() {
24+
return this.email;
25+
}
26+
27+
public void setEmail(String email) {
28+
this.email = email;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2016 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain a
6+
* 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.example.echo;
18+
19+
/** The message bean that will be used in the echo request and response. */
20+
public class Message {
21+
22+
private String message;
23+
24+
public String getMessage() {
25+
return this.message;
26+
}
27+
28+
public void setMessage(String message) {
29+
this.message = message;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2016 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain a
6+
* 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.example.echo;
18+
19+
import com.google.api.server.spi.ServiceException;
20+
21+
/**
22+
* Throw this when the user is unauthorized.
23+
*
24+
* Note that this must inherit from ServiceException.
25+
*/
26+
public class UnauthorizedException extends ServiceException {
27+
public UnauthorizedException() {
28+
super(401, "Unauthorized");
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2016 Google Inc. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
18+
<application>${endpoints.project.id}</application>
19+
<version>1</version>
20+
<threadsafe>true</threadsafe>
21+
22+
<basic-scaling>
23+
<max-instances>2</max-instances>
24+
</basic-scaling>
25+
26+
<system-properties>
27+
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
28+
</system-properties>
29+
</appengine-web-app>

0 commit comments

Comments
 (0)