Add another option for sending URLs via an XMPP bot. Experimental - I'm testing.

This commit is contained in:
costin
2011-02-09 17:56:39 +00:00
parent f630fdb96c
commit 316ed87550
2 changed files with 93 additions and 2 deletions

View File

@@ -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.

View File

@@ -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<String, String> params = new HashMap<String, String>();
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);
}
}