Unit tests
[vid.git] / vid-app-common / src / main / java / org / onap / vid / utils / Logging.java
1 package org.onap.vid.utils;
2
3 import com.att.eelf.configuration.EELFLogger;
4 import com.fasterxml.jackson.core.JsonProcessingException;
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
7 import org.onap.portalsdk.core.util.SystemProperties;
8 import org.springframework.http.HttpMethod;
9 import org.springframework.web.context.request.RequestContextHolder;
10 import org.springframework.web.context.request.ServletRequestAttributes;
11
12 import javax.servlet.http.HttpServletRequest;
13 import javax.ws.rs.ProcessingException;
14 import javax.ws.rs.core.Response;
15 import java.util.Arrays;
16 import java.util.Optional;
17
18 import static org.onap.vid.utils.Streams.not;
19
20 public class Logging {
21
22     Logging() {
23     }
24
25     public static final String HTTP_REQUESTS_OUTGOING = "http.requests.outgoing.";
26
27     public static final String requestIdHeaderKey = SystemProperties.ECOMP_REQUEST_ID;
28
29     private static ObjectMapper objectMapper = new ObjectMapper();
30
31     public static String getMethodName() {
32         return getMethodName(0);
33     }
34
35     public static String getMethodCallerName() {
36         return getMethodName(1);
37     }
38
39     private static String getMethodName(int depth) {
40         final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
41         String thisClassName = stackTrace[1].getClassName();
42         final Optional<String> caller =
43                 Arrays.stream(stackTrace)
44                         .skip(1)
45                         .filter(not(frame -> frame.getClassName().equals(thisClassName)))
46                         .skip(depth)
47                         .map(StackTraceElement::getMethodName)
48                         .findFirst();
49         return caller.orElse("<unknonwn method name>");
50     }
51
52     public static EELFLogger getRequestsLogger(String serverName) {
53         return EELFLoggerDelegate.getLogger(HTTP_REQUESTS_OUTGOING +serverName);
54     }
55
56     public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url, final Object body) {
57         if (!logger.isDebugEnabled()) {
58             return;
59         }
60
61         if (body == null) {
62             logRequest(logger, method, url);
63             return;
64         }
65
66         try {
67             String bodyAsJson = objectMapper.writeValueAsString(body);
68             logger.debug("Sending  {} {} Body: {}", method.name(), url, bodyAsJson);
69         } catch (JsonProcessingException e) {
70             logRequest(logger, method, url);
71             logger.debug("Failed to parse object in logRequest. {}", body);
72         }
73     }
74
75     public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url) {
76         logger.debug("Sending  {} {}", method.name(), url);
77     }
78
79     public static <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response, final Class<T> entityClass) {
80         if (!logger.isDebugEnabled()) {
81             return;
82         }
83         if (response == null) {
84             logger.debug("Received {} {} response: null", method.name(), url);
85             return;
86         }
87         try {
88             response.bufferEntity();
89             logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.readEntity(entityClass));
90         }
91         catch (ProcessingException | IllegalStateException e) {
92             logger.debug("Received {} {} Status: {} . Failed to read response as {}", method.name(), url, response.getStatus(), entityClass.getName());
93         }
94     }
95
96     public static void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response) {
97         logResponse(logger, method, url, response, String.class);
98     }
99
100     public static HttpServletRequest getHttpServletRequest(){
101         return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
102     }
103
104
105 }