Search in sources :

Example 1 with HttpClientAdapter

use of io.crnk.client.http.apache.HttpClientAdapter in project crnk-framework by crnk-project.

the class BraveModule method setHttpAdapter.

@Override
public void setHttpAdapter(HttpAdapter adapter) {
    if (adapter instanceof OkHttpAdapter) {
        OkHttpAdapter okHttpAdapter = (OkHttpAdapter) adapter;
        okHttpAdapter.addListener(new OkHttpBraveIntegration(brave));
    } else if (adapter instanceof HttpClientAdapter) {
        HttpClientAdapter okHttpAdapter = (HttpClientAdapter) adapter;
        okHttpAdapter.addListener(new HttpClientBraveIntegration(brave, spanNameProvider));
    } else {
        throw new IllegalArgumentException(adapter.getClass() + " not supported yet");
    }
}
Also used : HttpClientAdapter(io.crnk.client.http.apache.HttpClientAdapter) OkHttpAdapter(io.crnk.client.http.okhttp.OkHttpAdapter) OkHttpBraveIntegration(io.crnk.monitor.brave.internal.OkHttpBraveIntegration) HttpClientBraveIntegration(io.crnk.monitor.brave.internal.HttpClientBraveIntegration)

Example 2 with HttpClientAdapter

use of io.crnk.client.http.apache.HttpClientAdapter in project crnk-framework by crnk-project.

the class BraveClientModule method setHttpAdapter.

@Override
public void setHttpAdapter(HttpAdapter adapter) {
    try {
        // load classes lazily since necessary classes may not be on classpath
        if (adapter instanceof OkHttpAdapter) {
            OkHttpAdapter okHttpAdapter = (OkHttpAdapter) adapter;
            Class integrationClass = getClass().getClassLoader().loadClass("io.crnk.monitor.brave.internal.OkHttpBraveIntegration");
            Constructor constructor = integrationClass.getConstructor(HttpTracing.class);
            OkHttpAdapterListener listener = (OkHttpAdapterListener) constructor.newInstance(tracing);
            okHttpAdapter.addListener(listener);
        } else if (adapter instanceof HttpClientAdapter) {
            HttpClientAdapter httpClientAdapter = (HttpClientAdapter) adapter;
            Class integrationClass = getClass().getClassLoader().loadClass("io.crnk.monitor.brave.internal.HttpClientBraveIntegration");
            Constructor constructor = integrationClass.getConstructor(HttpTracing.class);
            HttpClientAdapterListener listener = (HttpClientAdapterListener) constructor.newInstance(tracing);
            httpClientAdapter.addListener(listener);
        } else {
            throw new IllegalArgumentException(adapter.getClass() + " not supported yet");
        }
    } catch (InvocationTargetException | IllegalAccessException | InstantiationException | NoSuchMethodException | ClassNotFoundException e) {
        throw new IllegalStateException("failed to setup brave integration", e);
    }
}
Also used : HttpTracing(brave.http.HttpTracing) Constructor(java.lang.reflect.Constructor) HttpClientAdapterListener(io.crnk.client.http.apache.HttpClientAdapterListener) InvocationTargetException(java.lang.reflect.InvocationTargetException) HttpClientAdapter(io.crnk.client.http.apache.HttpClientAdapter) OkHttpAdapterListener(io.crnk.client.http.okhttp.OkHttpAdapterListener) OkHttpAdapter(io.crnk.client.http.okhttp.OkHttpAdapter)

Example 3 with HttpClientAdapter

use of io.crnk.client.http.apache.HttpClientAdapter in project crnk-framework by crnk-project.

the class CharsetTest method testUTF8isDefault.

public void testUTF8isDefault(boolean okHttp) throws InstantiationException, IllegalAccessException {
    requestContentType = null;
    responseContentType = null;
    if (okHttp) {
        OkHttpAdapter adapter = OkHttpAdapter.newInstance();
        adapter.addListener(new OkHttpAdapterListener() {

            @Override
            public void onBuild(OkHttpClient.Builder builder) {
                builder.addInterceptor(new Interceptor() {

                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        requestContentType = chain.request().header(HttpHeaders.HTTP_CONTENT_TYPE);
                        Response response = chain.proceed(chain.request());
                        responseContentType = response.header(HttpHeaders.HTTP_CONTENT_TYPE);
                        return response;
                    }
                });
            }
        });
        client.setHttpAdapter(adapter);
    } else {
        HttpClientAdapter adapter = HttpClientAdapter.newInstance();
        adapter.addListener(new HttpClientAdapterListener() {

            @Override
            public void onBuild(HttpClientBuilder builder) {
                builder.addInterceptorFirst(new HttpRequestInterceptor() {

                    @Override
                    public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
                        Header header = httpRequest.getFirstHeader(HttpHeaders.HTTP_CONTENT_TYPE);
                        requestContentType = header != null ? header.getValue() : null;
                    }
                });
                builder.addInterceptorFirst(new HttpResponseInterceptor() {

                    @Override
                    public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
                        Header header = httpResponse.getFirstHeader(HttpHeaders.HTTP_CONTENT_TYPE);
                        responseContentType = header != null ? header.getValue() : null;
                    }
                });
            }
        });
        client.setHttpAdapter(adapter);
    }
    ResourceRepositoryV2<Task, Long> testRepo = client.getRepositoryForType(Task.class);
    Task entity = new Task();
    entity.setId(1L);
    entity.setName("äöüé@¢€");
    testRepo.create(entity);
    Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, requestContentType);
    Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, responseContentType);
    Task savedEntity = testRepo.findOne(1L, new QuerySpec(Task.class));
    Assert.assertEquals(entity.getName(), savedEntity.getName());
    Assert.assertNull(requestContentType);
    Assert.assertEquals(HttpHeaders.JSONAPI_CONTENT_TYPE_AND_CHARSET, responseContentType);
}
Also used : HttpRequest(org.apache.http.HttpRequest) Task(io.crnk.test.mock.models.Task) OkHttpClient(okhttp3.OkHttpClient) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpClientAdapterListener(io.crnk.client.http.apache.HttpClientAdapterListener) Response(okhttp3.Response) HttpResponse(org.apache.http.HttpResponse) HttpClientAdapter(io.crnk.client.http.apache.HttpClientAdapter) Header(org.apache.http.Header) OkHttpAdapterListener(io.crnk.client.http.okhttp.OkHttpAdapterListener) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) OkHttpAdapter(io.crnk.client.http.okhttp.OkHttpAdapter) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) QuerySpec(io.crnk.core.queryspec.QuerySpec) Interceptor(okhttp3.Interceptor) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor)

Example 4 with HttpClientAdapter

use of io.crnk.client.http.apache.HttpClientAdapter in project crnk-framework by crnk-project.

the class HttpClientAdapterTest method testCannotSetTimeoutAfterInitialization.

@Test
public void testCannotSetTimeoutAfterInitialization() {
    HttpClientAdapter adapter = new HttpClientAdapter();
    adapter.getImplementation();
    try {
        adapter.setReceiveTimeout(0, TimeUnit.DAYS);
        Assert.fail();
    } catch (IllegalStateException e) {
    // ok
    }
}
Also used : HttpClientAdapter(io.crnk.client.http.apache.HttpClientAdapter) Test(org.junit.Test)

Example 5 with HttpClientAdapter

use of io.crnk.client.http.apache.HttpClientAdapter in project crnk-framework by crnk-project.

the class ApacheHttpClientTest method setupClient.

@Override
protected void setupClient(CrnkClient client) {
    super.setupClient(client);
    HttpClientAdapter adapter = HttpClientAdapter.newInstance();
    adapter.setReceiveTimeout(30000, TimeUnit.MILLISECONDS);
    listener = Mockito.mock(HttpClientAdapterListener.class);
    adapter.addListener(listener);
    adapter.addListener(new HttpClientAdapterListenerBase());
    client.setHttpAdapter(adapter);
}
Also used : HttpClientAdapter(io.crnk.client.http.apache.HttpClientAdapter) HttpClientAdapterListenerBase(io.crnk.client.http.apache.HttpClientAdapterListenerBase) HttpClientAdapterListener(io.crnk.client.http.apache.HttpClientAdapterListener)

Aggregations

HttpClientAdapter (io.crnk.client.http.apache.HttpClientAdapter)7 HttpClientAdapterListener (io.crnk.client.http.apache.HttpClientAdapterListener)4 OkHttpAdapter (io.crnk.client.http.okhttp.OkHttpAdapter)3 Test (org.junit.Test)3 OkHttpAdapterListener (io.crnk.client.http.okhttp.OkHttpAdapterListener)2 HttpTracing (brave.http.HttpTracing)1 AbstractClientTest (io.crnk.client.AbstractClientTest)1 HttpClientAdapterListenerBase (io.crnk.client.http.apache.HttpClientAdapterListenerBase)1 HttpClientAdapterProvider (io.crnk.client.http.apache.HttpClientAdapterProvider)1 QuerySpec (io.crnk.core.queryspec.QuerySpec)1 HttpClientBraveIntegration (io.crnk.monitor.brave.internal.HttpClientBraveIntegration)1 OkHttpBraveIntegration (io.crnk.monitor.brave.internal.OkHttpBraveIntegration)1 Task (io.crnk.test.mock.models.Task)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Interceptor (okhttp3.Interceptor)1 OkHttpClient (okhttp3.OkHttpClient)1 Response (okhttp3.Response)1 Header (org.apache.http.Header)1 HttpRequest (org.apache.http.HttpRequest)1