Search in sources :

Example 6 with RequestEntity

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);
}
Also used : HelloRequest(com.tvd12.ezyhttp.client.test.request.HelloRequest)

Example 7 with RequestEntity

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();
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) HttpURLConnection(java.net.HttpURLConnection) ArrayList(java.util.ArrayList) List(java.util.List) MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap)

Example 8 with RequestEntity

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);
}
Also used : PostRequest(com.tvd12.ezyhttp.client.request.PostRequest) HttpClient(com.tvd12.ezyhttp.client.HttpClient) HelloRequest(com.tvd12.ezyhttp.client.test.request.HelloRequest) RequestEntity(com.tvd12.ezyhttp.client.request.RequestEntity)

Example 9 with RequestEntity

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);
}
Also used : RequestEntity(com.tvd12.ezyhttp.client.request.RequestEntity) MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap) Test(org.testng.annotations.Test)

Example 10 with RequestEntity

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);
}
Also used : RequestEntity(com.tvd12.ezyhttp.client.request.RequestEntity) Test(org.testng.annotations.Test)

Aggregations

RequestEntity (com.tvd12.ezyhttp.client.request.RequestEntity)10 Test (org.testng.annotations.Test)5 HttpClient (com.tvd12.ezyhttp.client.HttpClient)4 MultiValueMap (com.tvd12.ezyhttp.core.data.MultiValueMap)4 PostRequest (com.tvd12.ezyhttp.client.request.PostRequest)3 GetRequest (com.tvd12.ezyhttp.client.request.GetRequest)2 Request (com.tvd12.ezyhttp.client.request.Request)2 HelloRequest (com.tvd12.ezyhttp.client.test.request.HelloRequest)2 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)1 Customer (com.tvd12.ezyhttp.client.test.request.Customer)1 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 User (org.youngmonkeys.example.ezyhttp.website.user_management.entity.User)1