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