From 85fb6b2cc4a8299883605a7098dfe16d2c5b29e4 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Mon, 7 Dec 2015 16:56:52 -0800 Subject: [PATCH 1/4] Add an App Identity example. This moves the first example from: https://cloud.google.com/appengine/docs/java/appidentity/ to GitHub. I also add a simple unit test to at least make sure this code builds & runs. Since this sample just wraps: ApiProxy.getCurrentEnvironment() .getAttributes() .get("com.google.appengine.runtime.default_version_hostname")) there isn't really much more we can test in this example. I will move the other examples from that page over in later PRs. --- appengine/appidentity/.gitignore | 7 ++ appengine/appidentity/README.md | 12 +++ appengine/appidentity/pom.xml | 102 ++++++++++++++++++ .../appidentity/IdentityServlet.java | 41 +++++++ .../src/main/webapp/WEB-INF/appengine-web.xml | 6 ++ .../src/main/webapp/WEB-INF/web.xml | 14 +++ .../appidentity/IdentityServletTest.java | 76 +++++++++++++ pom.xml | 1 + 8 files changed, 259 insertions(+) create mode 100644 appengine/appidentity/.gitignore create mode 100644 appengine/appidentity/README.md create mode 100644 appengine/appidentity/pom.xml create mode 100644 appengine/appidentity/src/main/java/com/example/appengine/appidentity/IdentityServlet.java create mode 100644 appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml create mode 100644 appengine/appidentity/src/main/webapp/WEB-INF/web.xml create mode 100644 appengine/appidentity/src/test/java/com/example/appengine/appidentity/IdentityServletTest.java diff --git a/appengine/appidentity/.gitignore b/appengine/appidentity/.gitignore new file mode 100644 index 00000000000..471339729ae --- /dev/null +++ b/appengine/appidentity/.gitignore @@ -0,0 +1,7 @@ +# Eclipse files +.project +.classpath +.settings + +# Target folders +target/ diff --git a/appengine/appidentity/README.md b/appengine/appidentity/README.md new file mode 100644 index 00000000000..62755293871 --- /dev/null +++ b/appengine/appidentity/README.md @@ -0,0 +1,12 @@ +# App Identity sample for Google App Engine +This sample demonstrates how to use the App Identity APIs on Google App Engine + +## Setup +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 + +## Running locally + $ mvn appengine:devserver + +## Deploying + $ mvn appengine:update diff --git a/appengine/appidentity/pom.xml b/appengine/appidentity/pom.xml new file mode 100644 index 00000000000..1bd1f5495b6 --- /dev/null +++ b/appengine/appidentity/pom.xml @@ -0,0 +1,102 @@ + + + + 4.0.0 + war + 1.0-SNAPSHOT + com.example.appengine + appidentity + + + 1.9.30 + UTF-8 + + + + + com.google.appengine + appengine-api-1.0-sdk + ${appengine.target.version} + + + javax.servlet + servlet-api + 2.5 + jar + provided + + + + + junit + junit + 4.10 + test + + + org.mockito + mockito-all + 1.10.19 + test + + + com.google.appengine + appengine-testing + ${appengine.target.version} + test + + + com.google.appengine + appengine-api-stubs + ${appengine.target.version} + test + + + com.google.appengine + appengine-tools-sdk + ${appengine.target.version} + test + + + com.google.truth + truth + 0.27 + test + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + org.apache.maven.plugins + 3.3 + maven-compiler-plugin + + 1.7 + 1.7 + + + + com.google.appengine + appengine-maven-plugin + 1.9.28 + + + + diff --git a/appengine/appidentity/src/main/java/com/example/appengine/appidentity/IdentityServlet.java b/appengine/appidentity/src/main/java/com/example/appengine/appidentity/IdentityServlet.java new file mode 100644 index 00000000000..3464ed47e17 --- /dev/null +++ b/appengine/appidentity/src/main/java/com/example/appengine/appidentity/IdentityServlet.java @@ -0,0 +1,41 @@ +/** + * Copyright 2015 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.appidentity; + +import com.google.apphosting.api.ApiProxy; +import com.google.apphosting.api.ApiProxy.Environment; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@SuppressWarnings("serial") +public class IdentityServlet extends HttpServlet { + + // [START versioned_hostnames] + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + resp.setContentType("text/plain"); + ApiProxy.Environment env = ApiProxy.getCurrentEnvironment(); + resp.getWriter().print("default_version_hostname: "); + resp.getWriter() + .println(env.getAttributes().get("com.google.appengine.runtime.default_version_hostname")); + } + // [END versioned_hostnames] +} diff --git a/appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..c9e245399bf --- /dev/null +++ b/appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,6 @@ + + + YOUR-PROJECT-ID + YOUR-VERSION-ID + true + diff --git a/appengine/appidentity/src/main/webapp/WEB-INF/web.xml b/appengine/appidentity/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..09472a93622 --- /dev/null +++ b/appengine/appidentity/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,14 @@ + + + + appidentity + com.example.appengine.appidentity.IdentityServlet + + + appidentity + / + + diff --git a/appengine/appidentity/src/test/java/com/example/appengine/appidentity/IdentityServletTest.java b/appengine/appidentity/src/test/java/com/example/appengine/appidentity/IdentityServletTest.java new file mode 100644 index 00000000000..c0d32159cfe --- /dev/null +++ b/appengine/appidentity/src/test/java/com/example/appengine/appidentity/IdentityServletTest.java @@ -0,0 +1,76 @@ +/** + * Copyright 2015 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.appidentity; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.google.appengine.tools.development.ApiProxyLocal; +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; +import com.google.apphosting.api.ApiProxy; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.File; +import java.io.PrintWriter; +import java.io.StringWriter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** Unit tests for {@link IdentityServlet}. */ +@RunWith(JUnit4.class) +public class IdentityServletTest { + + // Set up a helper so that the ApiProxy returns a valid environment for local testing. + private final LocalServiceTestHelper helper = new LocalServiceTestHelper(); + + @Mock private HttpServletRequest mockRequest; + @Mock private HttpServletResponse mockResponse; + private StringWriter responseWriter; + private IdentityServlet servletUnderTest; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + helper.setUp(); + + // Set up a fake HTTP response. + responseWriter = new StringWriter(); + when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); + + servletUnderTest = new IdentityServlet(); + } + + @Test + public void doGet_defaultEnvironment_writesResponse() throws Exception { + servletUnderTest.doGet(mockRequest, mockResponse); + + // We don't have any guarantee over what the local App Engine environment returns for + // "com.google.appengine.runtime.default_version_hostname". Only assert that the response + // contains part of the string we have control over. + assertThat(responseWriter.toString()) + .named("IdentityServlet response") + .contains("default_version_hostname:"); + } +} diff --git a/pom.xml b/pom.xml index 0d5f3ea374c..551c70afea7 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ + appengine/appidentity taskqueue/deferred unittests bigquery From 580b669f983dd97732ddbd7ae4dac99ea022b0a5 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Fri, 11 Dec 2015 15:57:15 -0800 Subject: [PATCH 2/4] Add instrcutions to override and tags. Since many users may want to run with multiple projects/versions (dev, staging, prod) document how to deploy this sample without having to modify the application-web.xml file. --- appengine/appidentity/README.md | 21 ++++++++++++++++++--- appengine/appidentity/pom.xml | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/appengine/appidentity/README.md b/appengine/appidentity/README.md index 62755293871..89a32c48402 100644 --- a/appengine/appidentity/README.md +++ b/appengine/appidentity/README.md @@ -1,12 +1,27 @@ # App Identity sample for Google App Engine This sample demonstrates how to use the App Identity APIs on Google App Engine +## Running locally + $ mvn appengine:devserver + +## Deploying +In the following command, replace YOUR-PROJECT-ID with your +[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber) +and YOUR-VERSION with a suitable version identifier. + + $ mvn appengine:update -Dappengine.appId=YOUR-PROJECT-ID -Dappengine.version=YOUR-VERSION + ## Setup +To save your project settings so that you don't need to enter the +`-Dappengine.appId=YOUR-CLOUD-PROJECT-ID` or +`-Dappengine.version=YOUR-VERSION-NAME` parameters, you can make the following +changes: + 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 -## Running locally - $ mvn appengine:devserver +You will now be able to run -## Deploying $ mvn appengine:update + +without the need for any additional paramters. diff --git a/appengine/appidentity/pom.xml b/appengine/appidentity/pom.xml index 1bd1f5495b6..d212b44fc33 100644 --- a/appengine/appidentity/pom.xml +++ b/appengine/appidentity/pom.xml @@ -95,7 +95,7 @@ Copyright 2015 Google Inc. All Rights Reserved. com.google.appengine appengine-maven-plugin - 1.9.28 + ${appengine.target.version} From a4111dd92322743414236da83018126867ca5a81 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Mon, 14 Dec 2015 11:24:24 -0800 Subject: [PATCH 3/4] Changed App Identity sample to use Managed VMs. --- appengine/appidentity/README.md | 15 +++++++++------ appengine/appidentity/pom.xml | 4 ++-- .../src/main/webapp/WEB-INF/appengine-web.xml | 1 + 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/appengine/appidentity/README.md b/appengine/appidentity/README.md index 89a32c48402..95f7dfd1ff2 100644 --- a/appengine/appidentity/README.md +++ b/appengine/appidentity/README.md @@ -2,26 +2,29 @@ This sample demonstrates how to use the App Identity APIs on Google App Engine ## Running locally - $ mvn appengine:devserver +This example uses the +[Maven gcloud plugin](https://cloud.google.com/appengine/docs/java/managed-vms/maven). +To run this sample locally: + + $ mvn gcloud:run ## Deploying In the following command, replace YOUR-PROJECT-ID with your [Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber) and YOUR-VERSION with a suitable version identifier. - $ mvn appengine:update -Dappengine.appId=YOUR-PROJECT-ID -Dappengine.version=YOUR-VERSION + $ mvn gcloud:deploy -Dversion=YOUR-VERSION -Dgcloud_project=YOUR-PROJECT-ID ## Setup To save your project settings so that you don't need to enter the -`-Dappengine.appId=YOUR-CLOUD-PROJECT-ID` or -`-Dappengine.version=YOUR-VERSION-NAME` parameters, you can make the following -changes: +`-Dgcloud_project=YOUR-CLOUD-PROJECT-ID` or `-Dversion=YOUR-VERSION-NAME` +parameters, you can make the following changes: 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 You will now be able to run - $ mvn appengine:update + $ mvn gcloud:deploy without the need for any additional paramters. diff --git a/appengine/appidentity/pom.xml b/appengine/appidentity/pom.xml index d212b44fc33..27559481a88 100644 --- a/appengine/appidentity/pom.xml +++ b/appengine/appidentity/pom.xml @@ -94,8 +94,8 @@ Copyright 2015 Google Inc. All Rights Reserved. com.google.appengine - appengine-maven-plugin - ${appengine.target.version} + gcloud-maven-plugin + 2.0.9.90.v20151210 diff --git a/appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml index c9e245399bf..c3df08fec7d 100644 --- a/appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml +++ b/appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml @@ -3,4 +3,5 @@ YOUR-PROJECT-ID YOUR-VERSION-ID true + true From 1051a59507df9a1f033c2000cf8f916926848e03 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Mon, 14 Dec 2015 11:58:55 -0800 Subject: [PATCH 4/4] Removed -Dversion parameter for App Identity example. This flag seems to be ignored by the gcloud plugin and the Managed VMs environment creates a new version if one isn't provided, anyway. --- appengine/appidentity/README.md | 12 +++++------- .../src/main/webapp/WEB-INF/appengine-web.xml | 1 - 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/appengine/appidentity/README.md b/appengine/appidentity/README.md index 95f7dfd1ff2..29b6beedf0f 100644 --- a/appengine/appidentity/README.md +++ b/appengine/appidentity/README.md @@ -10,18 +10,16 @@ To run this sample locally: ## Deploying In the following command, replace YOUR-PROJECT-ID with your -[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber) -and YOUR-VERSION with a suitable version identifier. +[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber). - $ mvn gcloud:deploy -Dversion=YOUR-VERSION -Dgcloud_project=YOUR-PROJECT-ID + $ mvn gcloud:deploy -Dgcloud_project=YOUR-PROJECT-ID ## Setup To save your project settings so that you don't need to enter the -`-Dgcloud_project=YOUR-CLOUD-PROJECT-ID` or `-Dversion=YOUR-VERSION-NAME` -parameters, you can make the following changes: +`-Dgcloud_project=YOUR-CLOUD-PROJECT-ID` parameters, you can: -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. Update the tag in src/main/webapp/WEB-INF/appengine-web.xml + with your project name. You will now be able to run diff --git a/appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml index c3df08fec7d..21d1d476a5f 100644 --- a/appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml +++ b/appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml @@ -1,7 +1,6 @@ YOUR-PROJECT-ID - YOUR-VERSION-ID true true