Use LogAccessor from SF
* Change main classes to use a `LogAccessor` API to simplify code flow * Fix tests according `LogAccessor` property * Fix some Sonar smells
This commit is contained in:
@@ -52,6 +52,7 @@ import org.springframework.integration.support.AbstractIntegrationMessageBuilder
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
/**
|
||||
* Base class for {@link MailReceiver} implementations.
|
||||
@@ -326,9 +327,7 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
|
||||
}
|
||||
}
|
||||
if (!this.store.isConnected()) {
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug("connecting to store [" + this.store.getURLName() + "]");
|
||||
}
|
||||
this.logger.debug(() -> "connecting to store [" + this.store.getURLName() + "]");
|
||||
this.store.connect();
|
||||
}
|
||||
}
|
||||
@@ -348,9 +347,8 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
|
||||
if (this.folder.isOpen()) {
|
||||
return;
|
||||
}
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug("opening folder [" + this.folder.getURLName() + "]");
|
||||
}
|
||||
URLName urlName = this.folder.getURLName();
|
||||
this.logger.debug(() -> "opening folder [" + urlName + "]");
|
||||
this.folder.open(this.folderOpenMode);
|
||||
}
|
||||
|
||||
@@ -401,27 +399,25 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
|
||||
}
|
||||
|
||||
private MimeMessage[] searchAndFilterMessages() throws MessagingException {
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug("attempting to receive mail from folder [" + this.folder.getFullName() + "]");
|
||||
}
|
||||
this.logger.debug(() -> "attempting to receive mail from folder [" + this.folder.getFullName() + "]");
|
||||
Message[] messagesToProcess;
|
||||
Message[] messages = searchForNewMessages();
|
||||
if (this.maxFetchSize > 0 && messages.length > this.maxFetchSize) {
|
||||
Message[] reducedMessages = new Message[this.maxFetchSize];
|
||||
System.arraycopy(messages, 0, reducedMessages, 0, this.maxFetchSize);
|
||||
messages = reducedMessages;
|
||||
messagesToProcess = reducedMessages;
|
||||
}
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug("found " + messages.length + " new messages");
|
||||
else {
|
||||
messagesToProcess = messages;
|
||||
}
|
||||
if (messages.length > 0) {
|
||||
this.logger.debug(() -> "found " + messagesToProcess.length + " new messages");
|
||||
if (messagesToProcess.length > 0) {
|
||||
fetchMessages(messages);
|
||||
}
|
||||
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug("Received " + messages.length + " messages");
|
||||
}
|
||||
this.logger.debug(() -> "Received " + messagesToProcess.length + " messages");
|
||||
|
||||
MimeMessage[] filteredMessages = filterMessagesThruSelector(messages);
|
||||
MimeMessage[] filteredMessages = filterMessagesThruSelector(messagesToProcess);
|
||||
|
||||
postProcessFilteredMessages(filteredMessages);
|
||||
return filteredMessages;
|
||||
@@ -465,11 +461,13 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
|
||||
}
|
||||
content = theMessage.getContent();
|
||||
if (content instanceof String) {
|
||||
headers.put(MessageHeaders.CONTENT_TYPE, "text/plain");
|
||||
String mailContentType = (String) headers.get(MailHeaders.CONTENT_TYPE);
|
||||
if (mailContentType != null && mailContentType.toLowerCase().startsWith("text")) {
|
||||
if (MimeTypeUtils.TEXT_PLAIN.getType().equals(mailContentType)) {
|
||||
headers.put(MessageHeaders.CONTENT_TYPE, mailContentType);
|
||||
}
|
||||
else {
|
||||
headers.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN_VALUE);
|
||||
}
|
||||
}
|
||||
else if (content instanceof InputStream) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
@@ -494,7 +492,7 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
|
||||
}
|
||||
|
||||
private Object byteArrayToContent(Map<String, Object> headers, ByteArrayOutputStream baos) {
|
||||
headers.put(MessageHeaders.CONTENT_TYPE, "application/octet-stream");
|
||||
headers.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_OCTET_STREAM_VALUE);
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
@@ -524,10 +522,8 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
|
||||
for (Message message : filteredMessages) {
|
||||
if (!recentFlagSupported) {
|
||||
if (flags != null && flags.contains(Flags.Flag.USER)) {
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug("USER flags are supported by this mail server. Flagging message with '"
|
||||
+ this.userFlag + "' user flag");
|
||||
}
|
||||
this.logger.debug(() -> "USER flags are supported by this mail server. Flagging message with '"
|
||||
+ this.userFlag + "' user flag");
|
||||
Flags siFlags = new Flags();
|
||||
siFlags.add(this.userFlag);
|
||||
message.setFlags(siFlags, true);
|
||||
@@ -556,10 +552,10 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
|
||||
filteredMessages.add(message);
|
||||
}
|
||||
else {
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug("Fetched email with subject '" + message.getSubject()
|
||||
+ "' will be discarded by the matching filter and will not be flagged as SEEN.");
|
||||
}
|
||||
String subject = message.getSubject();
|
||||
this.logger.debug(() ->
|
||||
"Fetched email with subject '" + subject
|
||||
+ "' will be discarded by the matching filter and will not be flagged as SEEN.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -660,9 +656,9 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
|
||||
try {
|
||||
complexContent = source.getContent();
|
||||
}
|
||||
catch (IOException e) {
|
||||
complexContent = "Unable to extract content; see logs: " + e.getMessage();
|
||||
AbstractMailReceiver.this.logger.error("Failed to extract content from " + source, e);
|
||||
catch (IOException ex) {
|
||||
complexContent = "Unable to extract content; see logs: " + ex.getMessage();
|
||||
AbstractMailReceiver.this.logger.error(ex, () -> "Failed to extract content from " + source);
|
||||
}
|
||||
this.content = complexContent;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -229,9 +229,7 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport implements Be
|
||||
this.applicationEventPublisher.publishEvent(new ImapIdleExceptionEvent(e));
|
||||
}
|
||||
else {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("No application event publisher for exception: " + e.getMessage());
|
||||
}
|
||||
logger.debug(() -> "No application event publisher for exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,13 +246,11 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport implements Be
|
||||
ImapIdleChannelAdapter.this.idleTask.run();
|
||||
logger.debug("Task completed successfully. Re-scheduling it again right away.");
|
||||
}
|
||||
catch (Exception e) { //run again after a delay
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Failed to execute IDLE task. Will attempt to resubmit in "
|
||||
+ ImapIdleChannelAdapter.this.reconnectDelay + " milliseconds.", e);
|
||||
}
|
||||
catch (Exception ex) { //run again after a delay
|
||||
logger.warn(ex, () -> "Failed to execute IDLE task. Will attempt to resubmit in "
|
||||
+ ImapIdleChannelAdapter.this.reconnectDelay + " milliseconds.");
|
||||
ImapIdleChannelAdapter.this.receivingTaskTrigger.delayNextExecution();
|
||||
publishException(e);
|
||||
publishException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -276,9 +272,7 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport implements Be
|
||||
Folder folder = ImapIdleChannelAdapter.this.mailReceiver.getFolder();
|
||||
if (folder != null && folder.isOpen() && isRunning()) {
|
||||
Object[] mailMessages = ImapIdleChannelAdapter.this.mailReceiver.receive();
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("received " + mailMessages.length + " mail messages");
|
||||
}
|
||||
logger.debug(() -> "received " + mailMessages.length + " mail messages");
|
||||
for (Object mailMessage : mailMessages) {
|
||||
Runnable messageSendingTask = createMessageSendingTask(mailMessage);
|
||||
if (isRunning()) {
|
||||
@@ -287,14 +281,14 @@ public class ImapIdleChannelAdapter extends MessageProducerSupport implements Be
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (MessagingException e) {
|
||||
logger.warn("error occurred in idle task", e);
|
||||
catch (MessagingException ex) {
|
||||
logger.warn(ex, "error occurred in idle task");
|
||||
if (ImapIdleChannelAdapter.this.shouldReconnectAutomatically) {
|
||||
throw new IllegalStateException("Failure in 'idle' task. Will resubmit.", e);
|
||||
throw new IllegalStateException("Failure in 'idle' task. Will resubmit.", ex);
|
||||
}
|
||||
else {
|
||||
throw new org.springframework.messaging.MessagingException(
|
||||
"Failure in 'idle' task. Will NOT resubmit.", e);
|
||||
"Failure in 'idle' task. Will NOT resubmit.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -141,7 +141,7 @@ public class ImapMailReceiver extends AbstractMailReceiver {
|
||||
this.isInternalScheduler = true;
|
||||
}
|
||||
Properties javaMailProperties = getJavaMailProperties();
|
||||
for (String name : new String[] { PROTOCOL, "imaps" }) {
|
||||
for (String name : new String[]{ PROTOCOL, "imaps" }) {
|
||||
String peek = "mail." + name + ".peek";
|
||||
if (javaMailProperties.getProperty(peek) == null) {
|
||||
javaMailProperties.setProperty(peek, "true");
|
||||
@@ -270,7 +270,7 @@ public class ImapMailReceiver extends AbstractMailReceiver {
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
logger.error("Error during resetting idle state.", ex);
|
||||
logger.error(ex, "Error during resetting idle state.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -345,13 +345,11 @@ public class ImapMailReceiver extends AbstractMailReceiver {
|
||||
}
|
||||
|
||||
private SearchTerm applyTermsWhenNoRecentFlag(Folder folder, SearchTerm searchTerm) {
|
||||
NotTerm notFlagged = null;
|
||||
NotTerm notFlagged;
|
||||
if (folder.getPermanentFlags().contains(Flag.USER)) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("This email server does not support RECENT flag, but it does support " +
|
||||
"USER flags which will be used to prevent duplicates during email fetch." +
|
||||
" This receiver instance uses flag: " + getUserFlag());
|
||||
}
|
||||
logger.debug(() -> "This email server does not support RECENT flag, but it does support " +
|
||||
"USER flags which will be used to prevent duplicates during email fetch." +
|
||||
" This receiver instance uses flag: " + getUserFlag());
|
||||
Flags siFlags = new Flags();
|
||||
siFlags.add(getUserFlag());
|
||||
notFlagged = new NotTerm(new FlagTerm(siFlags, true));
|
||||
|
||||
Reference in New Issue
Block a user