Skip to contentPackage: GmailProtocol$2
GmailProtocol$2
Coverage
1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.gimap.protocol;
18:
19: import java.io.*;
20: import java.util.*;
21: import java.nio.charset.StandardCharsets;
22:
23: import org.eclipse.angus.mail.iap.*;
24: import org.eclipse.angus.mail.imap.protocol.*;
25: import org.eclipse.angus.mail.util.MailLogger;
26: import org.eclipse.angus.mail.gimap.GmailFolder;
27: import org.eclipse.angus.mail.iap.ProtocolException;
28:
29: /**
30: * Extend IMAP support to handle Gmail-specific protocol extensions.
31: *
32: * @since JavaMail 1.4.6
33: * @author Bill Shannon
34: */
35:
36: public class GmailProtocol extends IMAPProtocol {
37:
38: /*
39: * Define the Gmail-specific FETCH items.
40: */
41: public static final FetchItem MSGID_ITEM =
42:         new FetchItem("X-GM-MSGID", GmailFolder.FetchProfileItem.MSGID) {
43:          public Object parseItem(FetchResponse r) {
44:                 return Long.valueOf(r.readLong());
45:          }
46:         };
47: public static final FetchItem THRID_ITEM =
48:         new FetchItem("X-GM-THRID", GmailFolder.FetchProfileItem.THRID) {
49:          public Object parseItem(FetchResponse r) {
50:                 return Long.valueOf(r.readLong());
51:          }
52:         };
53: public static final FetchItem LABELS_ITEM =
54:         new FetchItem("X-GM-LABELS", GmailFolder.FetchProfileItem.LABELS) {
55:          public Object parseItem(FetchResponse r) {
56:                 return r.readAtomStringList();
57:          }
58:         };
59:
60: private static final FetchItem[] myFetchItems = {
61:         MSGID_ITEM,
62:         THRID_ITEM,
63:         LABELS_ITEM
64: };
65:
66: private FetchItem[] fetchItems = null;
67:
68: /**
69: * Connect to Gmail.
70: *
71: * @param name        the protocol name
72: * @param host        host to connect to
73: * @param port        portnumber to connect to
74: * @param props        Properties object used by this protocol
75: * @param isSSL        use SSL?
76: * @param logger        for log messages
77: * @exception        IOException        for I/O errors
78: * @exception ProtocolException for protocol failures
79: */
80: public GmailProtocol(String name, String host, int port,
81:                         Properties props, boolean isSSL, MailLogger logger)
82:                         throws IOException, ProtocolException {
83:         super(name, host, port, props, isSSL, logger);
84:
85:         // check to see if this is really Gmail
86:         if (!hasCapability("X-GM-EXT-1")) {
87:          logger.fine("WARNING! Not connected to Gmail!");
88:          // XXX - could call "disconnect()" here and make this a fatal error
89:         } else {
90:          logger.fine("connected to Gmail");
91:         }
92: }
93:
94: /**
95: * Return the additional fetch items supported by the Gmail protocol.
96: * Combines our fetch items with those supported by the superclass.
97: */
98: public FetchItem[] getFetchItems() {
99:         if (fetchItems != null)
100:          return fetchItems;
101:         FetchItem[] sfi = super.getFetchItems();
102:         if (sfi == null || sfi.length == 0)
103:          fetchItems = myFetchItems;
104:         else {
105:          fetchItems = new FetchItem[sfi.length + myFetchItems.length];
106:          System.arraycopy(sfi, 0, fetchItems, 0, sfi.length);
107:          System.arraycopy(myFetchItems, 0, fetchItems, sfi.length,
108:                                                         myFetchItems.length);
109:         }
110:         return fetchItems;
111: }
112:
113: /**
114: * Set the specified labels on this message.
115: *
116: * @param        msgsets        the message sets
117: * @param        labels        the labels
118: * @param        set        true to set, false to clear
119: * @exception        ProtocolException        for protocol failures
120: * @since        JavaMail 1.5.5
121: */
122: public void storeLabels(MessageSet[] msgsets, String[] labels, boolean set)
123:                         throws ProtocolException {
124:         storeLabels(MessageSet.toString(msgsets), labels, set);
125: }
126:
127: /**
128: * Set the specified labels on this message.
129: *
130: * @param        start        the first message number
131: * @param        end        the last message number
132: * @param        labels        the labels
133: * @param        set        true to set, false to clear
134: * @exception        ProtocolException        for protocol failures
135: * @since        JavaMail 1.5.5
136: */
137: public void storeLabels(int start, int end, String[] labels, boolean set)
138:                         throws ProtocolException {
139:         storeLabels(String.valueOf(start) + ":" + String.valueOf(end),
140:                  labels, set);
141: }
142:
143: /**
144: * Set the specified labels on this message.
145: *
146: * @param        msg        the message number
147: * @param        labels        the labels
148: * @param        set        true to set, false to clear
149: * @exception        ProtocolException        for protocol failures
150: * @since        JavaMail 1.5.5
151: */
152: public void storeLabels(int msg, String[] labels, boolean set)
153:                         throws ProtocolException {
154:         storeLabels(String.valueOf(msg), labels, set);
155: }
156:
157: private void storeLabels(String msgset, String[] labels, boolean set)
158:                         throws ProtocolException {
159:         Response[] r;
160:         if (set)
161:          r = command("STORE " + msgset + " +X-GM-LABELS",
162:                          createLabelList(labels));
163:         else
164:          r = command("STORE " + msgset + " -X-GM-LABELS",
165:                         createLabelList(labels));
166:         
167:         // Dispatch untagged responses
168:         notifyResponseHandlers(r);
169:         handleResult(r[r.length-1]);
170: }
171:
172: // XXX - assume Gmail always supports UTF-8
173: private Argument createLabelList(String[] labels) {
174:         Argument args = new Argument();        
175:         Argument itemArgs = new Argument();
176:         for (int i = 0, len = labels.length; i < len; i++)
177:          itemArgs.writeString(labels[i], StandardCharsets.UTF_8);
178:         args.writeArgument(itemArgs);
179:         return args;
180: }
181:
182: /**
183: * Return a GmailSearchSequence.
184: */
185: protected SearchSequence getSearchSequence() {
186:         if (searchSequence == null)
187:          searchSequence = new GmailSearchSequence(this);
188:         return searchSequence;
189: }
190: }