Search in sources :

Example 1 with PathVariable

use of com.tvd12.ezyhttp.server.core.annotation.PathVariable in project ezyhttp by youngmonkeys.

the class RequestHandlerImplementer method makeHandleRequestMethodContent.

@SuppressWarnings("MethodLength")
protected String makeHandleRequestMethodContent() {
    EzyMethod method = getHandleRequestMethod();
    EzyFunction function = new EzyFunction(method).throwsException();
    EzyBody body = function.body();
    int paramCount = 0;
    int headerCount = 0;
    int parameterCount = 0;
    int pathVariableCount = 0;
    int cookieCount = 0;
    Parameter[] parameters = handlerMethod.getParameters();
    for (Parameter parameter : parameters) {
        Class<?> parameterType = parameter.getType();
        Class<?> genericType = getGenericType(parameter);
        String genericTypeClass = genericType != null ? genericType.getName() + ".class" : "null";
        EzyInstruction instruction = new EzyInstruction("\t", "\n").clazz(parameterType).append(" ").append(PARAMETER_PREFIX).append(paramCount).equal();
        boolean hasAnnotation = false;
        RequestParam requestParamAnno = parameter.getAnnotation(RequestParam.class);
        if (requestParamAnno != null) {
            String paramKey = RequestParamAnnotations.getParamKeyString(requestParamAnno, parameterCount);
            String defaultValue = requestParamAnno.defaultValue();
            String getValueExpression = defaultValue.equals(EzyStrings.NULL) ? "arg0.getParameter(" + paramKey + ")" : "arg0.getParameter(" + paramKey + ", " + quote(defaultValue) + ")";
            String valueExpression = "this.deserializeParameter(" + paramKey + ", " + getValueExpression + ", " + parameterType.getTypeName() + ".class" + ", " + genericTypeClass + ")";
            instruction.cast(parameterType, valueExpression);
            ++parameterCount;
            hasAnnotation = true;
        }
        RequestHeader requestHeaderAnno = parameter.getAnnotation(RequestHeader.class);
        if (requestHeaderAnno != null) {
            String headerKey = RequestHeaderAnnotations.getHeaderKeyString(requestHeaderAnno, headerCount);
            String defaultValue = requestHeaderAnno.defaultValue();
            String getValueExpression = defaultValue.equals(EzyStrings.NULL) ? "arg0.getHeader(" + headerKey + ")" : "arg0.getHeader(" + headerKey + ", " + quote(defaultValue) + ")";
            String valueExpression = "this.deserializeHeader(" + headerKey + ", " + getValueExpression + ", " + parameterType.getTypeName() + ".class" + ", " + genericTypeClass + ")";
            instruction.cast(parameterType, valueExpression);
            ++headerCount;
            hasAnnotation = true;
        }
        PathVariable pathVariableAnno = parameter.getAnnotation(PathVariable.class);
        if (pathVariableAnno != null) {
            String varNameKey = PathVariableAnnotations.getVariableNameKeyString(pathVariableAnno, pathVariableCount);
            String valueExpression = "this.deserializePathVariable(" + varNameKey + ", arg0.getPathVariable(" + varNameKey + ")" + ", " + parameterType.getTypeName() + ".class" + ", " + genericTypeClass + ")";
            instruction.cast(parameterType, valueExpression);
            ++pathVariableCount;
            hasAnnotation = true;
        }
        RequestCookie requestCookieAnno = parameter.getAnnotation(RequestCookie.class);
        if (requestCookieAnno != null) {
            String cookieKey = RequestCookieAnnotations.getCookieKeyString(requestCookieAnno, cookieCount);
            String defaultValue = requestCookieAnno.defaultValue();
            String getValueExpression = defaultValue.equals(EzyStrings.NULL) ? "arg0.getCookieValue(" + cookieKey + ")" : "arg0.getCookieValue(" + cookieKey + ", " + quote(defaultValue) + ")";
            String valueExpression = "this.deserializeCookie(" + cookieKey + ", " + getValueExpression + ", " + parameterType.getTypeName() + ".class" + ", " + genericTypeClass + ")";
            instruction.cast(parameterType, valueExpression);
            ++cookieCount;
            hasAnnotation = true;
        }
        RequestBody requestBodyAnno = parameter.getAnnotation(RequestBody.class);
        if (requestBodyAnno != null) {
            instruction.brackets(parameterType).append("this.deserializeBody(").append("arg0, ").clazz(parameterType, true).append(")");
            hasAnnotation = true;
        }
        if (!hasAnnotation) {
            String valueExpression = "arg0";
            if (parameterType != RequestArguments.class) {
                String argumentKey = RequestParameters.getArgumentKeyString(parameter);
                valueExpression = "arg0.getArgument(" + argumentKey + ")";
            }
            instruction.cast(parameterType, valueExpression);
        }
        body.append(instruction);
        ++paramCount;
    }
    EzyInstruction instruction = new EzyInstruction("\t", "\n");
    Class<?> returnType = handlerMethod.getReturnType();
    if (returnType != void.class) {
        instruction.answer();
    }
    StringBuilder answerExpression = new StringBuilder();
    answerExpression.append("this.controller.").append(handlerMethod.getName()).append("(");
    for (int i = 0; i < paramCount; ++i) {
        answerExpression.append(PARAMETER_PREFIX).append(i);
        if (i < paramCount - 1) {
            answerExpression.append(", ");
        }
    }
    answerExpression.append(")");
    if (returnType != void.class) {
        instruction.valueOf(returnType, answerExpression.toString());
    } else {
        instruction.append(answerExpression);
    }
    body.append(instruction);
    if (returnType == void.class) {
        body.append(new EzyInstruction("\t", "\n").append("return null"));
    }
    return function.toString();
}
Also used : EzyBody(com.tvd12.ezyfox.asm.EzyFunction.EzyBody) RequestParam(com.tvd12.ezyhttp.server.core.annotation.RequestParam) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) EzyInstruction(com.tvd12.ezyfox.asm.EzyInstruction) Parameter(java.lang.reflect.Parameter) RequestHeader(com.tvd12.ezyhttp.server.core.annotation.RequestHeader) EzyFunction(com.tvd12.ezyfox.asm.EzyFunction) EzyMethod(com.tvd12.ezyfox.reflect.EzyMethod) PathVariable(com.tvd12.ezyhttp.server.core.annotation.PathVariable) RequestBody(com.tvd12.ezyhttp.server.core.annotation.RequestBody)

Example 2 with PathVariable

use of com.tvd12.ezyhttp.server.core.annotation.PathVariable in project ezyfox-examples by tvd12.

the class BookController method getBook.

@DoGet("/books/{bookId}")
public BookResponse getBook(@PathVariable Long bookId) {
    Book book = bookRepository.findById(bookId);
    if (book == null) {
        throw new HttpNotFoundException("not found book with id: " + bookId);
    }
    Author author = authorRepository.findById(book.getAuthorId());
    Category category = categoryRepository.findById(book.getCategoryId());
    return entityToResponseConverter.toBookResponse(book, author, category);
}
Also used : HttpNotFoundException(com.tvd12.ezyhttp.core.exception.HttpNotFoundException) Category(com.tvd12.ezydata.example.mongo.entity.Category) Book(com.tvd12.ezydata.example.mongo.entity.Book) Author(com.tvd12.ezydata.example.mongo.entity.Author)

Example 3 with PathVariable

use of com.tvd12.ezyhttp.server.core.annotation.PathVariable in project ezyfox-examples by tvd12.

the class BookController method getBook.

@DoGet("/books/{bookId}")
public BookResponse getBook(@PathVariable Long bookId) {
    Book book = bookMap.get(bookId);
    if (book == null) {
        throw new HttpNotFoundException("not found book with id: " + bookId);
    }
    Author author = authorMap.get(book.getAuthorId());
    Category category = categoryMap.get(book.getCategoryId());
    return entityToResponseConverter.toBookResponse(book, author, category);
}
Also used : HttpNotFoundException(com.tvd12.ezyhttp.core.exception.HttpNotFoundException) Category(com.tvd12.ezydata.example.redis.entity.Category) Book(com.tvd12.ezydata.example.redis.entity.Book) Author(com.tvd12.ezydata.example.redis.entity.Author)

Example 4 with PathVariable

use of com.tvd12.ezyhttp.server.core.annotation.PathVariable in project ezyfox-examples by tvd12.

the class UserViewController method getUser.

@DoGet("/{username}")
public View getUser(@PathVariable("username") String username) {
    User user = userService.getUser(username);
    String msg = user != null ? "User " + username + " with info: " + user : "User " + username + " not found";
    return View.builder().addVariable("message", msg).template("user-info").build();
}
Also used : User(org.youngmonkeys.example.ezyhttp.website.user_management.entity.User) DoGet(com.tvd12.ezyhttp.server.core.annotation.DoGet)

Example 5 with PathVariable

use of com.tvd12.ezyhttp.server.core.annotation.PathVariable in project java-examples by tvd12.

the class BookController method getBook.

@GetMapping("/books/{bookId}")
public BookResponse getBook(@PathVariable Long bookId) {
    Book book = bookRepository.findById(bookId).orElseThrow(() -> new HttpNotFoundException("not found book with id: " + bookId));
    Author author = authorRepository.findById(book.getAuthorId()).orElseThrow(() -> new IllegalStateException("maybe someone has change redis"));
    Category category = categoryRepository.findById(book.getCategoryId()).orElseThrow(() -> new IllegalStateException("maybe someone has change redis"));
    return entityToResponseConverter.toBookResponse(book, author, category);
}
Also used : HttpNotFoundException(com.tvd12.example.spring_boot_redis.exception.HttpNotFoundException) Category(com.tvd12.example.spring_boot_redis.entity.Category) Book(com.tvd12.example.spring_boot_redis.entity.Book) Author(com.tvd12.example.spring_boot_redis.entity.Author)

Aggregations

HttpNotFoundException (com.tvd12.ezyhttp.core.exception.HttpNotFoundException)2 Author (com.tvd12.example.spring_boot_redis.entity.Author)1 Book (com.tvd12.example.spring_boot_redis.entity.Book)1 Category (com.tvd12.example.spring_boot_redis.entity.Category)1 HttpNotFoundException (com.tvd12.example.spring_boot_redis.exception.HttpNotFoundException)1 Author (com.tvd12.ezydata.example.mongo.entity.Author)1 Book (com.tvd12.ezydata.example.mongo.entity.Book)1 Category (com.tvd12.ezydata.example.mongo.entity.Category)1 Author (com.tvd12.ezydata.example.redis.entity.Author)1 Book (com.tvd12.ezydata.example.redis.entity.Book)1 Category (com.tvd12.ezydata.example.redis.entity.Category)1 EzyFunction (com.tvd12.ezyfox.asm.EzyFunction)1 EzyBody (com.tvd12.ezyfox.asm.EzyFunction.EzyBody)1 EzyInstruction (com.tvd12.ezyfox.asm.EzyInstruction)1 EzyMethod (com.tvd12.ezyfox.reflect.EzyMethod)1 DoGet (com.tvd12.ezyhttp.server.core.annotation.DoGet)1 PathVariable (com.tvd12.ezyhttp.server.core.annotation.PathVariable)1 RequestBody (com.tvd12.ezyhttp.server.core.annotation.RequestBody)1 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)1 RequestHeader (com.tvd12.ezyhttp.server.core.annotation.RequestHeader)1