1 |
torben |
1165 |
package dk.thoerup.traininfoservice; |
2 |
|
|
|
3 |
|
|
import java.io.IOException; |
4 |
|
|
import java.io.PrintWriter; |
5 |
|
|
|
6 |
|
|
import javax.servlet.ServletException; |
7 |
|
|
import javax.servlet.http.HttpServlet; |
8 |
|
|
import javax.servlet.http.HttpServletRequest; |
9 |
|
|
import javax.servlet.http.HttpServletResponse; |
10 |
|
|
|
11 |
|
|
import com.google.appengine.api.xmpp.JID; |
12 |
|
|
import com.google.appengine.api.xmpp.Message; |
13 |
|
|
import com.google.appengine.api.xmpp.MessageBuilder; |
14 |
|
|
import com.google.appengine.api.xmpp.SendResponse; |
15 |
|
|
import com.google.appengine.api.xmpp.XMPPService; |
16 |
|
|
import com.google.appengine.api.xmpp.XMPPServiceFactory; |
17 |
|
|
|
18 |
|
|
public class XmppServlet extends HttpServlet { |
19 |
|
|
private static final long serialVersionUID = 1L; |
20 |
|
|
|
21 |
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
22 |
|
|
|
23 |
|
|
PrintWriter out = response.getWriter(); |
24 |
|
|
|
25 |
|
|
String msgBody = request.getParameter("message"); |
26 |
|
|
if (msgBody == null || msgBody.trim().length() == 0) { |
27 |
|
|
out.print("Forgot to write message parameter"); |
28 |
|
|
return; |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
JID jid = new JID("hoerup1981@gmail.com"); |
32 |
|
|
|
33 |
|
|
Message msg = new MessageBuilder() |
34 |
|
|
.withRecipientJids(jid) |
35 |
|
|
.withBody(msgBody) |
36 |
|
|
.build(); |
37 |
|
|
|
38 |
|
|
boolean messageSent = false; |
39 |
|
|
XMPPService xmpp = XMPPServiceFactory.getXMPPService(); |
40 |
|
|
if (xmpp.getPresence(jid).isAvailable()) { |
41 |
|
|
SendResponse status = xmpp.sendMessage(msg); |
42 |
|
|
messageSent = (status.getStatusMap().get(jid) == SendResponse.Status.SUCCESS); |
43 |
|
|
|
44 |
|
|
if (messageSent) { |
45 |
|
|
out.print("Message sent"); |
46 |
|
|
} else { |
47 |
|
|
out.print("Could not send message"); |
48 |
|
|
} |
49 |
|
|
} else { |
50 |
|
|
out.print("user not available"); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
|
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
} |