Search in sources :

Example 16 with Args

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();
}
Also used : SimpleRequestProducer(org.apache.hc.client5.http.async.methods.SimpleRequestProducer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) AtomicReference(java.util.concurrent.atomic.AtomicReference) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback)

Example 17 with Args

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());
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) HttpResponse(com.megaease.easeagent.plugin.tools.trace.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Test(org.junit.Test)

Example 18 with Args

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:");
}
Also used : HttpHost(org.apache.hc.core5.http.HttpHost) HttpClient(org.apache.hc.client5.http.classic.HttpClient)

Example 19 with Args

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());
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpEntity(org.apache.hc.core5.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) BufferedReader(java.io.BufferedReader)

Example 20 with Args

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));
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpEntity(org.apache.hc.core5.http.HttpEntity) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)

Aggregations

HttpHost (org.apache.hc.core5.http.HttpHost)32 HttpRequest (org.apache.hc.core5.http.HttpRequest)25 HttpResponse (org.apache.hc.core5.http.HttpResponse)24 StatusLine (org.apache.hc.core5.http.message.StatusLine)22 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)22 IOException (java.io.IOException)21 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)19 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)19 HttpConnection (org.apache.hc.core5.http.HttpConnection)19 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)17 Header (org.apache.hc.core5.http.Header)17 HttpException (org.apache.hc.core5.http.HttpException)16 CountDownLatch (java.util.concurrent.CountDownLatch)13 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)13 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)12 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)12 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)12 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)12 EntityDetails (org.apache.hc.core5.http.EntityDetails)11 Http1StreamListener (org.apache.hc.core5.http.impl.Http1StreamListener)11