Search in sources :

Example 6 with HttpMethod

use of com.tvd12.ezyhttp.core.constant.HttpMethod in project ezyhttp by youngmonkeys.

the class UnhandledErrorHandlerTest method returnResponseEntity.

@Test
public void returnResponseEntity() {
    // given
    ResponseEntity result = ResponseEntity.ok();
    UnhandledErrorHandler sut = new UnhandledErrorHandler() {

        @Override
        public Object processError(HttpMethod method, HttpServletRequest request, HttpServletResponse response, int errorStatusCode, Exception exception) {
            return result;
        }
    };
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    // when
    Object actual = sut.handleError(HttpMethod.GET, request, response, StatusCodes.BAD_REQUEST, null);
    // then
    Asserts.assertEquals(result, actual);
    verify(response, times(1)).setContentType(null);
    verify(response, times(1)).setContentType(ContentTypes.APPLICATION_JSON);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) UnhandledErrorHandler(com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpMethod(com.tvd12.ezyhttp.core.constant.HttpMethod) Test(org.testng.annotations.Test)

Example 7 with HttpMethod

use of com.tvd12.ezyhttp.core.constant.HttpMethod in project ezyhttp by youngmonkeys.

the class UnhandledErrorHandlerTest method returnView.

@Test
public void returnView() {
    // given
    View result = View.builder().template("foo").build();
    UnhandledErrorHandler sut = new UnhandledErrorHandler() {

        @Override
        public Object processError(HttpMethod method, HttpServletRequest request, HttpServletResponse response, int errorStatusCode, Exception exception) {
            return result;
        }
    };
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getContentType()).thenReturn(ContentTypes.TEXT_HTML_UTF8);
    // when
    Object actual = sut.handleError(HttpMethod.GET, request, response, StatusCodes.BAD_REQUEST, null);
    // then
    Asserts.assertEquals(result, actual);
    verify(response, times(1)).setContentType(ContentTypes.TEXT_HTML_UTF8);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UnhandledErrorHandler(com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) View(com.tvd12.ezyhttp.server.core.view.View) HttpMethod(com.tvd12.ezyhttp.core.constant.HttpMethod) Test(org.testng.annotations.Test)

Example 8 with HttpMethod

use of com.tvd12.ezyhttp.core.constant.HttpMethod 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 9 with HttpMethod

use of com.tvd12.ezyhttp.core.constant.HttpMethod in project ezyhttp by youngmonkeys.

the class BlockingServlet method handleException.

protected void handleException(HttpMethod method, RequestArguments arguments, Exception e) {
    UncaughtExceptionHandler handler = getUncaughtExceptionHandler(e.getClass());
    HttpServletRequest request = arguments.getRequest();
    HttpServletResponse response = arguments.getResponse();
    Exception exception = e;
    if (handler != null) {
        try {
            Object result = handler.handleException(arguments, e);
            if (result != null) {
                String responseContentType = handler.getResponseContentType();
                if (responseContentType != null) {
                    response.setContentType(responseContentType);
                }
                handleResponseData(request, response, result);
            } else {
                handleError(method, request, response, HttpServletResponse.SC_BAD_REQUEST);
            }
            exception = null;
        } catch (Exception ex) {
            exception = ex;
        }
    }
    if (exception != null) {
        handleError(method, request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, exception);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) UncaughtExceptionHandler(com.tvd12.ezyhttp.server.core.handler.UncaughtExceptionHandler) ServletException(javax.servlet.ServletException) DeserializeValueException(com.tvd12.ezyhttp.core.exception.DeserializeValueException) IOException(java.io.IOException) HttpRequestException(com.tvd12.ezyhttp.core.exception.HttpRequestException)

Example 10 with HttpMethod

use of com.tvd12.ezyhttp.core.constant.HttpMethod in project ezyhttp by youngmonkeys.

the class RequestHandlerManager method getMatchedURI.

public String getMatchedURI(HttpMethod method, String requestURI) {
    String matchedURI = null;
    if (handledURIs.contains(requestURI)) {
        matchedURI = requestURI;
    }
    if (matchedURI == null && requestURI != null) {
        URITree uriTree = uriTreeByMethod.get(method);
        matchedURI = uriTree.getMatchedURI(requestURI);
    }
    return matchedURI;
}
Also used : URITree(com.tvd12.ezyhttp.core.net.URITree)

Aggregations

HttpMethod (com.tvd12.ezyhttp.core.constant.HttpMethod)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 RequestHandler (com.tvd12.ezyhttp.server.core.handler.RequestHandler)3 UnhandledErrorHandler (com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler)3 Test (org.testng.annotations.Test)3 DeserializeValueException (com.tvd12.ezyhttp.core.exception.DeserializeValueException)2 HttpRequestException (com.tvd12.ezyhttp.core.exception.HttpRequestException)2 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)2 RequestURI (com.tvd12.ezyhttp.server.core.request.RequestURI)2 SimpleRequestArguments (com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ServletException (javax.servlet.ServletException)2 EzyLoggable (com.tvd12.ezyfox.util.EzyLoggable)1 MultiValueMap (com.tvd12.ezyhttp.core.data.MultiValueMap)1 URITree (com.tvd12.ezyhttp.core.net.URITree)1 DoGet (com.tvd12.ezyhttp.server.core.annotation.DoGet)1 DoPost (com.tvd12.ezyhttp.server.core.annotation.DoPost)1