diff --git a/appengine/appidentity/pom.xml b/appengine/appidentity/pom.xml index 27559481a88..c0012f2c939 100644 --- a/appengine/appidentity/pom.xml +++ b/appengine/appidentity/pom.xml @@ -33,6 +33,11 @@ Copyright 2015 Google Inc. All Rights Reserved. appengine-api-1.0-sdk ${appengine.target.version} + + com.google.guava + guava + 19.0 + javax.servlet servlet-api @@ -40,6 +45,11 @@ Copyright 2015 Google Inc. All Rights Reserved. jar provided + + org.json + json + 20151123 + diff --git a/appengine/appidentity/src/main/java/com/example/appengine/appidentity/UrlShortener.java b/appengine/appidentity/src/main/java/com/example/appengine/appidentity/UrlShortener.java new file mode 100644 index 00000000000..cecebe82d6a --- /dev/null +++ b/appengine/appidentity/src/main/java/com/example/appengine/appidentity/UrlShortener.java @@ -0,0 +1,79 @@ +/** + * 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.appidentity; + +import com.google.appengine.api.appidentity.AppIdentityService; +import com.google.appengine.api.appidentity.AppIdentityServiceFactory; +import com.google.common.io.CharStreams; + +import org.json.JSONObject; +import org.json.JSONTokener; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; + +@SuppressWarnings("serial") +class UrlShortener { + // [START asserting_identity_to_Google_APIs] + /** + * Returns a shortened URL by calling the Google URL Shortener API. + * + *

Note: Error handling elided for simplicity. + */ + public String createShortUrl(String longUrl) throws Exception { + ArrayList scopes = new ArrayList(); + scopes.add("https://www.googleapis.com/auth/urlshortener"); + AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService(); + AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes); + // The token asserts the identity reported by appIdentity.getServiceAccountName() + JSONObject request = new JSONObject(); + request.put("longUrl", longUrl); + + URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?pp=1"); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDoOutput(true); + connection.setRequestMethod("POST"); + connection.addRequestProperty("Content-Type", "application/json"); + connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken()); + + OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); + request.write(writer); + writer.close(); + + if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { + // Note: Should check the content-encoding. + // Any JSON parser can be used; this one is used for illustrative purposes. + JSONTokener response_tokens = new JSONTokener(connection.getInputStream()); + JSONObject response = new JSONObject(response_tokens); + return (String) response.get("id"); + } else { + try (InputStream s = connection.getErrorStream(); + InputStreamReader r = new InputStreamReader(s, StandardCharsets.UTF_8)) { + throw new RuntimeException(String.format( + "got error (%d) response %s from %s", + connection.getResponseCode(), + CharStreams.toString(r), + connection.toString())); + } + } + } + // [END asserting_identity_to_Google_APIs] +} diff --git a/appengine/appidentity/src/main/java/com/example/appengine/appidentity/UrlShortenerServlet.java b/appengine/appidentity/src/main/java/com/example/appengine/appidentity/UrlShortenerServlet.java new file mode 100644 index 00000000000..8bab40e8115 --- /dev/null +++ b/appengine/appidentity/src/main/java/com/example/appengine/appidentity/UrlShortenerServlet.java @@ -0,0 +1,75 @@ +/** + * 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.appidentity; + +import com.google.appengine.api.users.UserService; +import com.google.appengine.api.users.UserServiceFactory; + +import java.io.IOException; +import java.io.PrintWriter; +import java.net.URLDecoder; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@SuppressWarnings("serial") +public class UrlShortenerServlet extends HttpServlet { + private final UrlShortener shortener; + + public UrlShortenerServlet() { + shortener = new UrlShortener(); + } + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + PrintWriter w = resp.getWriter(); + w.println(""); + w.println(""); + w.println("Asserting Identity to Google APIs - App Engine App Identity Example"); + w.println("

"); + w.println(""); + w.println(""); + w.println(""); + w.println("
"); + } + + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + resp.setContentType("text/plain"); + String longUrl = req.getParameter("longUrl"); + if (longUrl == null) { + resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "missing longUrl parameter"); + return; + } + + String shortUrl; + PrintWriter w = resp.getWriter(); + try { + shortUrl = shortener.createShortUrl(longUrl); + } catch (Exception e) { + resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + w.println("error shortening URL: " + longUrl); + e.printStackTrace(w); + return; + } + + w.print("long URL: "); + w.println(longUrl); + w.print("short URL: "); + w.println(shortUrl); + } +} diff --git a/appengine/appidentity/src/main/webapp/WEB-INF/web.xml b/appengine/appidentity/src/main/webapp/WEB-INF/web.xml index 09472a93622..3ebbce0e290 100644 --- a/appengine/appidentity/src/main/webapp/WEB-INF/web.xml +++ b/appengine/appidentity/src/main/webapp/WEB-INF/web.xml @@ -7,8 +7,16 @@ appidentity com.example.appengine.appidentity.IdentityServlet + + urlshortener + com.example.appengine.appidentity.UrlShortenerServlet + appidentity / + + urlshortener + /shorten +