X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=datarouter-node%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fnode%2FNodeUtils.java;h=da84ae544e2001188be02dafbab1ff144e74c6e5;hb=5e6a9f65049e8e8d39e8dcab227e5d75b328b173;hp=ea2ce34b00ea383b01c1f920c8bcfa5badf3bae7;hpb=428c7cf9852acf38fb3d043b17af07a88ebee7c9;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 ea2ce34b..da84ae54 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 @@ -24,37 +24,38 @@ package org.onap.dmaap.datarouter.node; -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.File; +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 java.util.UUID; +import java.util.zip.GZIPInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.apache.commons.codec.binary.Base64; -import org.apache.log4j.Logger; +import org.apache.commons.lang3.StringUtils; import org.onap.dmaap.datarouter.node.eelf.EelfMsgs; import org.slf4j.MDC; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; +import static com.att.eelf.configuration.Configuration.*; /** * 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(NodeUtils.class); private NodeUtils() { } @@ -62,7 +63,7 @@ public class NodeUtils { /** * Base64 encode a byte array * - * @param raw The bytes to be encoded + * @param raw The bytes to be encoded * @return The encoded string */ public static String base64Encode(byte[] raw) { @@ -72,7 +73,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,14 +96,17 @@ public class NodeUtils { md.update(node.getBytes()); md.update(key.getBytes()); return (getAuthHdr(node, base64Encode(md.digest()))); - } catch (Exception e) { - LOGGER.error("Exception in generating Credentials for given node name:= " + e.getMessage()); + } catch (Exception exception) { + eelfLogger + .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 @@ -110,19 +114,19 @@ public class NodeUtils { * @return CN of the certificate subject or null */ public static String getCanonicalName(String kstype, String ksfile, String kspass) { - KeyStore ks=null; + KeyStore ks; try { ks = KeyStore.getInstance(kstype); - try(FileInputStream fileInputStream=new FileInputStream(ksfile)) { + try (FileInputStream fileInputStream = new FileInputStream(ksfile)) { ks.load(fileInputStream, kspass.toCharArray()); + } catch (IOException ioException) { + eelfLogger.error("IOException occurred while opening FileInputStream: " + ioException.getMessage(), + ioException); + return (null); } - } catch(IOException ioException) { - LOGGER.error("Exception occurred while opening FileInputStream",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, e, ksfile); return (null); } return (getCanonicalName(ks)); @@ -147,7 +151,7 @@ public class NodeUtils { if (parts.length < 1) { return (null); } - subject = parts[0].trim(); + subject = parts[5].trim(); if (!subject.startsWith("CN=")) { return (null); @@ -157,7 +161,7 @@ public class NodeUtils { } } } catch (Exception e) { - LOGGER.error("NODE0402 Error extracting my name from my keystore file " + e.toString(), e); + eelfLogger.error("NODE0402 Error extracting my name from my keystore file " + e.toString(), e); } return (null); } @@ -171,8 +175,10 @@ public class NodeUtils { public static byte[] getInetAddress(String ip) { try { return (InetAddress.getByName(ip).getAddress()); - } catch (Exception e) { - LOGGER.error("Exception in generating byte array for given IP address := " + e.getMessage()); + } catch (Exception exception) { + eelfLogger + .error("Exception in generating byte array for given IP address := " + exception.toString(), + exception); } return (null); } @@ -198,11 +204,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) { @@ -232,7 +239,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 @@ -245,10 +254,54 @@ public class NodeUtils { try { MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName()); MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress()); - } catch (Exception e) { - LOGGER.error("Exception in generating byte array for given IP address := " + e.getMessage()); + } catch (Exception exception) { + eelfLogger + .error("Exception in generating byte array for given IP address := " + exception.toString(), + exception); + } + + } + + /* Method sets RequestIs and InvocationId for se in EELF logs + * @Method - setIpAndFqdnForEelf + * @Params - Req, Request used to get RequestId and InvocationId + */ + public static void setRequestIdAndInvocationId(HttpServletRequest req) { + String reqId = req.getHeader("X-ONAP-RequestID"); + if (StringUtils.isBlank(reqId)) { + reqId = UUID.randomUUID().toString(); } + MDC.put(MDC_KEY_REQUEST_ID, reqId); + String invId = req.getHeader("X-InvocationID"); + if (StringUtils.isBlank(invId)) { + invId = UUID.randomUUID().toString(); + } + MDC.put("InvocationId", invId); + } + public static void sendResponseError(HttpServletResponse response, int errorCode, EELFLogger intlogger) { + try { + response.sendError(errorCode); + } catch (IOException ioe) { + intlogger.error("IOException", ioe); + } + } + + /** + * Method to check to see if file is of type gzip + * + * @param file The name of the file to be checked + * @return True if the file is of type gzip + */ + public static boolean isFiletypeGzip(File file){ + try(FileInputStream fileInputStream = new FileInputStream(file); + GZIPInputStream gzip = new GZIPInputStream(fileInputStream)) { + + return true; + }catch (IOException e){ + eelfLogger.error("NODE0403 " + file.toString() + " Not in gzip(gz) format: " + e.toString() + e); + return false; + } }