Search in sources :

Example 1 with Header

use of org.apache.hc.core5.http.Header in project retrofit by square.

the class RequestBuilderTest method malformedContentTypeParameterThrows.

@Test
public void malformedContentTypeParameterThrows() {
    class Example {

        // 
        @POST("/")
        Call<ResponseBody> method(@Header("Content-Type") String contentType, @Body RequestBody body) {
            return null;
        }
    }
    RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
    try {
        buildRequest(Example.class, "hello, world!", body);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("Malformed content type: hello, world!");
    }
}
Also used : Header(retrofit2.http.Header) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) MultipartBody(okhttp3.MultipartBody) Body(retrofit2.http.Body) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 2 with Header

use of org.apache.hc.core5.http.Header in project retrofit by square.

the class RequestBuilderTest method headerParamToString.

@Test
public void headerParamToString() {
    class Example {

        // 
        @GET("/foo/bar/")
        Call<ResponseBody> method(@Header("kit") BigInteger kit) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, new BigInteger("1234"));
    assertThat(request.method()).isEqualTo("GET");
    okhttp3.Headers headers = request.headers();
    assertThat(headers.size()).isEqualTo(1);
    assertThat(headers.get("kit")).isEqualTo("1234");
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
    assertThat(request.body()).isNull();
}
Also used : Header(retrofit2.http.Header) Request(okhttp3.Request) BigInteger(java.math.BigInteger) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 3 with Header

use of org.apache.hc.core5.http.Header in project retrofit by square.

the class RequestBuilderTest method headerParam.

@Test
public void headerParam() {
    class Example {

        // 
        @GET("/foo/bar/")
        // 
        @Headers("ping: pong")
        Call<ResponseBody> method(@Header("kit") String kit) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "kat");
    assertThat(request.method()).isEqualTo("GET");
    okhttp3.Headers headers = request.headers();
    assertThat(headers.size()).isEqualTo(2);
    assertThat(headers.get("ping")).isEqualTo("pong");
    assertThat(headers.get("kit")).isEqualTo("kat");
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
    assertThat(request.body()).isNull();
}
Also used : Header(retrofit2.http.Header) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 4 with Header

use of org.apache.hc.core5.http.Header in project feign by OpenFeign.

the class AsyncApacheHttp5Client method toFeignResponse.

Response toFeignResponse(SimpleHttpResponse httpResponse, Request request) {
    final int statusCode = httpResponse.getCode();
    final String reason = httpResponse.getReasonPhrase();
    final Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>();
    for (final Header header : httpResponse.getHeaders()) {
        final String name = header.getName();
        final String value = header.getValue();
        Collection<String> headerValues = headers.get(name);
        if (headerValues == null) {
            headerValues = new ArrayList<String>();
            headers.put(name, headerValues);
        }
        headerValues.add(value);
    }
    return Response.builder().status(statusCode).reason(reason).headers(headers).request(request).body(httpResponse.getBodyBytes()).build();
}
Also used : Header(org.apache.hc.core5.http.Header)

Example 5 with Header

use of org.apache.hc.core5.http.Header in project cxf by apache.

the class AsyncHTTPConduit method setupConnection.

@Override
protected void setupConnection(Message message, Address address, HTTPClientPolicy csPolicy) throws IOException {
    if (factory.isShutdown()) {
        message.put(USE_ASYNC, Boolean.FALSE);
        super.setupConnection(message, address, csPolicy);
        return;
    }
    propagateJaxwsSpecTimeoutSettings(message, csPolicy);
    boolean addressChanged = false;
    // need to do some clean up work on the URI address
    URI uri = address.getURI();
    String uriString = uri.toString();
    if (uriString.startsWith("hc://")) {
        uriString = uriString.substring(5);
        addressChanged = true;
    } else if (uriString.startsWith("hc5://")) {
        uriString = uriString.substring(6);
        addressChanged = true;
    }
    if (addressChanged) {
        try {
            uri = new URI(uriString);
        } catch (URISyntaxException ex) {
            throw new MalformedURLException("unsupport uri: " + uriString);
        }
    }
    String s = uri.getScheme();
    if (!"http".equals(s) && !"https".equals(s)) {
        throw new MalformedURLException("unknown protocol: " + s);
    }
    Object o = message.getContextualProperty(USE_ASYNC);
    if (o == null) {
        o = factory.getUseAsyncPolicy();
    }
    switch(UseAsyncPolicy.getPolicy(o)) {
        case ALWAYS:
            o = true;
            break;
        case NEVER:
            o = false;
            break;
        case ASYNC_ONLY:
        default:
            o = !message.getExchange().isSynchronous();
            break;
    }
    // check tlsClientParameters from message header
    TLSClientParameters clientParameters = message.get(TLSClientParameters.class);
    if (clientParameters == null) {
        clientParameters = tlsClientParameters;
    }
    if ("https".equals(uri.getScheme()) && clientParameters != null && clientParameters.getSSLSocketFactory() != null) {
        // if they configured in an SSLSocketFactory, we cannot do anything
        // with it as the NIO based transport cannot use socket created from
        // the SSLSocketFactory.
        o = false;
    }
    if (!PropertyUtils.isTrue(o)) {
        message.put(USE_ASYNC, Boolean.FALSE);
        super.setupConnection(message, addressChanged ? new Address(uriString, uri) : address, csPolicy);
        return;
    }
    if (StringUtils.isEmpty(uri.getPath())) {
        // hc needs to have the path be "/"
        uri = uri.resolve("/");
    }
    message.put(USE_ASYNC, Boolean.TRUE);
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Asynchronous connection to " + uri.toString() + " has been set up");
    }
    message.put("http.scheme", uri.getScheme());
    String httpRequestMethod = (String) message.get(Message.HTTP_REQUEST_METHOD);
    if (httpRequestMethod == null) {
        httpRequestMethod = "POST";
        message.put(Message.HTTP_REQUEST_METHOD, httpRequestMethod);
    }
    final CXFHttpRequest e = new CXFHttpRequest(httpRequestMethod, uri);
    final String contentType = (String) message.get(Message.CONTENT_TYPE);
    final MutableHttpEntity entity = new MutableHttpEntity(contentType, null, true) {

        public boolean isRepeatable() {
            return e.getOutputStream().retransmitable();
        }
    };
    e.setEntity(entity);
    final RequestConfig.Builder b = RequestConfig.custom().setConnectTimeout(Timeout.ofMilliseconds(csPolicy.getConnectionTimeout())).setResponseTimeout(Timeout.ofMilliseconds(csPolicy.getReceiveTimeout())).setConnectionRequestTimeout(Timeout.ofMilliseconds(csPolicy.getConnectionRequestTimeout()));
    final Proxy p = proxyFactory.createProxy(csPolicy, uri);
    if (p != null && p.type() != Proxy.Type.DIRECT) {
        InetSocketAddress isa = (InetSocketAddress) p.address();
        HttpHost proxy = new HttpHost(isa.getHostString(), isa.getPort());
        b.setProxy(proxy);
    }
    e.setConfig(b.build());
    message.put(CXFHttpRequest.class, e);
}
Also used : TLSClientParameters(org.apache.cxf.configuration.jsse.TLSClientParameters) RequestConfig(org.apache.hc.client5.http.config.RequestConfig) MalformedURLException(java.net.MalformedURLException) InetSocketAddress(java.net.InetSocketAddress) Address(org.apache.cxf.transport.http.Address) InetSocketAddress(java.net.InetSocketAddress) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Proxy(java.net.Proxy) HttpHost(org.apache.hc.core5.http.HttpHost)

Aggregations

Test (org.junit.Test)7 ResponseBody (okhttp3.ResponseBody)6 Header (retrofit2.http.Header)6 Request (okhttp3.Request)5 List (java.util.List)3 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)3 U (com.weicoder.common.U)2 W (com.weicoder.common.W)2 HttpParams (com.weicoder.common.http.params.HttpParams)2 IOUtil (com.weicoder.common.io.IOUtil)2 Lists (com.weicoder.common.lang.Lists)2 Map (java.util.Map)2 MultipartBody (okhttp3.MultipartBody)2 RequestBody (okhttp3.RequestBody)2 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)2 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)2 UrlEncodedFormEntity (org.apache.hc.client5.http.entity.UrlEncodedFormEntity)2 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)2 HttpClientBuilder (org.apache.hc.client5.http.impl.classic.HttpClientBuilder)2 PoolingHttpClientConnectionManager (org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)2