use of com.tvd12.ezyhttp.client.request.RequestEntity in project ezyhttp by youngmonkeys.
the class HttpClientProxyTest method postTest.
protected static void postTest(HttpClientProxy client) throws Exception {
HelloRequest body = new HelloRequest();
body.setWho("dzung");
RequestEntity entity = RequestEntity.body(body);
PostRequest request = new PostRequest().setURL("http://localhost:8081/").setEntity(entity).setResponseType(String.class).setReadTimeout(HttpClient.NO_TIMEOUT).setConnectTimeout(HttpClient.NO_TIMEOUT);
String response = client.call(request, 1000);
System.out.println(response);
}
use of com.tvd12.ezyhttp.client.request.RequestEntity in project ezyhttp by youngmonkeys.
the class HttpClient method request.
@SuppressWarnings("MethodLength")
public ResponseEntity request(HttpMethod method, String url, RequestEntity entity, Map<Integer, Class<?>> responseTypes, int connectTimeout, int readTimeout) throws Exception {
if (url == null) {
throw new IllegalArgumentException("url can not be null");
}
logger.debug("start: {} - {} - {}", method, url, entity != null ? entity.getHeaders() : null);
HttpURLConnection connection = connect(url);
try {
connection.setConnectTimeout(connectTimeout > 0 ? connectTimeout : defaultConnectTimeout);
connection.setReadTimeout(readTimeout > 0 ? readTimeout : defaultReadTimeout);
connection.setRequestMethod(method.toString());
connection.setDoInput(true);
connection.setDoOutput(method.hasOutput());
connection.setInstanceFollowRedirects(method == HttpMethod.GET);
MultiValueMap requestHeaders = entity != null ? entity.getHeaders() : null;
if (requestHeaders != null) {
Map<String, String> encodedHeaders = requestHeaders.toMap();
for (Entry<String, String> requestHeader : encodedHeaders.entrySet()) {
connection.setRequestProperty(requestHeader.getKey(), requestHeader.getValue());
}
}
Object requestBody = null;
if (method != HttpMethod.GET && entity != null) {
requestBody = entity.getBody();
}
byte[] requestBodyBytes = null;
if (requestBody != null) {
String requestContentType = connection.getRequestProperty(Headers.CONTENT_TYPE);
if (requestContentType == null) {
requestContentType = ContentTypes.APPLICATION_JSON;
connection.setRequestProperty(Headers.CONTENT_TYPE, ContentTypes.APPLICATION_JSON);
}
requestBodyBytes = serializeRequestBody(requestContentType, requestBody);
int requestContentLength = requestBodyBytes.length;
connection.setFixedLengthStreamingMode(requestContentLength);
}
connection.connect();
if (requestBodyBytes != null) {
if (method.hasOutput()) {
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBodyBytes);
outputStream.flush();
outputStream.close();
} else {
throw new IllegalArgumentException(method + " method can not have a payload body");
}
}
int responseCode = connection.getResponseCode();
Map<String, List<String>> headerFields = connection.getHeaderFields();
MultiValueMap responseHeaders = MultiValueMap.of(headerFields);
String responseContentType = responseHeaders.getValue(Headers.CONTENT_TYPE);
if (responseContentType == null) {
responseContentType = ContentTypes.APPLICATION_JSON;
}
InputStream inputStream = responseCode >= 400 ? connection.getErrorStream() : connection.getInputStream();
Object responseBody = null;
if (inputStream != null) {
try {
int responseContentLength = connection.getContentLength();
Class<?> responseType = responseTypes.get(responseCode);
responseBody = deserializeResponseBody(responseContentType, responseContentLength, inputStream, responseType);
} finally {
inputStream.close();
}
}
logger.debug("end: {} - {} - {} - {}", method, url, responseCode, responseHeaders);
return new ResponseEntity(responseCode, responseHeaders, responseBody);
} finally {
connection.disconnect();
}
}
use of com.tvd12.ezyhttp.client.request.RequestEntity in project ezyhttp by youngmonkeys.
the class HttpClientTest method postTest.
protected static void postTest() throws Exception {
HttpClient client = HttpClient.builder().build();
HelloRequest body = new HelloRequest();
body.setWho("dzung");
RequestEntity entity = RequestEntity.body(body);
PostRequest request = new PostRequest().setURL("http://localhost:8081/").setEntity(entity).setResponseType(String.class).setReadTimeout(HttpClient.NO_TIMEOUT).setConnectTimeout(HttpClient.NO_TIMEOUT);
String response = client.call(request);
System.out.println(response);
}
use of com.tvd12.ezyhttp.client.request.RequestEntity in project ezyhttp by youngmonkeys.
the class RequestEntityTest method commonTest.
@Test
public void commonTest() {
// given
String body = RandomUtil.randomShortAlphabetString();
// when
RequestEntity sut = RequestEntity.of(body).header("1", (String) null).header("1", "hello").header("1", "world").header("2", Arrays.asList("foo", "bar")).headers(Collections.singletonMap("3", "monkey")).contentType(ContentTypes.APPLICATION_JSON).build();
// then
Map<String, List<String>> headers = new HashMap<>();
headers.put("1", Arrays.asList("hello", "world"));
headers.put("2", Arrays.asList("foo", "bar"));
headers.put("3", Collections.singletonList("monkey"));
headers.put(Headers.CONTENT_TYPE, Collections.singletonList(ContentTypes.APPLICATION_JSON));
Asserts.assertEquals(new MultiValueMap(headers), sut.getHeaders());
Asserts.assertEquals(body, sut.getBody());
Asserts.assertEquals(ContentTypes.APPLICATION_JSON, sut.getHeader(Headers.CONTENT_TYPE));
Asserts.assertNull(sut.getHeader("unknown"));
Asserts.assertEquals(ContentTypes.APPLICATION_JSON, sut.getContentType());
System.out.println(sut);
}
use of com.tvd12.ezyhttp.client.request.RequestEntity in project ezyhttp by youngmonkeys.
the class RequestEntityTest method emptyHeadersTest.
@Test
public void emptyHeadersTest() {
// given
RequestEntity sut = new RequestEntity((Map<String, List<String>>) null, null);
// when
// then
Asserts.assertNull(sut.getHeader("unknown"));
Asserts.assertEquals(ContentTypes.APPLICATION_JSON, sut.getContentType());
System.out.println(sut);
}
Aggregations