Search in sources :

Example 1 with Redirect

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) ServletOutputStream(javax.servlet.ServletOutputStream) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) ToString(lombok.ToString) RequestInterceptor(com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor) Test(org.testng.annotations.Test)

Example 2 with Redirect

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);
}
Also used : Redirect(com.tvd12.ezyhttp.server.core.view.Redirect) Test(org.testng.annotations.Test)

Example 3 with Redirect

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&param2=two&param3=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");
}
Also used : Cookie(javax.servlet.http.Cookie) HashMap(java.util.HashMap) Redirect(com.tvd12.ezyhttp.server.core.view.Redirect) Test(org.testng.annotations.Test)

Example 4 with Redirect

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

Example 5 with Redirect

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);
    }
}
Also used : EzyApplication(com.tvd12.ezyfoxserver.EzyApplication) EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) EzyRequestAppParams(com.tvd12.ezyfoxserver.request.EzyRequestAppParams) EzyAppContext(com.tvd12.ezyfoxserver.context.EzyAppContext) EzyAppRequestController(com.tvd12.ezyfoxserver.app.EzyAppRequestController) EzyUserManager(com.tvd12.ezyfoxserver.wrapper.EzyUserManager) EzyUserRequestAppEvent(com.tvd12.ezyfoxserver.event.EzyUserRequestAppEvent) EzySession(com.tvd12.ezyfoxserver.entity.EzySession)

Aggregations

Redirect (com.tvd12.ezyhttp.server.core.view.Redirect)3 Cookie (javax.servlet.http.Cookie)3 Test (org.testng.annotations.Test)3 HashMap (java.util.HashMap)2 EzyApplication (com.tvd12.ezyfoxserver.EzyApplication)1 EzyAppRequestController (com.tvd12.ezyfoxserver.app.EzyAppRequestController)1 EzyAppContext (com.tvd12.ezyfoxserver.context.EzyAppContext)1 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)1 EzyUser (com.tvd12.ezyfoxserver.entity.EzyUser)1 EzyUserRequestAppEvent (com.tvd12.ezyfoxserver.event.EzyUserRequestAppEvent)1 EzyRequestAppParams (com.tvd12.ezyfoxserver.request.EzyRequestAppParams)1 EzyUserManager (com.tvd12.ezyfoxserver.wrapper.EzyUserManager)1 MultiValueMap (com.tvd12.ezyhttp.core.data.MultiValueMap)1 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)1 DoPost (com.tvd12.ezyhttp.server.core.annotation.DoPost)1 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)1 RequestInterceptor (com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor)1 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)1 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)1 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)1