001: package org.claros.chat.ajax;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.List;
006:
007: import javax.servlet.ServletException;
008: import javax.servlet.http.HttpServlet;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011:
012: import org.claros.chat.controllers.PreferencesController;
013: import org.claros.chat.models.Preference;
014: import org.jivesoftware.smack.XMPPConnection;
015: import org.jivesoftware.smackx.packet.VCard;
016:
017: public class Preferences extends HttpServlet {
018: /**
019: *
020: */
021: private static final long serialVersionUID = 7576677600733897150L;
022:
023: /**
024: * Constructor of the object.
025: */
026: public Preferences() {
027: super ();
028: }
029:
030: public void doGet(HttpServletRequest request,
031: HttpServletResponse response) throws ServletException,
032: IOException {
033: doPost(request, response);
034: }
035:
036: /**
037: * The doPost method of the servlet. <br>
038: *
039: * This method is called when a form has its tag value method equals to post.
040: *
041: * @param request the request send by the client to the server
042: * @param response the response send by the server to the client
043: * @throws ServletException if an error occurred
044: * @throws IOException if an error occurred
045: */
046: public void doPost(HttpServletRequest request,
047: HttpServletResponse response) throws ServletException,
048: IOException {
049: response.setHeader("Expires", "-1");
050: response.setHeader("Pragma", "no-cache");
051: response.setHeader("Cache-control", "no-cache");
052: response.setHeader("Content-Type", "text/xml; charset=utf-8");
053: PrintWriter out = response.getWriter();
054:
055: String user = (String) request.getSession()
056: .getAttribute("user");
057: String act = (String) request.getParameter("act");
058: if (user != null) {
059: if (act.equals("load")) {
060: try {
061: List prefs = PreferencesController
062: .getPreferencesByUser(user);
063: if (prefs != null) {
064: out
065: .write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
066: out.write("<data>");
067:
068: XMPPConnection conn = (XMPPConnection) request
069: .getSession().getAttribute("conn");
070: VCard vCard = new VCard();
071: vCard.load(conn);
072: String nickname = vCard.getNickName();
073: String firstName = vCard.getFirstName();
074: String middleName = vCard.getMiddleName();
075: String lastName = vCard.getLastName();
076: String fullName = vCard.getField("FN");
077:
078: if (fullName == null) {
079: if (firstName != null && lastName != null) {
080: if (middleName != null) {
081: fullName = firstName + " "
082: + middleName + " "
083: + lastName;
084: } else {
085: fullName = firstName + " "
086: + middleName + " "
087: + lastName;
088: }
089: } else if (nickname != null) {
090: fullName = nickname;
091: } else {
092: fullName = user;
093: }
094: }
095: out.write("<fullname>" + fullName
096: + "</fullname>");
097:
098: Preference p = null;
099: for (int i = 0; i < prefs.size(); i++) {
100: p = (Preference) prefs.get(i);
101: out.write("<preference>");
102: out.write("<key>" + p.getPrefKey()
103: + "</key>");
104: out.write("<value>" + p.getPrefValue()
105: + "</value>");
106: out.write("</preference>");
107: }
108: out.write("</data>");
109: }
110: } catch (Exception e) {
111: e.printStackTrace();
112: }
113: } else if (act.equals("save")) {
114: try {
115: String fullName = new String(request.getParameter(
116: "fullName").getBytes("iso-8859-9"), "utf-8");
117: XMPPConnection conn = (XMPPConnection) request
118: .getSession().getAttribute("conn");
119: VCard vcard = new VCard();
120: vcard.load(conn);
121: vcard.setField("FN", fullName);
122: vcard.save(conn);
123: } catch (Exception e) {
124: e.printStackTrace();
125: }
126:
127: try {
128: String awayTimeout = request
129: .getParameter("awayTimeout");
130: PreferencesController
131: .savePreference(new Preference(user,
132: "awayTimeout", awayTimeout));
133: } catch (Exception e) {
134: e.printStackTrace();
135: }
136:
137: try {
138: String animations = request
139: .getParameter("animations");
140: PreferencesController
141: .savePreference(new Preference(user,
142: "animations", animations));
143: } catch (Exception e) {
144: e.printStackTrace();
145: }
146:
147: try {
148: String soundAlert = request
149: .getParameter("soundAlert");
150: PreferencesController
151: .savePreference(new Preference(user,
152: "soundAlert", soundAlert));
153: } catch (Exception e) {
154: e.printStackTrace();
155: }
156: out.print("<result>ok</result>");
157: }
158:
159: }
160: out.flush();
161: out.close();
162: }
163: }
|