use of com.blade.mvc.view.ModelAndView in project blade by biezhi.
the class MethodArgument method getArgs.
public static Object[] getArgs(Request request, Response response, Method actionMethod) throws Exception {
Class<?>[] parameters = actionMethod.getParameterTypes();
Annotation[][] annotations = actionMethod.getParameterAnnotations();
Object[] args = new Object[parameters.length];
actionMethod.setAccessible(true);
String[] paramaterNames = AsmKit.getMethodParamNames(actionMethod);
for (int i = 0, len = parameters.length; i < len; i++) {
Class<?> argType = parameters[i];
if (argType == Request.class) {
args[i] = request;
continue;
}
if (argType == Response.class) {
args[i] = response;
continue;
}
if (argType == Session.class) {
args[i] = request.session();
continue;
}
if (argType == ModelAndView.class) {
args[i] = new ModelAndView();
continue;
}
if (argType == Map.class) {
args[i] = request.querys();
continue;
}
Annotation annotation = annotations[i][0];
if (null != annotation) {
// query param
if (annotation.annotationType() == QueryParam.class) {
QueryParam queryParam = (QueryParam) annotation;
String paramName = queryParam.value();
String val = request.query(paramName);
boolean required = queryParam.required();
if (StringKit.isBlank(paramName)) {
assert paramaterNames != null;
paramName = paramaterNames[i];
val = request.query(paramName);
}
if (StringKit.isBlank(val)) {
val = queryParam.defaultValue();
}
if (required && StringKit.isBlank(val)) {
throw new RouteException("query param [" + paramName + "] not is empty.");
}
args[i] = getRequestParam(argType, val);
continue;
}
// path param
if (annotation.annotationType() == PathParam.class) {
PathParam pathParam = (PathParam) annotation;
String paramName = pathParam.value();
String val = request.pathParam(paramName);
if (StringKit.isBlank(paramName)) {
assert paramaterNames != null;
paramName = paramaterNames[i];
val = request.pathParam(paramName);
}
if (StringKit.isBlank(val)) {
val = pathParam.defaultValue();
}
args[i] = getRequestParam(argType, val);
}
// header param
if (annotation.annotationType() == HeaderParam.class) {
HeaderParam headerParam = (HeaderParam) annotation;
String paramName = headerParam.value();
String val = request.header(paramName);
boolean required = headerParam.required();
if (StringKit.isBlank(paramName)) {
assert paramaterNames != null;
paramName = paramaterNames[i];
val = request.header(paramName);
}
if (StringKit.isBlank(val)) {
val = headerParam.defaultValue();
}
if (required && StringKit.isBlank(val)) {
throw new RouteException("header param [" + paramName + "] not is empty.");
}
args[i] = getRequestParam(argType, val);
continue;
}
// cookie param
if (annotation.annotationType() == CookieParam.class) {
CookieParam cookieParam = (CookieParam) annotation;
String paramName = cookieParam.value();
String val = request.cookie(paramName);
boolean required = cookieParam.required();
if (StringKit.isBlank(paramName)) {
assert paramaterNames != null;
paramName = paramaterNames[i];
val = request.cookie(paramName);
}
if (StringKit.isBlank(val)) {
val = cookieParam.defaultValue();
}
if (required && StringKit.isBlank(val)) {
throw new RouteException("cookie param [" + paramName + "] not is empty.");
}
args[i] = getRequestParam(argType, val);
continue;
}
// form multipart
if (annotation.annotationType() == MultipartParam.class && argType == FileItem.class) {
MultipartParam multipartParam = (MultipartParam) annotation;
String paramName = multipartParam.value();
FileItem val = request.fileItem(paramName);
if (StringKit.isBlank(paramName)) {
assert paramaterNames != null;
paramName = paramaterNames[i];
val = request.fileItem(paramName);
}
args[i] = val;
continue;
}
}
}
return args;
}
use of com.blade.mvc.view.ModelAndView in project blade by biezhi.
the class DispatcherHandler method render404.
/**
* 404 view render
*
* @param response response object
* @param uri 404 uri
* @throws IOException
* @throws TemplateException
*/
private void render404(Response response, String uri) throws Exception {
String view404 = ViewSettings.$().getView404();
if (StringKit.isNotBlank(view404)) {
ModelAndView modelAndView = new ModelAndView(view404);
modelAndView.add("viewName", uri);
response.render(modelAndView);
} else {
response.status(HttpStatus.NOT_FOUND);
response.html(String.format(Const.VIEW_404, uri));
}
}
use of com.blade.mvc.view.ModelAndView in project blade by biezhi.
the class DispatcherHandler method render405.
private void render405(Response response, String uri) throws Exception {
String view404 = ViewSettings.$().getView404();
if (StringKit.isNotBlank(view404)) {
ModelAndView modelAndView = new ModelAndView(view404);
modelAndView.add("viewName", uri);
response.render(modelAndView);
} else {
response.status(HttpStatus.METHOD_NOT_ALLOWED);
response.html(String.format(Const.VIEW_405, uri));
}
}
use of com.blade.mvc.view.ModelAndView in project blade by biezhi.
the class ServletResponse method render.
@Override
public Response render(String view) {
String viewPath = Path.cleanPath(view);
ModelAndView modelAndView = new ModelAndView(viewPath);
try {
templateEngine.render(modelAndView, response.getWriter());
} catch (Exception e) {
LOGGER.error("", e);
}
return this;
}
use of com.blade.mvc.view.ModelAndView in project blade by biezhi.
the class MethodArgumentN method getArgs.
public static Object[] getArgs(Request request, Response response, Method actionMethod) throws Exception {
int parameterCount = 0;
for (final Parameter parameter : actionMethod.getParameters()) {
System.out.println("\targ" + parameterCount++ + ": " + (parameter.isNamePresent() ? parameter.getName() : "Parameter Name not provided,") + (isParameterFinal(parameter) ? " IS " : " is NOT ") + "final, type " + parameter.getType().getCanonicalName() + ", and parameterized type of " + parameter.getParameterizedType() + " and " + (parameter.isVarArgs() ? "IS " : "is NOT ") + "variable.");
}
Parameter[] parameters = actionMethod.getParameters();
Object[] args = new Object[parameters.length];
actionMethod.setAccessible(true);
for (int i = 0, len = parameters.length; i < len; i++) {
Parameter parameter = parameters[i];
Class<?> argType = parameter.getType();
if (argType == Request.class) {
args[i] = request;
continue;
}
if (argType == Response.class) {
args[i] = response;
continue;
}
if (argType == ModelAndView.class) {
args[i] = new ModelAndView();
continue;
}
if (argType == Map.class) {
args[i] = request.querys();
continue;
}
QueryParam queryParam = parameter.getAnnotation(QueryParam.class);
if (null != queryParam) {
String paramName = queryParam.value();
String val = request.query(paramName);
if (StringKit.isBlank(paramName)) {
paramName = parameter.getName();
val = request.query(paramName);
}
if (StringKit.isBlank(val)) {
val = queryParam.defaultValue();
}
args[i] = MethodArgument.getRequestParam(argType, val);
}
PathParam pathParam = parameter.getAnnotation(PathParam.class);
if (null != pathParam) {
String paramName = pathParam.value();
String val = request.pathParam(paramName);
if (StringKit.isBlank(paramName)) {
paramName = parameter.getName();
val = request.pathParam(paramName);
}
if (StringKit.isBlank(val)) {
throw new NotFoundException("path param [" + paramName + "] is null");
}
args[i] = MethodArgument.getRequestParam(argType, val);
}
HeaderParam headerParam = parameter.getAnnotation(HeaderParam.class);
if (null != headerParam) {
String paramName = headerParam.value();
String val = request.header(paramName);
if (StringKit.isBlank(paramName)) {
paramName = parameter.getName();
val = request.header(paramName);
}
args[i] = MethodArgument.getRequestParam(argType, val);
}
CookieParam cookieParam = parameter.getAnnotation(CookieParam.class);
if (null != cookieParam) {
String paramName = cookieParam.value();
String val = request.cookie(paramName);
if (StringKit.isBlank(paramName)) {
paramName = parameter.getName();
val = request.cookie(paramName);
}
args[i] = MethodArgument.getRequestParam(argType, val);
}
MultipartParam multipartParam = parameter.getAnnotation(MultipartParam.class);
if (null != multipartParam && argType == FileItem.class) {
String paramName = multipartParam.value();
FileItem val = request.fileItem(paramName);
if (StringKit.isBlank(paramName)) {
paramName = parameter.getName();
val = request.fileItem(paramName);
}
args[i] = val;
}
}
return args;
}
Aggregations