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;
}
});
}
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;
}
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);
}
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());
}
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());
}
Aggregations