Search in sources :

Example 11 with JsonMockStrategy

use of com.pamirs.pradar.pressurement.mock.JsonMockStrategy in project LinkAgent by shulieTech.

the class RealCallExecuteV3Interceptor method beforeFirst.

@Override
public void beforeFirst(Advice advice) throws ProcessControlException {
    if (!Pradar.isClusterTest()) {
        return;
    }
    Object target = advice.getTarget();
    final Call call = (Call) target;
    final 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(PradarService.PRADAR_WHITE_LIST_CHECK);
    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 {
            String configUrl = config.getForwarding();
            if (httpUrl.queryParameterNames().size() > 0) {
                StringBuilder builder = new StringBuilder(128).append(configUrl).append("?");
                boolean first = true;
                for (String queryParameterName : httpUrl.queryParameterNames()) {
                    if (!first) {
                        builder.append("&");
                    }
                    builder.append(queryParameterName).append("=").append(httpUrl.queryParameter(queryParameterName));
                    first = false;
                }
                configUrl = builder.toString();
            }
            HttpUrl forwardHttpUrl = HttpUrl.parse(configUrl);
            Reflect.on(call.request()).set("url", forwardHttpUrl);
            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)

Example 12 with JsonMockStrategy

use of com.pamirs.pradar.pressurement.mock.JsonMockStrategy 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)12 JsonMockStrategy (com.pamirs.pradar.pressurement.mock.JsonMockStrategy)12 ProcessControlException (com.shulie.instrument.simulator.api.ProcessControlException)9 JSONObject (com.alibaba.fastjson.JSONObject)8 ExecutionCall (com.pamirs.pradar.internal.config.ExecutionCall)6 SocketTimeoutException (java.net.SocketTimeoutException)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 IOException (java.io.IOException)4 ExecutionForwardCall (com.pamirs.pradar.internal.adapter.ExecutionForwardCall)3 Map (java.util.Map)3 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)3 FutureCallback (org.apache.http.concurrent.FutureCallback)3 ExecutionStrategy (com.pamirs.pradar.internal.adapter.ExecutionStrategy)2 MockStrategy (com.pamirs.pradar.pressurement.mock.MockStrategy)2 HttpURLConnection (java.net.HttpURLConnection)2 URL (java.net.URL)2 RealResponseBody (okhttp3.internal.http.RealResponseBody)2