svn commit: r1817697 - in /ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/compdoc: CompDocEvents.java CompDocServices.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1817697 - in /ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/compdoc: CompDocEvents.java CompDocServices.java

mbrohl
Author: mbrohl
Date: Sun Dec 10 11:14:35 2017
New Revision: 1817697

URL: http://svn.apache.org/viewvc?rev=1817697&view=rev
Log:
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.

Modified:
    ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/compdoc/CompDocEvents.java
    ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/compdoc/CompDocServices.java

Modified: ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/compdoc/CompDocEvents.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/compdoc/CompDocEvents.java?rev=1817697&r1=1817696&r2=1817697&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/compdoc/CompDocEvents.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/compdoc/CompDocEvents.java Sun Dec 10 11:14:35 2017
@@ -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);

Modified: ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/compdoc/CompDocServices.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/compdoc/CompDocServices.java?rev=1817697&r1=1817696&r2=1817697&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/compdoc/CompDocServices.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/compdoc/CompDocServices.java Sun Dec 10 11:14:35 2017
@@ -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.Cor
 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());
         }