| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
package com.atlassian.mail.server.impl; |
| 12 |
|
|
| 13 |
|
import alt.javax.mail.Session; |
| 14 |
|
import alt.javax.mail.SessionImpl; |
| 15 |
|
import alt.javax.mail.internet.MimeMessage; |
| 16 |
|
import alt.javax.mail.internet.MimeMessageImpl; |
| 17 |
|
import com.atlassian.mail.Email; |
| 18 |
|
import com.atlassian.mail.MailException; |
| 19 |
|
import com.atlassian.mail.MailFactory; |
| 20 |
|
import com.atlassian.mail.config.PropertiesLoader; |
| 21 |
|
import com.atlassian.mail.server.AbstractMailServer; |
| 22 |
|
import com.atlassian.mail.server.MailServerManager; |
| 23 |
|
import com.atlassian.mail.server.SMTPMailServer; |
| 24 |
|
import com.atlassian.mail.server.impl.util.MessageCreator; |
| 25 |
|
import com.opensymphony.util.TextUtils; |
| 26 |
|
import org.apache.commons.lang.builder.ToStringBuilder; |
| 27 |
|
import org.apache.log4j.Category; |
| 28 |
|
|
| 29 |
|
import javax.mail.Authenticator; |
| 30 |
|
import javax.mail.MessagingException; |
| 31 |
|
import javax.mail.PasswordAuthentication; |
| 32 |
|
import javax.naming.Context; |
| 33 |
|
import javax.naming.InitialContext; |
| 34 |
|
import javax.naming.NamingException; |
| 35 |
|
import java.io.PrintStream; |
| 36 |
|
import java.io.UnsupportedEncodingException; |
| 37 |
|
import java.util.Properties; |
| 38 |
|
|
|
|
|
| 69.9% |
Uncovered Elements: 37 (123) |
Complexity: 40 |
Complexity Density: 0.51 |
|
| 39 |
|
public class SMTPMailServerImpl extends AbstractMailServer implements SMTPMailServer |
| 40 |
|
{ |
| 41 |
|
private static final Category log = Category.getInstance(SMTPMailServerImpl.class); |
| 42 |
|
|
| 43 |
|
private boolean isSessionServer; |
| 44 |
|
private String defaultFrom; |
| 45 |
|
private String prefix; |
| 46 |
|
private String jndiLocation; |
| 47 |
|
private String smtpPort = DEFAULT_SMTP_PORT; |
| 48 |
|
private boolean removePrecedence; |
| 49 |
|
private boolean debug; |
| 50 |
|
|
| 51 |
|
private transient PrintStream debugStream; |
| 52 |
|
private transient Session session; |
| 53 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 54 |
7
|
public SMTPMailServerImpl()... |
| 55 |
|
{ |
| 56 |
|
} |
| 57 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 58 |
54
|
public SMTPMailServerImpl(Long id, String name, String description, String from, String prefix, boolean isSession, String location, String username, String password)... |
| 59 |
|
{ |
| 60 |
54
|
this(id, name, description, from, prefix, isSession, location, username, password, SMTPMailServer.DEFAULT_SMTP_PORT); |
| 61 |
|
} |
| 62 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 63 |
54
|
public SMTPMailServerImpl(Long id, String name, String description, String from, String prefix, boolean isSession, String location, String username, String password, String smtpPort)... |
| 64 |
|
{ |
| 65 |
54
|
this(id, name, description, from, prefix, isSession, false, location, username, password, smtpPort); |
| 66 |
|
} |
| 67 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 68 |
54
|
public SMTPMailServerImpl(Long id, String name, String description, String from, String prefix, boolean isSession, boolean removePrecedence, String location, String username, String password, String smtpPort)... |
| 69 |
|
{ |
| 70 |
54
|
super(id, name, description, location, username, password); |
| 71 |
54
|
setDefaultFrom(from); |
| 72 |
54
|
setPrefix(prefix); |
| 73 |
54
|
setSessionServer(isSession); |
| 74 |
54
|
setSmtpPort(smtpPort); |
| 75 |
54
|
setRemovePrecedence(removePrecedence); |
| 76 |
|
|
| 77 |
54
|
if (isSession) |
| 78 |
|
{ |
| 79 |
8
|
setJndiLocation(location); |
| 80 |
8
|
setHostname(null); |
| 81 |
|
} |
| 82 |
|
} |
| 83 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 84 |
5
|
public String getJndiLocation()... |
| 85 |
|
{ |
| 86 |
5
|
return jndiLocation; |
| 87 |
|
} |
| 88 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 89 |
9
|
public void setJndiLocation(String jndiLocation)... |
| 90 |
|
{ |
| 91 |
9
|
this.jndiLocation = jndiLocation; |
| 92 |
9
|
propertyChanged(); |
| 93 |
|
} |
| 94 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 95 |
0
|
protected Authenticator getAuthenticator()... |
| 96 |
|
{ |
| 97 |
0
|
return new MyAuthenticator(); |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
|
|
|
| 76.9% |
Uncovered Elements: 9 (39) |
Complexity: 8 |
Complexity Density: 0.32 |
|
| 103 |
3
|
public Session getSession() throws NamingException, MailException... |
| 104 |
|
{ |
| 105 |
3
|
if (session == null) |
| 106 |
|
|
| 107 |
|
{ |
| 108 |
3
|
if (isSessionServer()) |
| 109 |
|
{ |
| 110 |
2
|
Object jndiSession = getJndiSession(); |
| 111 |
2
|
if (jndiSession instanceof javax.mail.Session) |
| 112 |
|
{ |
| 113 |
1
|
session = new SessionImpl((javax.mail.Session) jndiSession); |
| 114 |
|
} |
| 115 |
|
else |
| 116 |
|
{ |
| 117 |
1
|
log.error("Mail server at location [" + getJndiLocation() + "] is not of required type javax.mail.Session, or is in different classloader. " + |
| 118 |
1
|
"It is of type '" + (jndiSession != null ? jndiSession.getClass().getName() : null) + "' in classloader '"+jndiSession.getClass().getClassLoader()+"' instead"); |
| 119 |
1
|
throw new IllegalArgumentException("Mail server at location [" + getJndiLocation() + "] is not of required type javax.mail.Session. "); |
| 120 |
|
} |
| 121 |
|
} |
| 122 |
|
else |
| 123 |
|
{ |
| 124 |
1
|
Properties props = new Properties(); |
| 125 |
1
|
props.put("mail.smtp.host", getHostname()); |
| 126 |
1
|
props.put("mail.smtp.port", getSmtpPort()); |
| 127 |
1
|
props.put("mail.transport.protocol", "smtp"); |
| 128 |
1
|
props.put("mail.debug", ""+debug); |
| 129 |
|
|
| 130 |
1
|
if (Boolean.getBoolean("mail.debug")) |
| 131 |
|
{ |
| 132 |
0
|
props.put("mail.debug", "true"); |
| 133 |
|
} |
| 134 |
1
|
Authenticator auth = null; |
| 135 |
1
|
if (TextUtils.stringSet(getUsername())) |
| 136 |
|
{ |
| 137 |
1
|
props.put("mail.smtp.auth", "true"); |
| 138 |
1
|
auth = getAuthenticator(); |
| 139 |
|
} |
| 140 |
1
|
props.putAll(PropertiesLoader.ATLASSIAN_MAIL_PROPERTIES); |
| 141 |
1
|
session = MailFactory.getServerManager().getSession(props, auth); |
| 142 |
1
|
if (debugStream != null) |
| 143 |
|
{ |
| 144 |
0
|
try |
| 145 |
|
{ |
| 146 |
0
|
session.getWrappedSession().setDebugOut(debugStream); |
| 147 |
|
} catch (NoSuchMethodError nsme) |
| 148 |
|
{ |
| 149 |
|
|
| 150 |
0
|
log.error("Warning: An old (pre-1.3.2) version of the JavaMail library (javamail.jar or mail.jar) bundled with your app server, is in use. Some functions such as IMAPS/POPS/SMTPS will not work. Consider upgrading the app server's javamail jar to the version JIRA provides."); |
| 151 |
|
} |
| 152 |
|
} |
| 153 |
|
} |
| 154 |
|
} |
| 155 |
2
|
return session; |
| 156 |
|
} |
| 157 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 158 |
0
|
protected Object getJndiSession() throws NamingException... |
| 159 |
|
{ |
| 160 |
0
|
Context ctx = new InitialContext(); |
| 161 |
0
|
Object jndiSession = ctx.lookup(getJndiLocation()); |
| 162 |
0
|
return jndiSession; |
| 163 |
|
} |
| 164 |
|
|
|
|
|
| 57.1% |
Uncovered Elements: 6 (14) |
Complexity: 6 |
Complexity Density: 0.5 |
|
| 165 |
6
|
public void send(Email email) throws MailException... |
| 166 |
|
{ |
| 167 |
6
|
try |
| 168 |
|
{ |
| 169 |
6
|
Session thisSession = getSession(); |
| 170 |
|
|
| 171 |
6
|
MimeMessage message = new MimeMessageImpl(thisSession); |
| 172 |
6
|
MessageCreator messageCreator = new MessageCreator(); |
| 173 |
|
|
| 174 |
6
|
messageCreator.updateMimeMessage(email, getDefaultFrom(), prefix, message); |
| 175 |
|
|
| 176 |
|
|
| 177 |
5
|
thisSession.getTransport("smtp").send(message); |
| 178 |
|
|
| 179 |
|
|
| 180 |
5
|
if (message.getHeader("Message-Id") != null && message.getHeader("Message-Id").length > 0) |
| 181 |
|
{ |
| 182 |
0
|
email.setMessageId(message.getHeader("Message-Id")[0]); |
| 183 |
|
} |
| 184 |
|
} |
| 185 |
|
catch (NamingException e) |
| 186 |
|
{ |
| 187 |
0
|
throw new MailException(e); |
| 188 |
|
} |
| 189 |
|
catch (MessagingException e) |
| 190 |
|
{ |
| 191 |
0
|
throw new MailException(e); |
| 192 |
|
} |
| 193 |
|
catch (UnsupportedEncodingException e) |
| 194 |
|
{ |
| 195 |
0
|
log.debug("Error setting the 'from' address with an email and the user's fullname"); |
| 196 |
0
|
e.printStackTrace(); |
| 197 |
|
} |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 204 |
0
|
public void quietSend(Email email) throws MailException... |
| 205 |
|
{ |
| 206 |
0
|
try |
| 207 |
|
{ |
| 208 |
0
|
send(email); |
| 209 |
|
} |
| 210 |
|
catch (Exception e) |
| 211 |
|
{ |
| 212 |
0
|
log.error("Error sending mail. to:" + email.getTo() + ", cc:" + email.getCc() + ", bcc:" + email.getBcc() + ", subject:" + email.getSubject() + ", body:" + email.getBody() + ", mimeType:" + email.getMimeType() + ", encoding:" + email.getEncoding() + ", multipart:" + email.getMultipart() + ", error:" + e, e); |
| 213 |
|
} |
| 214 |
|
} |
| 215 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 216 |
62
|
public String getType()... |
| 217 |
|
{ |
| 218 |
62
|
return MailServerManager.SERVER_TYPES[1]; |
| 219 |
|
} |
| 220 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 221 |
27
|
public String getDefaultFrom()... |
| 222 |
|
{ |
| 223 |
27
|
return defaultFrom; |
| 224 |
|
} |
| 225 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 226 |
62
|
public void setDefaultFrom(String defaultFrom)... |
| 227 |
|
{ |
| 228 |
62
|
this.defaultFrom = defaultFrom; |
| 229 |
62
|
propertyChanged(); |
| 230 |
|
} |
| 231 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 232 |
19
|
public String getPrefix()... |
| 233 |
|
{ |
| 234 |
19
|
return prefix; |
| 235 |
|
} |
| 236 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 237 |
55
|
public void setPrefix(String prefix)... |
| 238 |
|
{ |
| 239 |
55
|
this.prefix = prefix; |
| 240 |
|
} |
| 241 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 242 |
0
|
public boolean isRemovePrecedence()... |
| 243 |
|
{ |
| 244 |
0
|
return removePrecedence; |
| 245 |
|
} |
| 246 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 247 |
54
|
public void setRemovePrecedence(boolean precedence)... |
| 248 |
|
{ |
| 249 |
54
|
this.removePrecedence = precedence; |
| 250 |
54
|
propertyChanged(); |
| 251 |
|
} |
| 252 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 253 |
18
|
public String getSmtpPort()... |
| 254 |
|
{ |
| 255 |
18
|
return smtpPort; |
| 256 |
|
} |
| 257 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 258 |
54
|
public void setSmtpPort(String smtpPort)... |
| 259 |
|
{ |
| 260 |
54
|
this.smtpPort = smtpPort; |
| 261 |
54
|
propertyChanged(); |
| 262 |
|
} |
| 263 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 264 |
0
|
public void setDebug(boolean debug) {... |
| 265 |
0
|
this.debug = debug; |
| 266 |
0
|
propertyChanged(); |
| 267 |
|
} |
| 268 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 269 |
0
|
public void setDebugStream(PrintStream debugStream) {... |
| 270 |
0
|
this.debugStream = debugStream; |
| 271 |
0
|
propertyChanged(); |
| 272 |
|
} |
| 273 |
|
|
| 274 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 275 |
0
|
public boolean getDebug() {... |
| 276 |
0
|
return this.debug; |
| 277 |
|
} |
| 278 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 279 |
0
|
public PrintStream getDebugStream() {... |
| 280 |
0
|
return this.debugStream; |
| 281 |
|
} |
| 282 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 283 |
22
|
public boolean isSessionServer()... |
| 284 |
|
{ |
| 285 |
22
|
return isSessionServer; |
| 286 |
|
} |
| 287 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 288 |
55
|
public void setSessionServer(boolean sessionServer)... |
| 289 |
|
{ |
| 290 |
55
|
isSessionServer = sessionServer; |
| 291 |
55
|
propertyChanged(); |
| 292 |
|
} |
| 293 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 294 |
|
private class MyAuthenticator extends Authenticator |
| 295 |
|
{ |
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 296 |
0
|
public PasswordAuthentication getPasswordAuthentication()... |
| 297 |
|
{ |
| 298 |
0
|
return new PasswordAuthentication(getUsername(), getPassword()); |
| 299 |
|
} |
| 300 |
|
} |
| 301 |
|
|
| 302 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 303 |
|
public boolean equals(Object o)... |
| 304 |
|
{ |
| 305 |
|
if (this == o) |
| 306 |
|
{ |
| 307 |
|
return true; |
| 308 |
|
} |
| 309 |
|
if (!(o instanceof SMTPMailServerImpl)) |
| 310 |
|
{ |
| 311 |
|
return false; |
| 312 |
|
} |
| 313 |
|
if (!super.equals(o)) |
| 314 |
|
{ |
| 315 |
|
return false; |
| 316 |
|
} |
| 317 |
|
|
| 318 |
|
final SMTPMailServerImpl smtpMailServer = (SMTPMailServerImpl) o; |
| 319 |
|
|
| 320 |
|
if (isSessionServer != smtpMailServer.isSessionServer) |
| 321 |
|
{ |
| 322 |
|
return false; |
| 323 |
|
} |
| 324 |
|
if (defaultFrom != null ? !defaultFrom.equals(smtpMailServer.defaultFrom) : smtpMailServer.defaultFrom != null) |
| 325 |
|
{ |
| 326 |
|
return false; |
| 327 |
|
} |
| 328 |
|
if (prefix != null ? !prefix.equals(smtpMailServer.prefix) : smtpMailServer.prefix != null) |
| 329 |
|
{ |
| 330 |
|
return false; |
| 331 |
|
} |
| 332 |
|
if (smtpPort != null ? !smtpPort.equals(smtpMailServer.smtpPort) : smtpMailServer.smtpPort != null) |
| 333 |
|
{ |
| 334 |
|
return false; |
| 335 |
|
} |
| 336 |
|
if (removePrecedence != smtpMailServer.removePrecedence) |
| 337 |
|
{ |
| 338 |
|
return false; |
| 339 |
|
} |
| 340 |
|
|
| 341 |
|
return true; |
| 342 |
|
} |
| 343 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 344 |
|
public int hashCode()... |
| 345 |
|
{ |
| 346 |
|
int result = super.hashCode(); |
| 347 |
|
result = 29 * result + (isSessionServer ? 1 : 0); |
| 348 |
|
result = 29 * result + (defaultFrom != null ? defaultFrom.hashCode() : 0); |
| 349 |
|
result = 29 * result + (prefix != null ? prefix.hashCode() : 0); |
| 350 |
|
result = 29 * result + (smtpPort != null ? smtpPort.hashCode() : 0); |
| 351 |
|
result = 29 * result + (removePrecedence ? 1 : 0); |
| 352 |
|
return result; |
| 353 |
|
} |
| 354 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 355 |
|
public String toString()... |
| 356 |
|
{ |
| 357 |
|
return new ToStringBuilder(this).append("id", getId()).append("name", getName()).append("description", getDescription()).append("server name", getHostname()).append("username", getUsername()).append("password", getPassword()).append("isSessionServer", isSessionServer).append("defaultFrom", defaultFrom).append("prefix", prefix).append("smtpPort", smtpPort).toString(); |
| 358 |
|
} |
| 359 |
|
|
| 360 |
|
|
| 361 |
|
|
| 362 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 363 |
|
protected void propertyChanged()... |
| 364 |
|
{ |
| 365 |
|
super.propertyChanged(); |
| 366 |
|
session = null; |
| 367 |
|
} |
| 368 |
|
} |