mirror of
https://github.com/fergalmoran/chrometophone.git
synced 2026-01-04 07:56:21 +00:00
Add per-device 'debug' option, allow OAuth auth, more GET->POST conversions.
This commit is contained in:
@@ -31,12 +31,23 @@ public class DeviceInfo {
|
|||||||
|
|
||||||
@Persistent
|
@Persistent
|
||||||
private String deviceRegistrationID;
|
private String deviceRegistrationID;
|
||||||
|
|
||||||
|
@Persistent
|
||||||
|
private boolean debug;
|
||||||
|
|
||||||
public DeviceInfo(Key key, String deviceRegistrationID) {
|
public DeviceInfo(Key key, String deviceRegistrationID) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.deviceRegistrationID = deviceRegistrationID;
|
this.deviceRegistrationID = deviceRegistrationID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getDebug() {
|
||||||
|
return debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDebug(boolean debug) {
|
||||||
|
this.debug = debug;
|
||||||
|
}
|
||||||
|
|
||||||
public Key getKey() {
|
public Key getKey() {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
import com.google.android.c2dm.server.C2DMessaging;
|
import com.google.android.c2dm.server.C2DMessaging;
|
||||||
import com.google.appengine.api.datastore.Key;
|
import com.google.appengine.api.datastore.Key;
|
||||||
import com.google.appengine.api.datastore.KeyFactory;
|
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.User;
|
||||||
import com.google.appengine.api.users.UserService;
|
import com.google.appengine.api.users.UserService;
|
||||||
import com.google.appengine.api.users.UserServiceFactory;
|
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 OK_STATUS = "OK";
|
||||||
private static final String ERROR_STATUS = "ERROR";
|
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
|
@Override
|
||||||
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||||
doGet(req, resp);
|
doPost(req, resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||||
resp.setContentType("text/plain");
|
resp.setContentType("text/plain");
|
||||||
|
|
||||||
String deviceRegistrationID = req.getParameter("devregid");
|
String deviceRegistrationID = req.getParameter("devregid");
|
||||||
@@ -54,9 +93,7 @@ public class RegisterServlet extends HttpServlet {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Authorize & store device info
|
User user = checkUser(req, resp, true);
|
||||||
UserService userService = UserServiceFactory.getUserService();
|
|
||||||
User user = userService.getCurrentUser();
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
Key key = KeyFactory.createKey(DeviceInfo.class.getSimpleName(), user.getEmail());
|
Key key = KeyFactory.createKey(DeviceInfo.class.getSimpleName(), user.getEmail());
|
||||||
DeviceInfo device = new DeviceInfo(key, deviceRegistrationID);
|
DeviceInfo device = new DeviceInfo(key, deviceRegistrationID);
|
||||||
@@ -73,9 +110,6 @@ public class RegisterServlet extends HttpServlet {
|
|||||||
} finally {
|
} finally {
|
||||||
pm.close();
|
pm.close();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
resp.setStatus(400);
|
|
||||||
resp.getWriter().println(ERROR_STATUS + " (Not authorized)");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,8 +76,7 @@ public class SendServlet extends HttpServlet {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
UserService userService = UserServiceFactory.getUserService();
|
User user = RegisterServlet.checkUser(req, resp, false);
|
||||||
User user = userService.getCurrentUser();
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
doSendToPhone(url, title, sel, user.getEmail(), apiVersion, resp);
|
doSendToPhone(url, title, sel, user.getEmail(), apiVersion, resp);
|
||||||
} else {
|
} else {
|
||||||
@@ -88,6 +87,7 @@ public class SendServlet extends HttpServlet {
|
|||||||
URLEncoder.encode(title, "UTF-8") +
|
URLEncoder.encode(title, "UTF-8") +
|
||||||
"&url=" + URLEncoder.encode(url, "UTF-8") +
|
"&url=" + URLEncoder.encode(url, "UTF-8") +
|
||||||
"&sel=" + URLEncoder.encode(sel, "UTF-8");
|
"&sel=" + URLEncoder.encode(sel, "UTF-8");
|
||||||
|
UserService userService = UserServiceFactory.getUserService();
|
||||||
resp.sendRedirect(userService.createLoginURL(followOnURL));
|
resp.sendRedirect(userService.createLoginURL(followOnURL));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -120,10 +120,25 @@ public class SendServlet extends HttpServlet {
|
|||||||
|
|
||||||
// Send push message to phone
|
// Send push message to phone
|
||||||
C2DMessaging push = C2DMessaging.get(getServletContext());
|
C2DMessaging push = C2DMessaging.get(getServletContext());
|
||||||
if (push.sendNoRetry(deviceInfo.getDeviceRegistrationID(),
|
boolean res = false;
|
||||||
"" + url.hashCode(), "url", url, "title", title,
|
String collapseKey = "" + url.hashCode();
|
||||||
"sel", sel)) {
|
if (deviceInfo.getDebug()) {
|
||||||
log.info("Link sent to phone!");
|
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);
|
resp.getWriter().println(OK_STATUS);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -39,8 +39,16 @@ public class UnregisterServlet extends HttpServlet {
|
|||||||
private static final String OK_STATUS = "OK";
|
private static final String OK_STATUS = "OK";
|
||||||
private static final String ERROR_STATUS = "ERROR";
|
private static final String ERROR_STATUS = "ERROR";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Will be removed in next rel cycle.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
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");
|
resp.setContentType("text/plain");
|
||||||
|
|
||||||
String deviceRegistrationID = req.getParameter("devregid");
|
String deviceRegistrationID = req.getParameter("devregid");
|
||||||
|
|||||||
Reference in New Issue
Block a user