This source file includes following definitions.
- SuppressWarnings
- doGet
package com.example;
import java.io.*;
import java.net.*;
import java.util.HashSet;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;
import com.google.appengine.repackaged.org.json.JSONObject;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
@SuppressWarnings("serial")
public class HelloLicenseServlet extends HttpServlet {
public static final String APP_ID = "[INSERT APP ID HERE]";
private static final String TOKEN = "[INSERT TOKEN HERE]";
private static final String TOKEN_SECRET = "[INSERT TOKEN SECRET HERE]";
public static final String SERVER_URL =
"https://www.googleapis.com/chromewebstore/v1/licenses/%s/%s";
public static final String CONSUMER_KEY = "anonymous";
public static final String CONSUMER_SECRET = CONSUMER_KEY;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html; charset=UTF-8");
UserService userService = UserServiceFactory.getUserService();
PrintWriter output = response.getWriter();
String url = request.getRequestURI();
if (userService.isUserLoggedIn()) {
User user = userService.getCurrentUser();
output.printf(
"<strong>%s</strong> | <a href=\"%s\">Sign out</a><br><br>",
user.getEmail(),
userService.createLogoutURL(url)
);
try {
OAuthConsumer oauth =
new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
oauth.setTokenWithSecret(TOKEN, TOKEN_SECRET);
URLConnection http =
new URL(
String.format(
SERVER_URL,
APP_ID,
URLEncoder.encode(user.getFederatedIdentity(), "UTF-8")
)
).openConnection();
oauth.sign(http);
http.connect();
BufferedReader input =
new BufferedReader(new InputStreamReader(http.getInputStream()));
String file = "";
for (String line; (line = input.readLine()) != null; file += line);
input.close();
JSONObject json = new JSONObject(file);
output.printf(
"Hello <strong>%s</strong> license!",
"YES".equals(json.get("result")) ?
"FULL".equals(json.get("accessLevel")) ? "full" : "free trial" :
"no"
);
} catch (Exception exception) {
output.printf("Oops! <strong>%s</strong>", exception.getMessage());
}
} else {
output.printf(
"<a href=\"%s\">Sign in</a>",
userService.createLoginURL(
url,
null,
"https://www.google.com/accounts/o8/id",
new HashSet<String>()
)
);
}
}
}