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 2009/07/04 15:26:49 UTC
svn commit: r791124 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/impl/ main/java/org/apache/camel/util/
test/java/org/apache/camel/processor/intercept/
test/java/org/apache/camel/util/
Author: davsclaus
Date: Sat Jul 4 13:26:49 2009
New Revision: 791124
URL: http://svn.apache.org/viewvc?rev=791124&view=rev
Log:
CAMEL-1756: endpoint resolution is improved to better detect similar endpoints even if parameters is not in same order or scheme // separator is missing.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java (with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java
camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointDynamicTest.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=791124&r1=791123&r2=791124&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Sat Jul 4 13:26:49 2009
@@ -23,7 +23,11 @@
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
+import java.util.Collections;
+import java.util.LinkedHashMap;
import java.util.concurrent.Callable;
+import java.net.URI;
+import java.net.URISyntaxException;
import javax.naming.Context;
@@ -74,6 +78,8 @@
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.ReflectionInjector;
import org.apache.camel.util.SystemHelper;
+import org.apache.camel.util.URISupport;
+import org.apache.camel.util.EndpointHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -359,6 +365,17 @@
public Endpoint getEndpoint(String uri) {
ObjectHelper.notEmpty(uri, "uri");
+ // normalize uri so we can do endpoint hits with minor mistakes and parameters is not in the same order
+ try {
+ uri = URISupport.normalizeUri(uri);
+ } catch (Exception e) {
+ throw new ResolveEndpointFailedException(uri, e);
+ }
+
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Getting endpoint with uri: " + uri);
+ }
+
Endpoint answer;
String scheme = null;
synchronized (endpoints) {
@@ -406,7 +423,7 @@
return answer;
}
-
+
public <T extends Endpoint> T getEndpoint(String name, Class<T> endpointType) {
Endpoint endpoint = getEndpoint(name);
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java?rev=791124&r1=791123&r2=791124&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java Sat Jul 4 13:26:49 2009
@@ -18,7 +18,13 @@
import java.util.Iterator;
import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
import java.util.regex.PatternSyntaxException;
+import java.net.URISyntaxException;
+import java.net.URI;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
@@ -93,6 +99,29 @@
* @return <tt>true</tt> if match, <tt>false</tt> otherwise.
*/
public static boolean matchEndpoint(String uri, String pattern) {
+ // we need to test with and without scheme separators (//)
+ if (uri.indexOf("://") != -1) {
+ // try without :// also
+ String scheme = ObjectHelper.before(uri, "://");
+ String path = ObjectHelper.after(uri, "://");
+ if (doMatchEndpoint(scheme + ":" + path, pattern)) {
+ return true;
+ }
+ } else {
+ // try with :// also
+ String scheme = ObjectHelper.before(uri, ":");
+ String path = ObjectHelper.after(uri, ":");
+ if (doMatchEndpoint(scheme + "://" + path, pattern)) {
+ return true;
+ }
+ }
+
+ // and fallback to test with the uri as is
+ return doMatchEndpoint(uri, pattern);
+ }
+
+
+ private static boolean doMatchEndpoint(String uri, String pattern) {
if (uri.equals(pattern)) {
// excact match
return true;
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=791124&r1=791123&r2=791124&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 Sat Jul 4 13:26:49 2009
@@ -25,6 +25,8 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import java.util.LinkedHashMap;
+import java.util.List;
/**
* URI utilities.
@@ -112,7 +114,8 @@
@SuppressWarnings("unchecked")
public static Map parseQuery(String uri) throws URISyntaxException {
try {
- Map rc = new HashMap();
+ // use a linked map so the parameters is in the same order
+ Map rc = new LinkedHashMap();
if (uri != null) {
String[] parameters = uri.split("&");
for (String parameter : parameters) {
@@ -334,5 +337,64 @@
}
return result;
}
+
+ /**
+ * Normalizes the uri by reordering the parameters so they are sorted and thus
+ * we can use the uris for endpoint matching.
+ *
+ * @param uri the uri
+ * @return the normalized uri
+ * @throws URISyntaxException in thrown if the uri syntax is invalid
+ */
+ @SuppressWarnings("unchecked")
+ public static String normalizeUri(String uri) throws URISyntaxException {
+
+ URI u = new URI(UnsafeUriCharactersEncoder.encode(uri));
+ String path = u.getSchemeSpecificPart();
+ String scheme = u.getScheme();
+
+ // not possible to normalize
+ if (scheme == null || path == null) {
+ return uri;
+ }
+
+ // lets trim off any query arguments
+ if (path.startsWith("//")) {
+ path = path.substring(2);
+ }
+ int idx = path.indexOf('?');
+ if (idx > 0) {
+ path = path.substring(0, idx);
+ }
+
+ // in case there are parameters we should reorder them
+ Map parameters = URISupport.parseParameters(u);
+ if (parameters.isEmpty()) {
+ // no parameters then just return
+ return buildUri(scheme, path, null);
+ } else {
+ // reorder parameters a..z
+ List<String> keys = new ArrayList<String>(parameters.keySet());
+ Collections.sort(keys);
+
+ Map<String, Object> sorted = new LinkedHashMap<String, Object>(parameters.size());
+ for (String key : keys) {
+ sorted.put(key, parameters.get(key));
+ }
+
+ // build uri object with sorted parameters
+ String query = URISupport.createQueryString(sorted);
+ return buildUri(scheme, path, query);
+ }
+ }
+
+ private static String buildUri(String scheme, String path, String query) {
+ // http scheme should have //
+ if (scheme.startsWith("http")) {
+ return scheme + "://" + path + (query != null ? "?" + query : "");
+ }
+ // the order should not as we really dont use them for other types of components
+ return scheme + ":" + path + (query != null ? "?" + query : "");
+ }
}
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointDynamicTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointDynamicTest.java?rev=791124&r1=791123&r2=791124&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointDynamicTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointDynamicTest.java Sat Jul 4 13:26:49 2009
@@ -85,7 +85,7 @@
context.start();
getMockEndpoint("mock:detour").expectedMessageCount(1);
- getMockEndpoint("mock:detour").expectedHeaderReceived(Exchange.INTERCEPTED_ENDPOINT, "file://foo");
+ getMockEndpoint("mock:detour").expectedHeaderReceived(Exchange.INTERCEPTED_ENDPOINT, "file:foo");
getMockEndpoint("mock:result").expectedMessageCount(1);
template.sendBody("direct:first", "Hello World");
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java?rev=791124&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java Sat Jul 4 13:26:49 2009
@@ -0,0 +1,88 @@
+/**
+ * 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.util;
+
+import org.apache.camel.ContextTestSupport;
+
+/**
+ * @version $Revision$
+ */
+public class URISupportTest extends ContextTestSupport {
+
+ public void testNormalizeEndpointUri() throws Exception {
+ String out1 = URISupport.normalizeUri("smtp://localhost?username=davsclaus&password=secret");
+ String out2 = URISupport.normalizeUri("smtp://localhost?password=secret&username=davsclaus");
+ assertEquals(out1, out2);
+
+ out1 = URISupport.normalizeUri("smtp://localhost?username=davsclaus&password=secret");
+ out2 = URISupport.normalizeUri("smtp:localhost?password=secret&username=davsclaus");
+ assertEquals(out1, out2);
+
+ out1 = URISupport.normalizeUri("smtp:localhost?password=secret&username=davsclaus");
+ out2 = URISupport.normalizeUri("smtp://localhost?username=davsclaus&password=secret");
+ assertEquals(out1, out2);
+
+ out1 = URISupport.normalizeUri("seda:foo?concurrentConsumer=2");
+ out2 = URISupport.normalizeUri("seda:foo?concurrentConsumer=2");
+ assertEquals(out1, out2);
+
+ out1 = URISupport.normalizeUri("seda:foo?concurrentConsumer=2");
+ out2 = URISupport.normalizeUri("seda:foo");
+ assertNotSame(out1, out2);
+ }
+
+ public void testNormalizeEndpointUriNoParam() throws Exception {
+ String out1 = URISupport.normalizeUri("direct:foo");
+ String out2 = URISupport.normalizeUri("direct:foo");
+ assertEquals(out1, out2);
+
+ out1 = URISupport.normalizeUri("direct://foo");
+ out2 = URISupport.normalizeUri("direct://foo");
+ assertEquals(out1, out2);
+
+ out1 = URISupport.normalizeUri("direct:foo");
+ out2 = URISupport.normalizeUri("direct://foo");
+ assertEquals(out1, out2);
+
+ out1 = URISupport.normalizeUri("direct://foo");
+ out2 = URISupport.normalizeUri("direct:foo");
+ assertEquals(out1, out2);
+
+ out1 = URISupport.normalizeUri("direct://foo");
+ out2 = URISupport.normalizeUri("direct:bar");
+ assertNotSame(out1, out2);
+ }
+
+ public void testNormalizeEndpointUriWithFragments() throws Exception {
+ String out1 = URISupport.normalizeUri("irc://someserver/#camel?user=davsclaus");
+ String out2 = URISupport.normalizeUri("irc:someserver/#camel?user=davsclaus");
+ assertEquals(out1, out2);
+
+ out1 = URISupport.normalizeUri("irc://someserver/#camel?user=davsclaus");
+ out2 = URISupport.normalizeUri("irc:someserver/#camel?user=hadrian");
+ assertNotSame(out1, out2);
+ }
+
+ public void testNormalizeHttpEndpoint() throws Exception {
+ String out1 = URISupport.normalizeUri("http://www.google.com?q=Camel");
+ String out2 = URISupport.normalizeUri("http:www.google.com?q=Camel");
+ assertEquals(out1, out2);
+ assertTrue("Should have //", out1.startsWith("http://"));
+ assertTrue("Should have //", out2.startsWith("http://"));
+ }
+
+}
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/util/URISupportTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date