Author: mbrohl
Date: Sat Oct 7 15:47:59 2017 New Revision: 1811438 URL: http://svn.apache.org/viewvc?rev=1811438&view=rev Log: Improved: Fixing defects reported by FindBugs, package org.apache.ofbiz.common. (OFBIZ-9681) Thanks Julian Leichert for reporting and providing the patch. Modified: ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/CommonServices.java ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFileMappingCreator.java ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/KeywordSearchUtil.java Modified: ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java?rev=1811438&r1=1811437&r2=1811438&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java Sat Oct 7 15:47:59 2017 @@ -170,12 +170,6 @@ public class CommonEvents { String followSessionId = request.getParameter("followSid"); Map<String, String> follow = appletSessions.get(followSessionId); if (follow == null) follow = new LinkedHashMap<String, String>(); - String followerListStr = follow.get("followers"); - if (followerListStr == null) { - followerListStr = followerSessionId; - } else { - followerListStr = followerListStr + "," + followerSessionId; - } appletSessions.put(followSessionId, follow); appletSessions.put(followerSessionId, null); } @@ -303,7 +297,7 @@ public class CommonEvents { try { JSON json = JSON.from(attrMap); writeJSONtoResponse(json, request, response); - } catch (Exception e) { + } catch (IOException e) { return "error"; } return "success"; @@ -311,10 +305,6 @@ public class CommonEvents { private static void writeJSONtoResponse(JSON json, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException { String jsonStr = json.toString(); - if (jsonStr == null) { - Debug.logError("JSON Object was empty; fatal error!", module); - return; - } String httpMethod = request.getMethod(); // This was added for security reason (OFBIZ-5409), you might need to remove the "//" prefix when handling the JSON response @@ -499,7 +489,7 @@ public class CommonEvents { session.setAttribute("_CAPTCHA_CODE_", captchaCodeMap); } captchaCodeMap.put(captchaCodeId, captchaCode); - } catch (Exception ioe) { + } catch (IOException | IllegalArgumentException | IllegalStateException ioe) { Debug.logError(ioe.getMessage(), module); } return "success"; Modified: ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/CommonServices.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/CommonServices.java?rev=1811438&r1=1811437&r2=1811438&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/CommonServices.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/CommonServices.java Sat Oct 7 15:47:59 2017 @@ -48,6 +48,7 @@ import org.apache.ofbiz.base.metrics.Met import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.UtilCodec; import org.apache.ofbiz.base.util.UtilDateTime; +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; @@ -471,10 +472,11 @@ public class CommonServices { InputStream in = (InputStream) context.get("inputStream"); OutputStream out = (OutputStream) context.get("outputStream"); - BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; - try (Writer writer = new OutputStreamWriter(out)) { + try ( + BufferedReader reader = new BufferedReader(new InputStreamReader(in, UtilIO.getUtf8())); + Writer writer = new OutputStreamWriter(out, UtilIO.getUtf8())) { while ((line = reader.readLine()) != null) { Debug.logInfo("Read line: " + line, module); writer.write(line); @@ -497,7 +499,7 @@ public class CommonServices { message = "PONG"; } - long count = -1; + long count; try { count = EntityQuery.use(delegator).from("SequenceValueItem").queryCount(); } catch (GenericEntityException e) { @@ -505,7 +507,7 @@ public class CommonServices { return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonPingDatasourceCannotConnect", locale)); } - if (count > 0) { + if (count != 0L) { Map<String, Object> result = ServiceUtil.returnSuccess(); result.put("message", message); return result; Modified: ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java?rev=1811438&r1=1811437&r2=1811438&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java Sat Oct 7 15:47:59 2017 @@ -29,6 +29,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.TimeZone; @@ -68,7 +69,7 @@ public class FindServices { public static final String module = FindServices.class.getName(); public static final String resource = "CommonUiLabels"; - public static Map<String, EntityComparisonOperator<?, ?>> entityOperators; + public static final Map<String, EntityComparisonOperator<?, ?>> entityOperators; static { entityOperators = new LinkedHashMap<String, EntityComparisonOperator<?, ?>>(); @@ -112,7 +113,8 @@ public class FindServices { // Contained in the associated entity. // Those extra fields will be ignored in the second half of this method. Map<String, Map<String, Map<String, Object>>> normalizedFields = new LinkedHashMap<String, Map<String, Map<String, Object>>>(); - for (String fieldNameRaw: inputFields.keySet()) { // The name as it appeas in the HTML form + for (Entry<String, ?> entry : inputFields.entrySet()) { // The name as it appeas in the HTML form + String fieldNameRaw = entry.getKey(); String fieldNameRoot = null; // The entity field name. Everything to the left of the first "_" if // it exists, or the whole word, if not. String fieldPair = null; // "fld0" or "fld1" - begin/end of range or just fld0 if no range. @@ -124,19 +126,19 @@ public class FindServices { Map<String, Object> subMap2 = null; String fieldMode = null; - fieldValue = inputFields.get(fieldNameRaw); + fieldValue = entry.getValue(); if (ObjectType.isEmpty(fieldValue)) { continue; } queryStringMap.put(fieldNameRaw, fieldValue); - iPos = fieldNameRaw.indexOf("_"); // Look for suffix + iPos = fieldNameRaw.indexOf('_'); // Look for suffix // This is a hack to skip fields from "multi" forms // These would have the form "fieldName_o_1" if (iPos >= 0) { String suffix = fieldNameRaw.substring(iPos + 1); - iPos2 = suffix.indexOf("_"); + iPos2 = suffix.indexOf('_'); if (iPos2 == 1) { continue; } @@ -153,7 +155,7 @@ public class FindServices { fieldNameRoot = fieldNameRaw.substring(0, iPos); String suffix = fieldNameRaw.substring(iPos + 1); - iPos2 = suffix.indexOf("_"); + iPos2 = suffix.indexOf('_'); if (iPos2 < 0) { if (suffix.startsWith("fld")) { // If only one token and it starts with "fld" @@ -342,7 +344,8 @@ public class FindServices { fieldObject = modelField.getModelEntity().convertFieldValue(modelField, fieldValue, delegator, context); } if (ignoreCase && fieldObject instanceof String) { - cond = EntityCondition.makeCondition(EntityFunction.UPPER_FIELD(fieldName), fieldOp, EntityFunction.UPPER(((String)fieldValue).toUpperCase())); + cond = EntityCondition.makeCondition(EntityFunction.UPPER_FIELD(fieldName), fieldOp, EntityFunction.UPPER( + ((String) fieldValue).toUpperCase(Locale.getDefault()))); } else { if (fieldObject.equals(GenericEntity.NULL_FIELD.toString())) { fieldObject = null; @@ -441,7 +444,7 @@ public class FindServices { try (EntityListIterator it = (EntityListIterator) result.get("listIt")) { list = it.getPartialList(start+1, viewSize); // list starts at '1' listSize = it.getResultsSizeAfterPartialList(); - } catch (Exception e) { + } catch (ClassCastException | NullPointerException | GenericEntityException e) { Debug.logInfo("Problem getting partial list" + e,module); } @@ -694,27 +697,28 @@ public class FindServices { ModelEntity modelEntity = delegator.getModelEntity(entityName); Map<String, Object> normalizedFields = new LinkedHashMap<String, Object>(); //StringBuffer queryStringBuf = new StringBuffer(); - for (String fieldNameRaw: inputFields.keySet()) { // The name as it appeas in the HTML form + for (Entry<String, ?> entry : inputFields.entrySet()) { // The name as it appeas in the HTML form + String fieldNameRaw = entry.getKey(); String fieldNameRoot = null; // The entity field name. Everything to the left of the first "_" if // it exists, or the whole word, if not. Object fieldValue = null; // If it is a "value" field, it will be the value to be used in the query. // If it is an "op" field, it will be "equals", "greaterThan", etc. int iPos = -1; int iPos2 = -1; - - fieldValue = inputFields.get(fieldNameRaw); + + fieldValue = entry.getValue(); if (ObjectType.isEmpty(fieldValue)) { continue; } //queryStringBuffer.append(fieldNameRaw + "=" + fieldValue); - iPos = fieldNameRaw.indexOf("_"); // Look for suffix + iPos = fieldNameRaw.indexOf('_'); // Look for suffix // This is a hack to skip fields from "multi" forms // These would have the form "fieldName_o_1" if (iPos >= 0) { String suffix = fieldNameRaw.substring(iPos + 1); - iPos2 = suffix.indexOf("_"); + iPos2 = suffix.indexOf('_'); if (iPos2 == 1) { continue; } @@ -755,7 +759,7 @@ public class FindServices { if (UtilValidate.isNotEmpty(list)) { item = list.get(0); } - } catch (Exception e) { + } catch (ClassCastException | NullPointerException | GenericEntityException e) { Debug.logInfo("Problem getting list Item" + e,module); } @@ -763,7 +767,7 @@ public class FindServices { result.put("item",item); } result.remove("listIt"); - + if (result.containsKey("listSize")) { result.remove("listSize"); } Modified: ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFileMappingCreator.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFileMappingCreator.java?rev=1811438&r1=1811437&r2=1811438&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFileMappingCreator.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFileMappingCreator.java Sat Oct 7 15:47:59 2017 @@ -20,8 +20,10 @@ package org.apache.ofbiz.common; import java.io.File; +import java.io.IOException; import java.io.StringWriter; import java.io.Writer; + import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -37,6 +39,8 @@ import org.apache.ofbiz.base.util.templa import org.apache.ofbiz.service.DispatchContext; import org.apache.ofbiz.service.ServiceUtil; +import freemarker.template.TemplateException; + // Use the createJsLanguageFileMapping service to create or update the JsLanguageFilesMapping.java. You will still need to compile thereafter public class JsLanguageFileMappingCreator { @@ -93,7 +97,7 @@ public class JsLanguageFileMappingCreato fileUrl = dateJsLocaleRelPath + dateJsLocalePrefix + modifiedDisplayCountry + jsFilePostFix; } else { // Try to guess a language - String tmpLocale = strippedLocale + "-" + strippedLocale.toUpperCase(); + String tmpLocale = strippedLocale + "-" + strippedLocale.toUpperCase(Locale.getDefault()); fileName = componentRoot + dateJsLocaleRelPath + dateJsLocalePrefix + tmpLocale + jsFilePostFix; file = FileUtil.getFile(fileName); if (file.exists()) { @@ -195,7 +199,7 @@ public class JsLanguageFileMappingCreato File file = new File(output); FileUtils.writeStringToFile(file, writer.toString(), encoding); } - catch (Exception e) { + catch (IOException | TemplateException e) { Debug.logError(e, module); return ServiceUtil.returnError(UtilProperties.getMessage("CommonUiLabels", "CommonOutputFileCouldNotBeCreated", UtilMisc.toMap("errorString", e.getMessage()), (Locale)context.get("locale"))); } Modified: ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/KeywordSearchUtil.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/KeywordSearchUtil.java?rev=1811438&r1=1811437&r2=1811438&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/KeywordSearchUtil.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/common/src/main/java/org/apache/ofbiz/common/KeywordSearchUtil.java Sat Oct 7 15:47:59 2017 @@ -21,6 +21,7 @@ package org.apache.ofbiz.common; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; @@ -192,7 +193,7 @@ public final class KeywordSearchUtil { StringTokenizer tokener = new StringTokenizer(str, separators, false); while (tokener.hasMoreTokens()) { // make sure it is lower case before doing anything else - String token = tokener.nextToken().toLowerCase(); + String token = tokener.nextToken().toLowerCase(Locale.getDefault()); if (forSearch) { // these characters will only be present if it is for a search, ie not for indexing |
Free forum by Nabble | Edit this page |