Improved: Fixing defects reported by FindBugs, package

org.apache.ofbiz.content.compdoc.
(OFBIZ-9825)

The patch was modified to optimize the code a bit more.

Thanks Dennis Balkir for reporting and providing the patch.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1817697 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Brohl
2017-12-10 11:14:35 +00:00
parent 4eadb514de
commit f1eb4c8a8a
2 changed files with 23 additions and 31 deletions
@@ -24,6 +24,7 @@ import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
@@ -102,7 +103,8 @@ public class CompDocEvents {
Map<String, Object> persistResult = dispatcher.runSync("persistContentAndAssoc", persistMap);
contentId = (String)persistResult.get("contentId");
//request.setAttribute("contentId", contentId);
for (Object obj : persistResult.keySet()) {
for (Entry<String, Object> entry : persistResult.entrySet()) {
Object obj = entry.getValue();
Object val = persistResult.get(obj);
request.setAttribute(obj.toString(), val);
}
@@ -112,7 +114,8 @@ public class CompDocEvents {
contentRevisionMap.put("contentId", contentId);
contentRevisionMap.put("userLogin", userLogin);
Map<String, Object> result = dispatcher.runSync("persistContentRevisionAndItem", contentRevisionMap);
for (Object obj : result.keySet()) {
for (Entry<String, Object> entry : result.entrySet()) {
Object obj = entry.getValue();
Object val = result.get(obj);
request.setAttribute(obj.toString(), val);
}
@@ -147,16 +150,10 @@ public class CompDocEvents {
Map<String, Object> paramMap = UtilHttp.getParameterMap(request);
String contentId = (String)paramMap.get("contentId");
Locale locale = UtilHttp.getLocale(request);
String rootDir = null;
String webSiteId = WebSiteWorker.getWebSiteId(request);
String https = null;
if (UtilValidate.isEmpty(rootDir)) {
rootDir = servletContext.getRealPath("/");
}
if (UtilValidate.isEmpty(https)) {
https = (String) servletContext.getAttribute("https");
}
String rootDir = servletContext.getRealPath("/");
String https = (String) servletContext.getAttribute("https");
Map<String, Object> mapIn = new HashMap<String, Object>();
mapIn.put("contentId", contentId);
@@ -209,16 +206,10 @@ public class CompDocEvents {
Map<String, Object> paramMap = UtilHttp.getParameterMap(request);
String contentId = (String)paramMap.get("contentId");
Locale locale = UtilHttp.getLocale(request);
String rootDir = null;
String webSiteId = WebSiteWorker.getWebSiteId(request);
String https = null;
if (UtilValidate.isEmpty(rootDir)) {
rootDir = servletContext.getRealPath("/");
}
if (UtilValidate.isEmpty(https)) {
https = (String) servletContext.getAttribute("https");
}
String rootDir = servletContext.getRealPath("/");
String https = (String) servletContext.getAttribute("https");
Map<String, Object> mapIn = new HashMap<String, Object>();
mapIn.put("contentId", contentId);
@@ -28,6 +28,7 @@ import java.util.Locale;
import java.util.Map;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.GeneralException;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.base.util.UtilProperties;
import org.apache.ofbiz.base.util.UtilValidate;
@@ -47,7 +48,9 @@ import org.apache.ofbiz.webapp.event.CoreEvents;
import org.apache.ofbiz.webapp.view.ViewHandlerException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BadPdfFormatException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
@@ -172,7 +175,7 @@ public class CompDocServices {
} else if (inputMimeType != null && "text/html".equals(inputMimeType)) {
ByteBuffer byteBuffer = DataResourceWorker.getContentAsByteBuffer(delegator, thisDataResourceId, https, webSiteId, locale, rootDir);
inputByteArray = byteBuffer.array();
String s = new String(inputByteArray);
String s = new String(inputByteArray, "UTF-8");
Debug.logInfo("text/html string:" + s, module);
continue;
} else if (inputMimeType != null && "application/vnd.ofbiz.survey.response".equals(inputMimeType)) {
@@ -192,7 +195,7 @@ public class CompDocServices {
acroFormContentId = survey.getString("acroFormContentId");
if (UtilValidate.isNotEmpty(acroFormContentId)) {
// TODO: is something supposed to be done here?
}
}
}
}
if (surveyResponse != null) {
@@ -237,10 +240,7 @@ public class CompDocServices {
return results;
} catch (GenericEntityException e) {
return ServiceUtil.returnError(e.toString());
} catch (IOException e) {
Debug.logError(e, "Error in CompDoc operation: ", module);
return ServiceUtil.returnError(e.toString());
} catch (Exception e) {
} catch (IOException | DocumentException | GeneralException e) {
Debug.logError(e, "Error in CompDoc operation: ", module);
return ServiceUtil.returnError(e.toString());
}
@@ -296,7 +296,7 @@ public class CompDocServices {
} else if (inputMimeType != null && "text/html".equals(inputMimeType)) {
ByteBuffer byteBuffer = DataResourceWorker.getContentAsByteBuffer(delegator, dataResourceId, https, webSiteId, locale, rootDir);
inputByteArray = byteBuffer.array();
String s = new String(inputByteArray);
String s = new String(inputByteArray, "UTF-8");
Debug.logInfo("text/html string:" + s, module);
} else if (inputMimeType != null && "application/vnd.ofbiz.survey.response".equals(inputMimeType)) {
String surveyResponseId = dataResource.getString("relatedDetailId");
@@ -315,7 +315,7 @@ public class CompDocServices {
acroFormContentId = survey.getString("acroFormContentId");
if (UtilValidate.isNotEmpty(acroFormContentId)) {
// TODO: is something supposed to be done here?
}
}
}
}
@@ -344,14 +344,15 @@ public class CompDocServices {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentMimeTypeNotSupported", locale));
}
if (inputByteArray == null) {
Debug.logError("Error in PDF generation: ", module);
return ServiceUtil.returnError("The array used to create outByteBuffer is still declared null");
}
ByteBuffer outByteBuffer = ByteBuffer.wrap(inputByteArray);
results.put("outByteBuffer", outByteBuffer);
} catch (GenericEntityException e) {
return ServiceUtil.returnError(e.toString());
} catch (IOException e) {
Debug.logError(e, "Error in PDF generation: ", module);
return ServiceUtil.returnError(e.toString());
} catch (Exception e) {
} catch (IOException | GeneralException e) {
Debug.logError(e, "Error in PDF generation: ", module);
return ServiceUtil.returnError(e.toString());
}