Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method interception or validation of request params in Sitebricks controller

I'm using Sitebricks with Guice to implement REST service and I have a set of methods like this:

@Get
@At("/:version/har/mostRecentEntry/assertResponseTimeWithin")
public Reply<?> doSomething(@Named("version") int version, Request<String> request) {
// Validation logic for request parameters ...

// Extracting parameters (converting url params to domain area objects)

// Actual business logic
}

Which leads to a lot of copy/pasted code.

I'm looking for some way to separate common validating & extracting data logic from request parameters. Probably I can use AOP to do it, but maybe there's easier way Sitebricks provides?

like image 279
XZen Avatar asked Mar 20 '19 07:03

XZen


1 Answers

A few considerations:

  • Google's Sitebricks project is dead
  • official site is down (sitebricks.org)
  • last Github commit - 2015

My recommendation is to not build anything with that framework.

You should definitely consider alternatives for implementing REST services (e.g. SpringBoot).

maybe there's easier way Sitebricks provides?

That being said, Sitebricks doesn't seem to offer validation out of the box.

The code you can find related to validation in Sitebrick is:

@ImplementedBy(AlwaysValidationValidator.class)
public interface SitebricksValidator {

    Set<? extends ConstraintViolation<?>> validate(Object object);

}

and this:

public class AlwaysValidationValidator implements SitebricksValidator {

    @Override
    public Set<? extends ConstraintViolation<?>> validate(Object object) {
        return null; //unfinished
    }

}

This is unfinished implementation!

Your best option is to use javax validation in a standalone setup. This includes hibernate-validator + javax expression language - reference implementation of JSR 380. It has a lot of build in constraints (e.g. @NotNull, @Size etc.) and is extensible - you can create your own constraints implementing the right interfaces (the AOP part is handled by the framework).

A more simple alternative is Guava's PreConditions.

like image 163
hovanessyan Avatar answered Oct 02 '22 11:10

hovanessyan