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