users@jersey.java.net

Re: [Jersey] Required parameters on a Resource

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Sat, 15 Aug 2009 10:24:00 +0200

Hi,

I am very very sorry, i led you on a wild goose chase with a late
Friday email and was not thinking properly :-(

After looking a bit more into the code this is not going to work.
StringReaderProvider operates on individual parameters (so will be
called multiple times for collections of stuff) and will only be
called if there is a non-null value present either because the
parameter is present or because it has a default value.

The only solutions i can think of are:

1) Jersey supports an @Required annotation; or

2) Exposes a lower-level abstraction of MultivaluedParameterExtractor
(which is an internally used interface):

      public interface MultivaluedParameterExtractor {
        Object extract(MultivaluedMap<String, String> parameters);
      }

      public interface MultivaluedParameterExtractorProvider { }

      public interface MultivaluedParameterExtractorWorkers { }

      and the same type of pattern can be utilized.

3) One can utilize AOP method-level interceptors to support @Required
and check for a null value. This solution is
      much better if one needs to take into account more than one value.

I also wonder if there is a more general approach to parameter
validation we can apply with the bean validation framework.

Currently I am leaning towards option 2. Could you log an issue so we
can track this?

Thanks,
Paul.

On Aug 14, 2009, at 10:47 PM, DirkM wrote:

>
> I had a go at writing the class, and although in the logs the Provider
> appears to get picked up, the getStringReader method is never actually
> called. I'm no expert in generics so perhaps I've missed something
> obvious.
> Here's my code:
>
> @Singleton
> @Provider
> public class SerializableStringProvider implements
> StringReaderProvider {
> @Context
> StringReaderWorkers srw;
>
> @Override
> public StringReader getStringReader(Class type, Type genericType,
> Annotation[] annotations) {
>
> // Create a copy of the annotations array, without the
> @Required
> // annotation
> Annotation[] otherAnnotations = new
> Annotation[annotations.length];
> int i = 0;
> for (Annotation annotation : annotations) {
> if (!annotation.getClass().equals(Required.class)) {
> otherAnnotations[i++] = annotation;
> }
> }
>
> // If all the annotations were copied, that means none of
> them was
> the
> // @Required annotation.
> if (i == annotations.length) {
> return null;
> }
>
> final StringReader<?> sr = srw.getStringReader(type,
> genericType,
> otherAnnotations);
> if (sr == null) {
> return null;
> }
>
> return new StringReader() {
> public Object fromString(String value) {
> if (value == null) {
> throw new WebApplicationException(400);
> }
> return sr.fromString(value);
> }
> };
> }
> }
> --
> View this message in context: http://n2.nabble.com/Required-parameters-on-a-Resource-tp3446928p3447394.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>