Search in sources :

Example 1 with MultiValueMap

use of com.tvd12.ezyhttp.core.data.MultiValueMap in project ezyhttp by youngmonkeys.

the class HttpClient method decorateConnection.

private void decorateConnection(HttpURLConnection connection, DownloadRequest request) {
    int connectTimeout = request.getReadTimeout();
    int readTimeout = request.getReadTimeout();
    connection.setConnectTimeout(connectTimeout > 0 ? connectTimeout : defaultConnectTimeout);
    connection.setReadTimeout(readTimeout > 0 ? readTimeout : defaultReadTimeout);
    MultiValueMap requestHeaders = request.getHeaders();
    if (requestHeaders != null) {
        Map<String, String> encodedHeaders = requestHeaders.toMap();
        for (Entry<String, String> requestHeader : encodedHeaders.entrySet()) {
            connection.setRequestProperty(requestHeader.getKey(), requestHeader.getValue());
        }
    }
}
Also used : MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap)

Example 2 with MultiValueMap

use of com.tvd12.ezyhttp.core.data.MultiValueMap in project ezyhttp by youngmonkeys.

the class RequestEntityTest method buildMultiMapBody.

@Test
public void buildMultiMapBody() {
    // given
    Map<String, List<String>> data = new HashMap<>();
    data.put("1", Arrays.asList("hello", "world"));
    data.put("2", Arrays.asList("foo", "bar"));
    data.put("3", Collections.singletonList("monkey"));
    data.put(Headers.CONTENT_TYPE, Collections.singletonList(ContentTypes.APPLICATION_JSON));
    RequestEntity sut = RequestEntity.builder().body(new MultiValueMap(data)).build();
    // when
    // then
    Map<String, String> expectation = new HashMap<>();
    expectation.put("1", "hello;world");
    expectation.put("2", "foo;bar");
    expectation.put("3", "monkey");
    expectation.put(Headers.CONTENT_TYPE, ContentTypes.APPLICATION_JSON);
    Asserts.assertEquals(expectation, sut.getBody());
    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 3 with MultiValueMap

use of com.tvd12.ezyhttp.core.data.MultiValueMap in project ezyhttp by youngmonkeys.

the class MultiValueMapTest method toMapTest.

@Test
public void toMapTest() {
    // given
    Map<String, Object> values = new HashMap<>();
    values.put("a", null);
    values.put("b", "");
    values.put("c", 100);
    values.put("d", "good");
    values.put("e", true);
    MultiValueMap sut = MultiValueMap.builder().setValue("1", "hello").setValues("2", Arrays.asList("foo", "bar")).setValues("3", Collections.emptyList()).setValues("m", values).build();
    // when
    Map<String, String> actual1 = sut.toMap();
    // then
    Map<String, String> expectation = new HashMap<>();
    expectation.put("1", "hello");
    expectation.put("2", "foo;bar");
    expectation.put("m", "a;b=;c=100;d=good;e=true");
    Asserts.assertEquals(expectation, actual1);
}
Also used : HashMap(java.util.HashMap) MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap) Test(org.testng.annotations.Test)

Example 4 with MultiValueMap

use of com.tvd12.ezyhttp.core.data.MultiValueMap in project ezyhttp by youngmonkeys.

the class MultiValueMapTest method ofTest.

@Test
public void ofTest() {
    // given
    Map<String, List<String>> map = new HashMap<>();
    map.put("1", Arrays.asList("hello", "world"));
    map.put("2", Arrays.asList("foo", "bar"));
    map.put("3", Collections.emptyList());
    // when
    MultiValueMap sut = MultiValueMap.of(map);
    // then
    Asserts.assertEquals(map.keySet(), sut.keySets());
    Asserts.assertNull(sut.getValue("nothing"));
    Asserts.assertNull(sut.getValue("3"));
    Asserts.assertEquals("hello", sut.getValue("1"));
    Asserts.assertEquals("foo", sut.getValue("2"));
    Asserts.assertEquals("foo", sut.getValue("2", "def"));
    Asserts.assertEquals("def", sut.getValue("3", "def"));
    Asserts.assertEquals(Arrays.asList("hello", "world"), sut.getValues("1"));
    Asserts.assertTrue(sut.getValues("unknow").isEmpty());
}
Also used : HashMap(java.util.HashMap) List(java.util.List) MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap) Test(org.testng.annotations.Test)

Example 5 with MultiValueMap

use of com.tvd12.ezyhttp.core.data.MultiValueMap in project ezyhttp by youngmonkeys.

the class BlockingServlet method handleResponseData.

protected void handleResponseData(HttpServletRequest request, HttpServletResponse response, Object data) throws Exception {
    Object body = data;
    if (data instanceof ResponseEntity) {
        ResponseEntity entity = (ResponseEntity) body;
        body = entity.getBody();
        response.setStatus(entity.getStatus());
        MultiValueMap headers = entity.getHeaders();
        if (headers != null) {
            Map<String, String> encodedHeaders = headers.toMap();
            for (Entry<String, String> entry : encodedHeaders.entrySet()) {
                response.addHeader(entry.getKey(), entry.getValue());
            }
        }
    } else if (data instanceof Redirect) {
        Redirect redirect = (Redirect) data;
        for (Cookie cookie : redirect.getCookies()) {
            response.addCookie(cookie);
        }
        for (Entry<String, String> e : redirect.getHeaders().entrySet()) {
            response.addHeader(e.getKey(), e.getValue());
        }
        Map<String, Object> attributes = redirect.getAttributes();
        if (attributes != null) {
            String attributesValue = objectMapper.writeValueAsString(attributes);
            Cookie attributesCookie = new Cookie(CoreConstants.COOKIE_REDIRECT_ATTRIBUTES_NAME, EzyBase64.encodeUtf(attributesValue));
            attributesCookie.setMaxAge(CoreConstants.COOKIE_REDIRECT_ATTRIBUTES_MAX_AGE);
            response.addCookie(attributesCookie);
        }
        response.sendRedirect(redirect.getUri() + redirect.getQueryString());
        return;
    } else if (data instanceof View) {
        if (viewContext == null) {
            throw new IllegalStateException("viewContext is null, " + "you must add ezyhttp-server-thymeleaf to your dependencies" + " or create viewContext by yourself");
        }
        View view = (View) data;
        for (Cookie cookie : view.getCookies()) {
            response.addCookie(cookie);
        }
        for (Entry<String, String> e : view.getHeaders().entrySet()) {
            response.addHeader(e.getKey(), e.getValue());
        }
        response.setContentType(view.getContentType());
        viewContext.render(getServletContext(), request, response, view);
        return;
    } else {
        response.setStatus(HttpServletResponse.SC_OK);
    }
    if (body != null) {
        responseBody(response, body);
    }
}
Also used : Cookie(javax.servlet.http.Cookie) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) Entry(java.util.Map.Entry) Redirect(com.tvd12.ezyhttp.server.core.view.Redirect) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap) View(com.tvd12.ezyhttp.server.core.view.View) MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap)

Aggregations

MultiValueMap (com.tvd12.ezyhttp.core.data.MultiValueMap)11 Test (org.testng.annotations.Test)8 HashMap (java.util.HashMap)5 RequestEntity (com.tvd12.ezyhttp.client.request.RequestEntity)3 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)3 List (java.util.List)3 DownloadRequest (com.tvd12.ezyhttp.client.request.DownloadRequest)1 Redirect (com.tvd12.ezyhttp.server.core.view.Redirect)1 View (com.tvd12.ezyhttp.server.core.view.View)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Cookie (javax.servlet.http.Cookie)1