0

i created a custom ConstraintValidator Annotation which called APIAuth, and i tried to fetch the remote user IP Address Using @Context HttpServletRequest , but the problem is when calling the APIAuth inside my Stateless , the APIAuth custom validator work , and it fetch the remote user ip address , but when i try to refresh the request again quickly , it throw an exception , because of @Context HttpServletRequest be null at this case , it need from me to wait some seconds before trying to request the Stateless Restful again .

my APIAuth Code :

APIAuth.class

@Documented
@Constraint(validatedBy = APIAuthValidator.class)
@Target({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> {

@Context
private HttpServletRequest client;

@Override
public void initialize(APIAuth constraintAnnotation) {
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
    boolean valid = "127.0.0.1".equals(client.getRemoteAddr());

    return valid;
}

Note: i noticed also that when i define the remote user ip address at first inside the initialize() constructor , then it cache the value and didn't update it else after some of seconds , although of refreshing requesting of Restful Stateless API class which contain the calling of my custom annotation many times.

Thank you,

user207421
  • 305,947
  • 44
  • 307
  • 483
Jason4Ever
  • 1,439
  • 4
  • 23
  • 43
  • You should be returning 'valid', not 'true'. Your code doesn't even work. It isn't likely to have anything to do with the fact that you're testing the IP address. – user207421 Jan 26 '14 at 21:11
  • Hello , i edited my post , of course i return 'valid' , but i was testing to determine where is the error, i have also other uses to do with this custom validator , not to determine remote user ip only, – Jason4Ever Jan 26 '14 at 21:58
  • And do they work? Or exhibit the same behaviour? Do you really have some evidence that it is getting the IP address that is causing the malfunction? – user207421 Jan 26 '14 at 22:02
  • yes the problem caused by this line : `client.getRemoteAddr()` , when the custom validator run for the first time , then `client.getRemoteAddr()` return the remote ip address correclty , but when try to recall it again and again , then the object 'client' be null , so when call the sub method `getRemoteAddr()` from it , it cause the error.. and to avoid this error , i must wait some of seconds between 2 to 5 seconds nearly before recalling the custom validator again. – Jason4Ever Jan 26 '14 at 22:06
  • So the actual problem isn't getting the IP address at all. That's just a symptom. It is that 'client' can be null. I suggest you amend your title and question accordingly. – user207421 Jan 26 '14 at 22:09

0 Answers0