Search in sources :

Example 6 with ProcessControlException

use of com.shulie.instrument.simulator.api.ProcessControlException in project LinkAgent by shulieTech.

the class HttpClientv3MethodInterceptor method beforeLast.

@Override
public void beforeLast(Advice advice) throws ProcessControlException {
    Object[] args = advice.getParameterArray();
    try {
        final HttpMethod method = (HttpMethod) args[1];
        if (method == null) {
            return;
        }
        int port = method.getURI().getPort();
        String path = method.getURI().getPath();
        String url = getService(method.getURI().getScheme(), method.getURI().getHost(), port, path);
        final MatchConfig config = ClusterTestUtils.httpClusterTest(url);
        Header header = method.getRequestHeader(PradarService.PRADAR_WHITE_LIST_CHECK);
        if (header == null) {
            config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, true);
        } else {
            config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, header.getValue());
        }
        config.addArgs("url", url);
        config.addArgs("isInterface", Boolean.FALSE);
        if (config.getStrategy() instanceof JsonMockStrategy) {
            config.addArgs("extraMethod", method);
            fixJsonStrategy.processBlock(java.lang.String.class, advice.getClassLoader(), config);
        }
        config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionForwardCall() {

            @Override
            public Object call(Object param) throws ProcessControlException {
                byte[] bytes = JSONObject.toJSONBytes(param);
                Reflect.on(method).set("responseBody", bytes);
                ProcessController.returnImmediately(int.class, 200);
                return true;
            }

            @Override
            public Object forward(Object param) throws ProcessControlException {
                String forwarding = config.getForwarding();
                try {
                    method.setURI(new URI(forwarding));
                } catch (URIException e) {
                }
                return null;
            }
        });
    } catch (URIException e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError(e);
        }
    } catch (ProcessControlException pce) {
        throw pce;
    } catch (Throwable t) {
        LOGGER.error("", t);
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError(t);
        }
    }
}
Also used : ProcessControlException(com.shulie.instrument.simulator.api.ProcessControlException) MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) URI(org.apache.commons.httpclient.URI) JsonMockStrategy(com.pamirs.pradar.pressurement.mock.JsonMockStrategy) ExecutionForwardCall(com.pamirs.pradar.internal.adapter.ExecutionForwardCall) URIException(org.apache.commons.httpclient.URIException) Header(org.apache.commons.httpclient.Header) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) JSONObject(com.alibaba.fastjson.JSONObject) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 7 with ProcessControlException

use of com.shulie.instrument.simulator.api.ProcessControlException in project LinkAgent by shulieTech.

the class HttpClientv4MethodInterceptor1 method beforeLast.

@Override
public void beforeLast(Advice advice) throws ProcessControlException {
    if (!Pradar.isClusterTest()) {
        return;
    }
    Object[] args = advice.getParameterArray();
    final HttpUriRequest request = (HttpUriRequest) args[0];
    if (request == null) {
        return;
    }
    String host = request.getURI().getHost();
    int port = request.getURI().getPort();
    String path = request.getURI().getPath();
    // 判断是否在白名单中
    String url = getService(request.getURI().getScheme(), host, port, path);
    MatchConfig config = ClusterTestUtils.httpClusterTest(url);
    Header[] headers = request.getHeaders(PradarService.PRADAR_WHITE_LIST_CHECK);
    if (headers.length > 0) {
        config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, headers[0].getValue());
    }
    config.addArgs("url", url);
    config.addArgs("request", request);
    config.addArgs("method", "uri");
    config.addArgs("isInterface", Boolean.FALSE);
    if (config.getStrategy() instanceof JsonMockStrategy) {
        fixJsonStrategy.processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config);
    }
    config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionCall() {

        @Override
        public Object call(Object param) {
            StatusLine statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "");
            try {
                HttpEntity entity = null;
                if (param instanceof String) {
                    entity = new StringEntity(String.valueOf(param), "UTF-8");
                } else {
                    entity = new ByteArrayEntity(JSONObject.toJSONBytes(param));
                }
                BasicHttpResponse response = new BasicHttpResponse(statusline);
                response.setEntity(entity);
                if (HttpClientConstants.clazz == null) {
                    HttpClientConstants.clazz = Class.forName("org.apache.http.impl.execchain.HttpResponseProxy");
                }
                return Reflect.on(HttpClientConstants.clazz).create(response, null).get();
            } catch (Exception e) {
            }
            return null;
        }
    });
}
Also used : MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) SocketTimeoutException(java.net.SocketTimeoutException) ProcessControlException(com.shulie.instrument.simulator.api.ProcessControlException) IOException(java.io.IOException) BasicStatusLine(org.apache.http.message.BasicStatusLine) JsonMockStrategy(com.pamirs.pradar.pressurement.mock.JsonMockStrategy) BasicStatusLine(org.apache.http.message.BasicStatusLine) StringEntity(org.apache.http.entity.StringEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) JSONObject(com.alibaba.fastjson.JSONObject) ExecutionCall(com.pamirs.pradar.internal.config.ExecutionCall)

Example 8 with ProcessControlException

use of com.shulie.instrument.simulator.api.ProcessControlException in project LinkAgent by shulieTech.

the class HttpClientv5MethodInterceptor1 method beforeLast.

@Override
public void beforeLast(Advice advice) throws ProcessControlException {
    Object[] args = advice.getParameterArray();
    // ClassicHttpRequest
    final ClassicHttpRequest request = (ClassicHttpRequest) args[0];
    if (request == null) {
        return;
    }
    URI uri = null;
    try {
        uri = request.getUri();
    } catch (URISyntaxException e) {
        logger.error("获取不到url", e);
    }
    String host = uri.getHost();
    int port = uri.getPort();
    String path = uri.getPath();
    // 判断是否在白名单中
    String url = getService(uri.getScheme(), host, port, path);
    MatchConfig config = ClusterTestUtils.httpClusterTest(url);
    Header[] headers = request.getHeaders(PradarService.PRADAR_WHITE_LIST_CHECK);
    config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, headers[0].getValue());
    config.addArgs("url", url);
    config.addArgs("request", request);
    config.addArgs("method", "uri");
    config.addArgs("isInterface", Boolean.FALSE);
    config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionCall() {

        @Override
        public Object call(Object param) {
            try {
                HttpEntity entity = null;
                if (param instanceof String) {
                    entity = new StringEntity(String.valueOf(param));
                } else {
                    entity = new ByteArrayEntity(JSONObject.toJSONBytes(param), ContentType.create(request.getEntity().getContentType()));
                }
                BasicClassicHttpResponse response = new BasicClassicHttpResponse(200);
                response.setEntity(entity);
                if (HttpClientConstants.clazz == null) {
                    HttpClientConstants.clazz = Class.forName("org.apache.hc.client5.http.impl.classic.CloseableHttpResponse");
                }
                return Reflect.on(HttpClientConstants.clazz).create(response, null).get();
            } catch (Exception e) {
            }
            return null;
        }
    });
}
Also used : MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) SocketTimeoutException(java.net.SocketTimeoutException) ProcessControlException(com.shulie.instrument.simulator.api.ProcessControlException) IOException(java.io.IOException) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) JSONObject(com.alibaba.fastjson.JSONObject) ExecutionCall(com.pamirs.pradar.internal.config.ExecutionCall) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Example 9 with ProcessControlException

use of com.shulie.instrument.simulator.api.ProcessControlException in project LinkAgent by shulieTech.

the class DubboConsumerInterceptor method beforeLast.

@Override
public void beforeLast(Advice advice) throws ProcessControlException {
    final RpcInvocation invocation = (RpcInvocation) advice.getParameterArray()[0];
    String interfaceName = getInterfaceName(invocation);
    String methodName = invocation.getMethodName();
    MatchConfig config = ClusterTestUtils.rpcClusterTest(interfaceName, methodName);
    invocation.setAttachment(PradarService.PRADAR_WHITE_LIST_CHECK, String.valueOf(config.isSuccess()));
    config.addArgs("args", advice.getParameterArray());
    config.addArgs("url", interfaceName.concat("#").concat(methodName));
    config.addArgs("isInterface", Boolean.TRUE);
    config.addArgs("class", interfaceName);
    config.addArgs("method", methodName);
    config.addArgs("invocation", invocation);
    if (isShentongEvent(interfaceName)) {
        config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, "true");
    }
    if (config.getStrategy() instanceof JsonMockStrategy) {
        fixJsonStrategy.processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config);
    }
    config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionCall() {

        @Override
        public Object call(Object param) {
            try {
                // for 2.8.4
                return Reflect.on("org.apache.dubbo.rpc.RpcResult").create(param).get();
            } catch (Exception e) {
                if (logger.isInfoEnabled()) {
                    logger.info("find dubbo 2.8.4 class org.apache.dubbo.rpc.RpcResult fail, find others!", e);
                }
            // 
            }
            try {
                Reflect reflect = Reflect.on("org.apache.dubbo.rpc.AsyncRpcResult");
                try {
                    // for 2.7.5
                    java.util.concurrent.CompletableFuture<AppResponse> future = new java.util.concurrent.CompletableFuture<AppResponse>();
                    future.complete(new AppResponse(param));
                    Reflect result = reflect.create(future, invocation);
                    return result.get();
                } catch (ReflectException e) {
                    // for 2.7.3
                    Reflect result = reflect.create(invocation);
                    return result.get();
                }
            } catch (Exception e) {
                logger.error("fail to load dubbo 2.7.x class org.apache.dubbo.rpc.AsyncRpcResult", e);
                throw new ReflectException("fail to load dubbo 2.7.x class org.apache.dubbo.rpc.AsyncRpcResult", e);
            }
        }
    });
}
Also used : MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) ReflectException(com.shulie.instrument.simulator.api.reflect.ReflectException) PradarException(com.pamirs.pradar.exception.PradarException) ReflectException(com.shulie.instrument.simulator.api.reflect.ReflectException) ProcessControlException(com.shulie.instrument.simulator.api.ProcessControlException) JsonMockStrategy(com.pamirs.pradar.pressurement.mock.JsonMockStrategy) Reflect(com.shulie.instrument.simulator.api.reflect.Reflect) ExecutionCall(com.pamirs.pradar.internal.config.ExecutionCall)

Example 10 with ProcessControlException

use of com.shulie.instrument.simulator.api.ProcessControlException in project LinkAgent by shulieTech.

the class MockStrategy method processNonBlock.

@Override
public Object processNonBlock(Class returnType, ClassLoader classLoader, Object params, ExecutionCall call) throws ProcessControlException {
    if (Pradar.isClusterTest()) {
        if (params instanceof MatchConfig) {
            try {
                MatchConfig config = (MatchConfig) params;
                String scriptContent = config.getScriptContent();
                ScriptEvaluator evaluator = ScriptManager.getInstance().getScriptEvaluator("bsh");
                Object result = evaluator.evaluate(scriptContent, config.getArgs());
                return call.call(result);
            } catch (ProcessControlException e) {
                throw e;
            } catch (Throwable e) {
                LOGGER.error("mock处理异常 {}", e);
                ErrorReporter.buildError().setErrorType(ErrorTypeEnum.mock).setErrorCode("mock-0001").setMessage("mock处理异常!" + e.getMessage()).setDetail("脚本内容" + ((MatchConfig) params).getScriptContent()).report();
                throw new PressureMeasureError(e);
            }
        }
    }
    return true;
}
Also used : ScriptEvaluator(com.pamirs.pradar.script.ScriptEvaluator) ProcessControlException(com.shulie.instrument.simulator.api.ProcessControlException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) MatchConfig(com.pamirs.pradar.internal.config.MatchConfig)

Aggregations

ProcessControlException (com.shulie.instrument.simulator.api.ProcessControlException)23 MatchConfig (com.pamirs.pradar.internal.config.MatchConfig)21 JSONObject (com.alibaba.fastjson.JSONObject)15 ExecutionCall (com.pamirs.pradar.internal.config.ExecutionCall)12 SocketTimeoutException (java.net.SocketTimeoutException)10 JsonMockStrategy (com.pamirs.pradar.pressurement.mock.JsonMockStrategy)9 IOException (java.io.IOException)8 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)7 ExecutionForwardCall (com.pamirs.pradar.internal.adapter.ExecutionForwardCall)6 Map (java.util.Map)6 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)5 StringEntity (org.apache.http.entity.StringEntity)5 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)5 BasicStatusLine (org.apache.http.message.BasicStatusLine)5 URISyntaxException (java.net.URISyntaxException)4 Buffer (okio.Buffer)4 Header (org.apache.hc.core5.http.Header)4 ScriptEvaluator (com.pamirs.pradar.script.ScriptEvaluator)3 ReflectException (com.shulie.instrument.simulator.api.reflect.ReflectException)3 URI (java.net.URI)3