X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-node%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fnode%2FNodeUtils.java;h=63691957f317a7f3c7f8c5ec4d8ac4ba1e08e0ef;hb=158f65097526b497de5f30f0c954e465bee0d355;hp=c57308b644add8e8af54f7e7b0ed4143f3525840;hpb=1841cb5d8da7b21996f8faad9d24f858e6ce8a41;p=dmaap%2Fdatarouter.git diff --git a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeUtils.java b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeUtils.java index c57308b6..63691957 100644 --- a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeUtils.java +++ b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeUtils.java @@ -28,33 +28,32 @@ import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN; import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS; import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME; -import java.security.*; -import java.io.*; -import java.util.*; -import java.security.cert.*; -import java.net.*; -import java.text.*; - +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.InetAddress; +import java.security.KeyStore; +import java.security.MessageDigest; +import java.security.cert.X509Certificate; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Enumeration; +import java.util.TimeZone; +import javax.servlet.http.HttpServletResponse; import org.apache.commons.codec.binary.Base64; import org.apache.log4j.Logger; import org.onap.dmaap.datarouter.node.eelf.EelfMsgs; import org.slf4j.MDC; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - /** * Utility functions for the data router node */ public class NodeUtils { - private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.node.NodeUtils"); - private static Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.node.NodeUtils"); - private static SimpleDateFormat logdate; - static { - logdate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); - logdate.setTimeZone(TimeZone.getTimeZone("GMT")); - } + private static EELFLogger eelfLogger = EELFManager.getInstance() + .getLogger("org.onap.dmaap.datarouter.node.NodeUtils"); + private static Logger nodeUtilsLogger = Logger.getLogger("org.onap.dmaap.datarouter.node.NodeUtils"); private NodeUtils() { } @@ -72,7 +71,7 @@ public class NodeUtils { /** * Given a user and password, generate the credentials * - * @param user User name + * @param user User name * @param password User password * @return Authorization header value */ @@ -95,13 +94,17 @@ public class NodeUtils { md.update(node.getBytes()); md.update(key.getBytes()); return (getAuthHdr(node, base64Encode(md.digest()))); - } catch (Exception e) { + } catch (Exception exception) { + nodeUtilsLogger + .error("Exception in generating Credentials for given node name:= " + exception.toString(), + exception); return (null); } } /** - * Given a keystore file and its password, return the value of the CN of the first private key entry with a certificate. + * Given a keystore file and its password, return the value of the CN of the first private key entry with a + * certificate. * * @param kstype The type of keystore * @param ksfile The file name of the keystore @@ -109,16 +112,23 @@ public class NodeUtils { * @return CN of the certificate subject or null */ public static String getCanonicalName(String kstype, String ksfile, String kspass) { + KeyStore ks; try { - KeyStore ks = KeyStore.getInstance(kstype); - ks.load(new FileInputStream(ksfile), kspass.toCharArray()); - return (getCanonicalName(ks)); + ks = KeyStore.getInstance(kstype); + try (FileInputStream fileInputStream = new FileInputStream(ksfile)) { + ks.load(fileInputStream, kspass.toCharArray()); + } catch (IOException ioException) { + nodeUtilsLogger.error("IOException occurred while opening FileInputStream: " + ioException.getMessage(), + ioException); + return (null); + } } catch (Exception e) { setIpAndFqdnForEelf("getCanonicalName"); - eelflogger.error(EelfMsgs.MESSAGE_KEYSTORE_LOAD_ERROR, ksfile, e.toString()); - logger.error("NODE0401 Error loading my keystore file + " + ksfile + " " + e.toString(), e); + eelfLogger.error(EelfMsgs.MESSAGE_KEYSTORE_LOAD_ERROR, ksfile, e.toString()); + nodeUtilsLogger.error("NODE0401 Error loading my keystore file + " + ksfile + " " + e.toString(), e); return (null); } + return (getCanonicalName(ks)); } /** @@ -140,7 +150,7 @@ public class NodeUtils { if (parts.length < 1) { return (null); } - subject = parts[0].trim(); + subject = parts[5].trim(); if (!subject.startsWith("CN=")) { return (null); @@ -150,7 +160,7 @@ public class NodeUtils { } } } catch (Exception e) { - logger.error("NODE0402 Error extracting my name from my keystore file " + e.toString(), e); + nodeUtilsLogger.error("NODE0402 Error extracting my name from my keystore file " + e.toString(), e); } return (null); } @@ -164,7 +174,10 @@ public class NodeUtils { public static byte[] getInetAddress(String ip) { try { return (InetAddress.getByName(ip).getAddress()); - } catch (Exception e) { + } catch (Exception exception) { + nodeUtilsLogger + .error("Exception in generating byte array for given IP address := " + exception.toString(), + exception); } return (null); } @@ -190,11 +203,12 @@ public class NodeUtils { if (i == -1) { return (null); } - return (new String[]{uriandparams.substring(i + 1, end - 1), uriandparams.substring(end + 1)}); + return (new String[]{uriandparams.substring(i + 1, end), uriandparams.substring(end + 1)}); } /** - * Escape fields that might contain vertical bar, backslash, or newline by replacing them with backslash p, backslash e and backslash n. + * Escape fields that might contain vertical bar, backslash, or newline by replacing them with backslash p, + * backslash e and backslash n. */ public static String loge(String s) { if (s == null) { @@ -224,7 +238,9 @@ public class NodeUtils { * Format a logging timestamp as yyyy-mm-ddThh:mm:ss.mmmZ */ public static synchronized String logts(Date when) { - return (logdate.format(when)); + SimpleDateFormat logDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + logDate.setTimeZone(TimeZone.getTimeZone("GMT")); + return (logDate.format(when)); } /* Method prints method name, server FQDN and IP Address of the machine in EELF logs @@ -237,11 +253,21 @@ public class NodeUtils { try { MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName()); MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress()); - } catch (Exception e) { - e.printStackTrace(); + } catch (Exception exception) { + nodeUtilsLogger + .error("Exception in generating byte array for given IP address := " + exception.toString(), + exception); } } + public static void sendResponseError(HttpServletResponse response, int errorCode, Logger intlogger) { + try { + response.sendError(errorCode); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } + } + }