Search in sources :

Example 31 with MatchConfig

use of com.pamirs.pradar.internal.config.MatchConfig in project LinkAgent by shulieTech.

the class RealCallExecuteV2Interceptor 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);
    }
    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 {
                byte[] bytes = null;
                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 32 with MatchConfig

use of com.pamirs.pradar.internal.config.MatchConfig in project LinkAgent by shulieTech.

the class RequestBuilderBuildMethodV2Interceptor method beforeFirst.

@Override
public void beforeFirst(Advice advice) throws ProcessControlException {
    ClusterTestUtils.validateClusterTest();
    if (Pradar.isClusterTest()) {
        Object target = advice.getTarget();
        Request.Builder builder = (Request.Builder) target;
        Object httpUrl = Reflect.on(builder).get(OKHttpConstants.DYNAMIC_FIELD_URL);
        String url = null;
        // 2的低版本
        if (httpUrl instanceof URL) {
            if (httpUrl == null) {
                url = Reflect.on(builder).get(OKHttpConstants.DYNAMIC_FIELD_URL_STRING);
            } else {
                url = getService(((URL) httpUrl).getProtocol(), ((URL) httpUrl).getHost(), ((URL) httpUrl).getPort(), ((URL) httpUrl).getPath());
            }
        } else {
            // 2的高版本
            if (httpUrl == null) {
                url = Reflect.on(builder).get(OKHttpConstants.DYNAMIC_FIELD_URL_STRING);
            } else {
                url = getService(((HttpUrl) httpUrl).scheme(), ((HttpUrl) httpUrl).host(), ((HttpUrl) httpUrl).port(), ((HttpUrl) httpUrl).encodedPath());
            }
        }
        MatchConfig config = ClusterTestUtils.httpClusterTest(url);
        Headers.Builder header = Reflect.on(builder).get(OKHttpConstants.DYNAMIC_FIELD_HEADER);
        String check = header.get(PradarService.PRADAR_WHITE_LIST_CHECK);
        config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, check);
        config.addArgs("url", url);
        config.addArgs("isInterface", Boolean.FALSE);
        config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config);
    }
}
Also used : Headers(com.squareup.okhttp.Headers) MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) Request(com.squareup.okhttp.Request) URL(java.net.URL) HttpUrl(com.squareup.okhttp.HttpUrl)

Example 33 with MatchConfig

use of com.pamirs.pradar.internal.config.MatchConfig in project LinkAgent by shulieTech.

the class RealCallEnqueueV3Interceptor method beforeFirst.

@Override
public void beforeFirst(Advice advice) throws ProcessControlException {
    Object target = advice.getTarget();
    final Call call = (Call) target;
    HttpUrl httpUrl = call.request().url();
    String url = OKHttpConstants.getService(httpUrl.scheme(), httpUrl.host(), httpUrl.port(), httpUrl.encodedPath());
    final MatchConfig config = ClusterTestUtils.httpClusterTest(url);
    String check = call.request().header(OKHttpConstants.DYNAMIC_FIELD_HEADER);
    config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, check);
    config.addArgs("url", url);
    config.addArgs("isInterface", Boolean.FALSE);
    if (config.getStrategy() instanceof JsonMockStrategy) {
        config.addArgs("call", call);
        MockReturnUtils.fixJsonStrategy.processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config);
    }
    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(call.request()).set("url", httpUrl);
            return null;
        }

        @Override
        public Object call(Object param) {
            Headers header = new Headers.Builder().build();
            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(call.request()).protocol(Protocol.HTTP_1_0).message("OK").build();
        }
    });
}
Also used : Buffer(okio.Buffer) ExecutionForwardCall(com.pamirs.pradar.internal.adapter.ExecutionForwardCall) ProcessControlException(com.shulie.instrument.simulator.api.ProcessControlException) MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) RealResponseBody(okhttp3.internal.http.RealResponseBody) IOException(java.io.IOException) JsonMockStrategy(com.pamirs.pradar.pressurement.mock.JsonMockStrategy) ExecutionForwardCall(com.pamirs.pradar.internal.adapter.ExecutionForwardCall) JSONObject(com.alibaba.fastjson.JSONObject)

Aggregations

MatchConfig (com.pamirs.pradar.internal.config.MatchConfig)33 ProcessControlException (com.shulie.instrument.simulator.api.ProcessControlException)22 JSONObject (com.alibaba.fastjson.JSONObject)15 ExecutionCall (com.pamirs.pradar.internal.config.ExecutionCall)12 JsonMockStrategy (com.pamirs.pradar.pressurement.mock.JsonMockStrategy)12 SocketTimeoutException (java.net.SocketTimeoutException)10 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)8 IOException (java.io.IOException)8 ExecutionForwardCall (com.pamirs.pradar.internal.adapter.ExecutionForwardCall)7 URISyntaxException (java.net.URISyntaxException)6 Map (java.util.Map)6 URI (java.net.URI)5 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 URL (java.net.URL)4 Buffer (okio.Buffer)4 ScriptEvaluator (com.pamirs.pradar.script.ScriptEvaluator)3 ReflectException (com.shulie.instrument.simulator.api.reflect.ReflectException)3