Search in sources :

Example 1 with PathPatternValidator

use of core.framework.impl.web.route.PathPatternValidator in project core-ng-project by neowu.

the class ModuleContext method route.

public void route(HTTPMethod method, String path, Controller controller, boolean skipInterceptor) {
    new PathPatternValidator(path).validate();
    ControllerInspector inspector = new ControllerInspector(controller);
    new ControllerClassValidator(inspector.targetClass, inspector.targetMethod).validate();
    String action = "http:" + ASCII.toLowerCase(method.name()) + ":" + path;
    httpServer.handler.route.add(method, path, new ControllerHolder(controller, inspector.targetMethod, inspector.controllerInfo, action, skipInterceptor));
}
Also used : ControllerClassValidator(core.framework.impl.web.ControllerClassValidator) PathPatternValidator(core.framework.impl.web.route.PathPatternValidator) ControllerHolder(core.framework.impl.web.ControllerHolder) ControllerInspector(core.framework.impl.web.ControllerInspector)

Example 2 with PathPatternValidator

use of core.framework.impl.web.route.PathPatternValidator in project core-ng-project by neowu.

the class WebServiceInterfaceValidator method validate.

private void validate(Method method) {
    validateHTTPMethod(method);
    HTTPMethod httpMethod = HTTPMethods.httpMethod(method);
    Path path = method.getDeclaredAnnotation(Path.class);
    if (path == null)
        throw Exceptions.error("method must have @Path, method={}", method);
    new PathPatternValidator(path.value()).validate();
    validateResponseBeanType(method.getGenericReturnType());
    Set<String> pathVariables = pathVariables(path.value());
    Type requestBeanType = null;
    Annotation[][] annotations = method.getParameterAnnotations();
    Type[] paramTypes = method.getGenericParameterTypes();
    Set<String> pathParams = Sets.newHashSet();
    for (int i = 0; i < paramTypes.length; i++) {
        Type paramType = paramTypes[i];
        PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
        if (pathParam != null) {
            validatePathParamType(paramType);
            pathParams.add(pathParam.value());
        } else {
            if (requestBeanType != null)
                throw Exceptions.error("service method must not have more than one bean param, previous={}, current={}", requestBeanType.getTypeName(), paramType.getTypeName());
            requestBeanType = paramType;
            if (httpMethod == HTTPMethod.GET || httpMethod == HTTPMethod.DELETE) {
                requestBeanMapper.registerQueryParamBean(requestBeanType);
            } else {
                requestBeanMapper.registerRequestBean(requestBeanType);
            }
        }
    }
    if (pathVariables.size() != pathParams.size() || !pathVariables.containsAll(pathParams))
        throw Exceptions.error("service method @PathParam params must match variable in path pattern, path={}, method={}", path.value(), method);
}
Also used : Path(core.framework.api.web.service.Path) Type(java.lang.reflect.Type) HTTPMethod(core.framework.http.HTTPMethod) PathPatternValidator(core.framework.impl.web.route.PathPatternValidator) PathParam(core.framework.api.web.service.PathParam)

Aggregations

PathPatternValidator (core.framework.impl.web.route.PathPatternValidator)2 Path (core.framework.api.web.service.Path)1 PathParam (core.framework.api.web.service.PathParam)1 HTTPMethod (core.framework.http.HTTPMethod)1 ControllerClassValidator (core.framework.impl.web.ControllerClassValidator)1 ControllerHolder (core.framework.impl.web.ControllerHolder)1 ControllerInspector (core.framework.impl.web.ControllerInspector)1 Type (java.lang.reflect.Type)1