camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1463496 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/util/ test/java/org/apache/camel/issues/
Date Tue, 02 Apr 2013 12:11:26 GMT
Author: davsclaus
Date: Tue Apr  2 12:11:26 2013
New Revision: 1463496

URL: http://svn.apache.org/r1463496
Log:
CAMEL-6230: When a component isUseRaw enabled, then the parameter values should also be raw
values.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/issues/ComponentUseRawUriTest.java
      - copied, changed from r1463391, camel/trunk/camel-core/src/test/java/org/apache/camel/issues/Camel4857UriIssueTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Component.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/Component.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Component.java?rev=1463496&r1=1463495&r2=1463496&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Component.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Component.java Tue Apr  2 12:11:26
2013
@@ -41,6 +41,8 @@ public interface Component extends Camel
 
     /**
      * Whether to use raw or encoded uri, when creating endpoints.
+     * <p/>
+     * <b>Notice:</b> When using raw uris, then the parameter values is raw as
well.
      *
      * @return <tt>true</tt> to use raw uris, <tt>false</tt> to use
encoded uris (default).
      *

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java?rev=1463496&r1=1463495&r2=1463496&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java Tue Apr
 2 12:11:26 2013
@@ -82,7 +82,24 @@ public abstract class DefaultComponent e
             path = path.substring(0, idx);
         }
 
-        Map<String, Object> parameters = URISupport.parseParameters(u);
+        Map<String, Object> parameters;
+        if (useRawUri()) {
+            // when using raw uri then the query is taking from the uri as is
+            String query;
+            idx = uri.indexOf('?');
+            if (idx > -1) {
+                query = uri.substring(idx + 1);
+            } else {
+                query = u.getRawQuery();
+            }
+            // and use method parseQuery
+            parameters = URISupport.parseQuery(query, true);
+        } else {
+            // however when using the encoded (default mode) uri then the query,
+            // is taken from the URI (ensures values is URI encoded)
+            // and use method parseParameters
+            parameters = URISupport.parseParameters(u);
+        }
         // parameters using raw syntax: RAW(value)
         // should have the token removed, so its only the value we have in parameters, as
we are about to create
         // an endpoint and want to have the parameter values without the RAW tokens

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java?rev=1463496&r1=1463495&r2=1463496&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java Tue Apr  2
12:11:26 2013
@@ -105,6 +105,24 @@ public final class URISupport {
      * @see #RAW_TOKEN_END
      */
     public static Map<String, Object> parseQuery(String uri) throws URISyntaxException
{
+        return parseQuery(uri, false);
+    }
+
+    /**
+     * Parses the query part of the uri (eg the parameters).
+     * <p/>
+     * The URI parameters will by default be URI encoded. However you can define a parameter
+     * values with the syntax: <tt>key=RAW(value)</tt> which tells Camel to not
encode the value,
+     * and use the value as is (eg key=value) and the value has <b>not</b> been
encoded.
+     *
+     * @param uri the uri
+     * @param useRaw whether to force using raw values
+     * @return the parameters, or an empty map if no parameters (eg never null)
+     * @throws URISyntaxException is thrown if uri has invalid syntax.
+     * @see #RAW_TOKEN_START
+     * @see #RAW_TOKEN_END
+     */
+    public static Map<String, Object> parseQuery(String uri, boolean useRaw) throws
URISyntaxException {
         // must check for trailing & as the uri.split("&") will ignore those
         if (uri != null && uri.endsWith("&")) {
             throw new URISyntaxException(uri, "Invalid uri syntax: Trailing & marker
found. "
@@ -157,7 +175,7 @@ public final class URISupport {
                     boolean end = ch == RAW_TOKEN_END.charAt(0) && (next == '&'
|| next == '\u0000');
                     if (end) {
                         // raw value end, so add that as a parameter, and reset flags
-                        addParameter(key.toString(), value.toString(), rc, isRaw);
+                        addParameter(key.toString(), value.toString(), rc, useRaw || isRaw);
                         key.setLength(0);
                         value.setLength(0);
                         isKey = true;
@@ -180,7 +198,7 @@ public final class URISupport {
                 // the & denote parameter is ended
                 if (ch == '&') {
                     // parameter is ended, as we hit & separator
-                    addParameter(key.toString(), value.toString(), rc, isRaw);
+                    addParameter(key.toString(), value.toString(), rc, useRaw || isRaw);
                     key.setLength(0);
                     value.setLength(0);
                     isKey = true;
@@ -199,7 +217,7 @@ public final class URISupport {
 
             // any left over parameters, then add that
             if (key.length() > 0) {
-                addParameter(key.toString(), value.toString(), rc, isRaw);
+                addParameter(key.toString(), value.toString(), rc, useRaw || isRaw);
             }
 
             return rc;
@@ -324,7 +342,7 @@ public final class URISupport {
      * @return the value without the prefix
      */
     public static String stripPrefix(String value, String prefix) {
-        if (value.startsWith(prefix)) {
+        if (value != null && value.startsWith(prefix)) {
             return value.substring(prefix.length());
         }
         return value;

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/issues/ComponentUseRawUriTest.java
(from r1463391, camel/trunk/camel-core/src/test/java/org/apache/camel/issues/Camel4857UriIssueTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/ComponentUseRawUriTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/issues/ComponentUseRawUriTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/issues/Camel4857UriIssueTest.java&r1=1463391&r2=1463496&rev=1463496&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/issues/Camel4857UriIssueTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/issues/ComponentUseRawUriTest.java
Tue Apr  2 12:11:26 2013
@@ -27,19 +27,15 @@ import org.apache.camel.impl.DefaultComp
 import org.apache.camel.impl.DefaultEndpoint;
 
 /**
- * CAMEL-4857 issue test
+ *
  */
-public class Camel4857UriIssueTest extends ContextTestSupport {
+public class ComponentUseRawUriTest extends ContextTestSupport {
 
-    /**
-     * An URI of Camel Beanstalk component consists of a hostname, port and a list
-     * of tube names. Tube names are separated by "+" character (which is more or less
-     * usually used on the Web to make lists), but every tube name may contain URI special
-     * characters like ? or +
-     */
-    class MyEndpoint extends DefaultEndpoint {
+    public static class MyEndpoint extends DefaultEndpoint {
         String uri;
         String remaining;
+        String foo;
+        String bar;
 
         public MyEndpoint(final String uri, final String remaining) {
             this.uri = uri;
@@ -54,6 +50,22 @@ public class Camel4857UriIssueTest exten
             throw new UnsupportedOperationException("Not supported yet.");
         }
 
+        public String getFoo() {
+            return foo;
+        }
+
+        public void setFoo(String foo) {
+            this.foo = foo;
+        }
+
+        public String getBar() {
+            return bar;
+        }
+
+        public void setBar(String bar) {
+            this.bar = bar;
+        }
+
         public boolean isSingleton() {
             return true;
         }
@@ -67,7 +79,9 @@ public class Camel4857UriIssueTest exten
 
         @Override
         protected Endpoint createEndpoint(final String uri, final String remaining, final
Map<String, Object> parameters) throws Exception {
-            return new MyEndpoint(uri, remaining);
+            MyEndpoint answer = new MyEndpoint(uri, remaining);
+            setProperties(answer, parameters);
+            return answer;
         }
 
         @Override
@@ -83,18 +97,14 @@ public class Camel4857UriIssueTest exten
         context.addComponent("my", new MyComponent());
     }
 
-    public void testExclamationInUri() {
-        // %3F is not an ?, it's part of tube name.
-        MyEndpoint endpoint = context.getEndpoint("my:host:11303/tube1+tube%2B+tube%3F",
MyEndpoint.class);
+    public void testUseRaw() {
+        String uri = "my:host:11303/tube1+tube?foo=%2B+tube%3F&bar=++%%w?rd";
+        MyEndpoint endpoint = context.getEndpoint(uri, MyEndpoint.class);
         assertNotNull("endpoint", endpoint);
-        assertEquals("my:host:11303/tube1+tube%2B+tube%3F", endpoint.getUri());
-    }
 
-    public void testPath() {
-         // Here a tube name is "tube+" and written in URI as "tube%2B", but it gets
-         // normalized, so that an endpoint sees "tube1+tube+"
-        MyEndpoint endpoint = context.getEndpoint("my:host:11303/tube1+tube%2B", MyEndpoint.class);
-        assertEquals("Path contains several tube names, every tube name may have + or ? characters",
"host:11303/tube1+tube%2B", endpoint.remaining);
+        assertEquals("%2B+tube%3F", endpoint.getFoo());
+        assertEquals("++%%w?rd", endpoint.getBar());
+        assertEquals(uri, endpoint.getUri());
     }
 
 }



Mime
View raw message