You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2008/05/11 19:19:44 UTC

svn commit: r655341 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/component/log/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/util/ test/java/org/apache/camel/impl/

Author: davsclaus
Date: Sun May 11 10:19:43 2008
New Revision: 655341

URL: http://svn.apache.org/viewvc?rev=655341&view=rev
Log:
CAMEL-433: better validation when creating an endpoint from uri

Added:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentValidateURITest.java   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ResolveEndpointFailedException.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ResolveEndpointFailedException.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ResolveEndpointFailedException.java?rev=655341&r1=655340&r2=655341&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ResolveEndpointFailedException.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ResolveEndpointFailedException.java Sun May 11 10:19:43 2008
@@ -29,7 +29,13 @@
         this.uri = uri;
     }
 
+    public ResolveEndpointFailedException(String uri, String message) {
+        super("Failed to resolve endpoint: " + uri + " due to: " + message);
+        this.uri = uri;
+    }
+
     public String getUri() {
         return uri;
     }
+    
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java?rev=655341&r1=655340&r2=655341&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java Sun May 11 10:19:43 2008
@@ -30,6 +30,9 @@
 import org.apache.commons.logging.LogFactory;
 
 /**
+ * The <a href="http://activemq.apache.org/camel/log.html">Log Component</a>
+ * to log message exchanges to the underlying logging mechanism.
+ * 
  * @version $Revision$
  */
 public class LogComponent extends DefaultComponent<Exchange> {
@@ -51,6 +54,7 @@
 
     protected LoggingLevel getLoggingLevel(Map parameters) {
         String levelText = (String) parameters.get("level");
+        parameters.remove("level");
         LoggingLevel level = null;
         if (levelText != null) {
             level = LoggingLevel.valueOf(levelText.toUpperCase());

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java?rev=655341&r1=655340&r2=655341&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java Sun May 11 10:19:43 2008
@@ -26,6 +26,7 @@
 import org.apache.camel.Component;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
+import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.spi.Injector;
 import org.apache.camel.spi.Registry;
 import org.apache.camel.util.CamelContextHelper;
@@ -33,13 +34,17 @@
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.URISupport;
 import org.apache.camel.util.UnsafeUriCharactersEncoder;
-
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 
 /**
+ * Default component to use for base for components implementations.
+ *
  * @version $Revision$
  */
 public abstract class DefaultComponent<E extends Exchange> extends ServiceSupport implements Component<E> {
+    private static final transient Log LOG = LogFactory.getLog(DefaultComponent.class);
 
     private int defaultThreadPoolSize = 5;
     private CamelContext camelContext;
@@ -54,7 +59,7 @@
 
     public Endpoint<E> createEndpoint(String uri) throws Exception {
         ObjectHelper.notNull(getCamelContext(), "camelContext");
-        //endcode uri string to the unsafe URI characters
+        //encode URI string to the unsafe URI characters
         URI u = new URI(UnsafeUriCharactersEncoder.encode(uri));
         String path = u.getSchemeSpecificPart();
 
@@ -66,21 +71,52 @@
         if (idx > 0) {
             path = path.substring(0, idx);
         }
-        Map parameters = URISupport.parseParamters(u);
+        Map parameters = URISupport.parseParameters(u);
+
+        validateURI(uri, path, parameters);
 
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Creating endpoint uri=[" + uri + "], path=[" + path + "], parameters=[" + parameters + "]");
+        }
         Endpoint<E> endpoint = createEndpoint(uri, path, parameters);
         if (endpoint == null) {
             return null;
         }
+
         if (parameters != null) {
             endpoint.configureProperties(parameters);
             if (useIntrospectionOnEndpoint()) {
                 setProperties(endpoint, parameters);
             }
+
+            // fail if there are parameters that could not be set, then they are probably miss spelt or not supported at all
+            if (parameters.size() > 0) {
+                throw new ResolveEndpointFailedException(uri, "There are " + parameters.size() +
+                    " parameters that couldn't be set on the endpoint." +
+                    " Check the uri if the parameters are spelt correctly and that they are properties of the endpoint." +
+                    " Unknown parameters=[" + parameters + "]");
+            }
         }
+
         return endpoint;
     }
 
+    /**
+     * Strategy for validation of the uri when creating the endpoint.
+     *
+     * @param uri        the uri - the uri the end user provided untouched
+     * @param path       the path - part after the scheme
+     * @param parameters the parameters, an empty map if no parameters given
+     * @throws ResolveEndpointFailedException should be thrown if the URI validation failed
+     */
+    protected void validateURI(String uri, String path, Map parameters) throws ResolveEndpointFailedException {
+        // check for uri containing & but no ? marker
+        if (uri.contains("&") && !uri.contains("?")) {
+            throw new ResolveEndpointFailedException(uri, "Invalid uri syntax: no ? marker however the uri "
+                + "has & parameter separators. Check the uri if its missing a ? marker.");
+        }
+    }
+
     public CamelContext getCamelContext() {
         return camelContext;
     }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java?rev=655341&r1=655340&r2=655341&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java Sun May 11 10:19:43 2008
@@ -28,6 +28,8 @@
 import java.util.Map;
 
 /**
+ * URI utilities.
+ *
  * @version $Revision$
  */
 public class URISupport {
@@ -123,7 +125,7 @@
         }
     }
 
-    public static Map parseParamters(URI uri) throws URISyntaxException {
+    public static Map parseParameters(URI uri) throws URISyntaxException {
         String query = uri.getQuery();
         if (query == null) {
             String schemeSpecificPart = uri.getSchemeSpecificPart();

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentValidateURITest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentValidateURITest.java?rev=655341&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentValidateURITest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentValidateURITest.java Sun May 11 10:19:43 2008
@@ -0,0 +1,51 @@
+/**
+ * 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.impl;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.ResolveEndpointFailedException;
+
+/**
+ * Unit test for URI validation when creating an endpoint
+ */
+public class DefaultComponentValidateURITest extends ContextTestSupport {
+
+    public void testNoParameters() throws Exception {
+        Endpoint endpoint = context.getEndpoint("timer://foo");
+        assertNotNull("Should have created an endpoint", endpoint);
+    }
+
+    public void testNoQuestionMarker() throws Exception {
+        try {
+            context.getEndpoint("timer://foo&fixedRate=true&delay=0&period=500");
+            fail("Should have thrown ResolveEndpointFailedException");
+        } catch (ResolveEndpointFailedException e) {
+            // ok
+        }
+    }
+
+    public void testUnknownParameter() throws Exception {
+        try {
+            context.getEndpoint("timer://foo?delay=250&unknown=1&period=500");
+            fail("Should have thrown ResolveEndpointFailedException");
+        } catch (ResolveEndpointFailedException e) {
+            // ok
+        }
+    }
+
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentValidateURITest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentValidateURITest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date