Author: mbrohl
Date: Mon Dec 18 14:44:28 2017 New Revision: 1818579 URL: http://svn.apache.org/viewvc?rev=1818579&view=rev Log: Improved: Fixing defects reported by code analysis tools, package org.apache.ofbiz.accounting.thirdparty.valuelink. (OFBIZ-10060) Thanks Julian Leichert for reporting and providing the patch. Modified: ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/valuelink/ValueLinkApi.java ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/valuelink/ValueLinkServices.java Modified: ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/valuelink/ValueLinkApi.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/valuelink/ValueLinkApi.java?rev=1818579&r1=1818578&r2=1818579&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/valuelink/ValueLinkApi.java (original) +++ ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/valuelink/ValueLinkApi.java Mon Dec 18 14:44:28 2017 @@ -20,6 +20,7 @@ package org.apache.ofbiz.accounting.thir import java.math.BigDecimal; import java.math.BigInteger; +import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.KeyFactory; @@ -36,6 +37,7 @@ import java.util.Date; import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Properties; import java.util.Random; @@ -75,7 +77,7 @@ public class ValueLinkApi { public static final String module = ValueLinkApi.class.getName(); // static object cache - private static Map<String, Object> objectCache = new HashMap<String, Object>(); + private static Map<String, Object> objectCache = new HashMap<>(); // instance variables protected Delegator delegator = null; @@ -156,7 +158,7 @@ public class ValueLinkApi { Cipher mwkCipher = this.getCipher(this.getMwkKey(), Cipher.ENCRYPT_MODE); // pin to bytes - byte[] pinBytes = pin.getBytes(); + byte[] pinBytes = pin.getBytes(StandardCharsets.UTF_8); // 7 bytes of random data byte[] random = this.getRandomBytes(7); @@ -209,7 +211,7 @@ public class ValueLinkApi { try { byte[] decryptedEan = mwkCipher.doFinal(StringUtil.fromHexString(pin)); byte[] decryptedPin = getByteRange(decryptedEan, 8, 8); - decryptedPinString = new String(decryptedPin); + decryptedPinString = new String(decryptedPin, StandardCharsets.UTF_8); } catch (IllegalStateException e) { Debug.logError(e, module); } catch (IllegalBlockSizeException e) { @@ -524,10 +526,9 @@ public class ValueLinkApi { byte[] des3 = copyBytes(desByte1, copyBytes(desByte2, desByte3, 0), 0); return generateMwk(des3); - } else { - Debug.logInfo("Null DES keys returned", module); } + Debug.logInfo("Null DES keys returned", module); return null; } @@ -562,9 +563,8 @@ public class ValueLinkApi { } if (mwk != null) { return generateMwk(mwk); - } else { - return null; } + return null; } /** @@ -694,7 +694,7 @@ public class ValueLinkApi { * @return Map containing the inital request values */ public Map<String, Object> getInitialRequestMap(Map<String, Object> context) { - Map<String, Object> request = new HashMap<String, Object>(); + Map<String, Object> request = new HashMap<>(); // merchant information request.put("MerchID", merchantId + terminalId); @@ -907,7 +907,7 @@ public class ValueLinkApi { } // covert to all lowercase and trim off the html header - String subResponse = response.toLowerCase(); + String subResponse = response.toLowerCase(Locale.getDefault()); int firstIndex = subResponse.indexOf("<tr>"); int lastIndex = subResponse.lastIndexOf("</tr>"); subResponse = subResponse.substring(firstIndex, lastIndex); @@ -939,7 +939,7 @@ public class ValueLinkApi { subResponse = StringUtil.replaceString(subResponse, "</td>", ""); // make the map - Map<String, Object> responseMap = new HashMap<String, Object>(); + Map<String, Object> responseMap = new HashMap<>(); responseMap.putAll(StringUtil.strToMap(subResponse, true)); // add the raw html back in just in case we need it later @@ -964,7 +964,7 @@ public class ValueLinkApi { } // covert to all lowercase and trim off the html header - String subResponse = response.toLowerCase(); + String subResponse = response.toLowerCase(Locale.getDefault()); int firstIndex = subResponse.indexOf("<tr>"); int lastIndex = subResponse.lastIndexOf("</tr>"); subResponse = subResponse.substring(firstIndex, lastIndex); @@ -995,7 +995,7 @@ public class ValueLinkApi { List<String> valueList = StringUtil.split(values, "&"); // create a List of Maps for each set of values - List<Map<String, String>> valueMap = new LinkedList<Map<String,String>>(); + List<Map<String, String>> valueMap = new LinkedList<>(); for (int i = 0; i < valueList.size(); i++) { valueMap.add(StringUtil.createMap(StringUtil.split(keys, "|"), StringUtil.split(valueList.get(i), "|"))); } Modified: ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/valuelink/ValueLinkServices.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/valuelink/ValueLinkServices.java?rev=1818579&r1=1818578&r2=1818579&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/valuelink/ValueLinkServices.java (original) +++ ofbiz/ofbiz-framework/trunk/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/valuelink/ValueLinkServices.java Mon Dec 18 14:44:28 2017 @@ -168,33 +168,27 @@ public class ValueLinkServices { Debug.logInfo("Response : " + response, module); // on success update the database / reload the cached api - if (response != null) { - String responseCode = (String) response.get("responsecode"); - if ("00".equals(responseCode)) { - GenericValue vlKeys = GenericValue.create(vl.getGenericValue()); - vlKeys.set("lastWorkingKey", vlKeys.get("workingKey")); - vlKeys.set("workingKey", StringUtil.toHexString(mwk)); - vlKeys.set("workingKeyIndex", request.get("EncryptID")); - vlKeys.set("lastModifiedDate", UtilDateTime.nowTimestamp()); - vlKeys.set("lastModifiedByUserLogin", userLogin != null ? userLogin.get("userLoginId") : null); - try { - vlKeys.store(); - } catch (GenericEntityException e) { - Debug.logError(e, "Unable to store updated keys; the keys were changed with ValueLink : " + vlKeys, module); - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "AccountingValueLinkCannotStoreWorkingKey", locale)); - } - vl.reload(); - return ServiceUtil.returnSuccess(); - } else { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "AccountingValueLinkTransactionFailed", - UtilMisc.toMap("responseCode", responseCode), locale)); - } - } else { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "AccountingValueLinkReceivedEmptyResponse", locale)); + String responseCode = (String) response.get("responsecode"); + if (!"00".equals(responseCode)) { + return ServiceUtil.returnError(UtilProperties.getMessage(resource, + "AccountingValueLinkTransactionFailed", + UtilMisc.toMap("responseCode", responseCode), locale)); + } + GenericValue vlKeys = GenericValue.create(vl.getGenericValue()); + vlKeys.set("lastWorkingKey", vlKeys.get("workingKey")); + vlKeys.set("workingKey", StringUtil.toHexString(mwk)); + vlKeys.set("workingKeyIndex", request.get("EncryptID")); + vlKeys.set("lastModifiedDate", UtilDateTime.nowTimestamp()); + vlKeys.set("lastModifiedByUserLogin", userLogin != null ? userLogin.get("userLoginId") : null); + try { + vlKeys.store(); + } catch (GenericEntityException e) { + Debug.logError(e, "Unable to store updated keys; the keys were changed with ValueLink : " + vlKeys, module); + return ServiceUtil.returnError(UtilProperties.getMessage(resource, + "AccountingValueLinkCannotStoreWorkingKey", locale)); } + vl.reload(); + return ServiceUtil.returnSuccess(); } public static Map<String, Object> activate(DispatchContext dctx, Map<String, Object> context) { @@ -252,29 +246,25 @@ public class ValueLinkServices { "AccountingValueLinkUnableToActivateGiftCard", locale)); } - if (response != null) { - String responseCode = (String) response.get("responsecode"); - Map<String, Object> result = ServiceUtil.returnSuccess(); - if ("00".equals(responseCode)) { - result.put("processResult", Boolean.TRUE); - result.put("pin", vl.decryptPin((String) response.get("pin"))); - } else { - result.put("processResult", Boolean.FALSE); - result.put("pin", response.get("PIN")); - } - result.put("responseCode", responseCode); - result.put("authCode", response.get("authcode")); - result.put("cardNumber", response.get("cardno")); - result.put("amount", vl.getAmount((String) response.get("currbal"))); - result.put("expireDate", response.get("expiredate")); - result.put("cardClass", response.get("cardclass")); - result.put("referenceNum", response.get("traceno")); - Debug.logInfo("Activate Result : " + result, module); - return result; + String responseCode = (String) response.get("responsecode"); + Map<String, Object> result = ServiceUtil.returnSuccess(); + if ("00".equals(responseCode)) { + result.put("processResult", Boolean.TRUE); + result.put("pin", vl.decryptPin((String) response.get("pin"))); } else { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "AccountingValueLinkReceivedEmptyResponse", locale)); + result.put("processResult", Boolean.FALSE); + result.put("pin", response.get("PIN")); } + result.put("responseCode", responseCode); + result.put("authCode", response.get("authcode")); + result.put("cardNumber", response.get("cardno")); + result.put("amount", vl.getAmount((String) response.get("currbal"))); + result.put("expireDate", response.get("expiredate")); + result.put("cardClass", response.get("cardclass")); + result.put("referenceNum", response.get("traceno")); + Debug.logInfo("Activate Result : " + result, module); + return result; + } public static Map<String, Object> linkPhysicalCard(DispatchContext dctx, Map<String, Object> context) { @@ -311,29 +301,19 @@ public class ValueLinkServices { "AccountingValueLinkUnableToLinkGiftCard", locale)); } - if (response != null) { - String responseCode = (String) response.get("responsecode"); - Map<String, Object> result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, - "AccountingValueLinkGiftCardActivated", locale)); - - if ("00".equals(responseCode)) { - - result.put("processResult", Boolean.TRUE); - } else { - result.put("processResult", Boolean.FALSE); - } - result.put("responseCode", responseCode); - result.put("authCode", response.get("authcode")); - result.put("amount", vl.getAmount((String) response.get("newbal"))); - result.put("expireDate", response.get("expiredate")); - result.put("cardClass", response.get("cardclass")); - result.put("referenceNum", response.get("traceno")); - Debug.logInfo("Link Result : " + result, module); - return result; - } else { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "AccountingValueLinkReceivedEmptyResponse", locale)); - } + String responseCode = (String) response.get("responsecode"); + Map<String, Object> result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, + "AccountingValueLinkGiftCardActivated", locale)); + + result.put("processResult", "00".equals(responseCode)); + result.put("responseCode", responseCode); + result.put("authCode", response.get("authcode")); + result.put("amount", vl.getAmount((String) response.get("newbal"))); + result.put("expireDate", response.get("expiredate")); + result.put("cardClass", response.get("cardclass")); + result.put("referenceNum", response.get("traceno")); + Debug.logInfo("Link Result : " + result, module); + return result; } public static Map<String, Object> disablePin(DispatchContext dctx, Map<String, Object> context) { @@ -374,26 +354,18 @@ public class ValueLinkServices { "AccountingValueLinkUnableToDisablePin", locale)); } - if (response != null) { - String responseCode = (String) response.get("responsecode"); - Map<String, Object> result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, - "AccountingValueLinkPinDisabled", locale)); - if ("00".equals(responseCode)) { - result.put("processResult", Boolean.TRUE); - } else { - result.put("processResult", Boolean.FALSE); - } - result.put("responseCode", responseCode); - result.put("balance", vl.getAmount((String) response.get("currbal"))); - result.put("expireDate", response.get("expiredate")); - result.put("cardClass", response.get("cardclass")); - result.put("referenceNum", response.get("traceno")); - Debug.logInfo("Disable Result : " + result, module); - return result; - } else { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "AccountingValueLinkReceivedEmptyResponse", locale)); - } + String responseCode = (String) response.get("responsecode"); + Map<String, Object> result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, + "AccountingValueLinkPinDisabled", locale)); + + result.put("processResult","00".equals(responseCode)); + result.put("responseCode", responseCode); + result.put("balance", vl.getAmount((String) response.get("currbal"))); + result.put("expireDate", response.get("expiredate")); + result.put("cardClass", response.get("cardclass")); + result.put("referenceNum", response.get("traceno")); + Debug.logInfo("Disable Result : " + result, module); + return result; } public static Map<String, Object> redeem(DispatchContext dctx, Map<String, Object> context) { @@ -442,28 +414,21 @@ public class ValueLinkServices { "AccountingValueLinkUnableToRedeemGiftCard", locale)); } - if (response != null) { - String responseCode = (String) response.get("responsecode"); - Map<String, Object> result = ServiceUtil.returnSuccess(); - if ("00".equals(responseCode)) { - result.put("processResult", Boolean.TRUE); - } else { - result.put("processResult", Boolean.FALSE); - } - result.put("responseCode", responseCode); - result.put("authCode", response.get("authcode")); - result.put("previousAmount", vl.getAmount((String) response.get("prevbal"))); - result.put("amount", vl.getAmount((String) response.get("newbal"))); - result.put("expireDate", response.get("expiredate")); - result.put("cardClass", response.get("cardclass")); - result.put("cashBack", vl.getAmount((String) response.get("cashback"))); - result.put("referenceNum", response.get("traceno")); - Debug.logInfo("Redeem Result : " + result, module); - return result; - } else { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "AccountingValueLinkReceivedEmptyResponse", locale)); - } + String responseCode = (String) response.get("responsecode"); + Map<String, Object> result = ServiceUtil.returnSuccess(); + + result.put("processResult","00".equals(responseCode)); + result.put("responseCode", responseCode); + result.put("authCode", response.get("authcode")); + result.put("previousAmount", vl.getAmount((String) response.get("prevbal"))); + result.put("amount", vl.getAmount((String) response.get("newbal"))); + result.put("expireDate", response.get("expiredate")); + result.put("cardClass", response.get("cardclass")); + result.put("cashBack", vl.getAmount((String) response.get("cashback"))); + result.put("referenceNum", response.get("traceno")); + Debug.logInfo("Redeem Result : " + result, module); + return result; + } public static Map<String, Object> reload(DispatchContext dctx, Map<String, Object> context) { @@ -512,27 +477,20 @@ public class ValueLinkServices { "AccountingValueLinkUnableToReloadGiftCard", locale)); } - if (response != null) { - String responseCode = (String) response.get("responsecode"); - Map<String, Object> result = ServiceUtil.returnSuccess(); - if ("00".equals(responseCode)) { - result.put("processResult", Boolean.TRUE); - } else { - result.put("processResult", Boolean.FALSE); - } - result.put("responseCode", responseCode); - result.put("authCode", response.get("authcode")); - result.put("previousAmount", vl.getAmount((String) response.get("prevbal"))); - result.put("amount", vl.getAmount((String) response.get("newbal"))); - result.put("expireDate", response.get("expiredate")); - result.put("cardClass", response.get("cardclass")); - result.put("referenceNum", response.get("traceno")); - Debug.logInfo("Reload Result : " + result, module); - return result; - } else { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "AccountingValueLinkReceivedEmptyResponse", locale)); - } + String responseCode = (String) response.get("responsecode"); + Map<String, Object> result = ServiceUtil.returnSuccess(); + + result.put("processResult","00".equals(responseCode)); + result.put("responseCode", responseCode); + result.put("authCode", response.get("authcode")); + result.put("previousAmount", vl.getAmount((String) response.get("prevbal"))); + result.put("amount", vl.getAmount((String) response.get("newbal"))); + result.put("expireDate", response.get("expiredate")); + result.put("cardClass", response.get("cardclass")); + result.put("referenceNum", response.get("traceno")); + Debug.logInfo("Reload Result : " + result, module); + return result; + } public static Map<String, Object> balanceInquire(DispatchContext dctx, Map<String, Object> context) { @@ -573,25 +531,18 @@ public class ValueLinkServices { "AccountingValueLinkUnableToCallBalanceInquiry", locale)); } - if (response != null) { - String responseCode = (String) response.get("responsecode"); - Map<String, Object> result = ServiceUtil.returnSuccess(); - if ("00".equals(responseCode)) { - result.put("processResult", Boolean.TRUE); - } else { - result.put("processResult", Boolean.FALSE); - } - result.put("responseCode", responseCode); - result.put("balance", vl.getAmount((String) response.get("currbal"))); - result.put("expireDate", response.get("expiredate")); - result.put("cardClass", response.get("cardclass")); - result.put("referenceNum", response.get("traceno")); - Debug.logInfo("Balance Result : " + result, module); - return result; - } else { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "AccountingValueLinkReceivedEmptyResponse", locale)); - } + String responseCode = (String) response.get("responsecode"); + Map<String, Object> result = ServiceUtil.returnSuccess(); + + result.put("processResult","00".equals(responseCode)); + result.put("responseCode", responseCode); + result.put("balance", vl.getAmount((String) response.get("currbal"))); + result.put("expireDate", response.get("expiredate")); + result.put("cardClass", response.get("cardclass")); + result.put("referenceNum", response.get("traceno")); + Debug.logInfo("Balance Result : " + result, module); + return result; + } public static Map<String, Object> transactionHistory(DispatchContext dctx, Map<String, Object> context) { @@ -630,26 +581,19 @@ public class ValueLinkServices { "AccountingValueLinkUnableToCallHistoryInquiry", locale)); } - if (response != null) { - String responseCode = (String) response.get("responsecode"); - Map<String, Object> result = ServiceUtil.returnSuccess(); - if ("00".equals(responseCode)) { - result.put("processResult", Boolean.TRUE); - } else { - result.put("processResult", Boolean.FALSE); - } - result.put("responseCode", responseCode); - result.put("balance", vl.getAmount((String) response.get("currbal"))); - result.put("history", response.get("history")); - result.put("expireDate", response.get("expiredate")); - result.put("cardClass", response.get("cardclass")); - result.put("referenceNum", response.get("traceno")); - Debug.logInfo("History Result : " + result, module); - return result; - } else { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "AccountingValueLinkReceivedEmptyResponse", locale)); - } + String responseCode = (String) response.get("responsecode"); + Map<String, Object> result = ServiceUtil.returnSuccess(); + + result.put("processResult", "00".equals(responseCode)); + result.put("responseCode", responseCode); + result.put("balance", vl.getAmount((String) response.get("currbal"))); + result.put("history", response.get("history")); + result.put("expireDate", response.get("expiredate")); + result.put("cardClass", response.get("cardclass")); + result.put("referenceNum", response.get("traceno")); + Debug.logInfo("History Result : " + result, module); + return result; + } public static Map<String, Object> refund(DispatchContext dctx, Map<String, Object> context) { @@ -698,27 +642,20 @@ public class ValueLinkServices { "AccountingValueLinkUnableToRefundGiftCard", locale)); } - if (response != null) { - String responseCode = (String) response.get("responsecode"); - Map<String, Object> result = ServiceUtil.returnSuccess(); - if ("00".equals(responseCode)) { - result.put("processResult", Boolean.TRUE); - } else { - result.put("processResult", Boolean.FALSE); - } - result.put("responseCode", responseCode); - result.put("authCode", response.get("authcode")); - result.put("previousAmount", vl.getAmount((String) response.get("prevbal"))); - result.put("amount", vl.getAmount((String) response.get("newbal"))); - result.put("expireDate", response.get("expiredate")); - result.put("cardClass", response.get("cardclass")); - result.put("referenceNum", response.get("traceno")); - Debug.logInfo("Refund Result : " + result, module); - return result; - } else { - return ServiceUtil.returnError(UtilProperties.getMessage(resource, - "AccountingValueLinkReceivedEmptyResponse", locale)); - } + String responseCode = (String) response.get("responsecode"); + Map<String, Object> result = ServiceUtil.returnSuccess(); + + result.put("processResult","00".equals(responseCode)); + result.put("responseCode", responseCode); + result.put("authCode", response.get("authcode")); + result.put("previousAmount", vl.getAmount((String) response.get("prevbal"))); + result.put("amount", vl.getAmount((String) response.get("newbal"))); + result.put("expireDate", response.get("expiredate")); + result.put("cardClass", response.get("cardclass")); + result.put("referenceNum", response.get("traceno")); + Debug.logInfo("Refund Result : " + result, module); + return result; + } public static Map<String, Object> voidRedeem(DispatchContext dctx, Map<String, Object> context) { @@ -1200,7 +1137,7 @@ public class ValueLinkServices { } Boolean processResult = (Boolean) activateResult.get("processResult"); - if (activateResult == null || activateResult.containsKey(ModelService.ERROR_MESSAGE) || !processResult.booleanValue()) { + if (activateResult.containsKey(ModelService.ERROR_MESSAGE) || !processResult.booleanValue()) { failure = true; } @@ -1461,7 +1398,7 @@ public class ValueLinkServices { } Boolean processResult = (Boolean) reloadResult.get("processResult"); - if (reloadResult == null || reloadResult.containsKey(ModelService.ERROR_MESSAGE) || !processResult.booleanValue()) { + if (reloadResult.containsKey(ModelService.ERROR_MESSAGE) || !processResult.booleanValue()) { Debug.logError("Reload Failed Need to Refund : " + reloadResult, module); // process the return |
Free forum by Nabble | Edit this page |