camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r1447001 - in /camel/trunk/components/camel-restlet/src: main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java test/java/org/apache/camel/component/restlet/RestletResponseTest.java
Date Sun, 17 Feb 2013 08:19:33 GMT
Author: ningjiang
Date: Sun Feb 17 08:19:32 2013
New Revision: 1447001

URL: http://svn.apache.org/r1447001
Log:
CAMEL-6085 provides the cache-control header setting in camel-restlet

Modified:
    camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
    camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java

Modified: camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java?rev=1447001&r1=1447000&r2=1447001&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
(original)
+++ camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
Sun Feb 17 08:19:32 2013
@@ -22,8 +22,10 @@ import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import javax.xml.transform.dom.DOMSource;
@@ -38,6 +40,7 @@ import org.apache.camel.spi.HeaderFilter
 import org.apache.camel.util.MessageHelper;
 import org.restlet.Request;
 import org.restlet.Response;
+import org.restlet.data.CacheDirective;
 import org.restlet.data.ChallengeResponse;
 import org.restlet.data.ChallengeScheme;
 import org.restlet.data.CharacterSet;
@@ -289,6 +292,8 @@ public class DefaultRestletBinding imple
         MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), false);
     }
 
+    
+    @SuppressWarnings("unchecked")
     protected void setResponseHeader(Exchange exchange, org.restlet.Message message, String
header, Object value) {
         // put the header first
         message.getAttributes().put(header, value);
@@ -297,9 +302,20 @@ public class DefaultRestletBinding imple
         if (value == null) {
             return;
         }
-
+        
         // special for certain headers
         if (message.getEntity() != null) {
+            if (header.equalsIgnoreCase(HeaderConstants.HEADER_CACHE_CONTROL)) {
+                if (value instanceof List) {
+                    message.setCacheDirectives((List<CacheDirective>) value);
+                }
+                if (value instanceof String) {
+                    List<CacheDirective> list = new ArrayList<CacheDirective>();
+                    // set the cache control value directive
+                    list.add(new CacheDirective((String) value));
+                    message.setCacheDirectives(list);
+                }
+            }
             if (header.equalsIgnoreCase(HeaderConstants.HEADER_EXPIRES)) {
                 if (value instanceof Calendar) {
                     message.getEntity().setExpirationDate(((Calendar) value).getTime());

Modified: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java?rev=1447001&r1=1447000&r2=1447001&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java
(original)
+++ camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java
Sun Feb 17 08:19:32 2013
@@ -16,7 +16,9 @@
  */
 package org.apache.camel.component.restlet;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.camel.CamelExecutionException;
@@ -26,6 +28,8 @@ import org.apache.camel.builder.RouteBui
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.junit.Test;
+import org.restlet.data.CacheDirective;
+import org.restlet.engine.http.header.HeaderConstants;
 
 /**
  *
@@ -46,17 +50,39 @@ public class RestletResponseTest extends
                         exchange.getOut().setBody("{" + userName + "}");
                         exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, "417");
                         exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/JSON");
+                        // set the cache control with String
+                        exchange.getOut().setHeader(HeaderConstants.HEADER_CACHE_CONTROL,
"max-age=20");
                     }        
                 });
+                
+                from("restlet:http://localhost:" + portNum + "/cached/{username}?restletMethod=POST").process(new
Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        String userName = exchange.getIn().getHeader("username", String.class);
                       
+                        assertNotNull("userName should not be null", userName);
+                        exchange.getOut().setBody("{" + userName + "}");
+                        exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, "417");
+                        exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/JSON");
+                        // set cache control with cacheDirectives
+                        List<CacheDirective> cacheDirectives = new ArrayList<CacheDirective>();
+                        cacheDirectives.add(CacheDirective.maxAge(20));
+                        exchange.getOut().setHeader(HeaderConstants.HEADER_CACHE_CONTROL,
cacheDirectives);
+                        
+                    }       
+                });
             }
         };
     }
     
+    private void getCustomResponse(String address) throws Exception {
+        HttpResponse response = doExecute(new HttpPost("http://localhost:" + portNum + address));
+        assertHttpResponse(response, 417, "application/JSON");
+        assertEquals("Get a wrong http header", "Cache-Control: max-age=20", response.getFirstHeader(HeaderConstants.HEADER_CACHE_CONTROL).toString());
+    }
+    
     @Test
     public void testCustomResponse() throws Exception {
-        HttpResponse response = doExecute(new HttpPost("http://localhost:" + portNum + "/users/homer"));
-
-        assertHttpResponse(response, 417, "application/JSON");
+        getCustomResponse("/users/homer");
+        getCustomResponse("/cached/homer");
     }
     
     @Test(expected = CamelExecutionException.class)



Mime
View raw message