use of org.apache.hc.core5.util.Args in project easeagent by megaease.
the class HttpClient5AsyncTracingInterceptorTest method runOne.
private static ReportSpan runOne(final Consumer<FutureCallback<HttpResponse>> consumer) throws InterruptedException {
SimpleHttpRequest simpleHttpRequest = SimpleHttpRequest.create("GET", "http://127.0.0.1:8080");
SimpleRequestProducer simpleRequestProducer = SimpleRequestProducer.create(simpleHttpRequest);
FutureCallback<HttpResponse> callback = new FutureCallback<HttpResponse>() {
@Override
public void completed(HttpResponse httpResponse) {
}
@Override
public void failed(Exception e) {
}
@Override
public void cancelled() {
}
};
MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { simpleRequestProducer, null, null, null, callback }).build();
MockEaseAgent.cleanLastSpan();
HttpClient5AsyncTracingInterceptor httpClient5AsyncTracingInterceptor = new HttpClient5AsyncTracingInterceptor();
httpClient5AsyncTracingInterceptor.doBefore(methodInfo, EaseAgent.getContext());
assertNotNull(EaseAgent.getContext().get(HttpClient5AsyncTracingInterceptor.class));
httpClient5AsyncTracingInterceptor.doAfter(methodInfo, EaseAgent.getContext());
assertNull(EaseAgent.getContext().get(HttpClient5AsyncTracingInterceptor.class));
AtomicReference<FutureCallback<HttpResponse>> newCallBack = new AtomicReference<>();
for (Object o : methodInfo.getArgs()) {
if (o instanceof FutureCallback) {
newCallBack.set((FutureCallback<HttpResponse>) o);
}
}
assertNotNull(newCallBack.get());
assertNull(MockEaseAgent.getLastSpan());
Thread thread = new Thread(() -> consumer.accept(newCallBack.get()));
thread.start();
thread.join();
return MockEaseAgent.getLastSpan();
}
use of org.apache.hc.core5.util.Args in project easeagent by megaease.
the class HttpClient5DoExecuteInterceptorTest method getResponse.
@Test
public void getResponse() {
Context context = EaseAgent.getContext();
HttpGet httpGet = new HttpGet("http://127.0.0.1:8080");
BasicHttpResponse basicHttpResponse = new BasicHttpResponse(200);
basicHttpResponse.setHeader("aa", "bb");
MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { httpGet }).retValue(basicHttpResponse).build();
HttpClient5DoExecuteInterceptor httpClientDoExecuteInterceptor = new HttpClient5DoExecuteInterceptor();
HttpResponse httpResponse = httpClientDoExecuteInterceptor.getResponse(methodInfo, context);
assertEquals(200, httpResponse.statusCode());
}
use of org.apache.hc.core5.util.Args in project californium by eclipse.
the class ExampleProxy2HttpClient method main.
public static void main(String[] args) {
HttpClient client = HttpClientBuilder.create().build();
// simple request to proxy as httpp-server (no proxy function)
request(client, "http://localhost:8080");
request(client, "http://localhost:8080/proxy/coap://localhost:5685/coap-target");
// keep the "coap://" after normalize the URI requires to use %2f%2f
// instead of //
request(client, "http://localhost:8080/proxy/coap:%2f%2flocalhost:5685/coap-target");
request(client, "http://localhost:8080/proxy?target_uri=coap://localhost:5685/coap-target");
// not really intended, http2http
request(client, "http://localhost:8080/proxy/http:%2f%2flocalhost:8000/http-target");
// request to local (in same process) coap-server
request(client, "http://localhost:8080/local/target");
// http-request via proxy
HttpHost proxy = new HttpHost("http", "localhost", 8080);
client = HttpClientBuilder.create().setProxy(proxy).build();
request(client, "http://localhost:5685/coap-target/coap:");
request(client, "http://californium.eclipseprojects.io:5683/test/coap:");
}
use of org.apache.hc.core5.util.Args in project eomcs-java by eomcs.
the class Exam0110 method main.
public static void main(String[] args) throws Exception {
// => HTTP 요청을 수행할 객체를 준비한다.
CloseableHttpClient httpClient = HttpClients.createDefault();
// => HTTP GET 요청 정보를 준비한다.
HttpGet get = new HttpGet("https://www.daum.net");
// => HttpClient 객체를 사용하여 GET 요청을 실행한다.
// => 리턴 값은 웹 서버의 응답 데이터를 다루는 도구이다.
CloseableHttpResponse response = httpClient.execute(get);
// => 응답 도구를 이용하여 서버가 보낸 데이터를 꺼낸다.
HttpEntity entity = response.getEntity();
if (entity != null) {
// => HttpEntity 객체에 들어 있는 값을 문자열로 변환하여 출력한다.
System.out.printf("응답 데이터 크기 => %d\n", entity.getContentLength());
System.out.printf("응답 데이터의 타입 => %s\n", entity.getContentType());
System.out.println("---------------------------------");
// HttpEntity에 들어 있는 서버 응답 데이터를 꺼내려면 getContent()를 사용해야 한다.
// getContent()의 리턴 값은 InputStream 객체이다.
// InputStream을 가지고 데이터를 읽으려면 입출력 코딩을 작성해야 한다.
BufferedReader in = new //
BufferedReader(new InputStreamReader(entity.getContent()));
StringBuilder strBuilder = new StringBuilder();
while (true) {
String line = in.readLine();
if (line == null) {
break;
}
strBuilder.append(line + "\n");
}
in.close();
System.out.println(strBuilder.toString());
}
}
use of org.apache.hc.core5.util.Args in project eomcs-java by eomcs.
the class Exam0120 method main.
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet("https://www.daum.net");
CloseableHttpResponse response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.printf("응답 데이터 크기 => %d\n", entity.getContentLength());
System.out.printf("응답 데이터의 타입 => %s\n", entity.getContentType());
System.out.println("---------------------------------");
// HttpEntity에서 응답 데이터를 꺼낼 때 도우미 클래스를 사용하면 편하다.
System.out.println(EntityUtils.toString(entity));
}
}
Aggregations