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