You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gn...@apache.org on 2020/09/15 12:51:38 UTC
[camel] 05/06: [CAMEL-15446] AbstractCamelContext methods with
similar behavior
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit ca5a55dede37a0a8f8ba5aeb53d9513eb4af7a30
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Tue Sep 15 09:49:29 2020 +0200
[CAMEL-15446] AbstractCamelContext methods with similar behavior
---
.../camel/impl/engine/AbstractCamelContext.java | 131 ++++-----------------
.../org/apache/camel/support/DefaultComponent.java | 90 +-------------
2 files changed, 26 insertions(+), 195 deletions(-)
diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 960a946..58fc598 100644
--- a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -771,31 +771,41 @@ public abstract class AbstractCamelContext extends BaseService
@Override
public Endpoint getEndpoint(String uri) {
- return doGetEndpoint(uri, false, false);
+ return doGetEndpoint(uri, null, false, false);
}
@Override
public Endpoint getEndpoint(NormalizedEndpointUri uri) {
- return doGetEndpoint(uri.getUri(), true, false);
+ return doGetEndpoint(uri.getUri(), null, true, false);
}
@Override
public Endpoint getPrototypeEndpoint(String uri) {
- return doGetEndpoint(uri, false, true);
+ return doGetEndpoint(uri, null, false, true);
}
@Override
public Endpoint getPrototypeEndpoint(NormalizedEndpointUri uri) {
- return doGetEndpoint(uri.getUri(), true, true);
+ return doGetEndpoint(uri.getUri(), null, true, true);
}
- protected Endpoint doGetEndpoint(String uri, boolean normalized, boolean prototype) {
- // ensure CamelContext are initialized before we can get a component
+ @Override
+ public Endpoint getEndpoint(String uri, Map<String, Object> parameters) {
+ return doGetEndpoint(uri, parameters, false, false);
+ }
+
+ @Override
+ public Endpoint getEndpoint(NormalizedEndpointUri uri, Map<String, Object> parameters) {
+ return doGetEndpoint(uri.getUri(), parameters, true, false);
+ }
+
+ protected Endpoint doGetEndpoint(String uri, Map<String, Object> parameters, boolean normalized, boolean prototype) {
+ // ensure CamelContext are initialized before we can get an endpoint
build();
StringHelper.notEmpty(uri, "uri");
- LOG.trace("Getting endpoint with uri: {}", uri);
+ LOG.trace("Getting endpoint with uri: {} and parameters: {}", uri, parameters);
// in case path has property placeholders then try to let property
// component resolve those
@@ -846,11 +856,9 @@ public abstract class AbstractCamelContext extends BaseService
LOG.trace("Creating endpoint from uri: {} using component: {}", uri, component);
// Have the component create the endpoint if it can.
- if (component.useRawUri()) {
- answer = component.createEndpoint(rawUri);
- } else {
- answer = component.createEndpoint(uri);
- }
+ answer = component.createEndpoint(
+ component.useRawUri() ? rawUri : uri,
+ parameters);
if (answer != null && LOG.isDebugEnabled()) {
LOG.debug("{} converted to endpoint: {} by component: {}", URISupport.sanitizeUri(uri), answer,
@@ -895,105 +903,6 @@ public abstract class AbstractCamelContext extends BaseService
}
@Override
- public Endpoint getEndpoint(String uri, Map<String, Object> parameters) {
- return doGetEndpoint(uri, parameters, false);
- }
-
- @Override
- public Endpoint getEndpoint(NormalizedEndpointUri uri, Map<String, Object> parameters) {
- return doGetEndpoint(uri.getUri(), parameters, true);
- }
-
- protected Endpoint doGetEndpoint(String uri, Map<String, Object> parameters, boolean normalized) {
- // ensure CamelContext are initialized before we can get an endpoint
- init();
-
- StringHelper.notEmpty(uri, "uri");
-
- LOG.trace("Getting endpoint with uri: {} and parameters: {}", uri, parameters);
-
- // in case path has property placeholders then try to let property
- // component resolve those
- if (!normalized) {
- try {
- uri = resolvePropertyPlaceholders(uri);
- } catch (Exception e) {
- throw new ResolveEndpointFailedException(uri, e);
- }
- }
-
- final String rawUri = uri;
-
- // normalize uri so we can do endpoint hits with minor mistakes and
- // parameters is not in the same order
- if (!normalized) {
- uri = EndpointHelper.normalizeEndpointUri(uri);
- }
-
- LOG.trace("Getting endpoint with raw uri: {}, normalized uri: {}", rawUri, uri);
-
- Endpoint answer;
- String scheme = null;
- // use optimized method to get the endpoint uri
- EndpointKey key = getEndpointKeyPreNormalized(uri);
- answer = endpoints.get(key);
- if (answer == null) {
- try {
- scheme = StringHelper.before(uri, ":");
- if (scheme == null) {
- // it may refer to a logical endpoint
- answer = getRegistry().lookupByNameAndType(uri, Endpoint.class);
- if (answer != null) {
- return answer;
- } else {
- throw new NoSuchEndpointException(uri);
- }
- }
- LOG.trace("Endpoint uri: {} is from component with name: {}", uri, scheme);
- Component component = getComponent(scheme);
- ServiceHelper.initService(component);
-
- // Ask the component to resolve the endpoint.
- if (component != null) {
- LOG.trace("Creating endpoint from uri: {} using component: {}", uri, component);
-
- // Have the component create the endpoint if it can.
- if (component.useRawUri()) {
- answer = component.createEndpoint(rawUri, parameters);
- } else {
- answer = component.createEndpoint(uri, parameters);
- }
-
- if (answer != null && LOG.isDebugEnabled()) {
- LOG.debug("{} converted to endpoint: {} by component: {}", URISupport.sanitizeUri(uri), answer,
- component);
- }
- }
-
- if (answer == null) {
- // no component then try in registry and elsewhere
- answer = createEndpoint(uri);
- LOG.trace("No component to create endpoint from uri: {} fallback lookup in registry -> {}", uri, answer);
- }
-
- if (answer != null) {
- addService(answer);
- answer = addEndpointToRegistry(uri, answer);
- }
- } catch (Exception e) {
- throw new ResolveEndpointFailedException(uri, e);
- }
- }
-
- // unknown scheme
- if (answer == null) {
- throw new ResolveEndpointFailedException(uri, "No component found with scheme: " + scheme);
- }
-
- return answer;
- }
-
- @Override
public <T extends Endpoint> T getEndpoint(String name, Class<T> endpointType) {
Endpoint endpoint = getEndpoint(name);
if (endpoint == null) {
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
index b6b555e..15cafd2 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
@@ -131,90 +131,6 @@ public abstract class DefaultComponent extends ServiceSupport implements Compone
// an endpoint and want to have the parameter values without the RAW tokens
URISupport.resolveRawParameterValues(parameters);
- validateURI(uri, path, parameters);
- if (LOG.isTraceEnabled()) {
- // at trace level its okay to have parameters logged, that may contain passwords
- LOG.trace("Creating endpoint uri=[{}], path=[{}], parameters=[{}]", URISupport.sanitizeUri(uri),
- URISupport.sanitizePath(path), parameters);
- } else if (LOG.isDebugEnabled()) {
- // but at debug level only output sanitized uris
- LOG.debug("Creating endpoint uri=[{}], path=[{}]", URISupport.sanitizeUri(uri), URISupport.sanitizePath(path));
- }
-
- // extract these global options and infer their value based on global/component level configuration
- boolean basic = getAndRemoveParameter(parameters, "basicPropertyBinding", boolean.class, basicPropertyBinding
- ? basicPropertyBinding : getCamelContext().getGlobalEndpointConfiguration().isBasicPropertyBinding());
- boolean bridge = getAndRemoveParameter(parameters, "bridgeErrorHandler", boolean.class, bridgeErrorHandler
- ? bridgeErrorHandler : getCamelContext().getGlobalEndpointConfiguration().isBridgeErrorHandler());
- boolean lazy = getAndRemoveParameter(parameters, "lazyStartProducer", boolean.class, lazyStartProducer
- ? lazyStartProducer : getCamelContext().getGlobalEndpointConfiguration().isLazyStartProducer());
-
- // create endpoint
- Endpoint endpoint = createEndpoint(uri, path, parameters);
- if (endpoint == null) {
- return null;
- }
- // inject camel context
- endpoint.setCamelContext(getCamelContext());
-
- // and setup those global options afterwards
- if (endpoint instanceof DefaultEndpoint) {
- DefaultEndpoint de = (DefaultEndpoint) endpoint;
- de.setBasicPropertyBinding(basic);
- de.setBridgeErrorHandler(bridge);
- de.setLazyStartProducer(lazy);
- }
-
- // configure remainder of the parameters
- setProperties(endpoint, parameters);
-
- // if endpoint is strict (not lenient) and we have unknown parameters configured then
- // fail if there are parameters that could not be set, then they are probably misspell or not supported at all
- if (!endpoint.isLenientProperties()) {
- validateParameters(uri, parameters, null);
- }
-
- afterConfiguration(uri, path, endpoint, parameters);
- return endpoint;
- }
-
- @Override
- public Endpoint createEndpoint(String uri) throws Exception {
- // need to encode before its safe to parse with java.net.Uri
- String encodedUri = UnsafeUriCharactersEncoder.encode(uri);
- URI u = new URI(encodedUri);
- String path;
- if (u.getScheme() != null) {
- // if there is a scheme then there is also a path
- path = URISupport.extractRemainderPath(u, useRawUri());
- } else {
- // this uri has no context-path as the leading text is the component name (scheme)
- path = null;
- }
-
- Map<String, Object> parameters;
- if (useRawUri()) {
- // when using raw uri then the query is taking from the uri as is
- String query;
- int 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
- URISupport.resolveRawParameterValues(parameters);
-
// use encoded or raw uri?
uri = useRawUri() ? uri : encodedUri;
@@ -236,6 +152,7 @@ public abstract class DefaultComponent extends ServiceSupport implements Compone
boolean lazy = getAndRemoveParameter(parameters, "lazyStartProducer", boolean.class, lazyStartProducer
? lazyStartProducer : getCamelContext().getGlobalEndpointConfiguration().isLazyStartProducer());
+ // create endpoint
Endpoint endpoint = createEndpoint(uri, path, parameters);
if (endpoint == null) {
return null;
@@ -270,6 +187,11 @@ public abstract class DefaultComponent extends ServiceSupport implements Compone
}
@Override
+ public Endpoint createEndpoint(String uri) throws Exception {
+ return createEndpoint(uri, null);
+ }
+
+ @Override
public boolean useRawUri() {
// should use encoded uri by default
return false;