Search in sources :

Example 1 with HttpMethod

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

the class BlockingServlet method newRequestArguments.

protected RequestArguments newRequestArguments(HttpMethod method, String uriTemplate, HttpServletRequest request, HttpServletResponse response) {
    SimpleRequestArguments arguments = new SimpleRequestArguments();
    arguments.setDebug(debug);
    arguments.setMethod(method);
    arguments.setRequest(request);
    arguments.setResponse(response);
    arguments.setUriTemplate(uriTemplate);
    arguments.setCookies(request.getCookies());
    arguments.setObjectMapper(objectMapper);
    arguments.setRedirectionAttributesFromCookie();
    Enumeration<String> paramNames = request.getParameterNames();
    while (paramNames.hasMoreElements()) {
        String paramName = paramNames.nextElement();
        String[] paramValues = request.getParameterValues(paramName);
        arguments.setParameter(paramName, paramValues);
    }
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
        String headerValue = request.getHeader(headerName);
        arguments.setHeader(headerName, headerValue);
    }
    return arguments;
}
Also used : SimpleRequestArguments(com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments)

Example 2 with HttpMethod

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

the class BlockingServlet method handleRequest.

@SuppressWarnings("MethodLength")
protected void handleRequest(HttpMethod method, HttpServletRequest request, HttpServletResponse response) throws IOException {
    String requestURI = request.getRequestURI();
    String matchedURI = requestHandlerManager.getMatchedURI(method, requestURI);
    if (matchedURI == null) {
        if (!handleError(method, request, response, HttpServletResponse.SC_NOT_FOUND)) {
            responseString(response, "uri " + requestURI + " not found");
        }
        return;
    }
    request.setAttribute(CoreConstants.ATTRIBUTE_MATCHED_URI, matchedURI);
    boolean isManagementURI = requestURIManager.isManagementURI(method, matchedURI);
    if (isManagementURI && !exposeManagementURIs && request.getServerPort() != managementPort) {
        handleError(method, request, response, HttpServletResponse.SC_NOT_FOUND);
        logger.warn("a normal client's not allowed call to: {}, " + "please check your proxy configuration", requestURI);
        return;
    }
    RequestHandler requestHandler = requestHandlerManager.getHandler(method, matchedURI, isManagementURI);
    if (requestHandler == RequestHandler.EMPTY) {
        if (!handleError(method, request, response, HttpServletResponse.SC_METHOD_NOT_ALLOWED)) {
            responseString(response, "method " + method + " not allowed");
        }
        return;
    }
    boolean acceptableRequest = false;
    boolean syncResponse = true;
    String uriTemplate = requestHandler.getRequestURI();
    RequestArguments arguments = newRequestArguments(method, uriTemplate, request, response);
    try {
        acceptableRequest = preHandleRequest(arguments, requestHandler);
        if (acceptableRequest) {
            if (requestHandler.isAsync()) {
                syncResponse = false;
                AsyncContext asyncContext = request.startAsync(request, response);
                if (asyncDefaultTimeout > 0) {
                    asyncContext.setTimeout(asyncDefaultTimeout);
                }
                asyncContext.addListener(newAsyncListener(arguments, requestHandler));
                requestHandler.handle(arguments);
            } else {
                Object responseData = requestHandler.handle(arguments);
                String responseContentType = requestHandler.getResponseContentType();
                if (responseContentType != null) {
                    response.setContentType(responseContentType);
                }
                if (responseData != null) {
                    handleResponseData(request, response, responseData);
                } else {
                    response.setStatus(HttpServletResponse.SC_OK);
                }
            }
        } else {
            handleError(method, request, response, HttpServletResponse.SC_NOT_ACCEPTABLE);
        }
    } catch (Exception e) {
        handleException(method, arguments, e);
    } finally {
        if (syncResponse) {
            if (acceptableRequest) {
                postHandleRequest(arguments, requestHandler);
            }
            arguments.release();
        }
    }
}
Also used : RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) AsyncContext(javax.servlet.AsyncContext) RequestArguments(com.tvd12.ezyhttp.server.core.request.RequestArguments) SimpleRequestArguments(com.tvd12.ezyhttp.server.core.request.SimpleRequestArguments) ServletException(javax.servlet.ServletException) DeserializeValueException(com.tvd12.ezyhttp.core.exception.DeserializeValueException) IOException(java.io.IOException) HttpRequestException(com.tvd12.ezyhttp.core.exception.HttpRequestException)

Example 3 with HttpMethod

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

the class RequestHandlerMethod method fetchHttpMethod.

protected HttpMethod fetchHttpMethod() {
    DoGet doGet = method.getAnnotation(DoGet.class);
    if (doGet != null) {
        return HttpMethod.GET;
    }
    DoPost doPost = method.getAnnotation(DoPost.class);
    if (doPost != null) {
        return HttpMethod.POST;
    }
    DoPut doPut = method.getAnnotation(DoPut.class);
    if (doPut != null) {
        return HttpMethod.PUT;
    }
    return HttpMethod.DELETE;
}
Also used : DoPost(com.tvd12.ezyhttp.server.core.annotation.DoPost) DoPut(com.tvd12.ezyhttp.server.core.annotation.DoPut) DoGet(com.tvd12.ezyhttp.server.core.annotation.DoGet)

Example 4 with HttpMethod

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

the class RequestHandlerManager method getHandler.

public RequestHandler getHandler(HttpMethod method, String matchedURI, boolean isManagement) {
    RequestURI requestURI = new RequestURI(method, matchedURI, isManagement);
    RequestHandler handler = handlers.get(requestURI);
    return handler != null ? handler : RequestHandler.EMPTY;
}
Also used : RequestHandler(com.tvd12.ezyhttp.server.core.handler.RequestHandler) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI)

Example 5 with HttpMethod

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

the class UnhandledErrorHandlerTest method returnString.

@Test
public void returnString() {
    // given
    String result = "bar";
    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(ContentTypes.APPLICATION_JSON);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) 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)

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