Skip to content

Commit b1b5cb1

Browse files
committed
Merge pull request #1 from GoogleCloudPlatform/master
New
2 parents 69a28d3 + d3e4955 commit b1b5cb1

File tree

24 files changed

+1338
-2
lines changed

24 files changed

+1338
-2
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
language: java
22
jdk:
3-
- oraclejdk7
4-
env: GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/service-account.json
3+
- oraclejdk8
4+
env:
5+
- GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/service-account.json GCLOUD_PROJECT=cloud-samples-tests
56
before_install:
67
- openssl aes-256-cbc -K $encrypted_99d8b304f94b_key -iv $encrypted_99d8b304f94b_iv -in service-account.json.enc -out service-account.json -d
78

appengine/appidentity/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Eclipse files
2+
.project
3+
.classpath
4+
.settings
5+
6+
# Target folders
7+
target/

appengine/appidentity/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# App Identity sample for Google App Engine
2+
This sample demonstrates how to use the App Identity APIs on Google App Engine
3+
4+
## Running locally
5+
This example uses the
6+
[Maven gcloud plugin](https://cloud.google.com/appengine/docs/java/managed-vms/maven).
7+
To run this sample locally:
8+
9+
$ mvn gcloud:run
10+
11+
## Deploying
12+
In the following command, replace YOUR-PROJECT-ID with your
13+
[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber).
14+
15+
$ mvn gcloud:deploy -Dgcloud.gcloud_project=YOUR-PROJECT-ID
16+
17+
## Setup
18+
To save your project settings so that you don't need to enter the
19+
`-Dgcloud.gcloud_project=YOUR-CLOUD-PROJECT-ID` parameters, you can:
20+
21+
1. Update the <application> tag in src/main/webapp/WEB-INF/appengine-web.xml
22+
with your project name.
23+
24+
You will now be able to run
25+
26+
$ mvn gcloud:deploy
27+
28+
without the need for any additional parameters.

appengine/appidentity/pom.xml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2015 Google Inc. All Rights Reserved.
4+
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+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<packaging>war</packaging>
21+
<version>1.0-SNAPSHOT</version>
22+
<groupId>com.example.appengine</groupId>
23+
<artifactId>appidentity</artifactId>
24+
25+
<properties>
26+
<appengine.target.version>1.9.30</appengine.target.version>
27+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28+
</properties>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>com.google.appengine</groupId>
33+
<artifactId>appengine-api-1.0-sdk</artifactId>
34+
<version>${appengine.target.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.google.guava</groupId>
38+
<artifactId>guava</artifactId>
39+
<version>19.0</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>javax.servlet</groupId>
43+
<artifactId>servlet-api</artifactId>
44+
<version>2.5</version>
45+
<type>jar</type>
46+
<scope>provided</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.json</groupId>
50+
<artifactId>json</artifactId>
51+
<version>20151123</version>
52+
</dependency>
53+
54+
<!-- Test Dependencies -->
55+
<dependency>
56+
<groupId>junit</groupId>
57+
<artifactId>junit</artifactId>
58+
<version>4.10</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.mockito</groupId>
63+
<artifactId>mockito-all</artifactId>
64+
<version>1.10.19</version>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>com.google.appengine</groupId>
69+
<artifactId>appengine-testing</artifactId>
70+
<version>${appengine.target.version}</version>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>com.google.appengine</groupId>
75+
<artifactId>appengine-api-stubs</artifactId>
76+
<version>${appengine.target.version}</version>
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>com.google.appengine</groupId>
81+
<artifactId>appengine-tools-sdk</artifactId>
82+
<version>${appengine.target.version}</version>
83+
<scope>test</scope>
84+
</dependency>
85+
<dependency>
86+
<groupId>com.google.truth</groupId>
87+
<artifactId>truth</artifactId>
88+
<version>0.27</version>
89+
<scope>test</scope>
90+
</dependency>
91+
</dependencies>
92+
<build>
93+
<!-- for hot reload of the web application -->
94+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
95+
<plugins>
96+
<plugin>
97+
<groupId>org.apache.maven.plugins</groupId>
98+
<version>3.3</version>
99+
<artifactId>maven-compiler-plugin</artifactId>
100+
<configuration>
101+
<source>1.7</source>
102+
<target>1.7</target>
103+
</configuration>
104+
</plugin>
105+
<plugin>
106+
<groupId>com.google.appengine</groupId>
107+
<artifactId>gcloud-maven-plugin</artifactId>
108+
<version>2.0.9.90.v20151210</version>
109+
</plugin>
110+
</plugins>
111+
</build>
112+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2015 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+
package com.example.appengine.appidentity;
17+
18+
import com.google.apphosting.api.ApiProxy;
19+
import com.google.apphosting.api.ApiProxy.Environment;
20+
21+
import java.io.IOException;
22+
import java.io.PrintWriter;
23+
24+
import javax.servlet.http.HttpServlet;
25+
import javax.servlet.http.HttpServletRequest;
26+
import javax.servlet.http.HttpServletResponse;
27+
28+
@SuppressWarnings("serial")
29+
public class IdentityServlet extends HttpServlet {
30+
31+
// [START versioned_hostnames]
32+
@Override
33+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
34+
resp.setContentType("text/plain");
35+
ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
36+
resp.getWriter().print("default_version_hostname: ");
37+
resp.getWriter()
38+
.println(env.getAttributes().get("com.google.appengine.runtime.default_version_hostname"));
39+
}
40+
// [END versioned_hostnames]
41+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
package com.example.appengine.appidentity;
17+
18+
import com.google.appengine.api.appidentity.AppIdentityService;
19+
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
20+
import com.google.common.io.CharStreams;
21+
22+
import org.json.JSONObject;
23+
import org.json.JSONTokener;
24+
25+
import java.io.InputStream;
26+
import java.io.InputStreamReader;
27+
import java.io.OutputStreamWriter;
28+
import java.net.HttpURLConnection;
29+
import java.net.URL;
30+
import java.nio.charset.StandardCharsets;
31+
import java.util.ArrayList;
32+
33+
@SuppressWarnings("serial")
34+
class UrlShortener {
35+
// [START asserting_identity_to_Google_APIs]
36+
/**
37+
* Returns a shortened URL by calling the Google URL Shortener API.
38+
*
39+
* <p>Note: Error handling elided for simplicity.
40+
*/
41+
public String createShortUrl(String longUrl) throws Exception {
42+
ArrayList<String> scopes = new ArrayList<String>();
43+
scopes.add("https://www.googleapis.com/auth/urlshortener");
44+
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
45+
AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes);
46+
// The token asserts the identity reported by appIdentity.getServiceAccountName()
47+
JSONObject request = new JSONObject();
48+
request.put("longUrl", longUrl);
49+
50+
URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?pp=1");
51+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
52+
connection.setDoOutput(true);
53+
connection.setRequestMethod("POST");
54+
connection.addRequestProperty("Content-Type", "application/json");
55+
connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken());
56+
57+
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
58+
request.write(writer);
59+
writer.close();
60+
61+
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
62+
// Note: Should check the content-encoding.
63+
// Any JSON parser can be used; this one is used for illustrative purposes.
64+
JSONTokener response_tokens = new JSONTokener(connection.getInputStream());
65+
JSONObject response = new JSONObject(response_tokens);
66+
return (String) response.get("id");
67+
} else {
68+
try (InputStream s = connection.getErrorStream();
69+
InputStreamReader r = new InputStreamReader(s, StandardCharsets.UTF_8)) {
70+
throw new RuntimeException(String.format(
71+
"got error (%d) response %s from %s",
72+
connection.getResponseCode(),
73+
CharStreams.toString(r),
74+
connection.toString()));
75+
}
76+
}
77+
}
78+
// [END asserting_identity_to_Google_APIs]
79+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
package com.example.appengine.appidentity;
17+
18+
import com.google.appengine.api.users.UserService;
19+
import com.google.appengine.api.users.UserServiceFactory;
20+
21+
import java.io.IOException;
22+
import java.io.PrintWriter;
23+
import java.net.URLDecoder;
24+
25+
import javax.servlet.http.HttpServlet;
26+
import javax.servlet.http.HttpServletRequest;
27+
import javax.servlet.http.HttpServletResponse;
28+
29+
@SuppressWarnings("serial")
30+
public class UrlShortenerServlet extends HttpServlet {
31+
private final UrlShortener shortener;
32+
33+
public UrlShortenerServlet() {
34+
shortener = new UrlShortener();
35+
}
36+
37+
@Override
38+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
39+
PrintWriter w = resp.getWriter();
40+
w.println("<!DOCTYPE html>");
41+
w.println("<meta charset=\"utf-8\">");
42+
w.println("<title>Asserting Identity to Google APIs - App Engine App Identity Example</title>");
43+
w.println("<form method=\"post\">");
44+
w.println("<label for=\"longUrl\">URL:</label>");
45+
w.println("<input id=\"longUrl\" name=\"longUrl\" type=\"text\">");
46+
w.println("<input type=\"submit\" value=\"Shorten\">");
47+
w.println("</form>");
48+
}
49+
50+
@Override
51+
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
52+
resp.setContentType("text/plain");
53+
String longUrl = req.getParameter("longUrl");
54+
if (longUrl == null) {
55+
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "missing longUrl parameter");
56+
return;
57+
}
58+
59+
String shortUrl;
60+
PrintWriter w = resp.getWriter();
61+
try {
62+
shortUrl = shortener.createShortUrl(longUrl);
63+
} catch (Exception e) {
64+
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
65+
w.println("error shortening URL: " + longUrl);
66+
e.printStackTrace(w);
67+
return;
68+
}
69+
70+
w.print("long URL: ");
71+
w.println(longUrl);
72+
w.print("short URL: ");
73+
w.println(shortUrl);
74+
}
75+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
3+
<application>YOUR-PROJECT-ID</application>
4+
<threadsafe>true</threadsafe>
5+
<vm>true</vm>
6+
</appengine-web-app>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5+
version="2.5">
6+
<servlet>
7+
<servlet-name>appidentity</servlet-name>
8+
<servlet-class>com.example.appengine.appidentity.IdentityServlet</servlet-class>
9+
</servlet>
10+
<servlet>
11+
<servlet-name>urlshortener</servlet-name>
12+
<servlet-class>com.example.appengine.appidentity.UrlShortenerServlet</servlet-class>
13+
</servlet>
14+
<servlet-mapping>
15+
<servlet-name>appidentity</servlet-name>
16+
<url-pattern>/</url-pattern>
17+
</servlet-mapping>
18+
<servlet-mapping>
19+
<servlet-name>urlshortener</servlet-name>
20+
<url-pattern>/shorten</url-pattern>
21+
</servlet-mapping>
22+
</web-app>

0 commit comments

Comments
 (0)