You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by apatel <ap...@asg.bellsouth.net> on 2013/02/14 15:49:05 UTC

Camel-restlet:setting Cache-Control metadata

We try to set the Cache-Control metadata in restlet route as below. But It
did not set Cache-Control on response. Also when i look at the code The
Restlet component Representation class does not have set Cache-Control
method.

Please help me to resolve this issue.


 <route xmlns="http://camel.apache.org/schema/spring" trace="true">
    <from
uri="restlet:/test/maxAgeCache?headerFilterStrategy=encoreRestletHeaderFilterStrategy"/>
    <setHeader headerName="Content-Type">
        <constant>application/json</constant>
    </setHeader>
    <setHeader headerName="Cache-Control">
        <constant>max-age=20</constant>
    </setHeader>
    <setBody>
        <constant>{ "status":"SUCCESS" }</constant>
    </setBody>
</route>

Struts2 also suggest to use the Cache-Control & Expires.
================================

Use the correct HTTP headers (Cache-Control & Expires).
When returning HTML views, make sure to add the correct headers so browsers
know how to cache them.

 http://struts.apache.org/development/2.x/docs/performance-tuning.html
=============================================
 
  
Below method in DefaultRestletBinding class only set the headers that set in
((Representation) message.getEntity()).setxxx methods.  I don't see the
Cache-Control sets anywhere.
 
 protected void setResponseHeader(Exchange exchange, org.restlet.Message
message, String header, Object value) {
        // put the header first
        message.getAttributes().put(header, value);
 
        // there must be a value going forward
        if (value == null) {
            return;
        }
 
        // special for certain headers
        if (message.getEntity() != null) {
            if (header.equalsIgnoreCase(HeaderConstants.HEADER_EXPIRES)) {
                if (value instanceof Calendar) {
                    message.getEntity().setExpirationDate(((Calendar)
value).getTime());
                } else if (value instanceof Date) {
                    message.getEntity().setExpirationDate((Date) value);
                } else if (value instanceof String) {
                    SimpleDateFormat format = new
SimpleDateFormat(RFC_2822_DATE_PATTERN, Locale.ENGLISH);
                    try {
                        Date date = format.parse((String) value);
                        message.getEntity().setExpirationDate(date);
                    } catch (ParseException e) {
                        LOG.debug("Header {} with value {} cannot be
converted as a Date. The value will be ignored.",
HeaderConstants.HEADER_EXPIRES, value);
                    }
                }
            }
 
            if
(header.equalsIgnoreCase(HeaderConstants.HEADER_LAST_MODIFIED)) {
                if (value instanceof Calendar) {
                    message.getEntity().setModificationDate(((Calendar)
value).getTime());
                } else if (value instanceof Date) {
                    message.getEntity().setModificationDate((Date) value);
                } else if (value instanceof String) {
                    SimpleDateFormat format = new
SimpleDateFormat(RFC_2822_DATE_PATTERN, Locale.ENGLISH);
                    try {
                        Date date = format.parse((String) value);
                        message.getEntity().setModificationDate(date);
                    } catch (ParseException e) {
                        LOG.debug("Header {} with value {} cannot be
converted as a Date. The value will be ignored.",
HeaderConstants.HEADER_LAST_MODIFIED, value);
                    }
                }
            }
 
            if
(header.equalsIgnoreCase(HeaderConstants.HEADER_CONTENT_LENGTH)) {
                if (value instanceof Long) {
                    message.getEntity().setSize((Long) value);
                } else if (value instanceof Integer) {
                    message.getEntity().setSize((Integer) value);
                } else {
                    Long num =
exchange.getContext().getTypeConverter().tryConvertTo(Long.class, value);
                    if (num != null) {
                        message.getEntity().setSize(num);
                    } else {
                        LOG.debug("Header {} with value {} cannot be
converted as a Long. The value will be ignored.",
HeaderConstants.HEADER_CONTENT_LENGTH, value);
                    }
                }
            }
 
            if
(header.equalsIgnoreCase(HeaderConstants.HEADER_CONTENT_TYPE)) {
                if (value instanceof MediaType) {
                    message.getEntity().setMediaType((MediaType) value);
                } else {
                    String type = value.toString();
                    MediaType media = MediaType.valueOf(type);
                    if (media != null) {
                        message.getEntity().setMediaType(media);
                    } else {
                        LOG.debug("Header {} with value {} cannot be
converted as a MediaType. The value will be ignored.",
HeaderConstants.HEADER_CONTENT_TYPE, value);
                    }
                }
            }
        }
    }
 



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-restlet-setting-Cache-Control-metadata-tp5727602.html
Sent from the Camel Development mailing list archive at Nabble.com.

Re: Camel-restlet:setting Cache-Control metadata

Posted by Willem jiang <wi...@gmail.com>.
Hi,

Please use the user@ mailing list instead of use dev@ to ask this kind of question.
I just checked the code of camel-restlet, it is a litter more complicate than just setup the Cache-Control header[1].

RestLet message provides the setCacheDirectives() method to do this kind of work.  I just fill a JIRA[1] in camel-restlet for it


[1]http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
[2]https://issues.apache.org/jira/browse/CAMEL-6085

--  
Willem Jiang

Red Hat, Inc.
FuseSource is now part of Red Hat
Web: http://www.fusesource.com | http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.iteye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem





On Thursday, February 14, 2013 at 10:49 PM, apatel wrote:

> We try to set the Cache-Control metadata in restlet route as below. But It
> did not set Cache-Control on response. Also when i look at the code The
> Restlet component Representation class does not have set Cache-Control
> method.
>  
> Please help me to resolve this issue.
>  
>  
> <route xmlns="http://camel.apache.org/schema/spring" trace="true">
> <from
> uri="restlet:/test/maxAgeCache?headerFilterStrategy=encoreRestletHeaderFilterStrategy"/>
> <setHeader headerName="Content-Type">
> <constant>application/json</constant>
> </setHeader>
> <setHeader headerName="Cache-Control">
> <constant>max-age=20</constant>
> </setHeader>
> <setBody>
> <constant>{ "status":"SUCCESS" }</constant>
> </setBody>
> </route>
>  
> Struts2 also suggest to use the Cache-Control & Expires.
> ================================
>  
> Use the correct HTTP headers (Cache-Control & Expires).
> When returning HTML views, make sure to add the correct headers so browsers
> know how to cache them.
>  
> http://struts.apache.org/development/2.x/docs/performance-tuning.html
> =============================================
>  
>  
> Below method in DefaultRestletBinding class only set the headers that set in
> ((Representation) message.getEntity()).setxxx methods. I don't see the
> Cache-Control sets anywhere.
>  
> protected void setResponseHeader(Exchange exchange, org.restlet.Message
> message, String header, Object value) {
> // put the header first
> message.getAttributes().put(header, value);
>  
> // there must be a value going forward
> if (value == null) {
> return;
> }
>  
> // special for certain headers
> if (message.getEntity() != null) {
> if (header.equalsIgnoreCase(HeaderConstants.HEADER_EXPIRES)) {
> if (value instanceof Calendar) {
> message.getEntity().setExpirationDate(((Calendar)
> value).getTime());
> } else if (value instanceof Date) {
> message.getEntity().setExpirationDate((Date) value);
> } else if (value instanceof String) {
> SimpleDateFormat format = new
> SimpleDateFormat(RFC_2822_DATE_PATTERN, Locale.ENGLISH);
> try {
> Date date = format.parse((String) value);
> message.getEntity().setExpirationDate(date);
> } catch (ParseException e) {
> LOG.debug("Header {} with value {} cannot be
> converted as a Date. The value will be ignored.",
> HeaderConstants.HEADER_EXPIRES, value);
> }
> }
> }
>  
> if
> (header.equalsIgnoreCase(HeaderConstants.HEADER_LAST_MODIFIED)) {
> if (value instanceof Calendar) {
> message.getEntity().setModificationDate(((Calendar)
> value).getTime());
> } else if (value instanceof Date) {
> message.getEntity().setModificationDate((Date) value);
> } else if (value instanceof String) {
> SimpleDateFormat format = new
> SimpleDateFormat(RFC_2822_DATE_PATTERN, Locale.ENGLISH);
> try {
> Date date = format.parse((String) value);
> message.getEntity().setModificationDate(date);
> } catch (ParseException e) {
> LOG.debug("Header {} with value {} cannot be
> converted as a Date. The value will be ignored.",
> HeaderConstants.HEADER_LAST_MODIFIED, value);
> }
> }
> }
>  
> if
> (header.equalsIgnoreCase(HeaderConstants.HEADER_CONTENT_LENGTH)) {
> if (value instanceof Long) {
> message.getEntity().setSize((Long) value);
> } else if (value instanceof Integer) {
> message.getEntity().setSize((Integer) value);
> } else {
> Long num =
> exchange.getContext().getTypeConverter().tryConvertTo(Long.class, value);
> if (num != null) {
> message.getEntity().setSize(num);
> } else {
> LOG.debug("Header {} with value {} cannot be
> converted as a Long. The value will be ignored.",
> HeaderConstants.HEADER_CONTENT_LENGTH, value);
> }
> }
> }
>  
> if
> (header.equalsIgnoreCase(HeaderConstants.HEADER_CONTENT_TYPE)) {
> if (value instanceof MediaType) {
> message.getEntity().setMediaType((MediaType) value);
> } else {
> String type = value.toString();
> MediaType media = MediaType.valueOf(type);
> if (media != null) {
> message.getEntity().setMediaType(media);
> } else {
> LOG.debug("Header {} with value {} cannot be
> converted as a MediaType. The value will be ignored.",
> HeaderConstants.HEADER_CONTENT_TYPE, value);
> }
> }
> }
> }
> }
>  
>  
>  
>  
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-restlet-setting-Cache-Control-metadata-tp5727602.html
> Sent from the Camel Development mailing list archive at Nabble.com (http://Nabble.com).