0

i have a problem because of custom ConstraintValidatorContext initialize() called first time only when this custom inject called , and after that it run the method isValid only() without construct initialize() method again every time ! .

example @APIAuth annotation:

@Documented
@Constraint(validatedBy = APIAuthValidator.class)
@Target({ElementType.CONSTRUCTOR,ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface APIAuth {
    String message() default "";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

APIAuthValidator.class

public class APIAuthValidator implements ConstraintValidator<APIAuth, String>{

    @Override
    public void initialize(APIAuth constraintAnnotation) {
        System.out.println("echo first time only");             
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {

        return true;
    }
}

Example for usage inside Restful Stateless Session bean

@Stateless
@Path("Accepted")
public class AcceptedAPI {

    @Context
    private UriInfo context;
    @Context
    private HttpServletRequest client;
    @PersistenceContext (unitName="APIPU") EntityManager EM;

    @GET
    @APIAuth
    @Produces(MediaType.TEXT_PLAIN)
    public String getIps() {
        return "Test";
    }
}

so when i request the stateless , it echo on console echo first time only first time , and when i request the stateless again , it don't echo any thing again !

so i think that my custom Validator inject is initialized and cached since first time of it's calling inside the java environment ,

so How i make my custom inject to force initialize every time i call it ?

Thank you,

Jason4Ever
  • 1,439
  • 4
  • 23
  • 43
  • A single ConstraintValidator is shared across all threads, this is pretty fundamental to the way Hibernate Validator is implemented, I don't think it's something you could just configure away. – Affe Jan 27 '14 at 17:03
  • so there is no a way to do that , so i must find another way to do what i want to do inside this custom inject ? – Jason4Ever Jan 27 '14 at 17:22
  • Perhaps if you described what you actually want to accomplish we could suggest a way? – Affe Jan 27 '14 at 17:23
  • i want to compare the remote ip of the request user , by assign ip value using `client.getRemoteAddr()` , i assign it's value to `String ip` inside `initialize()` , and it's work at the first time when user request the stateless , but when user try to re-request the `Stateless` again and this `Custom Inject` run again then it return an exception because `@APIAuth` inject don't start from the first , but it continue , so the `client` object be null at the second try and third, and.... , a reference for my problem also : http://stackoverflow.com/questions/21368583 – Jason4Ever Jan 27 '14 at 17:31
  • Don't understand why you can't just look up the remote IP in isValid() instead of initialize() as the API was designed to be used? – Affe Jan 27 '14 at 17:34
  • i tried of course to look up the remote IP in isValid() `client.getRemoteAddr()` , and it work fine at the first request of Stateless, but at the second time it throw exception , because it be null object ! , i don't know why ! but i must wait some of seconds before try request the `Stateless` again to re-fetch the remote ip again !.. i think that `@Context HttpServletRequest client` being null after the first calling time ! – Jason4Ever Jan 27 '14 at 17:40

0 Answers0