Add per-device 'debug' option, allow OAuth auth, more GET->POST conversions.

This commit is contained in:
costin
2010-06-21 22:37:07 +00:00
parent 714a16b463
commit 82d9b03eb9
4 changed files with 83 additions and 15 deletions

View File

@@ -32,11 +32,22 @@ public class DeviceInfo {
@Persistent
private String deviceRegistrationID;
@Persistent
private boolean debug;
public DeviceInfo(Key key, String deviceRegistrationID) {
this.key = key;
this.deviceRegistrationID = deviceRegistrationID;
}
public boolean getDebug() {
return debug;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public Key getKey() {
return key;
}

View File

@@ -27,6 +27,8 @@ import javax.servlet.http.HttpServletResponse;
import com.google.android.c2dm.server.C2DMessaging;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.oauth.OAuthService;
import com.google.appengine.api.oauth.OAuthServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
@@ -38,13 +40,50 @@ public class RegisterServlet extends HttpServlet {
private static final String OK_STATUS = "OK";
private static final String ERROR_STATUS = "ERROR";
/**
* Get the user using the UserService.
*
* If not logged in, return an error message.
*
* @return user, or null if not logged in.
* @throws IOException
*/
static User checkUser(HttpServletRequest req, HttpServletResponse resp,
boolean errorIfNotLoggedIn) throws IOException {
// Is it OAuth ?
User user = null;
OAuthService oauthService = OAuthServiceFactory.getOAuthService();
try {
user = oauthService.getCurrentUser();
if (user != null) {
log.info("Found OAuth user " + user);
return user;
}
} catch (Throwable t) {
user = null;
}
UserService userService = UserServiceFactory.getUserService();
user = userService.getCurrentUser();
if (user == null && errorIfNotLoggedIn) {
// TODO: redirect to OAuth/user service login, or send the URL
// TODO: 401 instead of 400
resp.setStatus(400);
resp.getWriter().println(ERROR_STATUS + " (Not authorized)");
}
return user;
}
/**
* @deprecated will be removed in next rel.
*/
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
doGet(req, resp);
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
doPost(req, resp);
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
String deviceRegistrationID = req.getParameter("devregid");
@@ -54,9 +93,7 @@ public class RegisterServlet extends HttpServlet {
return;
}
// Authorize & store device info
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
User user = checkUser(req, resp, true);
if (user != null) {
Key key = KeyFactory.createKey(DeviceInfo.class.getSimpleName(), user.getEmail());
DeviceInfo device = new DeviceInfo(key, deviceRegistrationID);
@@ -73,9 +110,6 @@ public class RegisterServlet extends HttpServlet {
} finally {
pm.close();
}
} else {
resp.setStatus(400);
resp.getWriter().println(ERROR_STATUS + " (Not authorized)");
}
}
}

View File

@@ -76,8 +76,7 @@ public class SendServlet extends HttpServlet {
return;
}
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
User user = RegisterServlet.checkUser(req, resp, false);
if (user != null) {
doSendToPhone(url, title, sel, user.getEmail(), apiVersion, resp);
} else {
@@ -88,6 +87,7 @@ public class SendServlet extends HttpServlet {
URLEncoder.encode(title, "UTF-8") +
"&url=" + URLEncoder.encode(url, "UTF-8") +
"&sel=" + URLEncoder.encode(sel, "UTF-8");
UserService userService = UserServiceFactory.getUserService();
resp.sendRedirect(userService.createLoginURL(followOnURL));
}
}
@@ -120,10 +120,25 @@ public class SendServlet extends HttpServlet {
// Send push message to phone
C2DMessaging push = C2DMessaging.get(getServletContext());
if (push.sendNoRetry(deviceInfo.getDeviceRegistrationID(),
"" + url.hashCode(), "url", url, "title", title,
"sel", sel)) {
log.info("Link sent to phone!");
boolean res = false;
String collapseKey = "" + url.hashCode();
if (deviceInfo.getDebug()) {
res = push.sendNoRetry(deviceInfo.getDeviceRegistrationID(),
collapseKey,
"url", url,
"title", title,
"sel", sel,
"debug", "1");
} else {
res = push.sendNoRetry(deviceInfo.getDeviceRegistrationID(),
collapseKey,
"url", url,
"title", title,
"sel", sel);
}
if (res) {
log.info("Link sent to phone! collapse_key:" + collapseKey);
resp.getWriter().println(OK_STATUS);
return true;
} else {

View File

@@ -39,8 +39,16 @@ public class UnregisterServlet extends HttpServlet {
private static final String OK_STATUS = "OK";
private static final String ERROR_STATUS = "ERROR";
/**
* @deprecated Will be removed in next rel cycle.
*/
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
doPost(req, resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
String deviceRegistrationID = req.getParameter("devregid");