Is there a way to check applicationContextPath on check config command?

74 views
Skip to first unread message

Innokentiy Struchkov

unread,
Sep 12, 2022, 12:16:46 PM9/12/22
to dropwizard-user
Hi! I'm upgrading an old server to newer version of Dropwizard (1.x.x to 2.1.0) and other dependencies too. Now I have to explicitly check if user set the only correct applicationContextPath in server section of configuration file. Is there a way to do this in our Configuration implementation in constructor code (or within init block in Kotlin) or bootstrap initialization?

Jochen Schalanda

unread,
Sep 17, 2022, 5:29:08 PM9/17/22
to dropwizard-user
Hi,

You could (mis-) use Hibernate Validator for this and add a validation method with @AssertTrue to your configuration file in which you check the applicationContextPath.

@AssertTrue
public boolean validateApplicationContextPath() {
return server.getApplicationContextPath().equals("…“);
}

Cheers,
Jochen

Innokentiy Struchkov

unread,
Sep 18, 2022, 5:59:50 AM9/18/22
to dropwizard-user
Hi, Jochen!

Thanks for the advice, it does work! But I had to use a bit different approach with custom class annotation, like:

My configuration class (all code below in Kotlin):
@CheckApplicationContextPath
class MyConfiguration(
  val project: String,
  val env: String,
  ...
) : io.dropwizard:Configuration()

CheckApplicationContextPath.kt:
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@Constraint(validatedBy = [ApplicationContextPathValidator::class])
annotation class CheckApplicationContextPath(
  val message: String = "Please correct applicationContextPath in the config file",
  val groups: Array<KClass<*>> = [],
  val payload: Array<KClass<out Payload>> = []
)

ApplicationContextPathValidator.kt:
class ApplicationContextPathValidator : ConstraintValidator<CheckApplicationContextPathMyConfiguration > {
   override fun isValid(value: MyConfiguration , context: ConstraintValidatorContext): Boolean {
     val applicationContextPath = (value.serverFactory as SimpleServerFactory).applicationContextPath
     println("Config's applicationContextPath is $applicationContextPath")
     return applicationContextPath == "/myCorrectApplicationContextPath "
   }
}

Thanks again,
Innokentiy
Reply all
Reply to author
Forward
0 new messages