mirror of
https://github.com/fergalmoran/chrometophone.git
synced 2025-12-22 09:41:51 +00:00
Better fix suggested by Dave: trim title first, trim sel if still needed. Refactored
the code a bit to be testable ( test needs a bit of cleanup, later ).
This commit is contained in:
@@ -64,6 +64,11 @@ public class C2DMessaging {
|
||||
|
||||
final C2DMConfigLoader serverConfig;
|
||||
|
||||
// Testing
|
||||
protected C2DMessaging() {
|
||||
serverConfig = null;
|
||||
}
|
||||
|
||||
private C2DMessaging(C2DMConfigLoader serverConfig) {
|
||||
this.serverConfig = serverConfig;
|
||||
}
|
||||
|
||||
@@ -76,15 +76,8 @@ public class SendServlet extends HttpServlet {
|
||||
resp.getWriter().println(ERROR_STATUS + " (Must specify url and title parameters)");
|
||||
return;
|
||||
}
|
||||
if (title == null || url.length() + title.length() + sel.length() >
|
||||
1000) {
|
||||
// Shorten the title - C2DM has a 1024 limit, some padding for keys
|
||||
title = ""; // any better default ?
|
||||
if (url.length() + sel.length() > 1000) {
|
||||
sel = "";
|
||||
}
|
||||
// TODO: when we have history, save the url/title/sel in the history
|
||||
// and send a pointer, have device fetch it.
|
||||
if (title == null) {
|
||||
title = "";
|
||||
}
|
||||
|
||||
String deviceId = req.getParameter("deviceId");
|
||||
@@ -139,12 +132,7 @@ public class SendServlet extends HttpServlet {
|
||||
|
||||
// if name or value are null - they'll be skipped
|
||||
try {
|
||||
res = push.sendNoRetry(deviceInfo.getDeviceRegistrationID(),
|
||||
collapseKey,
|
||||
"url", url,
|
||||
"title", title,
|
||||
"sel", sel,
|
||||
"debug", deviceInfo.getDebug() ? "1" : null);
|
||||
res = doSend(url, title, sel, push, collapseKey, deviceInfo);
|
||||
|
||||
if (res) {
|
||||
log.info("Link sent to phone! collapse_key:" + collapseKey);
|
||||
@@ -173,4 +161,36 @@ public class SendServlet extends HttpServlet {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
boolean doSend(String url, String title, String sel, C2DMessaging push,
|
||||
String collapseKey, DeviceInfo deviceInfo) throws IOException {
|
||||
|
||||
// Trim title, sel if needed.
|
||||
if (url.length() + title.length() + sel.length() > 1000) {
|
||||
// Shorten the title - C2DM has a 1024 limit, some padding for keys
|
||||
if (title.length() > 16) {
|
||||
title = title.substring(0, 16);
|
||||
}
|
||||
// still not enough ?
|
||||
if (title.length() + url.length() + sel.length() > 1000) {
|
||||
// how much space we have for sel ?
|
||||
int space = 1000 - url.length() - title.length();
|
||||
if (space > 0 && sel.length() > space) {
|
||||
sel = sel.substring(0, space);
|
||||
} // else: we'll get an error sending
|
||||
}
|
||||
// TODO: when we have history, save the url/title/sel in the history
|
||||
// and send a pointer, have device fetch it.
|
||||
}
|
||||
|
||||
|
||||
boolean res;
|
||||
res = push.sendNoRetry(deviceInfo.getDeviceRegistrationID(),
|
||||
collapseKey,
|
||||
"url", url,
|
||||
"title", title,
|
||||
"sel", sel,
|
||||
"debug", deviceInfo.getDebug() ? "1" : null);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user