From 316ed87550c92c265783f07a88a1a02e93f90668 Mon Sep 17 00:00:00 2001 From: costin Date: Wed, 9 Feb 2011 17:56:39 +0000 Subject: [PATCH] Add another option for sending URLs via an XMPP bot. Experimental - I'm testing. --- .../chrometophone/server/SendServlet.java | 4 +- .../chrometophone/server/XMPPSendServlet.java | 91 +++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 appengine/src/com/google/android/chrometophone/server/XMPPSendServlet.java diff --git a/appengine/src/com/google/android/chrometophone/server/SendServlet.java b/appengine/src/com/google/android/chrometophone/server/SendServlet.java index 5b0c30c..419f225 100644 --- a/appengine/src/com/google/android/chrometophone/server/SendServlet.java +++ b/appengine/src/com/google/android/chrometophone/server/SendServlet.java @@ -33,7 +33,7 @@ import com.google.appengine.api.users.User; @SuppressWarnings("serial") public class SendServlet extends HttpServlet { - private static final Logger log = + static final Logger log = Logger.getLogger(SendServlet.class.getName()); private static final String OK_STATUS = "OK"; private static final String LOGIN_REQUIRED_STATUS = "LOGIN_REQUIRED"; @@ -79,7 +79,7 @@ public class SendServlet extends HttpServlet { } } - private boolean doSendToDevice(String url, String title, String sel, String userAccount, + protected 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 device. diff --git a/appengine/src/com/google/android/chrometophone/server/XMPPSendServlet.java b/appengine/src/com/google/android/chrometophone/server/XMPPSendServlet.java new file mode 100644 index 0000000..0972a05 --- /dev/null +++ b/appengine/src/com/google/android/chrometophone/server/XMPPSendServlet.java @@ -0,0 +1,91 @@ +/* + * Copyright 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.chrometophone.server; + +import java.io.IOException; +import java.net.URLDecoder; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import com.google.appengine.api.users.User; +import com.google.appengine.api.xmpp.JID; +import com.google.appengine.api.xmpp.Message; +import com.google.appengine.api.xmpp.XMPPService; +import com.google.appengine.api.xmpp.XMPPServiceFactory; + +/** + * Receives XMPP messages sent to chrometophone@appspot.com or + * foo@chrometophone.appspotchat.com. + * + * TODO: we can use the address to identify a specific device ( phoneID@... ) + * + * The sender is trusted - authenticated with its XMPP client/server. + * + * The chat message can be either a simple URL, or the same as what SendServlet + * expects, i.e. form-data encoded name/value pairs. + */ +@SuppressWarnings("serial") +public class XMPPSendServlet extends SendServlet { + + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + XMPPService xmpp = XMPPServiceFactory.getXMPPService(); + Message message = xmpp.parseMessage(req); + + JID fromJid = message.getFromJid(); + String body = message.getBody(); + + String jid = fromJid.getId(); + int resIdx = jid.indexOf("/"); + if (resIdx > 0) { + jid = jid.substring(0, resIdx); + } + + Map params = new HashMap(); + String[] bodyParts = body.split("&"); + for (String part: bodyParts) { + String[] keyValue = part.split("="); + if (keyValue.length > 1) { + params.put(keyValue[0], URLDecoder.decode(keyValue[1], "UTF-8")); + } + } + + String sel = params.get("sel"); + if (sel == null) sel = ""; // optional + + String title = params.get("title"); + if (title == null) title = ""; // optional + + String url = params.get("url"); + if (url == null) { + // Assume the body is the URL - will be sent to all + // registered devices. + url = body; + } + + String deviceName = params.get("deviceName"); + String deviceType = params.get("deviceType"); + + + log.info("Sending " + jid); + doSendToDevice(url, title, sel, jid, + deviceName, deviceType, resp); + } +}