Search in sources :

Example 1 with RequestURIMeta

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

the class BlockingServletTest method doGetSecureTest.

@Test
public void doGetSecureTest() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    RequestResponseWatcher watcher = mock(RequestResponseWatcher.class);
    componentManager.addRequestResponseWatchers(Collections.singletonList(watcher));
    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.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();
    GetRequestHandler requestHandler = new GetRequestHandler();
    RequestURIMeta uriMeta = RequestURIMeta.builder().management(false).authenticated(true).api(true).build();
    requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, uriMeta), 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
    RequestURIManager requestURIManager = requestHandlerManager.getRequestURIManager();
    Asserts.assertTrue(requestURIManager.isAuthenticatedURI(HttpMethod.GET, "/get"));
    Asserts.assertTrue(requestURIManager.isAuthenticatedURI(HttpMethod.GET, "/get/"));
    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.OK);
    DataConverters dataConverters = componentManager.getDataConverters();
    BodySerializer bodySerializer = dataConverters.getBodySerializer(ContentTypes.APPLICATION_JSON);
    ExResponse responseData = new ExResponse("Hello ParameterValue, HeaderValue, CookieValue");
    verify(outputStream, times(1)).write(bodySerializer.serialize(responseData));
    verify(interceptor, times(1)).preHandle(any(), any());
    verify(interceptor, times(1)).postHandle(any(), any());
    verify(watcher, times(1)).watchRequest(HttpMethod.GET, request);
    verify(watcher, times(1)).watchResponse(HttpMethod.GET, request, response);
    componentManager.destroy();
}
Also used : Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) RequestURIManager(com.tvd12.ezyhttp.server.core.manager.RequestURIManager) ServletOutputStream(javax.servlet.ServletOutputStream) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) ToString(lombok.ToString) RequestInterceptor(com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor) DataConverters(com.tvd12.ezyhttp.core.codec.DataConverters) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestURIMeta(com.tvd12.ezyhttp.server.core.request.RequestURIMeta) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) BodySerializer(com.tvd12.ezyhttp.core.codec.BodySerializer) RequestResponseWatcher(com.tvd12.ezyhttp.server.core.handler.RequestResponseWatcher) Test(org.testng.annotations.Test)

Example 2 with RequestURIMeta

use of com.tvd12.ezyhttp.server.core.request.RequestURIMeta 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)

Aggregations

RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)2 RequestURIMeta (com.tvd12.ezyhttp.server.core.request.RequestURIMeta)2 EzyLoggable (com.tvd12.ezyfox.util.EzyLoggable)1 BodySerializer (com.tvd12.ezyhttp.core.codec.BodySerializer)1 DataConverters (com.tvd12.ezyhttp.core.codec.DataConverters)1 HttpMethod (com.tvd12.ezyhttp.core.constant.HttpMethod)1 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)1 RequestHandler (com.tvd12.ezyhttp.server.core.handler.RequestHandler)1 RequestResponseWatcher (com.tvd12.ezyhttp.server.core.handler.RequestResponseWatcher)1 RequestURIDecorator (com.tvd12.ezyhttp.server.core.handler.RequestURIDecorator)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 RequestURIManager (com.tvd12.ezyhttp.server.core.manager.RequestURIManager)1 ControllerProxy (com.tvd12.ezyhttp.server.core.reflect.ControllerProxy)1 RequestHandlerMethod (com.tvd12.ezyhttp.server.core.reflect.RequestHandlerMethod)1 BlockingServlet (com.tvd12.ezyhttp.server.core.servlet.BlockingServlet)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1