1 /* 2 * The "Hello world!" of the Chrome Web Store Licensing API, in Java. This 3 * program logs the user in with OpenID, fetches their license state with OAuth, 4 * and prints one of these greetings as appropriate: 5 * 6 * 1. Hello *no* license! 7 * 2. Hello *free trial* license! 8 * 3. Hello *full* license! 9 * 10 * Copyright 2010 the Chromium Authors 11 * 12 * Use of this source code is governed by a BSD-style license that can be found 13 * in the "LICENSE" file. 14 * 15 * Brian Kennish <bkennish@chromium.org> 16 */ 17 package com.example; 18 19 import java.io.*; 20 import java.net.*; 21 import java.util.HashSet; 22 import javax.servlet.http.*; 23 import com.google.appengine.api.users.*; 24 import com.google.appengine.repackaged.org.json.JSONObject; 25 import oauth.signpost.OAuthConsumer; 26 import oauth.signpost.basic.DefaultOAuthConsumer; 27 28 /* A Google App Engine servlet. */ 29 @SuppressWarnings("serial") 30 public class HelloLicenseServlet extends HttpServlet { 31 /* TODO: The app ID from the Chrome Developer Dashboard. */ 32 public static final String APP_ID = "[INSERT APP ID HERE]"; 33 34 /* TODO: The token from the Chrome Developer Dashboard. */ 35 private static final String TOKEN = "[INSERT TOKEN HERE]"; 36 37 /* TODO: The token secret from the Chrome Developer Dashboard. */ 38 private static final String TOKEN_SECRET = "[INSERT TOKEN SECRET HERE]"; 39 40 /* 41 * The license server URL, where %s are placeholders for app and 42 * user IDs 43 */ 44 public static final String SERVER_URL = 45 "https://www.googleapis.com/chromewebstore/v1/licenses/%s/%s"; 46 47 /* The consumer key. */ 48 public static final String CONSUMER_KEY = "anonymous"; 49 50 /* The consumer secret. */ 51 public static final String CONSUMER_SECRET = CONSUMER_KEY; 52 53 /* Handles "GET" requests. */ doGet(HttpServletRequest request, HttpServletResponse response)54 public void doGet(HttpServletRequest request, HttpServletResponse response) 55 throws IOException { 56 response.setContentType("text/html; charset=UTF-8"); 57 UserService userService = UserServiceFactory.getUserService(); 58 PrintWriter output = response.getWriter(); 59 String url = request.getRequestURI(); 60 61 if (userService.isUserLoggedIn()) { 62 // Provide a logout path. 63 User user = userService.getCurrentUser(); 64 output.printf( 65 "<strong>%s</strong> | <a href=\"%s\">Sign out</a><br><br>", 66 user.getEmail(), 67 userService.createLogoutURL(url) 68 ); 69 70 try { 71 // Send a signed request for the user's license state. 72 OAuthConsumer oauth = 73 new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 74 oauth.setTokenWithSecret(TOKEN, TOKEN_SECRET); 75 URLConnection http = 76 new URL( 77 String.format( 78 SERVER_URL, 79 APP_ID, 80 URLEncoder.encode(user.getFederatedIdentity(), "UTF-8") 81 ) 82 ).openConnection(); 83 oauth.sign(http); 84 http.connect(); 85 86 // Convert the response from the license server to a string. 87 BufferedReader input = 88 new BufferedReader(new InputStreamReader(http.getInputStream())); 89 String file = ""; 90 for (String line; (line = input.readLine()) != null; file += line); 91 input.close(); 92 93 // Parse the string as JSON and display the license state. 94 JSONObject json = new JSONObject(file); 95 output.printf( 96 "Hello <strong>%s</strong> license!", 97 "YES".equals(json.get("result")) ? 98 "FULL".equals(json.get("accessLevel")) ? "full" : "free trial" : 99 "no" 100 ); 101 } catch (Exception exception) { 102 // Dump any error. 103 output.printf("Oops! <strong>%s</strong>", exception.getMessage()); 104 } 105 } else { // The user isn't logged in. 106 // Prompt for login. 107 output.printf( 108 "<a href=\"%s\">Sign in</a>", 109 userService.createLoginURL( 110 url, 111 null, 112 "https://www.google.com/accounts/o8/id", 113 new HashSet<String>() 114 ) 115 ); 116 } 117 } 118 } 119