Author: mbrohl
Date: Sun Dec 10 09:33:46 2017
New Revision: 1817679
URL: http://svn.apache.org/viewvc?rev=1817679&view=rev
Log:
Improved: Fixing defects reported by FindBugs, package
org.apache.ofbiz.content.data.
(OFBIZ-9811)
Thanks Julian Leichert for reporting and providing the patch.
Modified:
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataServices.java
Modified:
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java
URL:
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java?rev=1817679&r1=1817678&r2=1817679&view=diff
==============================================================================
---
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java
(original)
+++
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataEvents.java
Sun Dec 10 09:33:46 2017
@@ -317,7 +317,7 @@ public class DataEvents {
Delegator delegator = (Delegator) request.getAttribute("delegator");
GenericValue userLogin = (GenericValue)
request.getSession().getAttribute("userLogin");
Map<String, Object> paramMap = UtilHttp.getParameterMap(request);
- String dataResourceId = (String)paramMap.get("dataResourceId");
+ String dataResourceId;
GenericValue dataResource = delegator.makeValue("DataResource");
dataResource.setPKFields(paramMap);
dataResource.setNonPKFields(paramMap);
Modified:
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java
URL:
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java?rev=1817679&r1=1817678&r2=1817679&view=diff
==============================================================================
---
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java
(original)
+++
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java
Sun Dec 10 09:33:46 2017
@@ -20,10 +20,11 @@ package org.apache.ofbiz.content.data;
import java.io.ByteArrayInputStream;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
@@ -177,12 +178,13 @@ public class DataResourceWorker impleme
public static void buildList(Map<String, Object> nd, List<Map<String,
Object>> lst, int depth) {
String id = (String) nd.get("id");
String nm = (String) nd.get("name");
- String spc = "";
+ StringBuilder spcBuilder = new StringBuilder();
for (int i = 0; i < depth; i++)
- spc += " ";
+ spcBuilder.append(" ");
Map<String, Object> map = new HashMap<String, Object>();
+ spcBuilder.append(nm);
map.put("dataCategoryId", id);
- map.put("categoryName", spc + nm);
+ map.put("categoryName", spcBuilder.toString());
if (id != null && !"ROOT".equals(id) && !id.equals("")) {
lst.add(map);
}
@@ -267,12 +269,12 @@ public class DataResourceWorker impleme
if (UtilValidate.isEmpty(imageFileName))
return mimeType;
- int pos = imageFileName.lastIndexOf(".");
+ int pos = imageFileName.lastIndexOf('.');
if (pos < 0)
return mimeType;
String suffix = imageFileName.substring(pos + 1);
- String suffixLC = suffix.toLowerCase();
+ String suffixLC = suffix.toLowerCase(Locale.getDefault());
if ("jpg".equals(suffixLC))
mimeType = "image/jpeg";
else
@@ -430,7 +432,7 @@ public class DataResourceWorker impleme
String prefix = System.getProperty("ofbiz.home");
String sep = "";
- if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") !=
(prefix.length() - 1)) {
+ if (objectInfo.indexOf('/') != 0 && prefix.lastIndexOf('/') !=
(prefix.length() - 1)) {
sep = "/";
}
file = FileUtil.getFile(prefix + sep + objectInfo);
@@ -443,7 +445,7 @@ public class DataResourceWorker impleme
}
String sep = "";
- if (objectInfo.indexOf("/") != 0 && contextRoot.lastIndexOf("/")
!= (contextRoot.length() - 1)) {
+ if (objectInfo.indexOf('/') != 0 && contextRoot.lastIndexOf('/')
!= (contextRoot.length() - 1)) {
sep = "/";
}
file = FileUtil.getFile(contextRoot + sep + objectInfo);
@@ -528,9 +530,12 @@ public class DataResourceWorker impleme
TreeMap<Long, File> dirMap = new TreeMap<Long, File>(desc);
if (parent.exists()) {
File[] subs = parent.listFiles();
- for (int i = 0; i < subs.length; i++) {
- if (subs[i].isDirectory()) {
- dirMap.put(Long.valueOf(subs[i].lastModified()), subs[i]);
+ if (subs != null) {
+ int length = subs.length;
+ for (int i = 0; i < length; i++) {
+ if (subs[i].isDirectory()) {
+ dirMap.put(Long.valueOf(subs[i].lastModified()),
subs[i]);
+ }
}
}
} else {
@@ -547,19 +552,25 @@ public class DataResourceWorker impleme
latestDir = dirMap.values().iterator().next();
if (latestDir != null) {
File[] dirList = latestDir.listFiles();
- if (dirList.length >= maxFiles) {
- latestDir = makeNewDirectory(parent);
+ if (dirList != null) {
+ int length = dirList.length;
+ if (length >= maxFiles) {
+ latestDir = makeNewDirectory(parent);
+ }
}
}
} else {
latestDir = makeNewDirectory(parent);
}
+ String name = "";
+ if (latestDir != null)
+ name = latestDir.getName();
- Debug.logInfo("Directory Name : " + latestDir.getName(), module);
+ Debug.logInfo("Directory Name : " + name, module);
if (absolute) {
- return latestDir.getAbsolutePath().replace('\\','/');
+ return latestDir.getAbsolutePath().replace('\\', '/');
} else {
- return initialPath + "/" + latestDir.getName();
+ return initialPath + "/" + name;
}
}
@@ -570,7 +581,9 @@ public class DataResourceWorker impleme
while (!newDir) {
latestDir = new File(parent, "" + System.currentTimeMillis());
if (!latestDir.exists()) {
- latestDir.mkdir();
+ if (!latestDir.mkdir()) {
+ Debug.logError("Directory: " + latestDir.getName() + ",
couldn't be created", module);
+ }
newDir = true;
}
}
@@ -812,20 +825,17 @@ public class DataResourceWorker impleme
}
String webSiteId = (String) templateContext.get("webSiteId");
if (UtilValidate.isEmpty(webSiteId)) {
- if (context != null)
- webSiteId = (String) context.get("webSiteId");
+ webSiteId = (String) context.get("webSiteId");
}
String https = (String) templateContext.get("https");
if (UtilValidate.isEmpty(https)) {
- if (context != null)
- https = (String) context.get("https");
+ https = (String) context.get("https");
}
String rootDir = (String) templateContext.get("rootDir");
if (UtilValidate.isEmpty(rootDir)) {
- if (context != null)
- rootDir = (String) context.get("rootDir");
+ rootDir = (String) context.get("rootDir");
}
String dataResourceId = dataResource.getString("dataResourceId");
@@ -871,7 +881,7 @@ public class DataResourceWorker impleme
} else {
String prefix =
DataResourceWorker.buildRequestPrefix(delegator, locale, webSiteId, https);
String sep = "";
- if (url.toString().indexOf("/") != 0 &&
prefix.lastIndexOf("/") != (prefix.length() - 1)) {
+ if (url.toString().indexOf('/') != 0 &&
prefix.lastIndexOf('/') != (prefix.length() - 1)) {
sep = "/";
}
String fixedUrlStr = prefix + sep + url.toString();
@@ -928,9 +938,7 @@ public class DataResourceWorker impleme
mimeContext.put("textData", textData);
String mimeString =
DataResourceWorker.renderMimeTypeTemplate(mimeTypeTemplate, mimeContext);
- if (mimeString != null) {
- out.append(mimeString);
- }
+ out.append(mimeString);
} else {
if (textData != null) {
out.append(textData);
@@ -961,27 +969,27 @@ public class DataResourceWorker impleme
if (!file.isAbsolute()) {
throw new GeneralException("File (" + objectInfo + ") is not
absolute");
}
- FileReader in = new FileReader(file);
+ InputStreamReader in = new InputStreamReader(new
FileInputStream(file), UtilIO.getUtf8());
UtilIO.copy(in, true, out);
} else if ("OFBIZ_FILE".equals(dataResourceTypeId) &&
UtilValidate.isNotEmpty(objectInfo)) {
String prefix = System.getProperty("ofbiz.home");
String sep = "";
- if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") !=
(prefix.length() - 1)) {
+ if (objectInfo.indexOf('/') != 0 && prefix.lastIndexOf('/') !=
(prefix.length() - 1)) {
sep = "/";
}
File file = FileUtil.getFile(prefix + sep + objectInfo);
- FileReader in = new FileReader(file);
+ InputStreamReader in = new InputStreamReader(new
FileInputStream(file), UtilIO.getUtf8());
UtilIO.copy(in, true, out);
} else if ("CONTEXT_FILE".equals(dataResourceTypeId) &&
UtilValidate.isNotEmpty(objectInfo)) {
String prefix = rootDir;
String sep = "";
- if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") !=
(prefix.length() - 1)) {
+ if (objectInfo.indexOf('/') != 0 && prefix.lastIndexOf('/') !=
(prefix.length() - 1)) {
sep = "/";
}
File file = FileUtil.getFile(prefix + sep + objectInfo);
- FileReader in = null;
+ InputStreamReader in = null;
try {
- in = new FileReader(file);
+ in = new InputStreamReader(new FileInputStream(file),
UtilIO.getUtf8());
String enc = in.getEncoding();
if (Debug.infoOn()) Debug.logInfo("in serveImage, encoding:" +
enc, module);
@@ -1037,7 +1045,7 @@ public class DataResourceWorker impleme
throw new GeneralException("Unsupported TEXT type; cannot
stream");
}
- byte[] bytes = text.getBytes();
+ byte[] bytes = text.getBytes(UtilIO.getUtf8());
return UtilMisc.toMap("stream", new ByteArrayInputStream(bytes),
"length", Long.valueOf(bytes.length));
// object (binary) data
Modified:
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataServices.java
URL:
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataServices.java?rev=1817679&r1=1817678&r2=1817679&view=diff
==============================================================================
---
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataServices.java
(original)
+++
ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/data/DataServices.java
Sun Dec 10 09:33:46 2017
@@ -21,8 +21,8 @@ package org.apache.ofbiz.content.data;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
-import java.io.FileWriter;
import java.io.IOException;
+import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.StringWriter;
import java.io.Writer;
@@ -36,6 +36,7 @@ import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.GeneralException;
import org.apache.ofbiz.base.util.UtilDateTime;
import org.apache.ofbiz.base.util.UtilGenerics;
+import org.apache.ofbiz.base.util.UtilIO;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.base.util.UtilProperties;
import org.apache.ofbiz.base.util.UtilValidate;
@@ -48,6 +49,8 @@ import org.apache.ofbiz.service.GenericS
import org.apache.ofbiz.service.ModelService;
import org.apache.ofbiz.service.ServiceUtil;
+import edu.emory.mathcs.backport.java.util.Arrays;
+
/**
* DataServices Class
*/
@@ -223,7 +226,7 @@ public class DataServices {
}
} else if ("OFBIZ_FILE".equals(dataResourceTypeId) ||
"OFBIZ_FILE_BIN".equals(dataResourceTypeId)) {
prefix = System.getProperty("ofbiz.home");
- if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") !=
(prefix.length() - 1)) {
+ if (objectInfo.indexOf('/') != 0 && prefix.lastIndexOf('/') !=
(prefix.length() - 1)) {
sep = "/";
}
file = new File(prefix + sep + objectInfo);
@@ -232,7 +235,7 @@ public class DataServices {
if (UtilValidate.isEmpty(prefix)) {
return
ServiceUtil.returnError(UtilProperties.getMessage(resource,
"ContentCannotFindContextFileWithEmptyContextRoot", locale));
}
- if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") !=
(prefix.length() - 1)) {
+ if (objectInfo.indexOf('/') != 0 && prefix.lastIndexOf('/') !=
(prefix.length() - 1)) {
sep = "/";
}
file = new File(prefix + sep + objectInfo);
@@ -243,10 +246,10 @@ public class DataServices {
// write the data to the file
if (UtilValidate.isNotEmpty(textData)) {
- try {
- FileWriter out = new FileWriter(file);
+ try (
+ OutputStreamWriter out = new OutputStreamWriter(new
FileOutputStream(file), UtilIO.getUtf8());
+ ) {
out.write(textData);
- out.close();
} catch (IOException e) {
Debug.logWarning(e, module);
return
ServiceUtil.returnError(UtilProperties.getMessage(resource,
"ContentUnableWriteCharacterDataToFile", UtilMisc.toMap("fileName",
file.getAbsolutePath()), locale));
@@ -415,27 +418,27 @@ public class DataServices {
}
} else if (dataResourceTypeId.startsWith("OFBIZ_FILE")) {
prefix = System.getProperty("ofbiz.home");
- if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") !=
(prefix.length() - 1)) {
+ if (objectInfo.indexOf('/') != 0 && prefix.lastIndexOf('/') !=
(prefix.length() - 1)) {
sep = "/";
}
file = new File(prefix + sep + objectInfo);
} else if (dataResourceTypeId.startsWith("CONTEXT_FILE")) {
prefix = (String) context.get("rootDir");
- if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") !=
(prefix.length() - 1)) {
+ if (objectInfo.indexOf('/') != 0 && prefix.lastIndexOf('/') !=
(prefix.length() - 1)) {
sep = "/";
}
file = new File(prefix + sep + objectInfo);
}
if (file == null) {
- throw new IOException("File: " + file + " is null");
+ throw new IOException("File is null");
}
// write the data to the file
if (UtilValidate.isNotEmpty(textData)) {
- try {
- FileWriter out = new FileWriter(file);
+ try (
+ OutputStreamWriter out = new OutputStreamWriter(new
FileOutputStream(file),UtilIO.getUtf8());
+ ) {
out.write(textData);
- out.close();
} catch (IOException e) {
Debug.logWarning(e, module);
return
ServiceUtil.returnError(UtilProperties.getMessage(resource,
"ContentUnableWriteCharacterDataToFile", UtilMisc.toMap("fileName",
file.getAbsolutePath()), locale));
@@ -518,7 +521,7 @@ public class DataServices {
GenericValue imageDataResource =
EntityQuery.use(delegator).from("ImageDataResource").where("dataResourceId",
dataResourceId).queryOne();
if (Debug.infoOn()) {
Debug.logInfo("imageDataResource(U):" + imageDataResource,
module);
- Debug.logInfo("imageBytes(U):" + imageBytes, module);
+ Debug.logInfo("imageBytes(U):" +
Arrays.toString(imageBytes), module);
}
if (imageDataResource == null) {
return createImageMethod(dctx, context);
@@ -603,13 +606,13 @@ public class DataServices {
Debug.logInfo("in createBinaryFileMethod, imageData:" +
imageData.length, module);
}
if (imageData != null && imageData.length > 0) {
- try {
+ try (
FileOutputStream out = new FileOutputStream(file);
+ ) {
out.write(imageData);
if (Debug.infoOn()) {
Debug.logInfo("in createBinaryFileMethod, length:" +
file.length(), module);
}
- out.close();
} catch (IOException e) {
Debug.logWarning(e, module);
throw new GenericServiceException(e.getMessage());
@@ -656,11 +659,12 @@ public class DataServices {
}
if (Debug.infoOn()) {
Debug.logInfo("in updateBinaryFileMethod, file:" + file, module);
- Debug.logInfo("in updateBinaryFileMethod, imageData:" + imageData,
module);
+ Debug.logInfo("in updateBinaryFileMethod, imageData:" +
Arrays.toString(imageData), module);
}
if (imageData != null && imageData.length > 0) {
- try {
- FileOutputStream out = new FileOutputStream(file);
+ try (
+ FileOutputStream out = new FileOutputStream(file);
+ ){
out.write(imageData);
if (Debug.infoOn()) {
Debug.logInfo("in updateBinaryFileMethod, length:" +
file.length(), module);