RE: Re: Multiple content types

2009-07-01 Thread Sherif Ahmed
Okay..
I had to add an XLS Representation for a resource and I wan to support a suffix 
of .json for JSON and .XLS for Excel , but using the code below (and rest of 
the code in the previous thread).

no matter what extension I use (even other than XLS OR JSON). the 
Variant.Representation in my represent(Variant variant) method in the resource 
class always picks up JSON as the type..

Thanks

public HelloWorldResource(Context context, Request request, Response response) {

 super(context, request, response);
getVariants().add(new Variant(MediaType.APPLICATION_EXCEL));
getVariants().add(new Variant(MediaType.APPLICATION_JSON));

}


 Hi Sherif,
 
 .js is javascript
 .json is json :0)
 
 There is no need to add js or json to the MetadataService as it comes 
 with a bunch of common ones already.
 Remove that line, and try
 
 http://localhost:8080/firstStepsServlet/Hello.json (Notice the JSON)
 
 
 Jon
 
 Sherif wrote:
 
  Thanks Jonathan..
 
  1. Here’s what I did to support lets say “.js” as an extension to 
  indicate JSON as the content type
 
  *public* *class* FirstStepsApplication *extends* Application {
 
  /**
 
  * Creates a root Restlet that will receive all incoming calls.
 
  */
 
  @Override
 
  *public* Restlet createRoot() {
 
  // Create a router Restlet that routes each call to a
 
  // new instance of HelloWorldResource.
 
  Router router = *new* Router(getContext());
 
  // Defines only one route
 
  router.attachDefault(HelloWorldResource.*class*);
 
  *this*.getTunnelService().setExtensionsTunnel(*true*);
 
  *this*.getTunnelService().setEncodingParameter(output);
 
  *this*.getMetadataService().addExtension(js, *new* 
  Metadata(MediaType./APPLICATION_JSON/.getName(), 
  MediaType./APPLICATION_JSON/.getDescription()), *true* );
 
  *return* router;
 
  }
 
  }
 
  *public* *class* HelloWorldResource *extends* Resource {
 
  *public* HelloWorldResource(Context context, Request request, Response 
  response) {
 
  *super*(context, request, response);
 
  // This representation has only one type of representation.
 
  getVariants().add(*new* Variant(MediaType./TEXT_PLAIN/));
 
  getVariants().add(*new* Variant(MediaType./APPLICATION_JSON/));
 
  }
 
  /**
 
  * Returns a full representation for a given variant.
 
  */
 
  @Override
 
  *public* Representation represent(Variant variant) *throws* 
  ResourceException {
 
  Representation representation = *null*;
 
  *if*(variant.getMediaType() == MediaType./APPLICATION_JSON/){
 
  representation = *new* JsonRepresentation(Hello World);
 
  }*else* *if* ( variant.getMediaType() == MediaType./TEXT_PLAIN/){
 
  representation = *new* StringRepresentation(
 
  hello, world, MediaType./TEXT_PLAIN/);
 
  }
 
  *return* representation;
 
  }
 
  }
 
  However when I call the service using the following URL 
  http://localhost:8080/firstStepsServlet/Hello.js the Variant type in 
  the HelloWorldResource is of MediaType.TEXT_PLAIN
 
  Thanks for Your help
 
  *From:* Jonathan Hall (via Nabble) 
  [mailto:ml-user+125526-1692215...@... 
  http://n2.nabble.com/user/SendEmail.jtp?type=nodenode=3043073i=0]
  *Sent:* Friday, June 05, 2009 1:29 PM
  *To:* Sherif
  *Subject:* Re: Multiple content types
 
  Have a look at
  http://www.restlet.org/documentation/2.0/api/org/restlet/service/TunnelService.html
 
  getTunnelService().setExtensionsTunnel(true);
 
  Jon
 
  Sherif wrote:
   I realize RESTLet supports multiple encodings based on the Accept 
  Encoding
   headers. Does Restlet also have a way to allow encodings based on 
  URI patter
   e.g. http://mystores/items/1000.json or something like that ?
  
 
  --
  http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2359775
   
  http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2359775
 
  
 
  This email is a reply to your post @ 
  http://n2.nabble.com/Multiple-content-types-tp3031817p3031841.html
  You can reply by email or by visting the link above.
 
 
  
  View this message in context: RE: Multiple content types 
  http://n2.nabble.com/Multiple-content-types-tp3031817p3043073.html
  Sent from the Restlet Discuss mailing list archive 
  http://n2.nabble.com/Restlet-Discuss-f1400322.html at Nabble.com.

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


Last-Modified Header

2009-06-10 Thread Sherif Ahmed
I've been trying to add Last-Modified Header via the following code in a Filter 
afterHandle method

Form responseHeaders = (Form) 
response.getAttributes().get(org.restlet.http.headers);   
  
if (responseHeaders == null)   
{   
responseHeaders = new Form();   
response.getAttributes().put(org.restlet.http.headers, 
responseHeaders);   
}   
responseHeaders.add(entity.modificationDate, Sun, 06 Nov 2005 14:59:42 
GMT);


However the header in the HTTP Response is not Last-Modified as I would have 
expected, rather a custom header

entity.modificationDate: Sun, 06 Nov 2005 14:59:42 GMT

Quick response would be much appreciated

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


RE: Re: Last-Modified Header

2009-06-10 Thread Sherif Ahmed
Thanks Jon.

What I am trying to accomplish is implementing the Last-Modified / 
If-Modified-Since logic. Where I would tag responses with a uniform 
Last-Modified header for all resources/representations and when a request 
comes in with a If-Modified-Since header I'd send appropriate HTTP 304 
response or completely process the response.

Ideas/Best Practices to accomplish this without having to get the 
HttpServletRequest/Response objects to do this would be great

 Hi Sherif,
 
 For custom headers whatever name you give,  entity.modificationDate,  
 will be used.
 
 However, what you probably meant to do is use setModificationDate(new 
 Date()) on the entity/response representation. ie. 
 representation.setModificationDate(new Date());
 
 Jon
 
 Sherif Ahmed wrote:
  I've been trying to add Last-Modified Header via the following code in a 
  Filter afterHandle method
 
  Form responseHeaders = (Form) 
  response.getAttributes().get(org.restlet.http.headers);   

  if (responseHeaders == null)   
  {   
  responseHeaders = new Form();   
  response.getAttributes().put(org.restlet.http.headers, 
  responseHeaders);   
  }   
  responseHeaders.add(entity.modificationDate, Sun, 06 Nov 2005 14:59:42 
  GMT);
 
 
  However the header in the HTTP Response is not Last-Modified as I would 
  have expected, rather a custom header
 
  entity.modificationDate: Sun, 06 Nov 2005 14:59:42 GMT
 
  Quick response would be much appreciated
 
  --
  http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2360858
 

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


RE: Last-Modified Header

2009-06-10 Thread Sherif Ahmed
Brilliant..
The more I use Restlet the more I am liking it indeed.

Now does Restlet has a framework to take care of Gzip encoding results when the 
Request headers indicate that the request is from a client that supports this 
encoding (all modern browsers do)

 I've been trying to add Last-Modified Header via the following code in a 
 Filter afterHandle method
 
 Form responseHeaders = (Form) 
 response.getAttributes().get(org.restlet.http.headers);   
 
 if (responseHeaders == null)   
 {   
   responseHeaders = new Form();   
   response.getAttributes().put(org.restlet.http.headers, 
 responseHeaders);   
 }   
 responseHeaders.add(entity.modificationDate, Sun, 06 Nov 2005 14:59:42 
 GMT);  
 
 
 However the header in the HTTP Response is not Last-Modified as I would 
 have expected, rather a custom header
 
 entity.modificationDate: Sun, 06 Nov 2005 14:59:42 GMT
 
 Quick response would be much appreciated

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


RE: Re: Re: Last-Modified Header

2009-06-10 Thread Sherif Ahmed
Cool,

This works as you indicate. However implementing this way has a downside. Would 
be nice that the framework could take care of sending a 304 even without having 
to get a concrete Representation which has a date set.

The idea is to avoid creating a Representation if the Resource has not changed 
and Restlet could send a 304 directly thus avoiding the cost that may be 
associated with building a Representation.

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