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=2ba97163cdbe44ba34b3606ba4067a2ae4437e39;hb=14b8d9552808063686e0c22760cec6e35b960b59;hp=2c013ca5729d3f8f5baa036aa8b8c76a0db21e78;hpb=49be863f0005d80724a99acf13f31c4e03c5be68;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 2c013ca5..2ba97163 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,10 +24,6 @@ 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 com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import java.io.FileInputStream; @@ -40,25 +36,25 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Enumeration; import java.util.TimeZone; +import java.util.UUID; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.onap.dmaap.datarouter.node.eelf.EelfMsgs; import org.slf4j.MDC; +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"); + .getLogger(NodeUtils.class); private static Logger nodeUtilsLogger = 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 NodeUtils() { } @@ -101,7 +97,8 @@ public class NodeUtils { return (getAuthHdr(node, base64Encode(md.digest()))); } catch (Exception exception) { nodeUtilsLogger - .error("Exception in generating Credentials for given node name:= " + exception.toString(), exception); + .error("Exception in generating Credentials for given node name:= " + exception.toString(), + exception); return (null); } } @@ -123,7 +120,7 @@ public class NodeUtils { ks.load(fileInputStream, kspass.toCharArray()); } catch (IOException ioException) { nodeUtilsLogger.error("IOException occurred while opening FileInputStream: " + ioException.getMessage(), - ioException); + ioException); return (null); } } catch (Exception e) { @@ -180,7 +177,8 @@ public class NodeUtils { return (InetAddress.getByName(ip).getAddress()); } catch (Exception exception) { nodeUtilsLogger - .error("Exception in generating byte array for given IP address := " + exception.toString(), exception); + .error("Exception in generating byte array for given IP address := " + exception.toString(), + exception); } return (null); } @@ -241,6 +239,8 @@ public class NodeUtils { * Format a logging timestamp as yyyy-mm-ddThh:mm:ss.mmmZ */ public static synchronized String logts(Date when) { + SimpleDateFormat logDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + logDate.setTimeZone(TimeZone.getTimeZone("GMT")); return (logDate.format(when)); } @@ -256,10 +256,36 @@ public class NodeUtils { MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress()); } catch (Exception exception) { nodeUtilsLogger - .error("Exception in generating byte array for given IP address := " + exception.toString(), exception); + .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, Logger intlogger) { + try { + response.sendError(errorCode); + } catch (IOException ioe) { + intlogger.error("IOException" + ioe.getMessage()); + } + } + }