60
60
* @author Iwein Fuld
61
61
* @author Oleg Zhurakousky
62
62
* @author Gary Russell
63
+ * @author Artem Bilan
63
64
*/
64
65
public abstract class AbstractMailReceiver extends IntegrationObjectSupport implements MailReceiver , DisposableBean {
65
66
@@ -69,43 +70,41 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
69
70
*/
70
71
public final static String DEFAULT_SI_USER_FLAG = "spring-integration-mail-adapter" ;
71
72
72
- protected final Log logger = LogFactory .getLog (getClass ());
73
+ protected final Log logger = LogFactory .getLog (getClass ()); // NOSONAR safe to use final
73
74
74
75
private final URLName url ;
75
76
76
77
private final Object folderMonitor = new Object ();
77
78
78
- private volatile String protocol ;
79
+ private String protocol ;
79
80
80
- private volatile int maxFetchSize = -1 ;
81
+ private int maxFetchSize = -1 ;
81
82
82
- private volatile Session session ;
83
+ private Session session ;
83
84
84
- private volatile Store store ;
85
-
86
- private volatile Folder folder ;
85
+ private boolean shouldDeleteMessages ;
87
86
88
- private volatile boolean shouldDeleteMessages ;
87
+ private int folderOpenMode = Folder . READ_ONLY ;
89
88
90
- protected volatile int folderOpenMode = Folder . READ_ONLY ;
89
+ private Properties javaMailProperties = new Properties () ;
91
90
92
- private volatile Properties javaMailProperties = new Properties () ;
91
+ private Authenticator javaMailAuthenticator ;
93
92
94
- private volatile Authenticator javaMailAuthenticator ;
93
+ private StandardEvaluationContext evaluationContext ;
95
94
96
- private volatile StandardEvaluationContext evaluationContext ;
95
+ private Expression selectorExpression ;
97
96
98
- private volatile Expression selectorExpression ;
97
+ private HeaderMapper < MimeMessage > headerMapper ;
99
98
100
- private volatile HeaderMapper < MimeMessage > headerMapper ;
99
+ private String userFlag = DEFAULT_SI_USER_FLAG ;
101
100
102
- protected volatile boolean initialized ;
101
+ private boolean embeddedPartsAsBytes = true ;
103
102
104
- private volatile String userFlag = DEFAULT_SI_USER_FLAG ;
103
+ private boolean simpleContent ;
105
104
106
- private volatile boolean embeddedPartsAsBytes = true ;
105
+ private volatile Store store ;
107
106
108
- private volatile boolean simpleContent ;
107
+ private volatile Folder folder ;
109
108
110
109
public AbstractMailReceiver () {
111
110
this .url = null ;
@@ -283,6 +282,10 @@ protected Folder getFolder() {
283
282
return this .folder ;
284
283
}
285
284
285
+ protected int getFolderOpenMode () {
286
+ return this .folderOpenMode ;
287
+ }
288
+
286
289
/**
287
290
* Subclasses must implement this method to return new mail messages.
288
291
*
@@ -291,7 +294,7 @@ protected Folder getFolder() {
291
294
*/
292
295
protected abstract Message [] searchForNewMessages () throws MessagingException ;
293
296
294
- private void openSession () throws MessagingException {
297
+ private void openSession () {
295
298
if (this .session == null ) {
296
299
if (this .javaMailAuthenticator != null ) {
297
300
this .session = Session .getInstance (this .javaMailProperties , this .javaMailAuthenticator );
@@ -316,7 +319,7 @@ else if (this.protocol != null) {
316
319
}
317
320
if (!this .store .isConnected ()) {
318
321
if (this .logger .isDebugEnabled ()) {
319
- this .logger .debug ("connecting to store [" + MailTransportUtils . toPasswordProtectedString ( this .url ) + "]" );
322
+ this .logger .debug ("connecting to store [" + this .store . getURLName ( ) + "]" );
320
323
}
321
324
this .store .connect ();
322
325
}
@@ -338,7 +341,7 @@ protected void openFolder() throws MessagingException {
338
341
return ;
339
342
}
340
343
if (this .logger .isDebugEnabled ()) {
341
- this .logger .debug ("opening folder [" + MailTransportUtils . toPasswordProtectedString ( this .url ) + "]" );
344
+ this .logger .debug ("opening folder [" + this .folder . getURLName ( ) + "]" );
342
345
}
343
346
this .folder .open (this .folderOpenMode );
344
347
}
@@ -353,7 +356,7 @@ public Object[] receive() throws javax.mail.MessagingException {
353
356
try {
354
357
this .openFolder ();
355
358
if (this .logger .isInfoEnabled ()) {
356
- this .logger .info ("attempting to receive mail from folder [" + this . getFolder ().getFullName () + "]" );
359
+ this .logger .info ("attempting to receive mail from folder [" + getFolder ().getFullName () + "]" );
357
360
}
358
361
Message [] messages = searchForNewMessages ();
359
362
if (this .maxFetchSize > 0 && messages .length > this .maxFetchSize ) {
@@ -496,25 +499,26 @@ private void setMessageFlags(Message[] filteredMessages) throws MessagingExcepti
496
499
* will be filtered out and remain on the server as never touched.
497
500
*/
498
501
private MimeMessage [] filterMessagesThruSelector (Message [] messages ) throws MessagingException {
499
- List <MimeMessage > filteredMessages = new LinkedList <MimeMessage >();
502
+ List <MimeMessage > filteredMessages = new LinkedList <>();
500
503
for (int i = 0 ; i < messages .length ; i ++) {
501
504
MimeMessage message = (MimeMessage ) messages [i ];
502
505
if (this .selectorExpression != null ) {
503
- if (this .selectorExpression .getValue (this .evaluationContext , message , Boolean .class )) {
506
+ if (Boolean .TRUE .equals (
507
+ this .selectorExpression .getValue (this .evaluationContext , message , Boolean .class ))) {
504
508
filteredMessages .add (message );
505
509
}
506
510
else {
507
511
if (this .logger .isDebugEnabled ()) {
508
- this .logger .debug ("Fetched email with subject '" + message .getSubject () + "' will be discarded by the matching filter" +
509
- " and will not be flagged as SEEN." );
512
+ this .logger .debug ("Fetched email with subject '" + message .getSubject ()
513
+ + "' will be discarded by the matching filter and will not be flagged as SEEN." );
510
514
}
511
515
}
512
516
}
513
517
else {
514
518
filteredMessages .add (message );
515
519
}
516
520
}
517
- return filteredMessages .toArray (new MimeMessage [filteredMessages . size () ]);
521
+ return filteredMessages .toArray (new MimeMessage [0 ]);
518
522
}
519
523
520
524
/**
@@ -562,7 +566,6 @@ public void destroy() {
562
566
MailTransportUtils .closeService (this .store );
563
567
this .folder = null ;
564
568
this .store = null ;
565
- this .initialized = false ;
566
569
}
567
570
}
568
571
@@ -571,7 +574,6 @@ protected void onInit() throws Exception {
571
574
super .onInit ();
572
575
this .folderOpenMode = Folder .READ_WRITE ;
573
576
this .evaluationContext = ExpressionUtils .createStandardEvaluationContext (getBeanFactory ());
574
- this .initialized = true ;
575
577
}
576
578
577
579
@ Override
0 commit comments