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.dnd)) {
084: stattxt = "disturb";
085: } else if (md.equals(Presence.Mode.xa)) {
086: stattxt = "extended_away";
087: }
088: }
089:
090: out.write("<data>");
091: out.write("<status>" + stattxt + "</status>");
092: out.write("<message>" + msg + "</message>");
093: out.write("</data>");
094: }
095: } else if (act.equals("save")) {
096: String newStat = request.getParameter("newstat");
097: String newMsg = request.getParameter("newstatmsg");
098:
099: Presence pr = new Presence(Presence.Type.available);
100: if (newMsg != null) {
101: pr.setStatus(newMsg);
102: }
103: if (newStat != null) {
104: if (newStat.equals("available")) {
105: pr.setMode(Presence.Mode.available);
106: } else if (newStat.equals("away")) {
107: pr.setMode(Presence.Mode.away);
108: } else if (newStat.equals("disturb")) {
109: pr.setMode(Presence.Mode.dnd);
110: }
111: conn.sendPacket(pr);
112: }
113: out.write("<data><result>ok</result></data>");
114: }
115:
116: out.flush();
117: out.close();
118: }
119: }
|