1 /*
2 * Copyright (c) 2004-2007 Creative Sphere Limited.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *
10 * Creative Sphere - initial API and implementation
11 *
12 */
13 package org.abstracthorizon.mercury.smtp.util;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import javax.mail.Folder;
19
20 /**
21 * This class represents path to mailbox. It stores mailbox name and domain and is used for
22 * storing JavaMail folder when (if) mailbox is found.
23 *
24 * @author Daniel Sendula
25 */
26 public class Path {
27
28 /** Mailbox name */
29 private String mailbox;
30
31 /** Mailbox domain */
32 private String domain;
33
34 /** Full path */
35 private List<String> returnPath;
36
37 /** Is local domain or not */
38 private boolean localDomain = false;
39
40 /** Folder or <code>null</code> if not found/set */
41 private Folder folder;
42
43 /**
44 * Constructor
45 */
46 public Path() {
47 }
48
49 /**
50 * Constructor
51 * @param mailbox mailbox name
52 * @param domain mailbox domain
53 */
54 public Path(String mailbox, String domain) {
55 this.mailbox = mailbox;
56 this.domain = domain;
57 }
58
59 /**
60 * Returns mailbox
61 * @return mailbox
62 */
63 public String getMailbox() {
64 return mailbox;
65 }
66
67 /**
68 * Sets mailbox
69 * @param mailbox mailbox
70 */
71 public void setMailbox(String mailbox) {
72 this.mailbox = mailbox;
73 }
74
75 /**
76 * Returns domain
77 * @return domain
78 */
79 public String getDomain() {
80 return domain;
81 }
82
83 /**
84 * Sets domain
85 * @param domain domain
86 */
87 public void setDomain(String domain) {
88 this.domain = domain;
89 }
90
91 /**
92 * Returns return path as string array
93 * @return return path as string array
94 */
95 public String[] getReturnPath() {
96 if (returnPath == null) { return new String[0]; }
97 String[] res = new String[returnPath.size()];
98 res = (String[]) returnPath.toArray(res);
99 return res;
100 }
101
102 /**
103 * Adds new element to return path
104 * @param returnPath new element to be added
105 */
106 public void addReturnPath(String returnPath) {
107 if (this.returnPath == null) {
108 this.returnPath = new ArrayList<String>();
109 }
110 this.returnPath.add(returnPath);
111 }
112
113 /**
114 * Sets if it is local domain
115 * @param localDomain local domain
116 */
117 public void setLocalDomain(boolean localDomain) {
118 this.localDomain = localDomain;
119 }
120
121 /**
122 * Returns if it is local domain
123 * @return <code>true</code> if it is local domain
124 */
125 public boolean isLocalDomain() {
126 return localDomain;
127 }
128
129 /**
130 * Returns <code>true</code> if it is local mailbox
131 * @return <code>true</code> if it is local mailbox
132 */
133 public boolean isLocalMailbox() {
134 return folder != null;
135 }
136
137 /**
138 * Sets destination folder
139 * @param folder destination folder
140 */
141 public void setFolder(Folder folder) {
142 this.folder = folder;
143 }
144
145 /**
146 * Returns destination folder (or null)
147 * @return destination folder
148 */
149 public Folder getFolder() {
150 return folder;
151 }
152
153 /**
154 * Returns string representation
155 * @return string representation
156 */
157 public String toMailboxString() {
158 StringBuffer buf = new StringBuffer();
159 buf.append(mailbox).append('@').append(domain);
160 return buf.toString();
161 }
162
163 /**
164 * Returns string representation
165 * @return string representation
166 */
167 public String toString() {
168 StringBuffer buf = new StringBuffer();
169 buf.append(mailbox).append('@').append(domain);
170 if (localDomain) {
171 if (folder != null) {
172 buf.append(" (").append(folder.getStore().getURLName()).append('#').append(folder.getName()).append(')');
173 } else {
174 buf.append(" (storage error)");
175 }
176 } else {
177 buf.append(" (foreign mailbox)");
178 }
179 return buf.toString();
180 }
181
182 /**
183 * Returns hash code as sum of mailbox and domain hash codes
184 *
185 * @return hash code as sum of mailbox and domain hash codes
186 */
187 public int hashCode() {
188 return mailbox.hashCode()+domain.hashCode();
189 }
190
191 /**
192 * Returns <code>true</code> if two objects are same
193 * @param o other object
194 * @return <code>true</code> if two objects are same
195 */
196 public boolean equals(Object o) {
197 if (o instanceof Path) {
198 Path p = (Path)o;
199 if (((p.domain == domain) || ((domain != null) && domain.equals(p.domain)))
200 && ((p.mailbox == mailbox) || ((mailbox != null) && mailbox.equals(p.mailbox)))) {
201 return true;
202 }
203 }
204 return false;
205 }
206
207 }