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