Author: wtam
Date: Fri Mar 25 19:56:41 2011
New Revision: 1085543

URL: http://svn.apache.org/viewvc?rev=1085543&view=rev
Log:
[CAMEL-3808] Restlet Producer to add query to request URI based on runtime 
information (e.g. Camel header)

Added:
    
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/AddQueryTest.java
   (with props)
Modified:
    
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
    
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletQueryTest.java

Modified: 
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java?rev=1085543&r1=1085542&r2=1085543&view=diff
==============================================================================
--- 
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
 (original)
+++ 
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
 Fri Mar 25 19:56:41 2011
@@ -111,6 +111,14 @@ public class RestletProducer extends Def
             matcher.reset(uri);
         }
 
+        String query = exchange.getIn().getHeader(Exchange.HTTP_QUERY, 
String.class);
+        if (query != null) {
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Adding query: " + query + " to uri: " + uri);
+            }
+            uri = addQueryToUri(uri, query);
+        }
+        
         if (LOG.isDebugEnabled()) {
             LOG.debug("Using uri: " + uri);
         }
@@ -118,6 +126,33 @@ public class RestletProducer extends Def
         return uri;
     }
 
+
+    protected static String addQueryToUri(String uri, String query) {
+        if (uri == null || uri.length() == 0) {
+            return uri;
+        }
+
+        StringBuffer answer = new StringBuffer();
+
+        int index = uri.indexOf('?');
+        if (index < 0) {
+            answer.append(uri);
+            answer.append("?");
+            answer.append(query);
+        } else {
+            answer.append(uri.substring(0, index));
+            answer.append("?");            
+            answer.append(query);
+            String remaining = uri.substring(index + 1);
+            if (remaining.length() > 0) {
+                answer.append("&");
+                answer.append(remaining);
+            }
+        }
+        return answer.toString();
+        
+    }
+
     protected RestletOperationException 
populateRestletProducerException(Exchange exchange, Response response, int 
responseCode) {
         RestletOperationException exception;
         String uri = exchange.getFromEndpoint().getEndpointUri();

Added: 
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/AddQueryTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/AddQueryTest.java?rev=1085543&view=auto
==============================================================================
--- 
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/AddQueryTest.java
 (added)
+++ 
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/AddQueryTest.java
 Fri Mar 25 19:56:41 2011
@@ -0,0 +1,38 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.restlet;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * 
+ * @version $Revision$
+ */
+public class AddQueryTest extends Assert {
+    
+    @Test
+    public void testAddQuery() throws Exception {
+        assertEquals("http://a/b/c?a=b";, 
RestletProducer.addQueryToUri("http://a/b/c";, "a=b"));
+        assertEquals("http://a/b/c?a=b&c=b";, 
RestletProducer.addQueryToUri("http://a/b/c";, "a=b&c=b"));
+        assertEquals("http://a/b/c?a=b&c=b&l=m";, 
RestletProducer.addQueryToUri("http://a/b/c?c=b&l=m";, "a=b"));
+        assertEquals("http://a/b/c?a=b&i=j&c=b&l=m";, 
RestletProducer.addQueryToUri("http://a/b/c?c=b&l=m";, "a=b&i=j"));
+
+
+    }
+
+}

Propchange: 
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/AddQueryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/AddQueryTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletQueryTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletQueryTest.java?rev=1085543&r1=1085542&r2=1085543&view=diff
==============================================================================
--- 
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletQueryTest.java
 (original)
+++ 
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletQueryTest.java
 Fri Mar 25 19:56:41 2011
@@ -37,6 +37,9 @@ public class RestletQueryTest extends Re
             public void configure() throws Exception {
                 from("restlet:http://localhost:9080/users/{username}";)
                     .process(new SetUserProcessor());
+                
+                
from("direct:start").to("restlet:http://localhost:9080/users/{username}";);
+
             }
         };
     }
@@ -55,4 +58,19 @@ public class RestletQueryTest extends Re
 
         assertHttpResponse(response, 200, "text/plain");
     }
+    
+    
+    @Test
+    public void testGetBodyByRestletProducer() throws Exception {        
+        Exchange ex = template.request("direct:start", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(Exchange.HTTP_QUERY, QUERY_STRING);
+                exchange.getIn().setHeader("username", "homer");
+                
+            }
+        });
+        assertEquals(200, ex.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
+        
+    }
 }
\ No newline at end of file


Reply via email to