You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by mb...@apache.org on 2017/10/12 21:18:35 UTC
svn commit: r1812051 - in
/ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail:
JavaMailContainer.java MimeMessageWrapper.java ServiceMcaAction.java
ServiceMcaCondition.java ServiceMcaRule.java
Author: mbrohl
Date: Thu Oct 12 21:18:35 2017
New Revision: 1812051
URL: http://svn.apache.org/viewvc?rev=1812051&view=rev
Log:
Improved: Fixing defects reported by FindBugs, package
org.apache.ofbiz.service.mail.
(OFBIZ-9690)
Thanks Dennis Balkir for reporting and providing the patch.
Modified:
ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java
ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/MimeMessageWrapper.java
ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaAction.java
ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaCondition.java
ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaRule.java
Modified: ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java?rev=1812051&r1=1812050&r2=1812051&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java Thu Oct 12 21:18:35 2017
@@ -20,6 +20,7 @@ package org.apache.ofbiz.service.mail;
import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executors;
@@ -84,7 +85,7 @@ public class JavaMailContainer implement
public void init(List<StartupCommand> ofbizCommands, String name, String configFile) throws ContainerException {
this.name = name;
this.configFile = configFile;
- this.stores = new LinkedHashMap<Store, Session>();
+ this.stores = new LinkedHashMap<>();
this.pollTimer = Executors.newScheduledThreadPool(1);
}
@@ -124,10 +125,8 @@ public class JavaMailContainer implement
for (ContainerConfig.Configuration.Property prop: configs) {
Session session = this.makeSession(prop);
Store store = this.getStore(session);
- if (store != null) {
- stores.put(store, session);
- store.addStoreListener(new LoggingStoreListener());
- }
+ stores.put(store, session);
+ store.addStoreListener(new LoggingStoreListener());
}
// start the polling timer
@@ -164,7 +163,7 @@ public class JavaMailContainer implement
Map<String, ContainerConfig.Configuration.Property> clientProps = client.properties;
if (clientProps != null) {
for (ContainerConfig.Configuration.Property p: clientProps.values()) {
- props.setProperty(p.name.toLowerCase(), p.value);
+ props.setProperty(p.name.toLowerCase(Locale.getDefault()), p.value);
}
}
return Session.getInstance(props);
@@ -242,7 +241,7 @@ public class JavaMailContainer implement
String portStr = props.getProperty("mail." + protocol + ".port");
if (UtilValidate.isNotEmpty(portStr)) {
try {
- portProps = Integer.valueOf(portStr);
+ portProps = Integer.parseInt(portStr);
} catch (NumberFormatException e) {
Debug.logError("The port given in property mail." + protocol + ".port is wrong, please check", module);
}
@@ -251,7 +250,7 @@ public class JavaMailContainer implement
portStr = props.getProperty("mail.port");
if (UtilValidate.isNotEmpty(portStr)) {
try {
- portProps = Integer.valueOf(props.getProperty("mail.port"));
+ portProps = Integer.parseInt(props.getProperty("mail.port"));
} catch (NumberFormatException e) {
Debug.logError("The port given in property mail.port is wrong, please check", module);
}
@@ -266,7 +265,7 @@ public class JavaMailContainer implement
return new URLName(protocol, host, port, file, userName, password);
}
- class LoggingStoreListener implements StoreListener {
+ static class LoggingStoreListener implements StoreListener {
@Override
public void notification(StoreEvent event) {
@@ -278,6 +277,8 @@ public class JavaMailContainer implement
case StoreEvent.NOTICE:
typeString = "NOTICE: ";
break;
+ default:
+ Debug.logWarning("There was a case error in LoggingStoreListener.notification", module);
}
if (Debug.verboseOn()) Debug.logVerbose("JavaMail " + typeString + event.getMessage(), module);
Modified: ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/MimeMessageWrapper.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/MimeMessageWrapper.java?rev=1812051&r1=1812050&r2=1812051&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/MimeMessageWrapper.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/MimeMessageWrapper.java Thu Oct 12 21:18:35 2017
@@ -26,6 +26,7 @@ import java.nio.ByteBuffer;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;
+import java.util.Locale;
import java.util.Properties;
import javax.mail.Address;
@@ -64,7 +65,7 @@ public class MimeMessageWrapper implemen
this.setSession(session);
}
- public void setSession(Session session) {
+ public synchronized void setSession(Session session) {
this.session = session;
this.mailProperties = session.getProperties();
}
@@ -76,12 +77,11 @@ public class MimeMessageWrapper implemen
return session;
}
- public void setMessage(MimeMessage message) {
+ public synchronized void setMessage(MimeMessage message) {
if (message != null) {
// serialize the message
this.message = message;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- try {
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
message.writeTo(baos);
baos.flush();
serializedBytes = baos.toByteArray();
@@ -95,16 +95,8 @@ public class MimeMessageWrapper implemen
} else {
this.parts = 0;
}
- } catch (MessagingException e) {
- Debug.logError(e, module);
- } catch (IOException e) {
+ } catch (IOException | MessagingException e) {
Debug.logError(e, module);
- } finally {
- try {
- baos.close();
- } catch (IOException e) {
- Debug.logError(e, module);
- }
}
}
}
@@ -129,10 +121,10 @@ public class MimeMessageWrapper implemen
String[] headers = getHeader(header);
if (headers != null && headers.length > 0) {
return headers[0];
- } else {
- return null;
}
+ return null;
}
+
public String[] getHeader(String header) {
MimeMessage message = getMessage();
try {
@@ -223,11 +215,11 @@ public class MimeMessageWrapper implemen
}
}
- public String getContentType() {
+ public synchronized String getContentType() {
return contentType;
}
- public int getMainPartCount() {
+ public synchronized int getMainPartCount() {
return this.parts;
}
@@ -237,9 +229,8 @@ public class MimeMessageWrapper implemen
Object content = part.getContent();
if (content instanceof Multipart) {
return ((Multipart) content).getCount();
- } else {
- return 0;
}
+ return 0;
} catch (Exception e) {
Debug.logError(e, module);
return -1;
@@ -247,30 +238,29 @@ public class MimeMessageWrapper implemen
}
public List<String> getAttachmentIndexes() {
- List<String> attachments = new LinkedList<String>();
+ List<String> attachments = new LinkedList<>();
if (getMainPartCount() == 0) { // single part message (no attachments)
return attachments;
- } else {
- for (int i = 0; i < getMainPartCount(); i++) {
- int subPartCount = getSubPartCount(i);
- String idx = Integer.toString(i);
- if (subPartCount > 0) {
- for (int si = 0; si < subPartCount; si++) {
- String sidx = idx + "." + Integer.toString(si);
- if (getPartDisposition(sidx) != null && (getPartDisposition(sidx).equalsIgnoreCase(Part.ATTACHMENT) ||
- getPartDisposition(sidx).equalsIgnoreCase(Part.INLINE))) {
- attachments.add(sidx);
- }
- }
- } else {
- if (getPartDisposition(idx) != null && (getPartDisposition(idx).equalsIgnoreCase(Part.ATTACHMENT) ||
- getPartDisposition(idx).equalsIgnoreCase(Part.INLINE))) {
- attachments.add(idx);
+ }
+ for (int i = 0; i < getMainPartCount(); i++) {
+ int subPartCount = getSubPartCount(i);
+ String idx = Integer.toString(i);
+ if (subPartCount > 0) {
+ for (int si = 0; si < subPartCount; si++) {
+ String sidx = idx + "." + Integer.toString(si);
+ if (getPartDisposition(sidx) != null && (getPartDisposition(sidx).equalsIgnoreCase(Part.ATTACHMENT) ||
+ getPartDisposition(sidx).equalsIgnoreCase(Part.INLINE))) {
+ attachments.add(sidx);
}
}
+ } else {
+ if (getPartDisposition(idx) != null && (getPartDisposition(idx).equalsIgnoreCase(Part.ATTACHMENT) ||
+ getPartDisposition(idx).equalsIgnoreCase(Part.INLINE))) {
+ attachments.add(idx);
+ }
}
- return attachments;
}
+ return attachments;
}
public String getMessageBody() {
@@ -283,56 +273,54 @@ public class MimeMessageWrapper implemen
Debug.logError(e, module);
return null;
}
- } else { // multi-part message
- StringBuffer body = new StringBuffer();
- for (int i = 0; i < getMainPartCount(); i++) {
- int subPartCount = getSubPartCount(i);
- String idx = Integer.toString(i);
- if (subPartCount > 0) {
- for (int si = 0; si < subPartCount; si++) {
- String sidx = idx + "." + Integer.toString(si);
- if (getPartContentType(sidx) != null && getPartContentType(sidx).toLowerCase().startsWith("text")) {
- if (getPartDisposition(sidx) == null || getPartDisposition(sidx).equals(Part.INLINE)) {
- body.append(getPartText(sidx)).append("\n");
- }
+ }
+ StringBuffer body = new StringBuffer();
+ for (int i = 0; i < getMainPartCount(); i++) {
+ int subPartCount = getSubPartCount(i);
+ String idx = Integer.toString(i);
+ if (subPartCount > 0) {
+ for (int si = 0; si < subPartCount; si++) {
+ String sidx = idx + "." + Integer.toString(si);
+ if (getPartContentType(sidx) != null && getPartContentType(sidx).toLowerCase(Locale.getDefault()).startsWith("text")) {
+ if (getPartDisposition(sidx) == null || getPartDisposition(sidx).equals(Part.INLINE)) {
+ body.append(getPartText(sidx)).append("\n");
}
}
- } else {
- if (getPartContentType(idx) != null && getPartContentType(idx).toLowerCase().startsWith("text")) {
- // make sure the part isn't an attachment
- if (getPartDisposition(idx) == null || getPartDisposition(idx).equals(Part.INLINE)) {
- body.append(getPartText(idx)).append("\n");
- }
+ }
+ } else {
+ if (getPartContentType(idx) != null && getPartContentType(idx).toLowerCase(Locale.getDefault()).startsWith("text")) {
+ // make sure the part isn't an attachment
+ if (getPartDisposition(idx) == null || getPartDisposition(idx).equals(Part.INLINE)) {
+ body.append(getPartText(idx)).append("\n");
}
}
}
- return body.toString();
}
+ return body.toString();
}
public String getMessageBodyContentType() {
String contentType = getContentType();
- if (contentType != null && contentType.toLowerCase().startsWith("text")) {
+ if (contentType != null && contentType.toLowerCase(Locale.getDefault()).startsWith("text")) {
return contentType;
- } else {
- for (int i = 0; i < getMainPartCount(); i++) {
- int subPartCount = getSubPartCount(i);
- String idx = Integer.toString(i);
- if (subPartCount > 0) {
- for (int si = 0; si < subPartCount; si++) {
- String sidx = idx + "." + Integer.toString(si);
- if (getPartContentType(sidx) != null && getPartContentType(sidx).toLowerCase().startsWith("text")) {
- if (getPartDisposition(sidx) == null || getPartDisposition(sidx).equals(Part.INLINE)) {
- return getPartContentType(sidx);
- }
+ }
+ for (int i = 0; i < getMainPartCount(); i++) {
+ int subPartCount = getSubPartCount(i);
+ String idx = Integer.toString(i);
+ if (subPartCount > 0) {
+ for (int si = 0; si < subPartCount; si++) {
+ String sidx = idx + "." + Integer.toString(si);
+ if (getPartContentType(sidx) != null && getPartContentType(sidx).toLowerCase(Locale.getDefault()).startsWith("text")) {
+ if (getPartDisposition(sidx) == null || getPartDisposition(sidx).equals(Part.INLINE)) {
+ return getPartContentType(sidx);
}
}
- } else {
- if (getPartContentType(idx) != null && getPartContentType(idx).toLowerCase().startsWith("text")) {
- // make sure the part isn't an attachment
- if (getPartDisposition(idx) == null || getPartDisposition(idx).equals(Part.INLINE)) {
- return getPartContentType(idx);
- }
+ }
+ } else {
+ if (getPartContentType(idx) != null && getPartContentType(idx).toLowerCase(Locale.getDefault()).startsWith("text")) {
+ // make sure the part isn't an attachment
+ if (getPartDisposition(idx) == null || getPartDisposition(idx).equals(Part.INLINE)) {
+ return getPartContentType(idx);
}
}
}
@@ -359,9 +347,8 @@ public class MimeMessageWrapper implemen
Debug.logError(e, module);
return null;
}
- } else {
- return null;
}
+ return null;
}
public String getPartContentType(String index) {
@@ -373,9 +360,8 @@ public class MimeMessageWrapper implemen
Debug.logError(e, module);
return null;
}
- } else {
- return null;
}
+ return null;
}
public String getPartDisposition(String index) {
@@ -387,9 +373,8 @@ public class MimeMessageWrapper implemen
Debug.logError(e, module);
return null;
}
- } else {
- return null;
}
+ return null;
}
public String getPartFilename(String index) {
@@ -401,9 +386,8 @@ public class MimeMessageWrapper implemen
Debug.logError(e, module);
return null;
}
- } else {
- return null;
}
+ return null;
}
public ByteBuffer getPartByteBuffer(String index) {
@@ -416,9 +400,8 @@ public class MimeMessageWrapper implemen
Debug.logError(e, module);
return null;
}
- } else {
- return null;
}
+ return null;
}
public String getPartText(String index) {
@@ -430,9 +413,8 @@ public class MimeMessageWrapper implemen
Debug.logError(e, module);
return null;
}
- } else {
- return null;
}
+ return null;
}
public String getPartRawText(String index) {
@@ -444,15 +426,14 @@ public class MimeMessageWrapper implemen
Debug.logError(e, module);
return null;
}
- } else {
- return null;
}
+ return null;
}
public BodyPart getPart(String indexStr) {
int mainIndex, subIndex;
try {
- if (indexStr.indexOf(".") == -1) {
+ if (indexStr.indexOf('.') == -1) {
mainIndex = Integer.parseInt(indexStr);
subIndex = -1;
} else {
@@ -474,18 +455,16 @@ public class MimeMessageWrapper implemen
Multipart mp = (Multipart) message.getContent();
if (subIndex == -1) {
return mp.getBodyPart(mainIndex);
- } else {
- BodyPart part = mp.getBodyPart(mainIndex);
- int subPartCount = getSubPartCount(mainIndex);
- if (subPartCount > subIndex) {
- Multipart sp = (Multipart) part.getContent();
- return sp.getBodyPart(subIndex);
- } else {
- Debug.logWarning("Requested a subpart [" + subIndex + "] which deos not exist; only [" + getSubPartCount(mainIndex) + "] parts", module);
- // there is no sub part to find
- return part;
- }
}
+ BodyPart part = mp.getBodyPart(mainIndex);
+ int subPartCount = getSubPartCount(mainIndex);
+ if (subPartCount > subIndex) {
+ Multipart sp = (Multipart) part.getContent();
+ return sp.getBodyPart(subIndex);
+ }
+ Debug.logWarning("Requested a subpart [" + subIndex + "] which deos not exist; only [" + getSubPartCount(mainIndex) + "] parts", module);
+ // there is no sub part to find
+ return part;
} catch (MessagingException e) {
Debug.logError(e, module);
return null;
@@ -493,10 +472,9 @@ public class MimeMessageWrapper implemen
Debug.logError(e, module);
return null;
}
- } else {
- Debug.logWarning("Requested a part [" + mainIndex + "] which deos not exist; only [" + getMainPartCount() + "] parts", module);
- return null;
}
+ Debug.logWarning("Requested a part [" + mainIndex + "] which deos not exist; only [" + getMainPartCount() + "] parts", module);
+ return null;
}
protected String getContentText(Object content) {
@@ -523,7 +501,7 @@ public class MimeMessageWrapper implemen
byte[] buffer = new byte[4096];
try {
for (int n; (n = stream.read(buffer)) != -1;) {
- builder.append(new String(buffer, 0, n));
+ builder.append(new String(buffer, 0, n, "UTF-8"));
}
} catch (IOException e) {
Debug.logError(e, module);
@@ -548,7 +526,7 @@ public class MimeMessageWrapper implemen
}
static {
- Converters.registerConverter(new MimeMessageToString<String>());
+ Converters.registerConverter(new MimeMessageToString<>());
}
/**
Modified: ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaAction.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaAction.java?rev=1812051&r1=1812050&r2=1812051&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaAction.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaAction.java Thu Oct 12 21:18:35 2017
@@ -52,7 +52,7 @@ public class ServiceMcaAction implements
}
public boolean runAction(LocalDispatcher dispatcher, MimeMessageWrapper messageWrapper, GenericValue userLogin) throws GenericServiceException {
- Map<String, Object> serviceContext = new HashMap<String, Object>();
+ Map<String, Object> serviceContext = new HashMap<>();
serviceContext.putAll(UtilMisc.toMap("messageWrapper", messageWrapper, "userLogin", userLogin));
serviceContext.put("userLogin", ServiceUtil.getUserLogin(dispatcher.getDispatchContext(), serviceContext, runAsUser));
@@ -61,9 +61,8 @@ public class ServiceMcaAction implements
if (ServiceUtil.isError(result)) {
Debug.logError(ServiceUtil.getErrorMessage(result), module);
return false;
- } else {
- return true;
}
+ return true;
} else if (serviceMode.equals("async")) {
dispatcher.runAsync(serviceName, serviceContext, persist);
return true;
Modified: ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaCondition.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaCondition.java?rev=1812051&r1=1812050&r2=1812051&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaCondition.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaCondition.java Thu Oct 12 21:18:35 2017
@@ -73,6 +73,8 @@ public class ServiceMcaCondition impleme
case CONDITION_SERVICE:
this.serviceName = condElement.getAttribute("service-name");
break;
+ default:
+ Debug.logWarning("There was an error in the switch-case in ServiceMcaCondition", module);
}
}
@@ -89,19 +91,16 @@ public class ServiceMcaCondition impleme
if (result == null) {
Debug.logError("Service MCA Condition Service [" + serviceName + "] returned null!", module);
return false;
- } else {
- if (ServiceUtil.isError(result)) {
- Debug.logError(ServiceUtil.getErrorMessage(result), module);
- return false;
- } else {
- Boolean reply = (Boolean) result.get("conditionReply");
- if (reply == null) {
- reply = Boolean.FALSE;
- }
- return reply.booleanValue();
- }
}
- // invoke the condition service
+ if (ServiceUtil.isError(result)) {
+ Debug.logError(ServiceUtil.getErrorMessage(result), module);
+ return false;
+ }
+ Boolean reply = (Boolean) result.get("conditionReply");
+ if (reply == null) {
+ reply = Boolean.FALSE;
+ }
+ return reply.booleanValue();
} else if (headerName != null) {
// compare the header field
MimeMessage message = messageWrapper.getMessage();
@@ -249,7 +248,7 @@ public class ServiceMcaCondition impleme
if (c instanceof String) {
return UtilMisc.toList((String) c);
} else if (c instanceof Multipart) {
- List<String> textContent = new LinkedList<String>();
+ List<String> textContent = new LinkedList<>();
int count = ((Multipart) c).getCount();
for (int i = 0; i < count; i++) {
BodyPart bp = ((Multipart) c).getBodyPart(i);
@@ -257,7 +256,7 @@ public class ServiceMcaCondition impleme
}
return textContent;
} else {
- return new LinkedList<String>();
+ return new LinkedList<>();
}
}
}
Modified: ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaRule.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaRule.java?rev=1812051&r1=1812050&r2=1812051&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaRule.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/ServiceMcaRule.java Thu Oct 12 21:18:35 2017
@@ -35,8 +35,8 @@ public class ServiceMcaRule implements j
public static final String module = ServiceMcaRule.class.getName();
protected String ruleName = null;
- protected List<ServiceMcaCondition> conditions = new LinkedList<ServiceMcaCondition>();
- protected List<ServiceMcaAction> actions = new LinkedList<ServiceMcaAction>();
+ protected List<ServiceMcaCondition> conditions = new LinkedList<>();
+ protected List<ServiceMcaAction> actions = new LinkedList<>();
protected boolean enabled = true;
public ServiceMcaRule(Element mca) {