From 2a66a06aef7d1fdce6a2dccbcb5f0fb3591ad430 Mon Sep 17 00:00:00 2001 From: Michael Brohl Date: Sat, 7 Oct 2017 14:10:50 +0000 Subject: [PATCH] Improved: Fixing defects reported by FindBugs, package org.apache.ofbiz.common.qrcode. (OFBIZ-9633) Thanks Dennis Balkir for reporting and providing the patch. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1811427 13f79535-47bb-0310-9956-ffa450edef68 --- .../ofbiz/common/qrcode/QRCodeEvents.java | 6 ++-- .../ofbiz/common/qrcode/QRCodeServices.java | 36 ++++++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/qrcode/QRCodeEvents.java b/framework/common/src/main/java/org/apache/ofbiz/common/qrcode/QRCodeEvents.java index 0715942678..7fd8b4597a 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/qrcode/QRCodeEvents.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/qrcode/QRCodeEvents.java @@ -47,7 +47,7 @@ public class QRCodeEvents { /** Streams QR Code to the output. */ public static String serveQRCodeImage(HttpServletRequest request, HttpServletResponse response) { - HttpSession session = ((HttpServletRequest) request).getSession(); + HttpSession session = request.getSession(); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); Map parameters = UtilHttp.getParameterMap(request); String message = (String) parameters.get("message"); @@ -72,9 +72,7 @@ public class QRCodeEvents { String logoImageMaxHeight = (String) parameters.get("logoImageMaxHeight"); try { - if (mimeType != null) { - response.setContentType(mimeType); - } + response.setContentType(mimeType); OutputStream os = response.getOutputStream(); Map context = UtilMisc.toMap("message", message, "format", format, "userLogin", userLogin, "locale", locale); if (UtilValidate.isNotEmpty(width)) { diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/qrcode/QRCodeServices.java b/framework/common/src/main/java/org/apache/ofbiz/common/qrcode/QRCodeServices.java index e9751bd99f..5c5031250f 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/qrcode/QRCodeServices.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/qrcode/QRCodeServices.java @@ -71,11 +71,11 @@ public class QRCodeServices { public static final String QRCODE_DEFAULT_LOGOIMAGE = UtilProperties.getPropertyValue("qrcode", "qrcode.default.logoimage"); - public static BufferedImage defaultLogoImage; + public static final BufferedImage defaultLogoImage; - public static final String[] FORMAT_NAMES = StringUtil.split(QRCODE_FORMAT_SUPPORTED, '|'); + private static final String[] FORMAT_NAMES = StringUtil.split(QRCODE_FORMAT_SUPPORTED, '|'); - public static final List FORMATS_SUPPORTED = Arrays.asList(FORMAT_NAMES); + private static final List FORMATS_SUPPORTED = Arrays.asList(FORMAT_NAMES); public static final int MIN_SIZE = 20; @@ -87,17 +87,21 @@ public class QRCodeServices { static { if (UtilValidate.isNotEmpty(QRCODE_DEFAULT_LOGOIMAGE)) { - try { - Map logoImageResult = ImageTransform.getBufferedImage(FileUtil.getFile(QRCODE_DEFAULT_LOGOIMAGE).getAbsolutePath(), Locale.getDefault()); - defaultLogoImage = (BufferedImage) logoImageResult.get("bufferedImage"); - if (UtilValidate.isEmpty(defaultLogoImage)) { - Debug.logError("Your logo image file(" + QRCODE_DEFAULT_LOGOIMAGE + ") cannot be read by javax.imageio.ImageIO. Please use png, jpeg formats instead of ico and etc.", module); - } - } catch (IllegalArgumentException e) { - defaultLogoImage = null; - } catch (IOException e) { - defaultLogoImage = null; + defaultLogoImage = getLogoImageResult(); + } else { + defaultLogoImage = null; + } + } + + private static BufferedImage getLogoImageResult() { + try { + Map logoImageResult = ImageTransform.getBufferedImage(FileUtil.getFile(QRCODE_DEFAULT_LOGOIMAGE).getAbsolutePath(), Locale.getDefault()); + if (UtilValidate.isEmpty(logoImageResult.get("bufferedImage"))) { + Debug.logError("Your logo image file(" + QRCODE_DEFAULT_LOGOIMAGE + ") cannot be read by javax.imageio.ImageIO. Please use png, jpeg formats instead of ico and etc.", module); } + return (BufferedImage) logoImageResult.get("bufferedImage"); + } catch (IllegalArgumentException | IOException e) { + return null; } } @@ -164,10 +168,10 @@ public class QRCodeServices { BufferedImage newBufferedImage = null; if (UtilValidate.isNotEmpty(logoBufferedImage)) { if (UtilValidate.isNotEmpty(logoImageMaxWidth) && UtilValidate.isNotEmpty(logoImageMaxHeight) && (logoBufferedImage.getWidth() > logoImageMaxWidth.intValue() || logoBufferedImage.getHeight() > logoImageMaxHeight.intValue())) { - Map typeMap = new HashMap(); + Map typeMap = new HashMap<>(); typeMap.put("width", logoImageMaxWidth.toString()); typeMap.put("height", logoImageMaxHeight.toString()); - Map> dimensionMap = new HashMap>(); + Map> dimensionMap = new HashMap<>(); dimensionMap.put("QRCode", typeMap); Map logoImageResult = ImageTransform.scaleImage(logoBufferedImage, (double) logoBufferedImage.getWidth(), (double) logoBufferedImage.getHeight(), dimensionMap, "QRCode", locale); logoBufferedImage = (BufferedImage) logoImageResult.get("bufferedImage"); @@ -248,7 +252,7 @@ public class QRCodeServices { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = null; - String osName = System.getProperty("os.name").toLowerCase(); + String osName = System.getProperty("os.name").toLowerCase(Locale.getDefault()); image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); if (osName.startsWith("mac os") && format.equals("png")) { image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);