diff --git a/appengine/remote/README.md b/appengine/remote/README.md new file mode 100644 index 00000000000..ae17b8bbf9f --- /dev/null +++ b/appengine/remote/README.md @@ -0,0 +1,24 @@ +# 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. Package the app as a jar + `mvn clean package` +1. Navigate to the target directory +1. Excute the jar file with the server connection string as the first argument + 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" + java -jar appengine-remote-client-1.0-SNAPSHOT-jar-with-dependencies.jar "YOUR-APP-NAME" + diff --git a/appengine/remote/remote-client/pom.xml b/appengine/remote/remote-client/pom.xml new file mode 100644 index 00000000000..7cc0db66a85 --- /dev/null +++ b/appengine/remote/remote-client/pom.xml @@ -0,0 +1,69 @@ + + + 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 + + + + + + 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..3a6c3b42b36 --- /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 { + + public static void main(String[] args) throws IOException { + String serverString = args[0]; + RemoteApiOptions options; + if (serverString.equals("localhost")) { + options = new RemoteApiOptions().server(serverString, + 8080).useDevelopmentServerCredential(); + } else { + options = new RemoteApiOptions().server(serverString, + 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] diff --git a/appengine/remote/remote-server/pom.xml b/appengine/remote/remote-server/pom.xml new file mode 100644 index 00000000000..4dd421113dc --- /dev/null +++ b/appengine/remote/remote-server/pom.xml @@ -0,0 +1,60 @@ + + + 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} + + + + 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 cf0cb626aab..623baa04a96 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,8 @@ appengine/oauth2 appengine/search appengine/sendgrid + appengine/remote/remote-client + appengine/remote/remote-server appengine/static-files appengine/twilio appengine/urlfetch