Search in sources :

Example 1 with ExecutionForwardCall

use of com.pamirs.pradar.internal.adapter.ExecutionForwardCall in project LinkAgent by shulieTech.

the class RealCallEnqueueV2Interceptor method beforeFirst.

@Override
public void beforeFirst(Advice advice) throws ProcessControlException {
    Object target = advice.getTarget();
    Request request = null;
    try {
        request = Reflect.on(target).get(OKHttpConstants.DYNAMIC_FIELD_REQUEST);
    } catch (ReflectException e) {
        request = Reflect.on(target).get(OKHttpConstants.DYNAMIC_FIELD_ORIGINAL_REQUEST);
    }
    final String url = request.urlString();
    final MatchConfig config = ClusterTestUtils.httpClusterTest(url);
    String check = request.header(OKHttpConstants.DYNAMIC_FIELD_HEADER);
    config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, check);
    config.addArgs("url", url);
    config.addArgs("isInterface", Boolean.FALSE);
    final Request finalRequest = request;
    config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionForwardCall() {

        @Override
        public Object forward(Object param) throws ProcessControlException {
            HttpUrl httpUrl = HttpUrl.parse(config.getForwarding());
            Reflect.on(finalRequest).set("url", httpUrl);
            return null;
        }

        @Override
        public Object call(Object param) {
            Headers header = Headers.of(new String[0]);
            Buffer buffer = new Buffer();
            try {
                if (param instanceof String) {
                    buffer.write(String.valueOf(param).getBytes("UTF-8"));
                } else {
                    buffer.write(JSONObject.toJSONBytes(param));
                }
            } catch (IOException e) {
            }
            return new Response.Builder().code(200).body(new RealResponseBody(header, buffer)).request(finalRequest).protocol(Protocol.HTTP_1_0).message("OK").build();
        }
    });
}
Also used : Buffer(okio.Buffer) ProcessControlException(com.shulie.instrument.simulator.api.ProcessControlException) Headers(com.squareup.okhttp.Headers) MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) RealResponseBody(com.squareup.okhttp.internal.http.RealResponseBody) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) ReflectException(com.shulie.instrument.simulator.api.reflect.ReflectException) HttpUrl(okhttp3.HttpUrl) ExecutionForwardCall(com.pamirs.pradar.internal.adapter.ExecutionForwardCall) Response(com.squareup.okhttp.Response) JSONObject(com.alibaba.fastjson.JSONObject)

Example 2 with ExecutionForwardCall

use of com.pamirs.pradar.internal.adapter.ExecutionForwardCall 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 3 with ExecutionForwardCall

use of com.pamirs.pradar.internal.adapter.ExecutionForwardCall in project LinkAgent by shulieTech.

the class ClientInterceptor method beforeFirst.

@Override
public void beforeFirst(Advice advice) throws ProcessControlException, MalformedURLException {
    Object[] args = advice.getParameterArray();
    if (!(args[0] instanceof ClientRequest)) {
        return;
    }
    final ClientRequest clientRequest = (ClientRequest) args[0];
    final String url = clientRequest.getUri().toURL().toString();
    final MatchConfig config = ClusterTestUtils.httpClusterTest(url);
    config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, clientRequest.getHeaderString(PradarService.PRADAR_WHITE_LIST_CHECK));
    config.addArgs("url", url);
    config.addArgs("isInterface", Boolean.FALSE);
    config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionForwardCall() {

        @Override
        public Object forward(Object param) {
            clientRequest.setUri(URI.create(config.getForwarding()));
            return null;
        }

        @Override
        public Object call(Object param) {
            return new ClientResponse(clientRequest, Response.ok(param).build());
        }
    });
}
Also used : ExecutionForwardCall(com.pamirs.pradar.internal.adapter.ExecutionForwardCall) ClientResponse(org.glassfish.jersey.client.ClientResponse) MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) ClientRequest(org.glassfish.jersey.client.ClientRequest)

Example 4 with ExecutionForwardCall

use of com.pamirs.pradar.internal.adapter.ExecutionForwardCall in project LinkAgent by shulieTech.

the class HttpRequestExecuteMethodInterceptor method afterFirst.

@Override
public void afterFirst(Advice advice) throws ProcessControlException {
    Object target = advice.getTarget();
    final HttpRequest request = (HttpRequest) target;
    String url = request.getUrl().build();
    String whiteList = request.getHeaders().getFirstHeaderStringValue(PradarService.PRADAR_WHITE_LIST_CHECK);
    final MatchConfig config = ClusterTestUtils.httpClusterTest(url);
    config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, whiteList);
    config.addArgs("url", url);
    config.addArgs("request", request);
    config.addArgs("method", url);
    config.addArgs("isInterface", Boolean.FALSE);
    config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionForwardCall() {

        @Override
        public Object forward(Object param) throws ProcessControlException {
            GenericUrl url1 = new GenericUrl(config.getForwarding());
            Reflect.on(request).set("url", url1);
            return null;
        }

        @Override
        public Object call(Object param) throws ProcessControlException {
            return param;
        }
    });
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) ExecutionForwardCall(com.pamirs.pradar.internal.adapter.ExecutionForwardCall) ProcessControlException(com.shulie.instrument.simulator.api.ProcessControlException) MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) GenericUrl(com.google.api.client.http.GenericUrl)

Example 5 with ExecutionForwardCall

use of com.pamirs.pradar.internal.adapter.ExecutionForwardCall in project LinkAgent by shulieTech.

the class ForwardStrategy method processBlock.

@Override
public Object processBlock(Class returnType, ClassLoader classLoader, Object params, ExecutionCall call) throws ProcessControlException {
    if (call instanceof ExecutionForwardCall) {
        ExecutionForwardCall forwardCall = (ExecutionForwardCall) call;
        Object block = processBlock(returnType, classLoader, params);
        return forwardCall.forward(block);
    }
    return processBlock(returnType, classLoader, params);
}
Also used : ExecutionForwardCall(com.pamirs.pradar.internal.adapter.ExecutionForwardCall)

Aggregations

ExecutionForwardCall (com.pamirs.pradar.internal.adapter.ExecutionForwardCall)9 MatchConfig (com.pamirs.pradar.internal.config.MatchConfig)7 ProcessControlException (com.shulie.instrument.simulator.api.ProcessControlException)6 JSONObject (com.alibaba.fastjson.JSONObject)5 IOException (java.io.IOException)4 Buffer (okio.Buffer)4 JsonMockStrategy (com.pamirs.pradar.pressurement.mock.JsonMockStrategy)3 ReflectException (com.shulie.instrument.simulator.api.reflect.ReflectException)2 Headers (com.squareup.okhttp.Headers)2 Request (com.squareup.okhttp.Request)2 Response (com.squareup.okhttp.Response)2 RealResponseBody (com.squareup.okhttp.internal.http.RealResponseBody)2 HttpUrl (okhttp3.HttpUrl)2 RealResponseBody (okhttp3.internal.http.RealResponseBody)2 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpRequest (com.google.api.client.http.HttpRequest)1 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)1 Header (org.apache.commons.httpclient.Header)1 HttpMethod (org.apache.commons.httpclient.HttpMethod)1 URI (org.apache.commons.httpclient.URI)1