use of com.tvd12.ezyhttp.server.core.view.Redirect in project ezyhttp by youngmonkeys.
the class BlockingServletTest method doPutWithRedirectTest.
@Test
public void doPutWithRedirectTest() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/put-with-redirect-attributes";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getMethod()).thenReturn(HttpMethod.PUT.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(PORT);
when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
when(request.getParameter("param")).thenReturn("ParameterValue");
when(request.getParameterValues("param")).thenReturn(new String[] { "ParameterValue" });
when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
when(request.getHeader("header")).thenReturn("HeaderValue");
when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
PutWithRedirectAttributesRequestHandler requestHandler = new PutWithRedirectAttributesRequestHandler();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.PUT, requestURI, false), requestHandler);
RequestInterceptor interceptor = mock(RequestInterceptor.class);
when(interceptor.preHandle(any(), any())).thenReturn(true);
componentManager.getInterceptorManager().addRequestInterceptors(Collections.singletonList(interceptor));
HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
// when
sut.service(request, response);
// then
verify(request, times(1)).getMethod();
verify(request, times(1)).getRequestURI();
verify(response, times(1)).sendRedirect("/home");
verify(interceptor, times(1)).preHandle(any(), any());
verify(interceptor, times(1)).postHandle(any(), any());
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.view.Redirect in project ezyhttp by youngmonkeys.
the class RedirectTest method toTest.
@Test
public void toTest() {
// given
Redirect sut = Redirect.to("/home");
// when
String uri = sut.getUri();
// then
Asserts.assertEquals("/home", uri);
}
use of com.tvd12.ezyhttp.server.core.view.Redirect in project ezyhttp by youngmonkeys.
the class RedirectTest method getQueryStringTest.
@Test
public void getQueryStringTest() throws Exception {
// given
Redirect sut = Redirect.builder().uri("/home").addHeader("foo", "bar").addHeader("hello", "world").addHeaders(Collections.singletonMap("one", 1)).addParameter("param1", "one").addParameter("param2", "two").addParameters(Collections.singletonMap("param3", "three")).addCookie("cookie1", "cvalue1").addCookie("cookie2", "cvalue2").addCookie("cookie3", "cvalue3", "/path").build();
// when
String uri = sut.getUri();
Map<String, String> headers = sut.getHeaders();
List<Cookie> cookies = sut.getCookies();
// then
Asserts.assertEquals("/home", uri);
Asserts.assertEquals("?param1=one¶m2=two¶m3=three", sut.getQueryString());
Map<String, Object> expectedHeaders = new HashMap<>();
expectedHeaders.put("foo", "bar");
expectedHeaders.put("hello", "world");
expectedHeaders.put("one", "1");
Asserts.assertEquals(expectedHeaders, headers, false);
Cookie cookie3 = new Cookie("cookie3", "cvalue3");
cookie3.setPath("/path");
List<Cookie> expectedCookies = Lists.newArrayList(new Cookie("cookie1", "cvalue1"), new Cookie("cookie2", "cvalue2"), cookie3);
Asserts.assertEquals(expectedCookies, cookies, false);
Asserts.assertEquals(sut.getCookies().get(2).getPath(), "/path");
}
use of com.tvd12.ezyhttp.server.core.view.Redirect 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);
}
}
use of com.tvd12.ezyhttp.server.core.view.Redirect in project ezyfox-server by youngmonkeys.
the class EzyRequestAppController method handle.
@Override
public void handle(EzyServerContext ctx, EzyRequestAppRequest request) {
EzyRequestAppParams params = request.getParams();
EzyAppContext appCtx = ctx.getAppContext(params.getAppId());
EzyApplication app = appCtx.getApp();
EzyAppRequestController requestController = app.getRequestController();
// user manager for checking, user must be managed
EzyUserManager userManger = appCtx.getApp().getUserManager();
EzyUser user = request.getUser();
// check user joined app or not to prevent spam request
boolean hasAccessed = userManger.containsUser(user);
if (hasAccessed) {
// redirect handling to app
EzyUserRequestAppEvent event = newRequestAppEvent(request);
requestController.handle(appCtx, event);
} else {
EzySession session = request.getSession();
responseRequestAppError(ctx, session);
}
}
Aggregations