Skip to content

Commit 6dae2cb

Browse files
Shun Fanlesv
Shun Fan
authored andcommitted
Add remote api sample (GoogleCloudPlatform#170)
* Add remote api sample * Allow users to input server name from command line for remote sample
1 parent 771da44 commit 6dae2cb

File tree

8 files changed

+301
-0
lines changed

8 files changed

+301
-0
lines changed

appengine/remote/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Google App Engine Standard Environment Remote API Sample
2+
3+
This sample demonstrates how to access App Engine Standard Environment APIs remotely,
4+
using the [Remote API](https://cloud.google.com/appengine/docs/java/tools/remoteapi).
5+
6+
## Set up the server component of Remote API
7+
1. Navigate to the remote-server directory
8+
1. Update the `<application>` tag in `src/main/webapp/WEB-INF/appengine-web.xml`
9+
with your project name.
10+
1. Update the `<version>` tag in `src/main/webapp/WEB-INF/appengine-web.xml`
11+
with your version name.
12+
1. Deploy the app
13+
`mvn appengine:update`
14+
1. Alternatively, run the app locally with
15+
`mvn appengine:devserver`
16+
## Set up the client component of Remote API
17+
1. Package the app as a jar
18+
`mvn clean package`
19+
1. Navigate to the target directory
20+
1. Excute the jar file with the server connection string as the first argument
21+
1. If you deployed the app, it should be "YOUR-APP-ID.appspot.com"
22+
1. If you are running on the development server, it should be "localhost"
23+
java -jar appengine-remote-client-1.0-SNAPSHOT-jar-with-dependencies.jar "YOUR-APP-NAME"
24+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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>jar</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.appengine</groupId>
21+
<artifactId>appengine-remote-client</artifactId>
22+
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../../..</relativePath>
28+
</parent>
29+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
30+
<dependencies>
31+
<dependency>
32+
<groupId>com.google.appengine</groupId>
33+
<artifactId>appengine-remote-api</artifactId>
34+
<version>${appengine.sdk.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.google.appengine</groupId>
38+
<artifactId>appengine-api-1.0-sdk</artifactId>
39+
</dependency>
40+
</dependencies>
41+
<build>
42+
<!-- for hot reload of the web application -->
43+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
44+
<plugins>
45+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
46+
<plugin>
47+
<artifactId>maven-assembly-plugin</artifactId>
48+
<executions>
49+
<execution>
50+
<phase>package</phase>
51+
<goals>
52+
<goal>single</goal>
53+
</goals>
54+
</execution>
55+
</executions>
56+
<configuration>
57+
<archive>
58+
<manifest>
59+
<mainClass>com.example.appengine.remote.RemoteApiExample</mainClass>
60+
</manifest>
61+
</archive>
62+
<descriptorRefs>
63+
<descriptorRef>jar-with-dependencies</descriptorRef>
64+
</descriptorRefs>
65+
</configuration>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
17+
package com.example.appengine.remote;
18+
19+
import com.google.appengine.api.datastore.DatastoreService;
20+
import com.google.appengine.api.datastore.DatastoreServiceFactory;
21+
import com.google.appengine.api.datastore.Entity;
22+
import com.google.appengine.tools.remoteapi.RemoteApiInstaller;
23+
import com.google.appengine.tools.remoteapi.RemoteApiOptions;
24+
25+
import java.io.IOException;
26+
27+
// [START example]
28+
public class RemoteApiExample {
29+
30+
public static void main(String[] args) throws IOException {
31+
String serverString = args[0];
32+
RemoteApiOptions options;
33+
if (serverString.equals("localhost")) {
34+
options = new RemoteApiOptions().server(serverString,
35+
8080).useDevelopmentServerCredential();
36+
} else {
37+
options = new RemoteApiOptions().server(serverString,
38+
443).useApplicationDefaultCredential();
39+
}
40+
RemoteApiInstaller installer = new RemoteApiInstaller();
41+
installer.install(options);
42+
try {
43+
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
44+
System.out.println("Key of new entity is " + ds.put(new Entity("Hello Remote API!")));
45+
} finally {
46+
installer.uninstall();
47+
}
48+
}
49+
}
50+
//[END example]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
<groupId>com.example.appengine</groupId>
21+
<artifactId>appengine-remote-server</artifactId>
22+
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../../..</relativePath>
28+
</parent>
29+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
30+
<dependencies>
31+
<dependency>
32+
<groupId>com.google.appengine</groupId>
33+
<artifactId>appengine-api-1.0-sdk</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.apache.httpcomponents</groupId>
37+
<artifactId>httpclient</artifactId>
38+
<version>4.5.1</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>javax.servlet</groupId>
42+
<artifactId>javax.servlet-api</artifactId>
43+
<version>3.1.0</version>
44+
<type>jar</type>
45+
<scope>provided</scope>
46+
</dependency>
47+
</dependencies>
48+
<build>
49+
<!-- for hot reload of the web application -->
50+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
51+
<plugins>
52+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
53+
<plugin>
54+
<groupId>com.google.appengine</groupId>
55+
<artifactId>appengine-maven-plugin</artifactId>
56+
<version>${appengine.sdk.version}</version>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
17+
package com.example.appengine.remote;
18+
19+
import java.io.IOException;
20+
import java.io.PrintWriter;
21+
22+
import javax.servlet.http.HttpServlet;
23+
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.HttpServletResponse;
25+
26+
// [START example]
27+
@SuppressWarnings("serial")
28+
public class RemoteServlet extends HttpServlet {
29+
30+
@Override
31+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
32+
PrintWriter out = resp.getWriter();
33+
out.println("Hello, world");
34+
}
35+
}
36+
// [END example]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2016 Google Inc. All Rights Reserved.
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+
http://www.apache.org/licenses/LICENSE-2.0
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+
<!-- [END_EXCLUDE] -->
16+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
17+
<application>YOUR-PROJECT-ID</application>
18+
<version>YOUR-VERSION-ID</version>
19+
<threadsafe>true</threadsafe>
20+
</appengine-web-app>
21+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2016 Google Inc. All Rights Reserved.
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+
http://www.apache.org/licenses/LICENSE-2.0
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+
<!-- [END_EXCLUDE] -->
16+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
17+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
18+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
19+
version="2.5">
20+
<servlet>
21+
<display-name>Remote API Servlet</display-name>
22+
<servlet-name>RemoteApiServlet</servlet-name>
23+
<servlet-class>com.google.apphosting.utils.remoteapi.RemoteApiServlet</servlet-class>
24+
<load-on-startup>1</load-on-startup>
25+
</servlet>
26+
<servlet-mapping>
27+
<servlet-name>RemoteApiServlet</servlet-name>
28+
<url-pattern>/remote_api</url-pattern>
29+
</servlet-mapping>
30+
<servlet>
31+
<servlet-name>remote</servlet-name>
32+
<servlet-class>com.example.appengine.remote.RemoteServlet</servlet-class>
33+
</servlet>
34+
<servlet-mapping>
35+
<servlet-name>remote</servlet-name>
36+
<url-pattern>/</url-pattern>
37+
</servlet-mapping>
38+
</web-app>
39+

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
<module>appengine/oauth2</module>
5656
<module>appengine/search</module>
5757
<module>appengine/sendgrid</module>
58+
<module>appengine/remote/remote-client</module>
59+
<module>appengine/remote/remote-server</module>
5860
<module>appengine/static-files</module>
5961
<module>appengine/twilio</module>
6062
<module>appengine/urlfetch</module>

0 commit comments

Comments
 (0)