ClientResource.wrap Response Variants?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

ClientResource.wrap Response Variants?

Norm Deane
I'm using the ClientResource (Restlet 2.0.10) and would like to explicitly control the media types that my client will accept. More specifically I only want my client to accept "application/xml" but in looking at the code in ClientResource.wrap...

                        // Updates the client preferences
                        List<org.restlet.representation.Variant> responseVariants = annotation
                                .getResponseVariants(getMetadataService(),
                                        getConverterService());

                        if (responseVariants != null) {
                            request.setClientInfo(new ClientInfo(
                                    responseVariants));
                        }

It looks like it will always override any preferences I set on my ClientInfo with whatever the ConvertService is capable of handling. Is there a way around this?

Thanks,

Norm

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: ClientResource.wrap Response Variants?

Norm Deane
I was able to restrict the media types that my client was willing to accept by registering only the converters for the media types I wanted the client to accept.

List<ConverterHelper> converters = new ArrayList<ConverterHelper>();
converters.add(new OrderingContextConverter(new XmlOrderingContextDataBinder()));
converters.add(new OrderModificationPlanConverter(new XmlOrderModificationPlanDataBinder()));
   
Engine.getInstance().setRegisteredConverters(converters);

Is this the only way to do this? If not, is this the correct approach?

Thanks,

Norm

> I'm using the ClientResource (Restlet 2.0.10) and would like to explicitly control the media types that my client will accept. More specifically I only want my client to accept "application/xml" but in looking at the code in ClientResource.wrap...
>
>                         // Updates the client preferences
>                         List<org.restlet.representation.Variant> responseVariants = annotation
>                                 .getResponseVariants(getMetadataService(),
>                                         getConverterService());
>
>                         if (responseVariants != null) {
>                             request.setClientInfo(new ClientInfo(
>                                     responseVariants));
>                         }
>
> It looks like it will always override any preferences I set on my ClientInfo with whatever the ConvertService is capable of handling. Is there a way around this?
>
> Thanks,
>
> Norm

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2884392
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ClientResource.wrap Response Variants?

Fabian Mandelbaum
Hello. The correct way to restrict accepted media types for a client
(read: in an HTTP request) is by means of the HTTP header Accept.
Please take a look here:
http://wiki.restlet.org/docs_2.0/130-restlet.html for Restlet mappings
for HTTP headers (that page will tell you the property of which class
you have to set/get to manipulate the headers).

In  your case, I guess it's something like:

Request theRequest = ....
theRequest.getClientInfo().getAcceptedMediaTypes().put(MediaType.APPLICATION_XML);
// Perform theRequest here

Hope this helps.

On Tue, Nov 22, 2011 at 1:31 PM, Norm Deane <[hidden email]> wrote:

> I was able to restrict the media types that my client was willing to accept by registering only the converters for the media types I wanted the client to accept.
>
> List<ConverterHelper> converters = new ArrayList<ConverterHelper>();
> converters.add(new OrderingContextConverter(new XmlOrderingContextDataBinder()));
> converters.add(new OrderModificationPlanConverter(new XmlOrderModificationPlanDataBinder()));
>
> Engine.getInstance().setRegisteredConverters(converters);
>
> Is this the only way to do this? If not, is this the correct approach?
>
> Thanks,
>
> Norm
>
>> I'm using the ClientResource (Restlet 2.0.10) and would like to explicitly control the media types that my client will accept. More specifically I only want my client to accept "application/xml" but in looking at the code in ClientResource.wrap...
>>
>>                         // Updates the client preferences
>>                         List<org.restlet.representation.Variant> responseVariants = annotation
>>                                 .getResponseVariants(getMetadataService(),
>>                                         getConverterService());
>>
>>                         if (responseVariants != null) {
>>                             request.setClientInfo(new ClientInfo(
>>                                     responseVariants));
>>                         }
>>
>> It looks like it will always override any preferences I set on my ClientInfo with whatever the ConvertService is capable of handling. Is there a way around this?
>>
>> Thanks,
>>
>> Norm
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2884392
>



--
Fabián Mandelbaum
IS Engineer

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2884402
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Re: ClientResource.wrap Response Variants?

Norm Deane
Yes but when using the ClientResource.wrap approach you're abstracted from all of that (the Request, Response, ClientInfo, etc) and the wrap method creates an anonymous InvocationHandler/Dynamic Proxy that sets the ClientInfo for you like so...

// Updates the client preferences
List<org.restlet.representation.Variant> responseVariants =
annotation.getResponseVariants(getMetadataService(),
getConverterService());

if (responseVariants != null) {
   request.setClientInfo(new ClientInfo(responseVariants));
}

So even if I had previously set my media type preferences on the Request (through ClientResource.setClientInfo()) the anonymous InvocationHandler created by wrap will override them and specify all media types that the ConverterService is capable of handling.

Norm


> Hello. The correct way to restrict accepted media types for a client
> (read: in an HTTP request) is by means of the HTTP header Accept.
> Please take a look here:
> http://wiki.restlet.org/docs_2.0/130-restlet.html for Restlet mappings
> for HTTP headers (that page will tell you the property of which class
> you have to set/get to manipulate the headers).
>
> In  your case, I guess it's something like:
>
> Request theRequest = ....
> theRequest.getClientInfo().getAcceptedMediaTypes().put(MediaType.APPLICATION_XML);
> // Perform theRequest here
>
> Hope this helps.
>
> On Tue, Nov 22, 2011 at 1:31 PM, Norm Deane <norm dot deane at vanderbilt dot edu> wrote:
> > I was able to restrict the media types that my client was willing to accept by registering only the converters for the media types I wanted the client to accept.
> >
> > List<ConverterHelper> converters = new ArrayList<ConverterHelper>();
> > converters.add(new OrderingContextConverter(new XmlOrderingContextDataBinder()));
> > converters.add(new OrderModificationPlanConverter(new XmlOrderModificationPlanDataBinder()));
> >
> > Engine.getInstance().setRegisteredConverters(converters);
> >
> > Is this the only way to do this? If not, is this the correct approach?
> >
> > Thanks,
> >
> > Norm
> >
> >> I'm using the ClientResource (Restlet 2.0.10) and would like to explicitly control the media types that my client will accept. More specifically I only want my client to accept "application/xml" but in looking at the code in ClientResource.wrap...
> >>
> >>                         // Updates the client preferences
> >>                         List<org.restlet.representation.Variant> responseVariants = annotation
> >>                                 .getResponseVariants(getMetadataService(),
> >>                                         getConverterService());
> >>
> >>                         if (responseVariants != null) {
> >>                             request.setClientInfo(new ClientInfo(
> >>                                     responseVariants));
> >>                         }
> >>
> >> It looks like it will always override any preferences I set on my ClientInfo with whatever the ConvertService is capable of handling. Is there a way around this?
> >>
> >> Thanks,
> >>
> >> Norm
> >
> > ------------------------------------------------------
> > http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2884392
> >
>
>
>
> --
> Fabián Mandelbaum
> IS Engineer

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2884405
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Re: ClientResource.wrap Response Variants?

jlouvel
Administrator
Hi Norm,

Could you explain why you want to restrict the accepted media types in this case (Restlet annotated interfaces)?

Another workaround is to add an annotation value such as @Get("xml") to enforce XML usage. Otherwise, we plan to add a per-application control of the converter used/excepted and their relative order in next Restlet version. Right now this setting is global to the Restlet engine.

Best regards,
Jerome
--
http://www.restlet.org
http://twitter.com/#!/jlouvel




-----Message d'origine-----
De : Norm Deane [mailto:[hidden email]]
Envoyé : mardi 22 novembre 2011 18:26
À : [hidden email]
Objet : RE: Re: ClientResource.wrap Response Variants?

Yes but when using the ClientResource.wrap approach you're abstracted from all of that (the Request, Response, ClientInfo, etc) and the wrap method creates an anonymous InvocationHandler/Dynamic Proxy that sets the ClientInfo for you like so...

// Updates the client preferences
List<org.restlet.representation.Variant> responseVariants = annotation.getResponseVariants(getMetadataService(),
getConverterService());

if (responseVariants != null) {
   request.setClientInfo(new ClientInfo(responseVariants)); }

So even if I had previously set my media type preferences on the Request (through ClientResource.setClientInfo()) the anonymous InvocationHandler created by wrap will override them and specify all media types that the ConverterService is capable of handling.

Norm


> Hello. The correct way to restrict accepted media types for a client
> (read: in an HTTP request) is by means of the HTTP header Accept.
> Please take a look here:
> http://wiki.restlet.org/docs_2.0/130-restlet.html for Restlet mappings
> for HTTP headers (that page will tell you the property of which class
> you have to set/get to manipulate the headers).
>
> In  your case, I guess it's something like:
>
> Request theRequest = ....
> theRequest.getClientInfo().getAcceptedMediaTypes().put(MediaType.APPLI
> CATION_XML);
> // Perform theRequest here
>
> Hope this helps.
>
> On Tue, Nov 22, 2011 at 1:31 PM, Norm Deane <norm dot deane at vanderbilt dot edu> wrote:
> > I was able to restrict the media types that my client was willing to accept by registering only the converters for the media types I wanted the client to accept.
> >
> > List<ConverterHelper> converters = new ArrayList<ConverterHelper>();
> > converters.add(new OrderingContextConverter(new
> > XmlOrderingContextDataBinder())); converters.add(new
> > OrderModificationPlanConverter(new
> > XmlOrderModificationPlanDataBinder()));
> >
> > Engine.getInstance().setRegisteredConverters(converters);
> >
> > Is this the only way to do this? If not, is this the correct approach?
> >
> > Thanks,
> >
> > Norm
> >
> >> I'm using the ClientResource (Restlet 2.0.10) and would like to explicitly control the media types that my client will accept. More specifically I only want my client to accept "application/xml" but in looking at the code in ClientResource.wrap...
> >>
> >>                         // Updates the client preferences
> >>                         List<org.restlet.representation.Variant>
> >> responseVariants = annotation
> >>                                
> >> .getResponseVariants(getMetadataService(),
> >>                                         getConverterService());
> >>
> >>                         if (responseVariants != null) {
> >>                             request.setClientInfo(new ClientInfo(
> >>                                     responseVariants));
> >>                         }
> >>
> >> It looks like it will always override any preferences I set on my ClientInfo with whatever the ConvertService is capable of handling. Is there a way around this?
> >>
> >> Thanks,
> >>
> >> Norm
> >
> > ------------------------------------------------------
> > http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessage
> > Id=2884392
> >
>
>
>
> --
> Fabián Mandelbaum
> IS Engineer

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2884405

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2885139
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Re: ClientResource.wrap Response Variants?

Norm Deane
Hi Jerome,

Thanks for your response.

> Could you explain why you want to restrict the accepted media types in this case (Restlet annotated interfaces)?
>

Mainly just because we would prefer XML over the wire instead of serialized java objects. It seems like the headaches of maintaining serial version compatibility would be undesirable. Also, initially all of our clients (one using Restlet framework and others using other technologies) will all be using XML and we've written our own custom converters so that we have complete control over the XML format.

> Another workaround is to add an annotation value such as @Get("xml") to enforce XML usage. Otherwise, we plan to add a per-application control of the converter used/excepted and their relative order in next Restlet version. Right now this setting is global to the Restlet engine.
>

I'd hate to start adding media type values in the annotations on my interface as there is a possibility that a client might come along that would prefer JSON or some other format. What version will include the per-application control?

Thanks,

Norm

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2885200
Loading...