From c4f0a3b18e012ee9b91f105280a5b5e6b4928a8f Mon Sep 17 00:00:00 2001 From: Shun Fan Date: Wed, 20 Apr 2016 16:38:52 -0700 Subject: [PATCH] Add remote api sample --- appengine/remote/README.md | 26 +++++++ appengine/remote/remote-client/pom.xml | 78 +++++++++++++++++++ .../appengine/remote/RemoteApiExample.java | 50 ++++++++++++ appengine/remote/remote-server/pom.xml | 69 ++++++++++++++++ .../appengine/remote/RemoteServlet.java | 36 +++++++++ .../src/main/webapp/WEB-INF/appengine-web.xml | 21 +++++ .../src/main/webapp/WEB-INF/web.xml | 39 ++++++++++ pom.xml | 4 +- 8 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 appengine/remote/README.md create mode 100644 appengine/remote/remote-client/pom.xml create mode 100644 appengine/remote/remote-client/src/main/java/com/example/appengine/remote/RemoteApiExample.java create mode 100644 appengine/remote/remote-server/pom.xml create mode 100644 appengine/remote/remote-server/src/main/java/com/example/appengine/remote/RemoteServlet.java create mode 100644 appengine/remote/remote-server/src/main/webapp/WEB-INF/appengine-web.xml create mode 100644 appengine/remote/remote-server/src/main/webapp/WEB-INF/web.xml diff --git a/appengine/remote/README.md b/appengine/remote/README.md new file mode 100644 index 00000000000..83c3ac9ce68 --- /dev/null +++ b/appengine/remote/README.md @@ -0,0 +1,26 @@ +# Google App Engine Standard Environment Remote API Sample + +This sample demonstrates how to access App Engine Standard Environment APIs remotely, +using the [Remote API](https://cloud.google.com/appengine/docs/java/tools/remoteapi). + +## Set up the server component of Remote API +1. Navigate to the remote-server directory +1. Update the `` tag in `src/main/webapp/WEB-INF/appengine-web.xml` + with your project name. +1. Update the `` tag in `src/main/webapp/WEB-INF/appengine-web.xml` + with your version name. +1. Deploy the app + `mvn appengine:update` +1. Alternatively, run the app locally with + `mvn appengine:devserver` +## Set up the client component of Remote API +1. Navigate to the remote-client directory +1. Update the SERVER_STRING constant with the deployed app name + `src/main/java/com/example/appengine/remote/RemoteApiExample.java` + 1. If you deployed the app, it should be "YOUR-APP-ID.appspot.com" + 1. If you are running on the development server, it should be "localhost" +1. Package the app as a jar + `mvn clean package` +1. Excute the jar file + java -jar appengine-remote-client-1.0-SNAPSHOT-jar-with-dependencies.jar + diff --git a/appengine/remote/remote-client/pom.xml b/appengine/remote/remote-client/pom.xml new file mode 100644 index 00000000000..6c1780730ac --- /dev/null +++ b/appengine/remote/remote-client/pom.xml @@ -0,0 +1,78 @@ + + + 4.0.0 + jar + 1.0-SNAPSHOT + com.example.appengine + appengine-remote-client + + + doc-samples + com.google.cloud + 1.0.0 + ../../.. + + + + + com.google.appengine + appengine-remote-api + ${appengine.sdk.version} + + + com.google.appengine + appengine-api-1.0-sdk + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + + maven-assembly-plugin + + + package + + single + + + + + + + com.example.appengine.remote.RemoteApiExample + + + + jar-with-dependencies + + + + + org.apache.maven.plugins + 3.3 + maven-compiler-plugin + + 1.7 + 1.7 + + + + + diff --git a/appengine/remote/remote-client/src/main/java/com/example/appengine/remote/RemoteApiExample.java b/appengine/remote/remote-client/src/main/java/com/example/appengine/remote/RemoteApiExample.java new file mode 100644 index 00000000000..9c717f26712 --- /dev/null +++ b/appengine/remote/remote-client/src/main/java/com/example/appengine/remote/RemoteApiExample.java @@ -0,0 +1,50 @@ +/** + * 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. + */ + +package com.example.appengine.remote; + +import com.google.appengine.api.datastore.DatastoreService; +import com.google.appengine.api.datastore.DatastoreServiceFactory; +import com.google.appengine.api.datastore.Entity; +import com.google.appengine.tools.remoteapi.RemoteApiInstaller; +import com.google.appengine.tools.remoteapi.RemoteApiOptions; + +import java.io.IOException; + +// [START example] +public class RemoteApiExample { + private static final String SERVER_STRING = "YOUR-APP-ID.appspot.com"; + + public static void main(String[] args) throws IOException { + RemoteApiOptions options; + if (SERVER_STRING.equals("localhost")) { + options = new RemoteApiOptions().server(SERVER_STRING, + 8080).useDevelopmentServerCredential(); + } else { + options = new RemoteApiOptions().server(SERVER_STRING, + 443).useApplicationDefaultCredential(); + } + RemoteApiInstaller installer = new RemoteApiInstaller(); + installer.install(options); + try { + DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); + System.out.println("Key of new entity is " + ds.put(new Entity("Hello Remote API!"))); + } finally { + installer.uninstall(); + } + } +} +//[END example] \ No newline at end of file diff --git a/appengine/remote/remote-server/pom.xml b/appengine/remote/remote-server/pom.xml new file mode 100644 index 00000000000..450e28e3285 --- /dev/null +++ b/appengine/remote/remote-server/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + war + 1.0-SNAPSHOT + com.example.appengine + appengine-remote-server + + + doc-samples + com.google.cloud + 1.0.0 + ../../.. + + + + + com.google.appengine + appengine-api-1.0-sdk + + + org.apache.httpcomponents + httpclient + 4.5.1 + + + javax.servlet + javax.servlet-api + 3.1.0 + jar + provided + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + + com.google.appengine + appengine-maven-plugin + ${appengine.sdk.version} + + + org.apache.maven.plugins + 3.3 + maven-compiler-plugin + + 1.7 + 1.7 + + + + + diff --git a/appengine/remote/remote-server/src/main/java/com/example/appengine/remote/RemoteServlet.java b/appengine/remote/remote-server/src/main/java/com/example/appengine/remote/RemoteServlet.java new file mode 100644 index 00000000000..45f910cddcc --- /dev/null +++ b/appengine/remote/remote-server/src/main/java/com/example/appengine/remote/RemoteServlet.java @@ -0,0 +1,36 @@ +/** + * 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. + */ + +package com.example.appengine.remote; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +// [START example] +@SuppressWarnings("serial") +public class RemoteServlet extends HttpServlet { + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + PrintWriter out = resp.getWriter(); + out.println("Hello, world"); + } +} +// [END example] diff --git a/appengine/remote/remote-server/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/remote/remote-server/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..089c609c550 --- /dev/null +++ b/appengine/remote/remote-server/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,21 @@ + + + + + + YOUR-PROJECT-ID + YOUR-VERSION-ID + true + + diff --git a/appengine/remote/remote-server/src/main/webapp/WEB-INF/web.xml b/appengine/remote/remote-server/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..6da6fe9bffa --- /dev/null +++ b/appengine/remote/remote-server/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,39 @@ + + + + + + + Remote API Servlet + RemoteApiServlet + com.google.apphosting.utils.remoteapi.RemoteApiServlet + 1 + + + RemoteApiServlet + /remote_api + + + remote + com.example.appengine.remote.RemoteServlet + + + remote + / + + + diff --git a/pom.xml b/pom.xml index 741e29e6b06..084aa542b65 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,7 @@ - 1.9.34 + 1.9.36 1 1.19.0 1.19.0 @@ -51,6 +51,8 @@ appengine/memcache appengine/oauth2 appengine/sendgrid + appengine/remote/remote-client + appengine/remote/remote-server appengine/static-files appengine/twilio appengine/urlfetch