Skip to content

Package: RemoteStore

RemoteStore

nameinstructionbranchcomplexitylinemethod
RemoteStore(Session, URLName)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
checkConnected()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
connect(String, int, String, String)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getDefaultFolder()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getFolder(String)
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getFolder(URLName)
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%
updateInbox()
M: 103 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 23 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.remote;
18:
19: import jakarta.mail.Flags;
20: import jakarta.mail.Folder;
21: import jakarta.mail.Message;
22: import jakarta.mail.MessagingException;
23: import jakarta.mail.Session;
24: import jakarta.mail.Store;
25: import jakarta.mail.URLName;
26: import org.eclipse.angus.mail.mbox.MboxStore;
27:
28: /**
29: * A wrapper around a local <code>MboxStore</code> that fetches data
30: * from the Inbox in a remote store and adds it to our local Inbox.
31: */
32: public abstract class RemoteStore extends MboxStore {
33:
34: protected Store remoteStore;
35: protected Folder remoteInbox;
36: protected Folder inbox;
37: protected String host, user, password;
38: protected int port;
39: protected long lastUpdate = 0;
40:
41: public RemoteStore(Session session, URLName url) {
42: super(session, url);
43: remoteStore = getRemoteStore(session, url);
44: }
45:
46: /**
47: * Subclasses override this method to return the appropriate
48: * <code>Store</code> object. This method will be called by
49: * the <code>RemoteStore</code> constructor.
50: */
51: protected abstract Store getRemoteStore(Session session, URLName url);
52:
53: /**
54: * Connect to the store.
55: */
56: public void connect(String host, int port, String user, String password)
57: throws MessagingException {
58: this.host = host;
59: this.port = port;
60: this.user = user;
61: this.password = password;
62: updateInbox();
63: }
64:
65: /**
66: * Fetch any new mail in the remote INBOX and add it to the local INBOX.
67: */
68: protected void updateInbox() throws MessagingException {
69: // is it time to do an update yet?
70: // XXX - polling frequency, rules, etc. should be in properties
71:• if (System.currentTimeMillis() < lastUpdate + (5 * 1000))
72: return;
73: try {
74: /*
75: * Connect to the remote store, using the saved
76: * authentication information.
77: */
78: remoteStore.connect(host, port, user, password);
79:
80: /*
81: * If this store isn't connected yet, do it now, because
82: * it needs to be connected to get the INBOX folder.
83: */
84:• if (!isConnected())
85: super.connect(host, port, user, password);
86:• if (remoteInbox == null)
87: remoteInbox = remoteStore.getFolder("INBOX");
88:• if (inbox == null)
89: inbox = getFolder("INBOX");
90: remoteInbox.open(Folder.READ_WRITE);
91: Message[] msgs = remoteInbox.getMessages();
92: inbox.appendMessages(msgs);
93: remoteInbox.setFlags(msgs, new Flags(Flags.Flag.DELETED), true);
94: remoteInbox.close(true);
95: remoteStore.close();
96: } catch (MessagingException ex) {
97: try {
98:• if (remoteInbox != null && remoteInbox.isOpen())
99: remoteInbox.close(false);
100: } finally {
101:• if (remoteStore != null && remoteStore.isConnected())
102: remoteStore.close();
103: }
104: throw ex;
105: }
106: }
107:
108: public Folder getDefaultFolder() throws MessagingException {
109: checkConnected();
110:
111: return new RemoteDefaultFolder(this, null);
112: }
113:
114: public Folder getFolder(String name) throws MessagingException {
115: checkConnected();
116:
117:• if (name.equalsIgnoreCase("INBOX"))
118: return new RemoteInbox(this, name);
119: else
120: return super.getFolder(name);
121: }
122:
123: public Folder getFolder(URLName url) throws MessagingException {
124: checkConnected();
125: return getFolder(url.getFile());
126: }
127:
128: private void checkConnected() throws MessagingException {
129:• if (!isConnected())
130: throw new MessagingException("Not connected");
131: }
132: }