001: package org.claros.chat.ajax;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.Iterator;
006: import java.util.Locale;
007:
008: import javax.servlet.ServletException;
009: import javax.servlet.http.HttpServlet;
010: import javax.servlet.http.HttpServletRequest;
011: import javax.servlet.http.HttpServletResponse;
012:
013: import org.claros.chat.utility.Utility;
014: import org.jivesoftware.smack.RosterEntry;
015: import org.jivesoftware.smack.XMPPConnection;
016: import org.jivesoftware.smack.XMPPException;
017: import org.jivesoftware.smack.packet.Presence;
018:
019: public class Roaster extends HttpServlet {
020:
021: /**
022: *
023: */
024: private static final long serialVersionUID = -7926350754674872925L;
025: private static final Locale loc = new Locale("en", "US");
026:
027: /**
028: * Constructor of the object.
029: */
030: public Roaster() {
031: super ();
032: }
033:
034: /**
035: * The doGet method of the servlet. <br>
036: *
037: * This method is called when a form has its tag value method equals to get.
038: *
039: * @param request the request send by the client to the server
040: * @param response the response send by the server to the client
041: * @throws ServletException if an error occurred
042: * @throws IOException if an error occurred
043: */
044: public void doGet(HttpServletRequest request,
045: HttpServletResponse response) throws ServletException,
046: IOException {
047: doPost(request, response);
048: }
049:
050: /**
051: * The doPost method of the servlet. <br>
052: *
053: * This method is called when a form has its tag value method equals to post.
054: *
055: * @param request the request send by the client to the server
056: * @param response the response send by the server to the client
057: * @throws ServletException if an error occurred
058: * @throws IOException if an error occurred
059: */
060: public void doPost(HttpServletRequest request,
061: HttpServletResponse response) throws ServletException,
062: IOException {
063: response.setHeader("Expires", "-1");
064: response.setHeader("Pragma", "no-cache");
065: response.setHeader("Cache-control", "no-cache");
066:
067: response.setContentType("text/html");
068: PrintWriter out = response.getWriter();
069:
070: XMPPConnection conn = (XMPPConnection) request.getSession()
071: .getAttribute("conn");
072:
073: String act = request.getParameter("act");
074: if (act.equals("save")) {
075: String buddy = request.getParameter("newBuddy")
076: .toLowerCase(loc);
077: buddy = convertToJabber(buddy);
078:
079: Presence pr = new Presence(Presence.Type.SUBSCRIBE);
080: pr.setTo(buddy);
081: conn.sendPacket(pr);
082: } else if (act.equals("remove")) {
083: String buddy = request.getParameter("removeBuddy");
084: RosterEntry en = null;
085: for (Iterator i = conn.getRoster().getEntries(); i
086: .hasNext();) {
087: en = (RosterEntry) i.next();
088: if (en.getUser().equals(buddy)) {
089: try {
090: conn.getRoster().removeEntry(en);
091: } catch (XMPPException e) {
092: e.printStackTrace();
093: }
094: break;
095: }
096: }
097: }
098: out.println("ok");
099: }
100:
101: /**
102: *
103: * @param buddy
104: * @return
105: */
106: private static String convertToJabber(String buddy) {
107: if (buddy.indexOf("hotmail.com") > 0) {
108: int atPos = buddy.indexOf("@");
109: buddy = buddy.substring(0, atPos) + "%"
110: + buddy.substring(atPos + 1) + "@"
111: + Utility.msnTransport;
112: } else if (buddy.indexOf("icq.com") > 0) {
113: int atPos = buddy.indexOf("@");
114: buddy = buddy.substring(0, atPos) + "%"
115: + buddy.substring(atPos + 1) + "@"
116: + Utility.icqTransport;
117: } else if (buddy.indexOf("aol.com") > 0) {
118: int atPos = buddy.indexOf("@");
119: buddy = buddy.substring(0, atPos) + "%"
120: + buddy.substring(atPos + 1) + "@"
121: + Utility.aolTransport;
122: } else if (buddy.indexOf("yahoo.com") > 0) {
123: int atPos = buddy.indexOf("@");
124: buddy = buddy.substring(0, atPos) + "%"
125: + buddy.substring(atPos + 1) + "@"
126: + Utility.yahooTransport;
127: }
128: return buddy;
129: }
130: }
|