Search in sources :

Example 1 with BasicClassicHttpResponse

use of org.apache.hc.core5.http.message.BasicClassicHttpResponse 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 2 with BasicClassicHttpResponse

use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project logbook by zalando.

the class LogbookHttpAsyncResponseConsumerTest method sendAndReceive.

@Override
protected ClassicHttpResponse sendAndReceive(@Nullable final String body) throws ExecutionException, InterruptedException {
    driver.addExpectation(onRequestTo("/"), giveResponse("Hello, world!", "text/plain"));
    SimpleRequestBuilder builder;
    if (body == null) {
        builder = SimpleRequestBuilder.get(driver.getBaseUrl());
    } else {
        builder = SimpleRequestBuilder.post(driver.getBaseUrl()).setBody(body, TEXT_PLAIN).setHeader(HttpHeaders.CONTENT_TYPE, TEXT_PLAIN.toString());
    }
    AtomicReference<String> responseRef = new AtomicReference<>(null);
    CountDownLatch latch = new CountDownLatch(1);
    HttpResponse response = client.execute(SimpleRequestProducer.create(builder.build()), new LogbookHttpAsyncResponseConsumer<>(SimpleResponseConsumer.create()), new FutureCallback<SimpleHttpResponse>() {

        @Override
        public void completed(SimpleHttpResponse result) {
            responseRef.set(result.getBodyText());
            latch.countDown();
        }

        @Override
        public void failed(Exception ex) {
            latch.countDown();
        }

        @Override
        public void cancelled() {
            latch.countDown();
        }
    }).get();
    BasicClassicHttpResponse httpResponse = new BasicClassicHttpResponse(response.getCode(), response.getReasonPhrase());
    latch.await(5, SECONDS);
    String responseBody = responseRef.get();
    if (responseBody != null)
        httpResponse.setEntity(new StringEntity(responseBody));
    return httpResponse;
}
Also used : StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Example 3 with BasicClassicHttpResponse

use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.

the class TestStandardInterceptors method testResponseContentEntityChunkedHTTP10.

@Test
public void testResponseContentEntityChunkedHTTP10() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    context.setProtocolVersion(HttpVersion.HTTP_1_0);
    final BasicClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
    response.setEntity(new BasicHttpEntity(EmptyInputStream.INSTANCE, null, true));
    final ResponseContent interceptor = new ResponseContent();
    interceptor.process(response, response.getEntity(), context);
    final Header h1 = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
    Assertions.assertNull(h1);
    final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
    Assertions.assertNull(h2);
}
Also used : Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicHttpEntity(org.apache.hc.core5.http.io.entity.BasicHttpEntity) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 4 with BasicClassicHttpResponse

use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.

the class TestStandardInterceptors method testResponseContentNoEntity.

@Test
public void testResponseContentNoEntity() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
    final ResponseContent interceptor = new ResponseContent();
    interceptor.process(response, response.getEntity(), context);
    final Header header = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
    Assertions.assertNotNull(header);
    Assertions.assertEquals("0", header.getValue());
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 5 with BasicClassicHttpResponse

use of org.apache.hc.core5.http.message.BasicClassicHttpResponse in project httpcomponents-core by apache.

the class TestStandardInterceptors method testResponseConnControlEntityUnknownContentLengthExplicitKeepAlive.

@Test
public void testResponseConnControlEntityUnknownContentLengthExplicitKeepAlive() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
    context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
    final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
    response.setEntity(new BasicHttpEntity(EmptyInputStream.INSTANCE, null));
    final ResponseConnControl interceptor = new ResponseConnControl();
    interceptor.process(response, response.getEntity(), context);
    final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
    Assertions.assertNotNull(header);
    Assertions.assertEquals("keep-alive", header.getValue());
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicHttpEntity(org.apache.hc.core5.http.io.entity.BasicHttpEntity) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)69 Test (org.junit.jupiter.api.Test)57 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)56 Header (org.apache.hc.core5.http.Header)34 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)29 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)19 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)14 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)13 HttpEntity (org.apache.hc.core5.http.HttpEntity)12 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)11 HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)10 HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)10 BasicHttpEntity (org.apache.hc.core5.http.io.entity.BasicHttpEntity)9 TimeValue (org.apache.hc.core5.util.TimeValue)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 HttpResponse (org.apache.hc.core5.http.HttpResponse)4 Timeout (org.apache.hc.core5.util.Timeout)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3