001: /**
002: * $Revision$
003: * $Date$
004: *
005: * Copyright (C) 2006-2007 Jive Software. All rights reserved.
006: *
007: * This software is published under the terms of the GNU Public License (GPL),
008: * a copy of which is included in this distribution.
009: */package org.jivesoftware.openfire.gateway.protocols.xmpp.packet;
010:
011: import org.jivesoftware.smack.packet.IQ;
012: import org.jivesoftware.smack.packet.PacketExtension;
013: import org.jivesoftware.smack.provider.IQProvider;
014: import org.xmlpull.v1.XmlPullParser;
015: import org.xmlpull.v1.XmlPullParserException;
016: import org.apache.log4j.Logger;
017:
018: import java.util.Vector;
019: import java.util.Date;
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.io.IOException;
023:
024: /**
025: * See: http://code.google.com/apis/talk/jep_extensions/gmail.html
026: *
027: * @author mecevit
028: * @author Daniel Henninger
029: */
030: public class GoogleMailBoxPacket extends IQ {
031:
032: static Logger Log = Logger.getLogger(GoogleMailBoxPacket.class);
033:
034: public static String MAILBOX_ELEMENT = "mailbox";
035: public static String MAILBOX_NAMESPACE = "google:mail:notify";
036:
037: private Date resultTime;
038: private Integer totalMatched;
039: private Boolean totalIsEstimate;
040: private String url;
041: private Vector<GoogleMailThread> mailThreads = new Vector<GoogleMailThread>();
042:
043: public GoogleMailBoxPacket() {
044: }
045:
046: public void addExtension(PacketExtension extension) {
047: }
048:
049: public void addMailThread(GoogleMailThread thread) {
050: mailThreads.add(thread);
051: }
052:
053: public Vector<GoogleMailThread> getMailThreads() {
054: return mailThreads;
055: }
056:
057: public Date getResultTime() {
058: return resultTime;
059: }
060:
061: public void setResultTime(Date resultTime) {
062: this .resultTime = resultTime;
063: }
064:
065: public Integer getTotalMatched() {
066: return totalMatched;
067: }
068:
069: public void setTotalMatched(Integer totalMatched) {
070: this .totalMatched = totalMatched;
071: }
072:
073: public Boolean getTotalIsEstimate() {
074: return totalIsEstimate;
075: }
076:
077: public void setTotalIsEstimate(Boolean totalIsEstimate) {
078: this .totalIsEstimate = totalIsEstimate;
079: }
080:
081: public String getUrl() {
082: return url;
083: }
084:
085: public void setUrl(String url) {
086: this .url = url;
087: }
088:
089: public String getChildElementXML() {
090: StringBuffer buf = new StringBuffer();
091: buf.append("<mailbox xmlns=\"").append(MAILBOX_NAMESPACE)
092: .append("\"");
093: if (resultTime != null) {
094: buf.append(" result-time=\"").append(resultTime.getTime())
095: .append("\"");
096: }
097: if (totalMatched != null) {
098: buf.append(" total-matched=\"").append(totalMatched)
099: .append("\"");
100: }
101: if (totalIsEstimate != null && totalIsEstimate) {
102: buf.append(" total-estimate=\"1\"");
103: }
104: if (url != null) {
105: buf.append(" url=\"").append(url).append("\"");
106: }
107: buf.append(">");
108: for (GoogleMailThread thread : mailThreads) {
109: buf.append(thread.toXML());
110: }
111: buf.append("</mailbox>");
112: return buf.toString();
113: }
114:
115: public static class Provider implements IQProvider {
116:
117: public Provider() {
118: super ();
119: }
120:
121: public IQ parseIQ(XmlPullParser parser) throws Exception {
122: GoogleMailBoxPacket mailPacket = new GoogleMailBoxPacket();
123: try {
124: GoogleMailThread thread = null;
125:
126: boolean done = false;
127: int eventType = parser.getEventType();
128: while (!done) {
129: if (eventType == XmlPullParser.START_TAG) {
130: if (parser.getName().equals("mailbox")) {
131: String dateString = parser
132: .getAttributeValue("",
133: "result-time");
134: try {
135: mailPacket.setResultTime(new Date(Long
136: .valueOf(dateString)));
137: } catch (Exception ex) {
138: // Well crap, ok then, ignore it.
139: }
140: try {
141: mailPacket
142: .setTotalMatched(Integer
143: .valueOf(
144: parser
145: .getAttributeValue(
146: "",
147: "total-matched"),
148: 0));
149: } catch (NumberFormatException ex) {
150: // Well crap, ok then, ignore it.
151: }
152: String estimateString = parser
153: .getAttributeValue("",
154: "total-estimate");
155: mailPacket
156: .setTotalIsEstimate(estimateString != null
157: && estimateString
158: .equals("1"));
159: mailPacket.setUrl(parser.getAttributeValue(
160: "", "url"));
161: } else if (parser.getName().equals(
162: "mail-thread-info")) {
163: Long tid = Long.valueOf(parser
164: .getAttributeValue("", "tid"));
165: Integer numParts = null;
166: try {
167: numParts = Integer.valueOf(parser
168: .getAttributeValue("",
169: "participation"), 0);
170: } catch (NumberFormatException ex) {
171: // Well crap, ok then, ignore it.
172: }
173: Integer numMsgs = null;
174: try {
175: numMsgs = Integer.valueOf(parser
176: .getAttributeValue("",
177: "messages"), 0);
178: } catch (NumberFormatException ex) {
179: // Well crap, ok then, ignore it.
180: }
181: String url = parser.getAttributeValue("",
182: "url");
183: String dateString = parser
184: .getAttributeValue("", "date");
185: Date date = null;
186: try {
187: date = new Date(Long
188: .valueOf(dateString));
189: } catch (Exception ex) {
190: // Well crap, ok then, ignore it.
191: }
192: thread = new GoogleMailThread(tid,
193: numParts, numMsgs, date, url,
194: new ArrayList<GoogleMailSender>(),
195: null, null, null);
196: } else if (parser.getName().equals("sender")) {
197: String address = parser.getAttributeValue(
198: "", "address");
199: String name = parser.getAttributeValue("",
200: "name");
201: String origString = parser
202: .getAttributeValue("", "originator");
203: String unreadString = parser
204: .getAttributeValue("", "unread");
205: thread
206: .getSenders()
207: .add(
208: new GoogleMailSender(
209: address,
210: name,
211: origString != null
212: && origString
213: .equals("1"),
214: unreadString != null
215: && unreadString
216: .equals("1")));
217: } else if (parser.getName().equals("labels")) {
218: thread.setLabels(Arrays.asList(parser
219: .nextText().split("|")));
220: } else if (parser.getName().equals("subject")) {
221: thread.setSubject(parser.nextText());
222: } else if (parser.getName().equals("snippet")) {
223: thread.setSnippit(parser.nextText());
224: }
225:
226: } else if (eventType == XmlPullParser.END_TAG) {
227: if (parser.getName().equals("mail-thread-info")) {
228: mailPacket.addMailThread(thread);
229: thread = null;
230: } else if (parser.getName().equals("iq")) {
231: done = true;
232: }
233: }
234: eventType = parser.next();
235: }
236: } catch (IOException ex) {
237: Log
238: .debug(
239: "XMPP: IO exception while parsing mailbox packet:",
240: ex);
241: } catch (XmlPullParserException ex) {
242: Log
243: .debug(
244: "XMPP: XML pull exception while parsing mailbox packet:",
245: ex);
246: } catch (Exception ex) {
247: Log
248: .debug(
249: "XMPP: Unknown exception while parsing mailbox packet:",
250: ex);
251: }
252:
253: return mailPacket;
254: }
255: }
256:
257: }
|