From affc70024b896fd62b6e48272ea858a343ebe6eb Mon Sep 17 00:00:00 2001 From: emeroad Date: Tue, 17 Jan 2023 16:56:13 +0900 Subject: [PATCH] [#9642] Add Generic ArgumentValidator --- .../util/argument/ArgumentValidator.java | 65 ++++++++++ .../util/argument/ClassPredicate.java | 37 ++++++ .../bootstrap/util/argument/Predicate.java | 10 ++ .../bootstrap/util/argument/Validation.java | 43 +++++++ .../bootstrap/util/argument/Validator.java | 4 +- .../util/argument/ValidationTest.java | 119 ++++++++++++++++++ .../util/ServletArgumentValidator.java | 86 ------------- .../util/ServletArgumentValidatorTest.java | 82 ------------ .../StandardHostValveInvokeInterceptor.java | 22 ++-- .../ServletInvocationServiceInterceptor.java | 22 ++-- .../StandardHostValveInvokeInterceptor.java | 20 ++- ...nnectorsExecuteRootHandlerInterceptor.java | 66 +++++----- .../WebContainerHandleRequestInterceptor.java | 19 ++- 13 files changed, 367 insertions(+), 228 deletions(-) create mode 100644 bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/ArgumentValidator.java create mode 100644 bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/ClassPredicate.java create mode 100644 bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Predicate.java create mode 100644 bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Validation.java rename plugins/common-servlet/src/main/java/com/navercorp/pinpoint/plugin/common/servlet/util/ArgumentValidator.java => bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Validator.java (87%) create mode 100644 bootstraps/bootstrap-core/src/test/java/com/navercorp/pinpoint/bootstrap/util/argument/ValidationTest.java delete mode 100644 plugins/common-servlet/src/main/java/com/navercorp/pinpoint/plugin/common/servlet/util/ServletArgumentValidator.java delete mode 100644 plugins/common-servlet/src/test/java/com/navercorp/pinpoint/plugin/common/servlet/util/ServletArgumentValidatorTest.java diff --git a/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/ArgumentValidator.java b/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/ArgumentValidator.java new file mode 100644 index 000000000000..7a3f9c9a140b --- /dev/null +++ b/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/ArgumentValidator.java @@ -0,0 +1,65 @@ +/* + * Copyright 2018 NAVER Corp. + * + * 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 com.navercorp.pinpoint.bootstrap.util.argument; + +import java.util.List; + +/** + * @author Woonduk Kang(emeroad) + */ +public class ArgumentValidator implements Validator { + private final Predicate[] predicates; + + private final int minArgsSize; + + public ArgumentValidator(List argumentList) { + this(argumentList, 0); + } + + public ArgumentValidator(List predicates, int minArgsSize) { + this.predicates = predicates.toArray(new Predicate[0]); + this.minArgsSize = minIndex(this.predicates, minArgsSize); + } + + private static int minIndex(Predicate[] predicates, int minArgsSize) { + if (minArgsSize != 0) { + return minArgsSize; + } + int max = -1; + for (Predicate predicate : predicates) { + max = Math.max(max, predicate.index()); + } + return max + 1; + } + + @Override + public boolean validate(Object[] args) { + if (args == null) { + return false; + } + if (args.length < minArgsSize) { + return false; + } + + for (Predicate predicate : this.predicates) { + if (!predicate.test(args)) { + return false; + } + } + return true; + } +} diff --git a/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/ClassPredicate.java b/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/ClassPredicate.java new file mode 100644 index 000000000000..d9ed66e6f53d --- /dev/null +++ b/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/ClassPredicate.java @@ -0,0 +1,37 @@ +package com.navercorp.pinpoint.bootstrap.util.argument; + +import com.navercorp.pinpoint.bootstrap.logging.PLogger; + +/** + * @author Woonduk Kang(emeroad) + */ +public class ClassPredicate implements Predicate { + private final PLogger logger; + + private final Class clazz; + private final int index; + + + public ClassPredicate(PLogger logger, Class clazz, int index) { + this.logger = logger; + this.clazz = clazz; + this.index = index; + } + + @Override + public boolean test(Object[] args) { + final Object param = args[index]; + if (!clazz.isInstance(param)) { + if (logger.isDebugEnabled()) { + logger.debug("Invalid args[{}] object, Not implemented of {}. args[{}]={}", index, clazz, index, param); + } + return false; + } + return true; + } + + @Override + public int index() { + return index; + } +} diff --git a/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Predicate.java b/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Predicate.java new file mode 100644 index 000000000000..0b2deea6ca02 --- /dev/null +++ b/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Predicate.java @@ -0,0 +1,10 @@ +package com.navercorp.pinpoint.bootstrap.util.argument; + +/** + * @author Woonduk Kang(emeroad) + */ +public interface Predicate { + boolean test(Object[] args); + + int index(); +} diff --git a/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Validation.java b/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Validation.java new file mode 100644 index 000000000000..b2fe23edadf9 --- /dev/null +++ b/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Validation.java @@ -0,0 +1,43 @@ +package com.navercorp.pinpoint.bootstrap.util.argument; + +import com.navercorp.pinpoint.bootstrap.logging.PLogger; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * @author Woonduk Kang(emeroad) + */ +public class Validation { + private final PLogger logger; + private final List list = new ArrayList<>(4); + private int min; + + public Validation(PLogger logger) { + this.logger = Objects.requireNonNull(logger, "logger"); + } + + public Validation addArgument(Class clazz, int index) { + this.list.add(new ClassPredicate(logger, clazz, index)); + return this; + } + + public Validation addPredicate(Predicate predicate) { + Objects.requireNonNull(predicate, "predicate"); + + this.list.add(predicate); + return this; + } + + public Validation minArgsSize(int min) { + this.min = min; + return this; + } + + public Validator build() { + List list = new ArrayList<>(this.list); + return new ArgumentValidator(list, min); + } + +} diff --git a/plugins/common-servlet/src/main/java/com/navercorp/pinpoint/plugin/common/servlet/util/ArgumentValidator.java b/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Validator.java similarity index 87% rename from plugins/common-servlet/src/main/java/com/navercorp/pinpoint/plugin/common/servlet/util/ArgumentValidator.java rename to bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Validator.java index 446d3e09ce9f..4de17d3da211 100644 --- a/plugins/common-servlet/src/main/java/com/navercorp/pinpoint/plugin/common/servlet/util/ArgumentValidator.java +++ b/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/util/argument/Validator.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package com.navercorp.pinpoint.plugin.common.servlet.util; +package com.navercorp.pinpoint.bootstrap.util.argument; /** * @author Woonduk Kang(emeroad) */ -public interface ArgumentValidator { +public interface Validator { boolean validate(Object[] args); } diff --git a/bootstraps/bootstrap-core/src/test/java/com/navercorp/pinpoint/bootstrap/util/argument/ValidationTest.java b/bootstraps/bootstrap-core/src/test/java/com/navercorp/pinpoint/bootstrap/util/argument/ValidationTest.java new file mode 100644 index 000000000000..1a55b542b0ab --- /dev/null +++ b/bootstraps/bootstrap-core/src/test/java/com/navercorp/pinpoint/bootstrap/util/argument/ValidationTest.java @@ -0,0 +1,119 @@ +package com.navercorp.pinpoint.bootstrap.util.argument; + +import com.navercorp.pinpoint.bootstrap.logging.PLogger; +import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * @author Woonduk Kang(emeroad) + */ +class ValidationTest { + + private final Validator validator = newArgumentValidator(); + + public Validator newArgumentValidator() { + PLogger logger = PLoggerFactory.getLogger(this.getClass()); + + Validation validation = new Validation(logger); + validation.addArgument(String.class, 0); + validation.addArgument(Long.class, 1); + return validation.build(); + } + + + @Test + void valid() { + Object[] arguments = new Object[2]; + arguments[0] = "abc"; + arguments[1] = 123L; + Assertions.assertTrue(validator.validate(arguments)); + } + + @Test + void empty() { + PLogger logger = PLoggerFactory.getLogger(this.getClass()); + + Validation validation = new Validation(logger); + Validator validator = validation.build(); + + Assertions.assertTrue(validator.validate(new Object[0])); + Assertions.assertTrue(validator.validate(new Object[2])); + } + + @Test + void predicate() { + PLogger logger = PLoggerFactory.getLogger(this.getClass()); + + Validation validation = new Validation(logger); + validation.addPredicate(new Predicate() { + @Override + public boolean test(Object[] args) { + Object arg = args[index()]; + return arg instanceof String; + } + + @Override + public int index() { + return 0; + } + }); + Validator validator = validation.build(); + + Assertions.assertFalse(validator.validate(new Object[]{1})); + Assertions.assertTrue(validator.validate(new Object[]{"abc"})); + } + + @Test + void valid_boundary_check() { + + Assertions.assertFalse(validator.validate(null)); + Assertions.assertFalse(validator.validate(new Object[0])); + Assertions.assertFalse(validator.validate(new Object[1])); + + Object[] arguments = new Object[10]; + arguments[0] = "abc"; + arguments[1] = 123L; + Assertions.assertTrue(validator.validate(arguments)); + } + + @Test + void valid_fail1() { + Object[] arguments = new Object[2]; + arguments[0] = "abc"; + arguments[1] = new Object(); + Assertions.assertFalse(validator.validate(arguments)); + } + + + @Test + void valid_fail2() { + Object[] arguments = new Object[2]; + arguments[0] = new Object(); + arguments[1] = 123L; + Assertions.assertFalse(validator.validate(arguments)); + } + + @Test + void valid_min() { + PLogger logger = PLoggerFactory.getLogger(this.getClass()); + + Validation validation = new Validation(logger); + validation.addArgument(String.class, 0); + validation.addArgument(Long.class, 1); + validation.minArgsSize(3); + Validator validator = validation.build(); + + Object[] arguments1 = new Object[2]; + arguments1[0] = "abc"; + arguments1[1] = 123L; + Assertions.assertFalse(validator.validate(arguments1)); + + Object[] arguments2 = new Object[5]; + arguments2[0] = "abc"; + arguments2[1] = 123L; + Assertions.assertTrue(validator.validate(arguments2)); + + + } +} \ No newline at end of file diff --git a/plugins/common-servlet/src/main/java/com/navercorp/pinpoint/plugin/common/servlet/util/ServletArgumentValidator.java b/plugins/common-servlet/src/main/java/com/navercorp/pinpoint/plugin/common/servlet/util/ServletArgumentValidator.java deleted file mode 100644 index 98d33ad1e672..000000000000 --- a/plugins/common-servlet/src/main/java/com/navercorp/pinpoint/plugin/common/servlet/util/ServletArgumentValidator.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2018 NAVER Corp. - * - * 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 com.navercorp.pinpoint.plugin.common.servlet.util; - -import com.navercorp.pinpoint.bootstrap.logging.PLogger; -import com.navercorp.pinpoint.common.util.Assert; - -import java.util.Objects; - -/** - * @author Woonduk Kang(emeroad) - */ -public class ServletArgumentValidator implements ArgumentValidator { - private final PLogger logger; - - private final int requestIndex; - private final Class requestClass; - - private final int responseIndex; - private final Class responseClass; - - private final int minArgsSize; - - public ServletArgumentValidator(PLogger logger, int requestIndex, Class requestClass, int responseIndex, Class responseClass) { - this(logger, requestIndex, requestClass, responseIndex, responseClass, defaultArgumentMin(requestIndex, responseIndex)); - } - - public ServletArgumentValidator(PLogger logger, int requestIndex, Class requestClass, int responseIndex, Class responseClass, int minArgsSize) { - this.logger = Objects.requireNonNull(logger, "logger"); - - Assert.isTrue(requestIndex >= 0, "requestIndex must be positive"); - this.requestIndex = requestIndex; - this.requestClass = Objects.requireNonNull(requestClass, "requestClass"); - - Assert.isTrue(responseIndex >= 0, "responseIndex must be positive"); - this.responseIndex = responseIndex; - this.responseClass = Objects.requireNonNull(responseClass, "responseClass"); - - Assert.isTrue(requestIndex != responseIndex, "requestIndex==responseIndex"); - this.minArgsSize = minArgsSize; - } - - private static int defaultArgumentMin(int requestIndex, int responseIndex) { - return Math.max(requestIndex, responseIndex) + 1; - } - - @Override - public boolean validate(Object[] args) { - if (args == null) { - return false; - } - if (args.length < minArgsSize) { - return false; - } - - final Object request = args[requestIndex]; - if (!(requestClass.isInstance(request))) { - if (logger.isDebugEnabled()) { - logger.debug("Invalid args[{}] object, Not implemented of {}. args[{}]={}", requestIndex, requestClass, requestIndex, request); - } - return false; - } - final Object response = args[responseIndex]; - if (!(responseClass.isInstance(response))) { - if (logger.isDebugEnabled()) { - logger.debug("Invalid args[{}] object, Not implemented of {}. args[{}]={}.", responseIndex, responseClass, responseIndex, response); - } - return false; - } - return true; - } -} diff --git a/plugins/common-servlet/src/test/java/com/navercorp/pinpoint/plugin/common/servlet/util/ServletArgumentValidatorTest.java b/plugins/common-servlet/src/test/java/com/navercorp/pinpoint/plugin/common/servlet/util/ServletArgumentValidatorTest.java deleted file mode 100644 index 306df422a682..000000000000 --- a/plugins/common-servlet/src/test/java/com/navercorp/pinpoint/plugin/common/servlet/util/ServletArgumentValidatorTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2018 NAVER Corp. - * - * 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 com.navercorp.pinpoint.plugin.common.servlet.util; - -import com.navercorp.pinpoint.bootstrap.logging.PLogger; -import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - -import static org.mockito.Mockito.mock; - -/** - * @author Woonduk Kang(emeroad) - */ -public class ServletArgumentValidatorTest { - - private ArgumentValidator validator; - - @BeforeEach - public void setUp() throws Exception { - PLogger logger = PLoggerFactory.getLogger(this.getClass()); - this.validator = new ServletArgumentValidator(logger, 0, ServletRequest.class, 1, ServletResponse.class); - } - - - @Test - public void valid() { - Object[] argument = new Object[2]; - argument[0] = mock(ServletRequest.class); - argument[1] = mock(ServletResponse.class); - Assertions.assertTrue(validator.validate(argument)); - } - - @Test - public void valid_boundary_check() { - - Assertions.assertFalse(validator.validate(null)); - Assertions.assertFalse(validator.validate(new Object[0])); - Assertions.assertFalse(validator.validate(new Object[1])); - - Object[] argument = new Object[10]; - argument[0] = mock(ServletRequest.class); - argument[1] = mock(ServletResponse.class); - Assertions.assertTrue(validator.validate(argument)); - } - - @Test - public void valid_fail1() { - Object[] argument = new Object[2]; - argument[0] = mock(ServletRequest.class); - argument[1] = new Object(); - Assertions.assertFalse(validator.validate(argument)); - } - - - @Test - public void valid_fail2() { - - Object[] argument = new Object[2]; - argument[0] = new Object(); - argument[1] = mock(ServletResponse.class); - Assertions.assertFalse(validator.validate(argument)); - } -} \ No newline at end of file diff --git a/plugins/jboss/src/main/java/com/navercorp/pinpoint/plugin/jboss/interceptor/StandardHostValveInvokeInterceptor.java b/plugins/jboss/src/main/java/com/navercorp/pinpoint/plugin/jboss/interceptor/StandardHostValveInvokeInterceptor.java index b1281596296f..7ce3c2f58434 100644 --- a/plugins/jboss/src/main/java/com/navercorp/pinpoint/plugin/jboss/interceptor/StandardHostValveInvokeInterceptor.java +++ b/plugins/jboss/src/main/java/com/navercorp/pinpoint/plugin/jboss/interceptor/StandardHostValveInvokeInterceptor.java @@ -31,11 +31,11 @@ import com.navercorp.pinpoint.bootstrap.plugin.request.util.ParameterRecorder; import com.navercorp.pinpoint.bootstrap.plugin.response.ServletResponseListener; import com.navercorp.pinpoint.bootstrap.plugin.response.ServletResponseListenerBuilder; -import com.navercorp.pinpoint.plugin.common.servlet.util.ArgumentValidator; +import com.navercorp.pinpoint.bootstrap.util.argument.Validation; +import com.navercorp.pinpoint.bootstrap.util.argument.Validator; import com.navercorp.pinpoint.plugin.common.servlet.util.HttpServletRequestAdaptor; import com.navercorp.pinpoint.plugin.common.servlet.util.HttpServletResponseAdaptor; import com.navercorp.pinpoint.plugin.common.servlet.util.ParameterRecorderFactory; -import com.navercorp.pinpoint.plugin.common.servlet.util.ServletArgumentValidator; import com.navercorp.pinpoint.plugin.jboss.JbossConfig; import com.navercorp.pinpoint.plugin.jboss.JbossConstants; import com.navercorp.pinpoint.plugin.jboss.util.Servlet2ApiHelper; @@ -59,7 +59,7 @@ public class StandardHostValveInvokeInterceptor implements AroundInterceptor { private final boolean isInfo = logger.isInfoEnabled(); private final MethodDescriptor methodDescriptor; - private final ArgumentValidator argumentValidator; + private final Validator validator; private final ServletRequestListener servletRequestListener; private final ServletResponseListener servletResponseListener; private final ServletApiHelper servletApiHelper; @@ -72,7 +72,8 @@ public class StandardHostValveInvokeInterceptor implements AroundInterceptor { */ public StandardHostValveInvokeInterceptor(final TraceContext traceContext, final MethodDescriptor descriptor, final RequestRecorderFactory requestRecorderFactory) { this.methodDescriptor = descriptor; - this.argumentValidator = new ServletArgumentValidator(logger, 0, HttpServletRequest.class, 1, HttpServletResponse.class); + + this.validator = buildValidator(); final JbossConfig config = new JbossConfig(traceContext.getProfilerConfig()); RequestAdaptor requestAdaptor = new HttpServletRequestAdaptor(); ParameterRecorder parameterRecorder = ParameterRecorderFactory.newParameterRecorderFactory(config.getExcludeProfileMethodFilter(), config.isTraceRequestParam()); @@ -91,11 +92,18 @@ public StandardHostValveInvokeInterceptor(final TraceContext traceContext, final this.servletRequestListener = reqBuilder.build(); - this.servletResponseListener = new ServletResponseListenerBuilder(traceContext, new HttpServletResponseAdaptor()).build(); + this.servletResponseListener = new ServletResponseListenerBuilder<>(traceContext, new HttpServletResponseAdaptor()).build(); this.servletApiHelper = newServletApi(); } + private Validator buildValidator() { + Validation validation = new Validation(logger); + validation.addArgument(HttpServletRequest.class, 0); + validation.addArgument(HttpServletResponse.class, 1); + return validation.build(); + } + private ServletApiHelper newServletApi() { try { ServletRequest.class.getMethod("isAsyncStarted"); @@ -111,7 +119,7 @@ public void before(final Object target, final Object[] args) { logger.beforeInterceptor(target, args); } - if (!argumentValidator.validate(args)) { + if (!validator.validate(args)) { return; } @@ -139,7 +147,7 @@ public void after(final Object target, final Object[] args, final Object result, logger.afterInterceptor(target, args, result, throwable); } - if (!argumentValidator.validate(args)) { + if (!validator.validate(args)) { return; } diff --git a/plugins/resin/src/main/java/com/navercorp/pinpoint/plugin/resin/interceptor/ServletInvocationServiceInterceptor.java b/plugins/resin/src/main/java/com/navercorp/pinpoint/plugin/resin/interceptor/ServletInvocationServiceInterceptor.java index d3108f3c6ba2..87ff0845ba6c 100644 --- a/plugins/resin/src/main/java/com/navercorp/pinpoint/plugin/resin/interceptor/ServletInvocationServiceInterceptor.java +++ b/plugins/resin/src/main/java/com/navercorp/pinpoint/plugin/resin/interceptor/ServletInvocationServiceInterceptor.java @@ -26,16 +26,16 @@ import com.navercorp.pinpoint.bootstrap.plugin.request.RequestAdaptor; import com.navercorp.pinpoint.bootstrap.plugin.request.ServerCookieRecorder; import com.navercorp.pinpoint.bootstrap.plugin.request.ServerHeaderRecorder; -import com.navercorp.pinpoint.bootstrap.plugin.request.ServletRequestListenerBuilder; import com.navercorp.pinpoint.bootstrap.plugin.request.ServletRequestListener; +import com.navercorp.pinpoint.bootstrap.plugin.request.ServletRequestListenerBuilder; import com.navercorp.pinpoint.bootstrap.plugin.request.util.ParameterRecorder; import com.navercorp.pinpoint.bootstrap.plugin.response.ServletResponseListener; import com.navercorp.pinpoint.bootstrap.plugin.response.ServletResponseListenerBuilder; -import com.navercorp.pinpoint.plugin.common.servlet.util.ArgumentValidator; +import com.navercorp.pinpoint.bootstrap.util.argument.Validation; +import com.navercorp.pinpoint.bootstrap.util.argument.Validator; import com.navercorp.pinpoint.plugin.common.servlet.util.HttpServletRequestAdaptor; import com.navercorp.pinpoint.plugin.common.servlet.util.HttpServletResponseAdaptor; import com.navercorp.pinpoint.plugin.common.servlet.util.ParameterRecorderFactory; -import com.navercorp.pinpoint.plugin.common.servlet.util.ServletArgumentValidator; import com.navercorp.pinpoint.plugin.resin.ResinConfig; import com.navercorp.pinpoint.plugin.resin.ResinConstants; @@ -52,13 +52,14 @@ public class ServletInvocationServiceInterceptor implements AroundInterceptor { private final boolean isInfo = logger.isInfoEnabled(); private final MethodDescriptor methodDescriptor; - private final ArgumentValidator argumentValidator; + private final Validator validator; private final ServletRequestListener servletRequestListener; private final ServletResponseListener servletResponseListener; public ServletInvocationServiceInterceptor(TraceContext traceContext, MethodDescriptor methodDescriptor, RequestRecorderFactory requestRecorderFactory) { this.methodDescriptor = methodDescriptor; - this.argumentValidator = new ServletArgumentValidator(logger, 0, HttpServletRequest.class, 1, HttpServletResponse.class); + + this.validator = buildValidator(); final ResinConfig config = new ResinConfig(traceContext.getProfilerConfig()); RequestAdaptor requestAdaptor = new HttpServletRequestAdaptor(); @@ -80,13 +81,20 @@ public ServletInvocationServiceInterceptor(TraceContext traceContext, MethodDesc this.servletResponseListener = new ServletResponseListenerBuilder(traceContext, new HttpServletResponseAdaptor()).build(); } + private Validator buildValidator() { + Validation validation = new Validation(logger); + validation.addArgument(HttpServletRequest.class, 0); + validation.addArgument(HttpServletResponse.class, 1); + return validation.build(); + } + @Override public void before(Object target, Object[] args) { if (isDebug) { logger.beforeInterceptor(target, args); } - if (!argumentValidator.validate(args)) { + if (!validator.validate(args)) { return; } @@ -115,7 +123,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab logger.afterInterceptor(target, args, result, throwable); } - if (!argumentValidator.validate(args)) { + if (!validator.validate(args)) { return; } diff --git a/plugins/tomcat/src/main/java/com/navercorp/pinpoint/plugin/tomcat/interceptor/StandardHostValveInvokeInterceptor.java b/plugins/tomcat/src/main/java/com/navercorp/pinpoint/plugin/tomcat/interceptor/StandardHostValveInvokeInterceptor.java index 3f034372ca64..d2e035210e6b 100644 --- a/plugins/tomcat/src/main/java/com/navercorp/pinpoint/plugin/tomcat/interceptor/StandardHostValveInvokeInterceptor.java +++ b/plugins/tomcat/src/main/java/com/navercorp/pinpoint/plugin/tomcat/interceptor/StandardHostValveInvokeInterceptor.java @@ -32,11 +32,11 @@ import com.navercorp.pinpoint.bootstrap.plugin.request.util.ParameterRecorder; import com.navercorp.pinpoint.bootstrap.plugin.response.ServletResponseListener; import com.navercorp.pinpoint.bootstrap.plugin.response.ServletResponseListenerBuilder; -import com.navercorp.pinpoint.plugin.common.servlet.util.ArgumentValidator; +import com.navercorp.pinpoint.bootstrap.util.argument.Validation; +import com.navercorp.pinpoint.bootstrap.util.argument.Validator; import com.navercorp.pinpoint.plugin.common.servlet.util.HttpServletRequestAdaptor; import com.navercorp.pinpoint.plugin.common.servlet.util.HttpServletResponseAdaptor; import com.navercorp.pinpoint.plugin.common.servlet.util.ParameterRecorderFactory; -import com.navercorp.pinpoint.plugin.common.servlet.util.ServletArgumentValidator; import com.navercorp.pinpoint.plugin.tomcat.TomcatConfig; import com.navercorp.pinpoint.plugin.tomcat.TomcatConstants; import org.apache.catalina.connector.Response; @@ -54,7 +54,7 @@ public class StandardHostValveInvokeInterceptor implements AroundInterceptor { private final boolean isInfo = logger.isInfoEnabled(); private final MethodDescriptor methodDescriptor; - private final ArgumentValidator argumentValidator; + private final Validator validator; private final ServletRequestListener servletRequestListener; private final ServletResponseListener servletResponseListener; @@ -63,7 +63,8 @@ public class StandardHostValveInvokeInterceptor implements AroundInterceptor { public StandardHostValveInvokeInterceptor(TraceContext traceContext, MethodDescriptor descriptor, RequestRecorderFactory requestRecorderFactory) { this.methodDescriptor = descriptor; - this.argumentValidator = new ServletArgumentValidator(logger, 0, HttpServletRequest.class, 1, HttpServletResponse.class); + + this.validator = buildValidator(); final TomcatConfig config = new TomcatConfig(traceContext.getProfilerConfig()); @@ -87,13 +88,20 @@ public StandardHostValveInvokeInterceptor(TraceContext traceContext, MethodDescr this.servletResponseListener = new ServletResponseListenerBuilder<>(traceContext, new HttpServletResponseAdaptor()).build(); } + private Validator buildValidator() { + Validation validation = new Validation(logger); + validation.addArgument(HttpServletRequest.class, 0); + validation.addArgument(HttpServletResponse.class, 1); + return validation.build(); + } + @Override public void before(Object target, Object[] args) { if (isDebug) { logger.beforeInterceptor(target, args); } - if (!argumentValidator.validate(args)) { + if (!validator.validate(args)) { return; } @@ -123,7 +131,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab logger.afterInterceptor(target, args, result, throwable); } - if (!argumentValidator.validate(args)) { + if (!validator.validate(args)) { return; } diff --git a/plugins/undertow/src/main/java/com/navercorp/pinpoint/plugin/undertow/interceptor/ConnectorsExecuteRootHandlerInterceptor.java b/plugins/undertow/src/main/java/com/navercorp/pinpoint/plugin/undertow/interceptor/ConnectorsExecuteRootHandlerInterceptor.java index 33089f4d4de0..2db65357061b 100644 --- a/plugins/undertow/src/main/java/com/navercorp/pinpoint/plugin/undertow/interceptor/ConnectorsExecuteRootHandlerInterceptor.java +++ b/plugins/undertow/src/main/java/com/navercorp/pinpoint/plugin/undertow/interceptor/ConnectorsExecuteRootHandlerInterceptor.java @@ -32,7 +32,9 @@ import com.navercorp.pinpoint.bootstrap.plugin.request.util.ParameterRecorder; import com.navercorp.pinpoint.bootstrap.plugin.response.ServletResponseListener; import com.navercorp.pinpoint.bootstrap.plugin.response.ServletResponseListenerBuilder; -import com.navercorp.pinpoint.plugin.common.servlet.util.ArgumentValidator; +import com.navercorp.pinpoint.bootstrap.util.argument.Predicate; +import com.navercorp.pinpoint.bootstrap.util.argument.Validation; +import com.navercorp.pinpoint.bootstrap.util.argument.Validator; import com.navercorp.pinpoint.plugin.undertow.ParameterRecorderFactory; import com.navercorp.pinpoint.plugin.undertow.UndertowConfig; import com.navercorp.pinpoint.plugin.undertow.UndertowConstants; @@ -49,7 +51,7 @@ public class ConnectorsExecuteRootHandlerInterceptor implements AroundIntercepto private final boolean isInfo = logger.isInfoEnabled(); private final MethodDescriptor methodDescriptor; - private final ArgumentValidator argumentValidator; + private final Validator validator; private final UndertowHttpHeaderFilter httpHeaderFilter; private final ServletRequestListener servletRequestListener; private final ServletResponseListener servletResponseListener; @@ -57,7 +59,9 @@ public class ConnectorsExecuteRootHandlerInterceptor implements AroundIntercepto public ConnectorsExecuteRootHandlerInterceptor(TraceContext traceContext, MethodDescriptor descriptor, RequestRecorderFactory requestRecorderFactory) { this.methodDescriptor = descriptor; final UndertowConfig config = new UndertowConfig(traceContext.getProfilerConfig()); - this.argumentValidator = new ConnectorsArgumentValidator(config.getHttpHandlerClassNameFilter()); + + this.validator = buildValidator(config); + RequestAdaptor requestAdaptor = new HttpServerExchangeAdaptor(); ParameterRecorder parameterRecorder = ParameterRecorderFactory.newParameterRecorderFactory(config.getExcludeProfileMethodFilter(), config.isTraceRequestParam()); @@ -80,13 +84,20 @@ public ConnectorsExecuteRootHandlerInterceptor(TraceContext traceContext, Method this.httpHeaderFilter = new UndertowHttpHeaderFilter(config.isHidePinpointHeader()); } + private Validator buildValidator(UndertowConfig config) { + Validation validation = new Validation(logger); + validation.addPredicate(handlerPredicate(config.getHttpHandlerClassNameFilter())); + validation.addArgument(HttpServerExchange.class, 1); + return validation.build(); + } + @Override public void before(Object target, Object[] args) { if (isDebug) { logger.beforeInterceptor(target, args); } - if (!argumentValidator.validate(args)) { + if (!validator.validate(args)) { return; } @@ -108,7 +119,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab logger.afterInterceptor(target, args, result, throwable); } - if (!argumentValidator.validate(args)) { + if (!validator.validate(args)) { return; } @@ -133,36 +144,27 @@ private int getStatusCode(final HttpServerExchange response) { return 0; } - private static class ConnectorsArgumentValidator implements ArgumentValidator { - private final Filter httpHandlerClassNameFilter; - - public ConnectorsArgumentValidator(final Filter httpHandlerClassNameFilter) { - this.httpHandlerClassNameFilter = httpHandlerClassNameFilter; - } - - @Override - public boolean validate(Object[] args) { - if (args == null) { - return false; - } - - if (args.length < 2) { + public Predicate handlerPredicate(Filter httpHandlerClassNameFilter) { + return new Predicate() { + private static final int index = 0; + @Override + public boolean test(Object[] args) { + Object arg = args[index]; + if (!(arg instanceof HttpHandler)) { + return false; + } + + final String httpHandlerClassName = arg.getClass().getName(); + if (!httpHandlerClassNameFilter.filter(httpHandlerClassName)) { + return false; + } return false; } - if (!(args[0] instanceof HttpHandler)) { - return false; - } - - final String httpHandlerClassName = args[0].getClass().getName(); - if (!this.httpHandlerClassNameFilter.filter(httpHandlerClassName)) { - return false; + @Override + public int index() { + return index; } - - if (!(args[1] instanceof HttpServerExchange)) { - return false; - } - return true; - } + }; } } \ No newline at end of file diff --git a/plugins/websphere/src/main/java/com/navercorp/pinpoint/plugin/websphere/interceptor/WebContainerHandleRequestInterceptor.java b/plugins/websphere/src/main/java/com/navercorp/pinpoint/plugin/websphere/interceptor/WebContainerHandleRequestInterceptor.java index 6d387697e18a..1e0d3f840e79 100644 --- a/plugins/websphere/src/main/java/com/navercorp/pinpoint/plugin/websphere/interceptor/WebContainerHandleRequestInterceptor.java +++ b/plugins/websphere/src/main/java/com/navercorp/pinpoint/plugin/websphere/interceptor/WebContainerHandleRequestInterceptor.java @@ -32,8 +32,8 @@ import com.navercorp.pinpoint.bootstrap.plugin.request.ServletRequestListener; import com.navercorp.pinpoint.bootstrap.plugin.request.ServletRequestListenerBuilder; import com.navercorp.pinpoint.bootstrap.plugin.request.util.ParameterRecorder; -import com.navercorp.pinpoint.plugin.common.servlet.util.ArgumentValidator; -import com.navercorp.pinpoint.plugin.common.servlet.util.ServletArgumentValidator; +import com.navercorp.pinpoint.bootstrap.util.argument.Validation; +import com.navercorp.pinpoint.bootstrap.util.argument.Validator; import com.navercorp.pinpoint.plugin.websphere.ParameterRecorderFactory; import com.navercorp.pinpoint.plugin.websphere.StatusCodeAccessor; import com.navercorp.pinpoint.plugin.websphere.WebsphereConfiguration; @@ -49,12 +49,12 @@ public class WebContainerHandleRequestInterceptor implements AroundInterceptor { private final boolean isDebug = logger.isDebugEnabled(); private final MethodDescriptor methodDescriptor; - private final ArgumentValidator argumentValidator; + private final Validator validator; private final ServletRequestListener servletRequestListener; public WebContainerHandleRequestInterceptor(TraceContext traceContext, MethodDescriptor descriptor, RequestRecorderFactory requestRecorderFactory) { this.methodDescriptor = descriptor; - this.argumentValidator = new ServletArgumentValidator(logger, 0, IRequest.class, 1, IResponse.class); + this.validator = buildValidator(); final WebsphereConfiguration config = new WebsphereConfiguration(traceContext.getProfilerConfig()); RequestAdaptor requestAdaptor = new IRequestAdaptor(); final ParameterRecorder parameterRecorder = ParameterRecorderFactory.newParameterRecorderFactory(config.getExcludeProfileMethodFilter(), config.isTraceRequestParam()); @@ -72,13 +72,20 @@ public WebContainerHandleRequestInterceptor(TraceContext traceContext, MethodDes this.servletRequestListener = builder.build(); } + private Validator buildValidator() { + Validation argBuilder = new Validation(logger); + argBuilder.addArgument(IRequest.class, 0); + argBuilder.addArgument(IResponse.class, 1); + return argBuilder.build(); + } + @Override public void before(Object target, Object[] args) { if (isDebug) { logger.beforeInterceptor(target, args); } - if (!argumentValidator.validate(args)) { + if (!validator.validate(args)) { return; } @@ -96,7 +103,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab logger.afterInterceptor(target, args, result, throwable); } - if (!argumentValidator.validate(args)) { + if (!validator.validate(args)) { return; }