Skip to content

Package: GmailProtocol

GmailProtocol

nameinstructionbranchcomplexitylinemethod
GmailProtocol(String, String, int, Properties, boolean, MailLogger)
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
createLabelList(String[])
M: 31 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getFetchItems()
M: 46 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
getSearchSequence()
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 34 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
storeLabels(MessageSet[], String[], boolean)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
storeLabels(String, String[], boolean)
M: 47 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
storeLabels(int, String[], boolean)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
storeLabels(int, int, String[], boolean)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

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 org.eclipse.angus.mail.gimap.GmailFolder;
20: import org.eclipse.angus.mail.iap.Argument;
21: import org.eclipse.angus.mail.iap.ProtocolException;
22: import org.eclipse.angus.mail.iap.Response;
23: import org.eclipse.angus.mail.imap.protocol.FetchItem;
24: import org.eclipse.angus.mail.imap.protocol.FetchResponse;
25: import org.eclipse.angus.mail.imap.protocol.IMAPProtocol;
26: import org.eclipse.angus.mail.imap.protocol.MessageSet;
27: import org.eclipse.angus.mail.imap.protocol.SearchSequence;
28: import org.eclipse.angus.mail.util.MailLogger;
29:
30: import java.io.IOException;
31: import java.nio.charset.StandardCharsets;
32: import java.util.Properties;
33:
34: /**
35: * Extend IMAP support to handle Gmail-specific protocol extensions.
36: *
37: * @author Bill Shannon
38: * @since JavaMail 1.4.6
39: */
40:
41: public class GmailProtocol extends IMAPProtocol {
42:
43: /*
44: * Define the Gmail-specific FETCH items.
45: */
46: public static final FetchItem MSGID_ITEM =
47: new FetchItem("X-GM-MSGID", GmailFolder.FetchProfileItem.MSGID) {
48: public Object parseItem(FetchResponse r) {
49: return Long.valueOf(r.readLong());
50: }
51: };
52: public static final FetchItem THRID_ITEM =
53: new FetchItem("X-GM-THRID", GmailFolder.FetchProfileItem.THRID) {
54: public Object parseItem(FetchResponse r) {
55: return Long.valueOf(r.readLong());
56: }
57: };
58: public static final FetchItem LABELS_ITEM =
59: new FetchItem("X-GM-LABELS", GmailFolder.FetchProfileItem.LABELS) {
60: public Object parseItem(FetchResponse r) {
61: return r.readAtomStringList();
62: }
63: };
64:
65: private static final FetchItem[] myFetchItems = {
66: MSGID_ITEM,
67: THRID_ITEM,
68: LABELS_ITEM
69: };
70:
71: private FetchItem[] fetchItems = null;
72:
73: /**
74: * Connect to Gmail.
75: *
76: * @param name the protocol name
77: * @param host host to connect to
78: * @param port portnumber to connect to
79: * @param props Properties object used by this protocol
80: * @param isSSL use SSL?
81: * @param logger for log messages
82: * @throws ProtocolException for protocol failures
83: * @exception IOException for I/O errors
84: */
85: public GmailProtocol(String name, String host, int port,
86: Properties props, boolean isSSL, MailLogger logger)
87: throws IOException, ProtocolException {
88: super(name, host, port, props, isSSL, logger);
89:
90: // check to see if this is really Gmail
91:• if (!hasCapability("X-GM-EXT-1")) {
92: logger.fine("WARNING! Not connected to Gmail!");
93: // XXX - could call "disconnect()" here and make this a fatal error
94: } else {
95: logger.fine("connected to Gmail");
96: }
97: }
98:
99: /**
100: * Return the additional fetch items supported by the Gmail protocol.
101: * Combines our fetch items with those supported by the superclass.
102: */
103: public FetchItem[] getFetchItems() {
104:• if (fetchItems != null)
105: return fetchItems;
106: FetchItem[] sfi = super.getFetchItems();
107:• if (sfi == null || sfi.length == 0)
108: fetchItems = myFetchItems;
109: else {
110: fetchItems = new FetchItem[sfi.length + myFetchItems.length];
111: System.arraycopy(sfi, 0, fetchItems, 0, sfi.length);
112: System.arraycopy(myFetchItems, 0, fetchItems, sfi.length,
113: myFetchItems.length);
114: }
115: return fetchItems;
116: }
117:
118: /**
119: * Set the specified labels on this message.
120: *
121: * @param msgsets the message sets
122: * @param labels the labels
123: * @param set true to set, false to clear
124: * @exception ProtocolException for protocol failures
125: * @since JavaMail 1.5.5
126: */
127: public void storeLabels(MessageSet[] msgsets, String[] labels, boolean set)
128: throws ProtocolException {
129: storeLabels(MessageSet.toString(msgsets), labels, set);
130: }
131:
132: /**
133: * Set the specified labels on this message.
134: *
135: * @param start the first message number
136: * @param end the last message number
137: * @param labels the labels
138: * @param set true to set, false to clear
139: * @exception ProtocolException for protocol failures
140: * @since JavaMail 1.5.5
141: */
142: public void storeLabels(int start, int end, String[] labels, boolean set)
143: throws ProtocolException {
144: storeLabels(String.valueOf(start) + ":" + String.valueOf(end),
145: labels, set);
146: }
147:
148: /**
149: * Set the specified labels on this message.
150: *
151: * @param msg the message number
152: * @param labels the labels
153: * @param set true to set, false to clear
154: * @exception ProtocolException for protocol failures
155: * @since JavaMail 1.5.5
156: */
157: public void storeLabels(int msg, String[] labels, boolean set)
158: throws ProtocolException {
159: storeLabels(String.valueOf(msg), labels, set);
160: }
161:
162: private void storeLabels(String msgset, String[] labels, boolean set)
163: throws ProtocolException {
164: Response[] r;
165:• if (set)
166: r = command("STORE " + msgset + " +X-GM-LABELS",
167: createLabelList(labels));
168: else
169: r = command("STORE " + msgset + " -X-GM-LABELS",
170: createLabelList(labels));
171:
172: // Dispatch untagged responses
173: notifyResponseHandlers(r);
174: handleResult(r[r.length - 1]);
175: }
176:
177: // XXX - assume Gmail always supports UTF-8
178: private Argument createLabelList(String[] labels) {
179: Argument args = new Argument();
180: Argument itemArgs = new Argument();
181:• for (int i = 0, len = labels.length; i < len; i++)
182: itemArgs.writeString(labels[i], StandardCharsets.UTF_8);
183: args.writeArgument(itemArgs);
184: return args;
185: }
186:
187: /**
188: * Return a GmailSearchSequence.
189: */
190: protected SearchSequence getSearchSequence() {
191:• if (searchSequence == null)
192: searchSequence = new GmailSearchSequence(this);
193: return searchSequence;
194: }
195: }