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..29b6beedf0f --- /dev/null +++ b/appengine/appidentity/README.md @@ -0,0 +1,28 @@ +# App Identity sample for Google App Engine +This sample demonstrates how to use the App Identity APIs on Google App Engine + +## Running locally +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). + + $ 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` parameters, you can: + +1. Update the tag in src/main/webapp/WEB-INF/appengine-web.xml + with your project name. + +You will now be able to run + + $ mvn gcloud:deploy + +without the need for any additional paramters. diff --git a/appengine/appidentity/pom.xml b/appengine/appidentity/pom.xml new file mode 100644 index 00000000000..27559481a88 --- /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 + gcloud-maven-plugin + 2.0.9.90.v20151210 + + + + 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..21d1d476a5f --- /dev/null +++ b/appengine/appidentity/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,6 @@ + + + YOUR-PROJECT-ID + true + 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