All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.avaje.http.generator.javalin.JavalinAdapter Maven / Gradle / Ivy

There is a newer version: 2.8-RC3
Show newest version
package io.avaje.http.generator.javalin;

import io.avaje.http.generator.core.Append;
import io.avaje.http.generator.core.ControllerReader;
import io.avaje.http.generator.core.ParamType;
import io.avaje.http.generator.core.PlatformAdapter;

import java.util.List;

class JavalinAdapter implements PlatformAdapter {

  static final String JAVALIN3_CONTEXT = "io.javalin.http.Context";
  static final String JAVALIN3_ROLES = "io.javalin.core.security.SecurityUtil.roles";

  @Override
  public boolean isContextType(String rawType) {
    return JAVALIN3_CONTEXT.equals(rawType);
  }

  @Override
  public String platformVariable(String rawType) {
    return "ctx";
  }

  @Override
  public boolean isBodyMethodParam() {
    return false;
  }

  @Override
  public String bodyAsClass(String shortType) {
    return "ctx.bodyAsClass(" + shortType + ".class)";
  }

  @Override
  public String indent() {
    return "    ";
  }

  @Override
  public void controllerRoles(List roles, ControllerReader controller) {
    addRoleImports(roles, controller);
  }

  @Override
  public void methodRoles(List roles, ControllerReader controller) {
    addRoleImports(roles, controller);
  }

  private void addRoleImports(List roles, ControllerReader controller) {
    controller.addStaticImportType(JAVALIN3_ROLES);
    for (String role : roles) {
      controller.addStaticImportType(role);
    }
  }

  @Override
  public void writeReadParameter(Append writer, ParamType paramType, String paramName) {
    writer.append("ctx.%s(\"%s\")", paramType, paramName);
  }

  @Override
  public void writeReadParameter(Append writer, ParamType paramType, String paramName, String paramDefault) {
    writer.append("ctx.%s(\"%s\",\"%s\")", paramType, paramName, paramDefault);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy