Search in sources :

Example 36 with RequestURI

use of com.tvd12.ezyhttp.server.core.request.RequestURI in project ezyhttp by youngmonkeys.

the class BlockingServletTest method doGetFromManagementButExpose.

@Test
public void doGetFromManagementButExpose() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    componentManager.setManagementPort(MANAGEMENT_POR);
    componentManager.setExposeManagementURIs(true);
    componentManager.getRequestHandlerManager().addHandler(new RequestURI(HttpMethod.GET, "/management", true), mock(RequestHandler.class));
    BlockingServlet sut = new BlockingServlet();
    sut.init();
    String requestURI = "/management";
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
    when(request.getRequestURI()).thenReturn(requestURI);
    when(request.getServerPort()).thenReturn(MANAGEMENT_POR);
    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") });
    HttpServletResponse response = mock(HttpServletResponse.class);
    // when
    sut.service(request, response);
    // then
    verify(request, times(1)).getMethod();
    verify(request, times(1)).getRequestURI();
    verify(response, times(1)).setStatus(StatusCodes.OK);
    Asserts.assertEquals(componentManager.getServerPort(), PORT);
    componentManager.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) 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) Test(org.testng.annotations.Test)

Example 37 with RequestURI

use of com.tvd12.ezyhttp.server.core.request.RequestURI in project ezyhttp by youngmonkeys.

the class BlockingServletTest method getRequestHttpRequestExceptionDataNotNull.

@Test
public void getRequestHttpRequestExceptionDataNotNull() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    BlockingServlet sut = new BlockingServlet();
    sut.init();
    String requestURI = "/get";
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn(HttpMethod.GET.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.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();
    GetRequestHttpRequestExceptionnHandler requestHandler = new GetRequestHttpRequestExceptionnHandler("hello");
    requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), requestHandler);
    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)).getContentType();
    verify(response, times(1)).getOutputStream();
    verify(response, times(1)).setStatus(StatusCodes.BAD_REQUEST);
    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) Test(org.testng.annotations.Test)

Example 38 with RequestURI

use of com.tvd12.ezyhttp.server.core.request.RequestURI in project ezyhttp by youngmonkeys.

the class RequestHandlersImplementerTest method implementOneAllowOverrideURI.

@Test
public void implementOneAllowOverrideURI() {
    // given
    RequestHandlersImplementer sut = new RequestHandlersImplementer();
    Controller controller = new Controller();
    RequestHandlerManager manager = new RequestHandlerManager();
    manager.setAllowOverrideURI(true);
    // when
    manager.addHandlers(sut.implement(Collections.singletonList(controller)));
    // then
    RequestURI uri = new RequestURI(HttpMethod.GET, "/get", false);
    Asserts.assertThat(manager.getHandlerListByURI().get(uri).size()).isEqualsTo(2);
}
Also used : RequestHandlersImplementer(com.tvd12.ezyhttp.server.core.asm.RequestHandlersImplementer) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) Test(org.testng.annotations.Test)

Example 39 with RequestURI

use of com.tvd12.ezyhttp.server.core.request.RequestURI in project ezyhttp by youngmonkeys.

the class RequestHandlersImplementer method implement.

public Map<RequestURI, List<RequestHandler>> implement(Object controller) {
    Map<RequestURI, List<RequestHandler>> handlers = new HashMap<>();
    ControllerProxy proxy = new ControllerProxy(controller);
    String feature = proxy.getFeature();
    for (RequestHandlerMethod method : proxy.getRequestHandlerMethods()) {
        RequestHandlerImplementer implementer = newImplementer(proxy, method);
        RequestHandler handler = implementer.implement();
        HttpMethod httpMethod = handler.getMethod();
        String requestURI = handler.getRequestURI();
        String methodFeature = method.getFeature();
        RequestURIMeta uriMeta = RequestURIMeta.builder().api(method.isApi() || proxy.isApi()).authenticated(method.isAuthenticated() || proxy.isAuthenticated()).management(method.isManagement() || proxy.isManagement()).payment(method.isPayment() || proxy.isPayment()).feature(methodFeature != null ? methodFeature : feature).build();
        RequestURI uri = new RequestURI(httpMethod, requestURI, uriMeta);
        handlers.computeIfAbsent(uri, k -> new ArrayList<>()).add(handler);
    }
    return handlers;
}
Also used : Setter(lombok.Setter) Collection(java.util.Collection) EzyLoggable(com.tvd12.ezyfox.util.EzyLoggable) HashMap(java.util.HashMap) RequestURIDecorator(com.tvd12.ezyhttp.server.core.handler.RequestURIDecorator) ArrayList(java.util.ArrayList) List(java.util.List) RequestURIMeta(com.tvd12.ezyhttp.server.core.request.RequestURIMeta) Map(java.util.Map) ControllerProxy(com.tvd12.ezyhttp.server.core.reflect.ControllerProxy) HttpMethod(com.tvd12.ezyhttp.core.constant.HttpMethod) RequestHandlerMethod(com.tvd12.ezyhttp.server.core.reflect.RequestHandlerMethod) RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) RequestHandlerMethod(com.tvd12.ezyhttp.server.core.reflect.RequestHandlerMethod) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RequestURIMeta(com.tvd12.ezyhttp.server.core.request.RequestURIMeta) RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) ControllerProxy(com.tvd12.ezyhttp.server.core.reflect.ControllerProxy) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) ArrayList(java.util.ArrayList) List(java.util.List) HttpMethod(com.tvd12.ezyhttp.core.constant.HttpMethod)

Example 40 with RequestURI

use of com.tvd12.ezyhttp.server.core.request.RequestURI in project ezyhttp by youngmonkeys.

the class ControllerProxy method fetchRequestHandlerMethods.

protected List<RequestHandlerMethod> fetchRequestHandlerMethods() {
    List<RequestHandlerMethod> list = new ArrayList<>();
    List<EzyMethod> methods = clazz.getPublicMethods(this::isRequestHandlerMethod);
    for (EzyMethod method : methods) {
        RequestHandlerMethod m = new RequestHandlerMethod(requestURI, method);
        list.add(m);
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) EzyMethod(com.tvd12.ezyfox.reflect.EzyMethod)

Aggregations

RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)36 Test (org.testng.annotations.Test)33 ComponentManager (com.tvd12.ezyhttp.server.core.manager.ComponentManager)29 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)29 HttpServletRequest (javax.servlet.http.HttpServletRequest)29 HttpServletResponse (javax.servlet.http.HttpServletResponse)29 ToString (lombok.ToString)29 RequestHandlerManager (com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager)27 ServletOutputStream (javax.servlet.ServletOutputStream)24 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)22 Cookie (javax.servlet.http.Cookie)22 RequestInterceptor (com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor)10 RequestHandler (com.tvd12.ezyhttp.server.core.handler.RequestHandler)9 BodySerializer (com.tvd12.ezyhttp.core.codec.BodySerializer)4 DataConverters (com.tvd12.ezyhttp.core.codec.DataConverters)4 UnhandledErrorHandler (com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler)4 ExceptionHandlerManager (com.tvd12.ezyhttp.server.core.manager.ExceptionHandlerManager)4 ArrayList (java.util.ArrayList)4 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)3 RequestHandlersImplementer (com.tvd12.ezyhttp.server.core.asm.RequestHandlersImplementer)3