From: YiLi Date: Thu, 15 Mar 2018 02:59:09 +0000 (+0800) Subject: Add log filter X-Git-Tag: 1.2.0~31 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=holmes%2Fcommon.git;a=commitdiff_plain;h=c93746f09c38562d25e51b8274d6385eaaf07a00 Add log filter Change-Id: I07a51e3675f7ff99936c5788d56243934a083791 Issue-ID: HOLMES-112 Signed-off-by: YiLi --- diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/transactionid/AddHeadersHttpServletRequestWrapper.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/transactionid/AddHeadersHttpServletRequestWrapper.java new file mode 100644 index 0000000..d61797b --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/transactionid/AddHeadersHttpServletRequestWrapper.java @@ -0,0 +1,40 @@ +/** + * Copyright 2018 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.holmes.common.utils.transactionid; + +import java.util.HashMap; +import java.util.Map; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; + +public class AddHeadersHttpServletRequestWrapper extends HttpServletRequestWrapper { + final private Map additionalHeaders = new HashMap<>(); + + public AddHeadersHttpServletRequestWrapper(HttpServletRequest request) { + super(request); + } + + public void addHeader(String name, String value) { + additionalHeaders.put(name, value); + } + + @Override + public String getHeader(String name) { + String header = additionalHeaders.get(name); + return (header != null) ? header : super.getHeader(name); + } + +} diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/transactionid/TransactionIdFilter.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/transactionid/TransactionIdFilter.java new file mode 100644 index 0000000..3b12399 --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/transactionid/TransactionIdFilter.java @@ -0,0 +1,99 @@ +/** + * Copyright 2018 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.holmes.common.utils.transactionid; + +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.jvnet.hk2.annotations.Service; +import org.slf4j.MDC; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; + + +@Service +@Slf4j +public class TransactionIdFilter implements Filter { + + public static final Marker INVOKE_SYNCHRONOUS; + public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY"); + public static final Marker EXIT = MarkerFactory.getMarker("EXIT"); + + static { + INVOKE_SYNCHRONOUS = MarkerFactory.getMarker("INVOKE"); + INVOKE_SYNCHRONOUS.add(MarkerFactory.getMarker("SYNCHRONOUS")); + } + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, + FilterChain filterChain) throws IOException, ServletException { + HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; + AddHeadersHttpServletRequestWrapper requestWithTransactionId = new AddHeadersHttpServletRequestWrapper( + httpServletRequest); + log.warn(ENTRY, "Entering."); + + String requestID = ensureTransactionIdIsPresent(requestWithTransactionId); + HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; + httpServletResponse.setHeader(TransactionIdUtils.REQUEST_ID_HEADER, requestID); + + String invocationID = TransactionIdUtils.getUUID(); + httpServletResponse.setHeader(TransactionIdUtils.INVOCATIONIDID_HEADER,invocationID); + + MDC.put("RequestID", requestID); + MDC.put("InvocationID", invocationID); + + log.warn(INVOKE_SYNCHRONOUS, "Invoking synchronously ... "); + try { + filterChain.doFilter(requestWithTransactionId, httpServletResponse); + } finally { + log.debug(EXIT, "Exiting."); + MDC.remove("RequestID"); + MDC.remove("InvocationID"); + } + } + + @Override + public void destroy() { + + } + + public String ensureTransactionIdIsPresent( + AddHeadersHttpServletRequestWrapper request) { + String requestId = request.getHeader(TransactionIdUtils.REQUEST_ID_HEADER); + + if (StringUtils.isBlank(requestId)) { + requestId = TransactionIdUtils.getUUID(); + log.info(INVOKE_SYNCHRONOUS, "This warning has a 'MY_MARKER' annotation."); + log.info("Request ID ({} header) not exist. It was generated: {}", + TransactionIdUtils.REQUEST_ID_HEADER, requestId); + request.addHeader(TransactionIdUtils.REQUEST_ID_HEADER, requestId); + } + return requestId; + } +} diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/transactionid/TransactionIdUtils.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/transactionid/TransactionIdUtils.java new file mode 100644 index 0000000..be61b92 --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/transactionid/TransactionIdUtils.java @@ -0,0 +1,28 @@ +/** + * Copyright 2018 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.holmes.common.utils.transactionid; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class TransactionIdUtils { + public static final String REQUEST_ID_HEADER = "X-TransactionID"; + public static final String INVOCATIONIDID_HEADER = "X-InvocationID"; + + public static String getUUID() { + return java.util.UUID.randomUUID().toString(); + } +} diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/transactionid/TransactionIdFilterTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/transactionid/TransactionIdFilterTest.java new file mode 100644 index 0000000..3ef092e --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/transactionid/TransactionIdFilterTest.java @@ -0,0 +1,58 @@ +/** + * Copyright 2018 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.holmes.common.utils.transactionid; + +import javax.servlet.FilterChain; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class TransactionIdFilterTest { + TransactionIdFilter filter = new TransactionIdFilter(); + + private HttpServletRequest requestMock; + private HttpServletResponse responseMock; + private FilterChain chainMock; + + @Before + public void setUp() throws Exception{ + requestMock = EasyMock.createMock(HttpServletRequest.class); + responseMock = EasyMock.createMock(HttpServletResponse.class); + chainMock = EasyMock.createMock(FilterChain.class); + } + @Test + public void callsChainDoFilter() throws Exception { + filter.doFilter(requestMock, responseMock, chainMock); + EasyMock.verify(); + } + @Test + public void requestIdExistTest() throws Exception{ + String requestID = TransactionIdUtils.getUUID(); + EasyMock.expect(requestMock.getHeader(TransactionIdUtils.REQUEST_ID_HEADER)).andReturn(requestID); + + EasyMock.replay(requestMock); + filter.doFilter(requestMock, responseMock, chainMock); + EasyMock.verify(); + + } + + +} \ No newline at end of file