Fix JDO so it returns blank not null strings. All unregistration to prune multiple devices (in case duplicates were accidentally introduced inan unorthodox upgrade. Misc cleanups.

This commit is contained in:
burke.davey
2010-09-16 23:28:25 +00:00
parent c0a48ad43a
commit ac5cb5d77d
5 changed files with 43 additions and 39 deletions

View File

@@ -97,6 +97,24 @@ public class DeviceInfo {
this.key = key;
}
public Key getKey() {
return key;
}
public void setKey(Key key) {
this.key = key;
}
// Accessor methods for properties added later (hence can be null)
public String getDeviceRegistrationID() {
return deviceRegistrationID;
}
public void setDeviceRegistrationID(String deviceRegistrationID) {
this.deviceRegistrationID = deviceRegistrationID;
}
public boolean getDebug() {
return (debug != null ? debug.booleanValue() : false);
}
@@ -114,28 +132,12 @@ public class DeviceInfo {
this.phoneToChromeExperimentEnabled = new Boolean(phoneToChromeExperimentEnabled);
}
public Key getKey() {
return key;
}
public void setKey(Key key) {
this.key = key;
}
public String getDeviceRegistrationID() {
return deviceRegistrationID;
}
public void setDeviceRegistrationID(String deviceRegistrationID) {
this.deviceRegistrationID = deviceRegistrationID;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
return type != null ? type : "";
}
public void setName(String name) {
@@ -143,7 +145,7 @@ public class DeviceInfo {
}
public String getName() {
return name;
return name != null ? name : "";
}
public void setRegistrationTimestamp(Date registrationTimestamp) {
@@ -165,7 +167,7 @@ public class DeviceInfo {
List<DeviceInfo> qresult = (List<DeviceInfo>) query.execute();
// Copy to array - we need to close the query
List<DeviceInfo> result = new ArrayList<DeviceInfo>();
for (DeviceInfo di: qresult) {
for (DeviceInfo di : qresult) {
result.add(di);
}
query.closeAll();

View File

@@ -160,7 +160,7 @@ public class RegisterServlet extends HttpServlet {
device.setName(deviceName); // update display name
if (device.getType() != null && device.getType().equals(DeviceInfo.TYPE_CHROME)) {
if (device.getType().equals(DeviceInfo.TYPE_CHROME)) {
if (device.getPhoneToChromeExperimentEnabled()) {
String channelId =
ChannelServiceFactory.getChannelService().createChannel(deviceRegistrationId);

View File

@@ -68,7 +68,6 @@ public class SendServlet extends HttpServlet {
String deviceName = req.getParameter("deviceName");
String deviceType = req.getParameter("deviceType");
if (deviceType == null) deviceType = DeviceInfo.TYPE_AC2DM; // default
User user = RegisterServlet.checkUser(req, resp, false);
if (user != null) {
@@ -82,7 +81,7 @@ public class SendServlet extends HttpServlet {
private boolean doSendToDevice(String url, String title, String sel, String userAccount,
String deviceName, String deviceType, HttpServletResponse resp) throws IOException {
// ok = we sent to at least one phone.
// ok = we sent to at least one device.
boolean ok = false;
// Send push message to phone
@@ -101,29 +100,22 @@ public class SendServlet extends HttpServlet {
pm.close();
}
if (registrations.size() == 0) {
log.warning("Device not registered " + userAccount);
resp.getWriter().println(DEVICE_NOT_REGISTERED_STATUS);
return false;
}
int numSendAttempts = 0;
for (DeviceInfo deviceInfo : registrations) {
if (deviceName != null && deviceInfo.getName() != null &&
!deviceName.equals(deviceInfo.getName())) {
if (deviceName != null && !deviceName.equals(deviceInfo.getName())) {
continue; // user-specified device name
}
if (deviceType != null && deviceInfo.getType() != null &&
!deviceType.equals(deviceInfo.getType())) {
if (deviceType != null && !deviceType.equals(deviceInfo.getType())) {
continue; // user-specified device type
}
// if name or value are null - they'll be skipped
try {
if (deviceInfo.getType() != null && deviceInfo.getType().equals(DeviceInfo.TYPE_CHROME)) {
if (deviceInfo.getType().equals(DeviceInfo.TYPE_CHROME)) {
res = doSendViaBrowserChannel(url, deviceInfo);
} else {
res = doSendViaC2dm(url, title, sel, push, collapseKey, deviceInfo);
}
numSendAttempts++;
if (res) {
log.info("Link sent to phone! collapse_key:" + collapseKey);
@@ -134,8 +126,8 @@ public class SendServlet extends HttpServlet {
} catch (IOException ex) {
if ("NotRegistered".equals(ex.getMessage()) ||
"InvalidRegistration".equals(ex.getMessage())) {
// remove registrations, it no longer works
pm.deletePersistent(deviceInfo);
// Prune device, it no longer works
pruneDevice(deviceInfo);
} else {
throw ex;
}
@@ -145,6 +137,10 @@ public class SendServlet extends HttpServlet {
if (ok) {
resp.getWriter().println(OK_STATUS);
return true;
} else if (numSendAttempts == 0) {
log.warning("Device not registered " + userAccount);
resp.getWriter().println(DEVICE_NOT_REGISTERED_STATUS);
return false;
} else {
resp.setStatus(500);
resp.getWriter().println(ERROR_STATUS + " (Unable to send link)");
@@ -152,6 +148,13 @@ public class SendServlet extends HttpServlet {
}
}
private void pruneDevice(DeviceInfo deviceInfo) {
PersistenceManager pm =
C2DMessaging.getPMF(getServletContext()).getPersistenceManager();
pm.deletePersistent(deviceInfo);
pm.close();
}
boolean doSendViaC2dm(String url, String title, String sel, C2DMessaging push,
String collapseKey, DeviceInfo deviceInfo) throws IOException {

View File

@@ -68,8 +68,7 @@ public class UnregisterServlet extends HttpServlet {
DeviceInfo deviceInfo = registrations.get(i);
if (deviceInfo.getDeviceRegistrationID().equals(deviceRegistrationID)) {
pm.deletePersistent(deviceInfo);
registrations.remove(i);
break;
// Keep looping in case of duplicates
}
}

View File

@@ -16,7 +16,7 @@
-->
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>chrometophone</application>
<version>6</version>
<version>7</version>
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>