From 571803511ad17e97750d0355ebb74abc4c7eeb0f Mon Sep 17 00:00:00 2001 From: Bharat saraswal Date: Thu, 21 Sep 2017 19:51:48 +0530 Subject: Resolved below sonar issues: reduced method complexitity. handled null pointer exceptions. creates static variables for string usability. Issue-Id:AAI-211 Change-Id: Ia110923b3e157f73fbc65d5629b5bd22a872b3de Signed-off-by: Bharat saraswal --- .../openecomp/aai/ajsc_aai/JaxrsUserService.java | 24 +- .../ajsc_aai/filemonitor/ServicePropertiesMap.java | 177 +++++---- .../org/openecomp/aai/audit/ListEndpoints.java | 411 +++++++++++---------- 3 files changed, 302 insertions(+), 310 deletions(-) diff --git a/ajsc-aai/src/main/java/org/openecomp/aai/ajsc_aai/JaxrsUserService.java b/ajsc-aai/src/main/java/org/openecomp/aai/ajsc_aai/JaxrsUserService.java index 39c78f6..360a50c 100644 --- a/ajsc-aai/src/main/java/org/openecomp/aai/ajsc_aai/JaxrsUserService.java +++ b/ajsc-aai/src/main/java/org/openecomp/aai/ajsc_aai/JaxrsUserService.java @@ -20,23 +20,24 @@ package org.openecomp.aai.ajsc_aai; +import java.util.HashMap; +import java.util.Map; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import java.util.Map; -import java.util.HashMap; @Path("/user") public class JaxrsUserService { - - private static final Map userIdToNameMap; - static { - userIdToNameMap = new HashMap(); - userIdToNameMap.put("userID1","Name1"); - userIdToNameMap.put("userID2","Name2"); - } - + + private static final Map userIdToNameMap; + + static { + userIdToNameMap = new HashMap<>(); + userIdToNameMap.put("userID1", "Name1"); + userIdToNameMap.put("userID2", "Name2"); + } + /** * Lookup user. * @@ -47,8 +48,7 @@ public class JaxrsUserService { @Path("/{userId}") @Produces("text/plain") public String lookupUser(@PathParam("userId") String userId) { - String name = userIdToNameMap.get(userId); + String name = userIdToNameMap.get(userId); return name != null ? name : "unknown id"; } - } diff --git a/ajsc-aai/src/main/java/org/openecomp/aai/ajsc_aai/filemonitor/ServicePropertiesMap.java b/ajsc-aai/src/main/java/org/openecomp/aai/ajsc_aai/filemonitor/ServicePropertiesMap.java index 0ecdf22..9bfcad0 100644 --- a/ajsc-aai/src/main/java/org/openecomp/aai/ajsc_aai/filemonitor/ServicePropertiesMap.java +++ b/ajsc-aai/src/main/java/org/openecomp/aai/ajsc_aai/filemonitor/ServicePropertiesMap.java @@ -20,108 +20,99 @@ package org.openecomp.aai.ajsc_aai.filemonitor; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.FileInputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; +public class ServicePropertiesMap { + + private static HashMap> mapOfMaps = new HashMap<>(); + private static final Logger logger = LoggerFactory.getLogger(ServicePropertiesMap.class); + + /** + * Refresh. + * + * @param file the file + * @throws Exception the exception + */ + public static void refresh(File file) throws Exception { + try { + logger.info(String.format("Loading properties - %s", file != null ? file.getName() : "")); + + //Store .json & .properties files into map of maps + if (file != null) { + String filePath = file.getPath(); + + if (filePath.lastIndexOf(".json") > 0) { + + ObjectMapper om = new ObjectMapper(); + TypeReference> typeRef = new TypeReference>() { + }; + HashMap propMap = om.readValue(file, typeRef); + HashMap lcasePropMap = new HashMap<>(); + for (HashMap.Entry entry : propMap.entrySet()) { + String lcaseKey = ifNullThenEmpty(entry.getKey()); + lcasePropMap.put(lcaseKey, entry.getValue()); + } + + mapOfMaps.put(file.getName(), lcasePropMap); + } else if (filePath.lastIndexOf(".properties") > 0) { + Properties prop = new Properties(); + FileInputStream fis = new FileInputStream(file); + prop.load(fis); + + @SuppressWarnings("unchecked") + HashMap propMap = new HashMap((Map) prop); + + mapOfMaps.put(file.getName(), propMap); + } + + logger.info("File - " + file.getName() + + " is loaded into the map and the corresponding system properties have been refreshed"); + } else { + logger.error("File cannot be loaded into the map "); + } + } catch (Exception e) { + logger.error("File " + (file != null ? file.getName() : "") + " cannot be loaded into the map ", e); + throw new Exception("Error reading map file " + (file != null ? file.getName() : ""), e); + } + } -public class ServicePropertiesMap -{ - private static HashMap> mapOfMaps = new HashMap>(); - static final Logger logger = LoggerFactory.getLogger(ServicePropertiesMap.class); - - /** - * Refresh. - * - * @param file the file - * @throws Exception the exception - */ - public static void refresh(File file) throws Exception - { - try - { - logger.info("Loading properties - " + (file != null?file.getName():"")); - - //Store .json & .properties files into map of maps - String filePath = file.getPath(); - - if(filePath.lastIndexOf(".json")>0){ - - ObjectMapper om = new ObjectMapper(); - TypeReference> typeRef = new TypeReference>() {}; - HashMap propMap = om.readValue(file, typeRef); - HashMap lcasePropMap = new HashMap(); - for (String key : propMap.keySet() ) - { - String lcaseKey = ifNullThenEmpty(key); - lcasePropMap.put(lcaseKey, propMap.get(key)); - } - - mapOfMaps.put(file.getName(), lcasePropMap); - - - }else if(filePath.lastIndexOf(".properties")>0){ - Properties prop = new Properties(); - FileInputStream fis = new FileInputStream(file); - prop.load(fis); - - @SuppressWarnings("unchecked") - HashMap propMap = new HashMap((Map)prop); - - mapOfMaps.put(file.getName(), propMap); - } + /** + * Gets the property. + * + * @param fileName the file name + * @param propertyKey the property key + * @return the property + */ + public static String getProperty(String fileName, String propertyKey) { + HashMap propMap = mapOfMaps.get(fileName); + return propMap != null ? propMap.get(ifNullThenEmpty(propertyKey)) : ""; + } - logger.info("File - " + file.getName() + " is loaded into the map and the corresponding system properties have been refreshed"); - } - catch (Exception e) - { - logger.error("File " + (file != null?file.getName():"") + " cannot be loaded into the map ", e); - throw new Exception("Error reading map file " + (file != null?file.getName():""), e); - } - } - - /** - * Gets the property. - * - * @param fileName the file name - * @param propertyKey the property key - * @return the property - */ - public static String getProperty(String fileName, String propertyKey) - { - HashMap propMap = mapOfMaps.get(fileName); - return propMap!=null?propMap.get(ifNullThenEmpty(propertyKey)):""; - } - - /** - * Gets the properties. - * - * @param fileName the file name - * @return the properties - */ - public static HashMap getProperties(String fileName){ - return mapOfMaps.get(fileName); - } - - /** - * If null then empty. - * - * @param key the key - * @return the string - */ - private static String ifNullThenEmpty(String key) { - if (key == null) { - return ""; - } else { - return key; - } - } + /** + * Gets the properties. + * + * @param fileName the file name + * @return the properties + */ + public static HashMap getProperties(String fileName) { + return mapOfMaps.get(fileName); + } + /** + * If null then empty. + * + * @param key the key + * @return the string + */ + private static String ifNullThenEmpty(String key) { + return key == null ? "" : key; + } } diff --git a/ajsc-aai/src/main/java/org/openecomp/aai/audit/ListEndpoints.java b/ajsc-aai/src/main/java/org/openecomp/aai/audit/ListEndpoints.java index e354973..7665673 100644 --- a/ajsc-aai/src/main/java/org/openecomp/aai/audit/ListEndpoints.java +++ b/ajsc-aai/src/main/java/org/openecomp/aai/audit/ListEndpoints.java @@ -20,15 +20,14 @@ package org.openecomp.aai.audit; +import com.google.common.base.CaseFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; - import org.apache.commons.lang.StringUtils; -import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext; import org.openecomp.aai.introspection.Introspector; import org.openecomp.aai.introspection.IntrospectorFactory; import org.openecomp.aai.introspection.Loader; @@ -36,214 +35,216 @@ import org.openecomp.aai.introspection.LoaderFactory; import org.openecomp.aai.introspection.ModelType; import org.openecomp.aai.introspection.Version; import org.openecomp.aai.logging.LogLineBuilder; - -import com.google.common.base.CaseFormat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * The Class ListEndpoints. */ public class ListEndpoints { - - private DynamicJAXBContext context = null; - - private final String start = "inventory"; - - private final String[] blacklist = { "search", "aai-internal", "models", "named-queries" }; - - private List endpoints = new ArrayList<>(); - - private Map endpointToLogicalName = new HashMap(); - - private final LogLineBuilder llBuilder = new LogLineBuilder(); - - /** - * Instantiates a new list endpoints. - * - * @param version the version - */ - public ListEndpoints(Version version) { - - Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version, llBuilder); - Introspector start = loader.introspectorFromName(this.start); - - beginAudit(start, "/aai/" + version); - - } - - /** - * Begin audit. - * - * @param obj the obj - * @param uri the uri - */ - private void beginAudit(Introspector obj, String uri) { - String currentUri = ""; - - if (!obj.getDbName().equals("inventory")) { - currentUri = uri + obj.getGenericURI(); - } else { - currentUri = uri; - } - if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) { - return; - } - if (!obj.isContainer()) { - endpoints.add(currentUri); - } - - populateLogicalName(obj, uri, currentUri); - - outer: for (String propName : obj.getProperties()) { - for (String item : blacklist) { - if (propName.equals(item)) { - continue outer; - } - } - if (obj.isListType(propName)) { - if (obj.isComplexGenericType(propName)) { - beginAudit( - IntrospectorFactory.newInstance(ModelType.MOXY, obj.newInstanceOfNestedProperty(propName), llBuilder), - currentUri); - } - } else if (obj.isComplexType(propName)) { - beginAudit(IntrospectorFactory.newInstance(ModelType.MOXY, obj.newInstanceOfProperty(propName), llBuilder), - currentUri); - } - } - - } - - /** - * Populate logical name. - * - * @param obj the obj - * @param uri the uri - * @param currentUri the current uri - */ - private void populateLogicalName(Introspector obj, String uri, String currentUri) { - - if (obj.getDbName().equals("inventory") || currentUri.split("/").length <= 4 || currentUri.endsWith("relationship-list")) { - return; - } - - if (uri.endsWith("/relationship-list")) { - uri = uri.substring(0, uri.lastIndexOf("/")); - } - - String logicalName = ""; - String keys = ""; - - - if (!obj.getAllKeys().isEmpty()) { - - Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$"); - Matcher m = p.matcher(currentUri); - - if (m.find()) { - keys = StringUtils.join(obj.getAllKeys(), "-and-"); - } else { - keys = StringUtils.join(obj.getAllKeys(), "-or-"); - } - keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys); - if (!keys.isEmpty()) { - keys = "With" + keys; - } - } - - logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys; - - if (endpointToLogicalName.containsKey(uri) && uri.endsWith("}")) { - logicalName = logicalName + "From" + endpointToLogicalName.get(uri); - } else if (endpointToLogicalName.containsKey(uri.substring(0, uri.lastIndexOf("/")))) { - logicalName = logicalName + "From" + endpointToLogicalName.get(uri.substring(0, uri.lastIndexOf("/"))); - } - - endpointToLogicalName.put(currentUri, logicalName); - - } - - /** - * Gets the logical names. - * - * @return the logical names - */ - public Map getLogicalNames() { - - return endpointToLogicalName; - - } - - /** - * Gets the endpoints. - * - * @return the endpoints - */ - public List getEndpoints() { - - return this.getEndpoints(""); - - } - - /** - * Gets the endpoints. - * - * @param filterOut the filter out - * @return the endpoints - */ - public List getEndpoints(String filterOut) { - List result = new ArrayList<>(); - Pattern p = null; - Matcher m = null; - if (!filterOut.equals("")) { - p = Pattern.compile(filterOut); - m = null; - } - for (String s : endpoints) { - if (p != null) { - m = p.matcher(s); - if (m.find()) { - continue; - } - } - - result.add(s); - } - - return result; - - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - for (String s : endpoints) { - sb.append(s + "\n"); - } - return sb.toString(); - - } - - /** - * To string. - * - * @param filterOut the filter out - * @return the string - */ - public String toString(String filterOut) { - StringBuilder sb = new StringBuilder(); - Pattern p = Pattern.compile(filterOut); - Matcher m = null; - for (String s : endpoints) { - m = p.matcher(s); - if (!m.find()) { - sb.append(s + "\n"); - } - } - return sb.toString(); - } + private static final Logger log = LoggerFactory.getLogger(ListEndpoints.class); + private static final String INVENTORY = "inventory"; + + private static final String[] blacklist = {"search", "aai-internal", "models", "named-queries"}; + + private List endpoints = new ArrayList<>(); + + private Map endpointToLogicalName = new HashMap<>(); + + private final LogLineBuilder llBuilder = new LogLineBuilder(); + + /** + * Instantiates a new list endpoints. + * + * @param version the version + */ + public ListEndpoints(Version version) { + + Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version, llBuilder); + if (loader != null) { + Introspector introspector = loader.introspectorFromName(INVENTORY); + + beginAudit(introspector, "/aai/" + version); + } else { + log.error(String.format("failed to create logger for %s version", version)); + } + } + + /** + * Begin audit. + * + * @param obj the obj + * @param uri the uri + */ + private void beginAudit(Introspector obj, String uri) { + String currentUri = getCurrentUri(obj, uri); + + if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) { + return; + } + if (!obj.isContainer()) { + endpoints.add(currentUri); + } + + populateLogicalName(obj, uri, currentUri); + + handleAudit(obj, currentUri); + } + + private String getCurrentUri(Introspector obj, String uri) { + if (!obj.getDbName().equals(INVENTORY)) { + return uri + obj.getGenericURI(); + } + return uri; + } + + private void handleAudit(Introspector obj, String currentUri) { + outer: + for (String propName : obj.getProperties()) { + for (String item : blacklist) { + if (propName.equals(item)) { + continue outer; + } + } + if (obj.isListType(propName)) { + if (obj.isComplexGenericType(propName)) { + beginAudit( + IntrospectorFactory + .newInstance(ModelType.MOXY, obj.newInstanceOfNestedProperty(propName), llBuilder), + currentUri); + } + } else if (obj.isComplexType(propName)) { + beginAudit( + IntrospectorFactory.newInstance(ModelType.MOXY, obj.newInstanceOfProperty(propName), llBuilder), + currentUri); + } + } + } + + /** + * Populate logical name. + * + * @param obj the obj + * @param uriString the uri + * @param currentUri the current uri + */ + private void populateLogicalName(Introspector obj, String uriString, String currentUri) { + + if (obj.getDbName().equals(INVENTORY) || currentUri.split("/").length <= 4 || currentUri + .endsWith("relationship-list")) { + return; + } + String uri = ""; + if (uriString.endsWith("/relationship-list")) { + uri = uriString.substring(0, uriString.lastIndexOf('/')); + } + + String logicalName; + String keys = ""; + + if (!obj.getAllKeys().isEmpty()) { + + Pattern p = Pattern.compile("/\\{[\\w\\d\\-]+\\}/\\{[\\w\\d\\-]+\\}+$"); + Matcher m = p.matcher(currentUri); + + if (m.find()) { + keys = StringUtils.join(obj.getAllKeys(), "-and-"); + } else { + keys = StringUtils.join(obj.getAllKeys(), "-or-"); + } + keys = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, keys); + if (!keys.isEmpty()) { + keys = "With" + keys; + } + } + + logicalName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName()) + keys; + + if (endpointToLogicalName.containsKey(uri) && uri.endsWith("}")) { + logicalName = logicalName + "From" + endpointToLogicalName.get(uri); + } else if (endpointToLogicalName.containsKey(uri.substring(0, uri.lastIndexOf('/')))) { + logicalName = logicalName + "From" + endpointToLogicalName.get(uri.substring(0, uri.lastIndexOf('/'))); + } + + endpointToLogicalName.put(currentUri, logicalName); + } + + /** + * Gets the logical names. + * + * @return the logical names + */ + public Map getLogicalNames() { + + return endpointToLogicalName; + } + + /** + * Gets the endpoints. + * + * @return the endpoints + */ + public List getEndpoints() { + + return this.getEndpoints(""); + } + + /** + * Gets the endpoints. + * + * @param filterOut the filter out + * @return the endpoints + */ + public List getEndpoints(String filterOut) { + List result = new ArrayList<>(); + Pattern p = null; + Matcher m; + if (!"".equals(filterOut)) { + p = Pattern.compile(filterOut); + } + for (String s : endpoints) { + if (p != null) { + m = p.matcher(s); + if (m.find()) { + continue; + } + } + result.add(s); + } + + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (String s : endpoints) { + sb.append(s).append("\n"); + } + return sb.toString(); + } + + /** + * To string. + * + * @param filterOut the filter out + * @return the string + */ + public String toString(String filterOut) { + StringBuilder sb = new StringBuilder(); + Pattern p = Pattern.compile(filterOut); + Matcher m; + for (String s : endpoints) { + m = p.matcher(s); + if (!m.find()) { + sb.append(s).append("\n"); + } + } + return sb.toString(); + } } -- cgit 1.2.3-korg