001: package org.claros.chat.ajax;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005:
006: import javax.servlet.ServletException;
007: import javax.servlet.http.HttpServlet;
008: import javax.servlet.http.HttpServletRequest;
009: import javax.servlet.http.HttpServletResponse;
010:
011: import org.jivesoftware.smack.XMPPConnection;
012: import org.jivesoftware.smack.packet.Presence;
013: import org.jivesoftware.smack.packet.Presence.Mode;
014:
015: public class Status extends HttpServlet {
016:
017: /**
018: *
019: */
020: private static final long serialVersionUID = -831691288847101256L;
021:
022: /**
023: * Constructor of the object.
024: */
025: public Status() {
026: super ();
027: }
028:
029: /**
030: * The doGet method of the servlet. <br>
031: *
032: * This method is called when a form has its tag value method equals to get.
033: *
034: * @param request the request send by the client to the server
035: * @param response the response send by the server to the client
036: * @throws ServletException if an error occurred
037: * @throws IOException if an error occurred
038: */
039: public void doGet(HttpServletRequest request,
040: HttpServletResponse response) throws ServletException,
041: IOException {
042: doPost(request, response);
043: }
044:
045: /**
046: * The doPost method of the servlet. <br>
047: *
048: * This method is called when a form has its tag value method equals to post.
049: *
050: * @param request the request send by the client to the server
051: * @param response the response send by the server to the client
052: * @throws ServletException if an error occurred
053: * @throws IOException if an error occurred
054: */
055: public void doPost(HttpServletRequest request,
056: HttpServletResponse response) throws ServletException,
057: IOException {
058: response.setHeader("Expires", "-1");
059: response.setHeader("Pragma", "no-cache");
060: response.setHeader("Cache-control", "no-cache");
061: response.setHeader("Content-Type", "text/xml; charset=utf-8");
062: PrintWriter out = response.getWriter();
063: out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
064:
065: XMPPConnection conn = (XMPPConnection) request.getSession()
066: .getAttribute("conn");
067: String act = request.getParameter("act");
068: if (act.equals("load")) {
069: Presence prs = null;
070: if (prs != null) {
071: Mode md = prs.getMode();
072: String msg = (prs.getStatus() != null) ? prs
073: .getStatus() : "";
074: String stattxt = "offline";
075:
076: if (md != null) {
077: if (md.equals(Presence.Mode.AVAILABLE)) {
078: stattxt = "available";
079: } else if (md.equals(Presence.Mode.AWAY)) {
080: stattxt = "away";
081: } else if (md.equals(Presence.Mode.CHAT)) {
082: stattxt = "chat";
083: } else if (md.equals(Presence.Mode.DO_NOT_DISTURB)) {
084: stattxt = "disturb";
085: } else if (md.equals(Presence.Mode.EXTENDED_AWAY)) {
086: stattxt = "extended_away";
087: } else if (md.equals(Presence.Mode.INVISIBLE)) {
088: stattxt = "invisible";
089: }
090: }
091:
092: out.write("<data>");
093: out.write("<status>" + stattxt + "</status>");
094: out.write("<message>" + msg + "</message>");
095: out.write("</data>");
096: }
097: } else if (act.equals("save")) {
098: String newStat = request.getParameter("newstat");
099: String newMsg = request.getParameter("newstatmsg");
100:
101: Presence pr = new Presence(Presence.Type.AVAILABLE);
102: if (newMsg != null) {
103: pr.setStatus(newMsg);
104: }
105: if (newStat != null) {
106: if (newStat.equals("available")) {
107: pr.setMode(Presence.Mode.AVAILABLE);
108: } else if (newStat.equals("away")) {
109: pr.setMode(Presence.Mode.AWAY);
110: } else if (newStat.equals("disturb")) {
111: pr.setMode(Presence.Mode.DO_NOT_DISTURB);
112: }
113: conn.sendPacket(pr);
114: }
115: out.write("<data><result>ok</result></data>");
116: }
117:
118: out.flush();
119: out.close();
120: }
121: }
|