Marc Hadley wrote:
> On Feb 11, 2008, at 6:24 PM, Dhanji R. Prasanna wrote:
>>
>> On Feb 12, 2008 8:31 AM, Bill Burke <bburke_at_redhat.com> wrote:
>> I think some form of @Cookie injection would be useful:
>>
>> @GET
>> public String get(@Cookie("myCookie") int value)
>> {
>> }
>>
>> Would save a lot of typing. The only problem I see with this is that it
>> overloads with javax.ws.rs.core.Cookie. Maybe rename the object to
>> CookieInstance?
>>
>> I'm thinking you could also do:
>>
>> @GET
>> public String get(@Cookie("myCookie") CookieInstance instance){}
>>
>> Hmm. Not a bad idea. Why not:
>>
>> public Response get(@Param("cookieName") javax.servlet...Cookie cookie);
>>
>> ?
>>
> Do you mean @HeaderParam or a new annotation @Param ?
>
A new @Param. I guess @CookieParam is it. Didn't think of following
the convention.
> I wonder if there's a way to unify all of this somehow like we did for
> method designators by making @HttpMethod a meta annotation. Something like:
>
> @Target({ElementType.ANNOTATION_TYPE})
> @Retention(RetentionPolicy.RUNTIME)
> @Documented
> public @interface RequestParam {
> public enum Source {HEADER, QUERY, MATRIX, PATH, COOKIE, FORM};
> Source source();
> }
>
> and then we could redefine our existing XXXParam annotations using this
> new meta annotation, e.g.:
>
> @Target({ElementType.PARAMETER})
> @Retention(RetentionPolicy.RUNTIME)
> @RequestParam(source=QUERY)
> public @interface QueryParam {
> String value();
> }
>
> Not sure if this really helps, it does allow others to define their own
> annotations but the list of sources is still fixed.
>
This does not support extensions very easily. How about a pluggable
InjectionProvider mechanism?
@InjectionPlugin
public @interface CookieParam {
String value();
}
@InjectionProvider(CookieParam.class)
public class CookieInjectionProvider {
@Injector
Object extractCookie(@Metadata CookieParam metadata, @HttpContext
HttpHeaders headers) {
String cookieName = metadata.value();
Cookie cookie = ...; // find cookie from HttpHeaders by cookieName
return cookie.getValue();
}
}
The InjectionProvider allows you to inject anything from the HTTP
Request like you would a regular JAX-RS method. Additionally a @Metadata
annotation is provided so that the provider can get access to the
injection annotation instance you used in your application code.
So, you could do this with all injectors except maybe @HttpContext as
you would need basic/core access to the request.
--
Bill Burke
JBoss, a division of Red Hat
http://bill.burkecentral.com