use of org.apache.camel.ResolveEndpointFailedException in project camel by apache.
the class CamelPostProcessorHelperTest method testEndpointInjectProducerTemplateFieldUrlUnknown.
public void testEndpointInjectProducerTemplateFieldUrlUnknown() throws Exception {
CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
MyEndpointInjectProducerTemplateUrlUnknown bean = new MyEndpointInjectProducerTemplateUrlUnknown();
Field field = bean.getClass().getField("producer");
EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
Class<?> type = field.getType();
String propertyName = "producer";
try {
helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
fail("Should throw exception");
} catch (ResolveEndpointFailedException e) {
assertEquals("Failed to resolve endpoint: xxx://foo due to: No component found with scheme: xxx", e.getMessage());
}
}
use of org.apache.camel.ResolveEndpointFailedException in project camel by apache.
the class RoutingSlipIgnoreInvalidEndpointsTest method testEndpointResolvedFailedWithoutIgnoreInvalidEndpoints.
public void testEndpointResolvedFailedWithoutIgnoreInvalidEndpoints() throws Exception {
MockEndpoint result = getMockEndpoint("mock:result");
result.expectedMessageCount(0);
MockEndpoint end = getMockEndpoint("mock:end");
end.expectedMessageCount(0);
try {
template.sendBodyAndHeader("direct:b", "Hello", "myHeader", "direct:start,fail:endpoint,mock:result");
fail("Expect the exception here.");
} catch (Exception ex) {
assertTrue("Get a wrong cause of the exception", ex.getCause() instanceof ResolveEndpointFailedException);
}
assertMockEndpointsSatisfied();
}
use of org.apache.camel.ResolveEndpointFailedException in project camel by apache.
the class HttpComponent method createEndpoint.
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
String addressUri = "http://" + remaining;
if (uri.startsWith("https:")) {
addressUri = "https://" + remaining;
}
Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
// must extract well known parameters before we create the endpoint
HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBinding", HttpBinding.class);
HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
UrlRewrite urlRewrite = resolveAndRemoveReferenceParameter(parameters, "urlRewrite", UrlRewrite.class);
// http client can be configured from URI options
HttpClientParams clientParams = new HttpClientParams();
Map<String, Object> httpClientOptions = IntrospectionSupport.extractProperties(parameters, "httpClient.");
IntrospectionSupport.setProperties(clientParams, httpClientOptions);
// validate that we could resolve all httpClient. parameters as this component is lenient
validateParameters(uri, httpClientOptions, null);
// http client can be configured from URI options
HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
// setup the httpConnectionManagerParams
Map<String, Object> httpConnectionManagerOptions = IntrospectionSupport.extractProperties(parameters, "httpConnectionManager.");
IntrospectionSupport.setProperties(connectionManagerParams, httpConnectionManagerOptions);
// validate that we could resolve all httpConnectionManager. parameters as this component is lenient
validateParameters(uri, httpConnectionManagerOptions, null);
// make sure the component httpConnectionManager is take effect
HttpConnectionManager thisHttpConnectionManager = httpConnectionManager;
if (thisHttpConnectionManager == null) {
// only set the params on the new created http connection manager
thisHttpConnectionManager = new MultiThreadedHttpConnectionManager();
thisHttpConnectionManager.setParams(connectionManagerParams);
}
// create the configurer to use for this endpoint (authMethods contains the used methods created by the configurer)
final Set<AuthMethod> authMethods = new LinkedHashSet<AuthMethod>();
HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, authMethods);
addressUri = UnsafeUriCharactersEncoder.encodeHttpURI(addressUri);
URI endpointUri = URISupport.createRemainingURI(new URI(addressUri), httpClientParameters);
// create the endpoint and connectionManagerParams already be set
HttpEndpoint endpoint = createHttpEndpoint(endpointUri.toString(), this, clientParams, thisHttpConnectionManager, configurer);
// configure the endpoint with the common configuration from the component
if (getHttpConfiguration() != null) {
Map<String, Object> properties = new HashMap<>();
IntrospectionSupport.getProperties(getHttpConfiguration(), properties, null);
setProperties(endpoint, properties);
}
if (headerFilterStrategy != null) {
endpoint.setHeaderFilterStrategy(headerFilterStrategy);
} else {
setEndpointHeaderFilterStrategy(endpoint);
}
if (urlRewrite != null) {
// let CamelContext deal with the lifecycle of the url rewrite
// this ensures its being shutdown when Camel shutdown etc.
getCamelContext().addService(urlRewrite);
endpoint.setUrlRewrite(urlRewrite);
}
// prefer to use endpoint configured over component configured
if (binding == null) {
// fallback to component configured
binding = getHttpBinding();
}
if (binding != null) {
endpoint.setBinding(binding);
}
setProperties(endpoint, parameters);
// restructure uri to be based on the parameters left as we dont want to include the Camel internal options
URI httpUri = URISupport.createRemainingURI(new URI(addressUri), parameters);
// validate http uri that end-user did not duplicate the http part that can be a common error
String part = httpUri.getSchemeSpecificPart();
if (part != null) {
part = part.toLowerCase();
if (part.startsWith("//http//") || part.startsWith("//https//") || part.startsWith("//http://") || part.startsWith("//https://")) {
throw new ResolveEndpointFailedException(uri, "The uri part is not configured correctly. You have duplicated the http(s) protocol.");
}
}
endpoint.setHttpUri(httpUri);
endpoint.setHttpClientOptions(httpClientOptions);
return endpoint;
}
use of org.apache.camel.ResolveEndpointFailedException in project camel by apache.
the class HttpComponent method createEndpoint.
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
final Map<String, Object> httpClientOptions = new HashMap<>();
final HttpClientBuilder clientBuilder = createHttpClientBuilder(uri, parameters, httpClientOptions);
HttpBinding httpBinding = resolveAndRemoveReferenceParameter(parameters, "httpBinding", HttpBinding.class);
HttpContext httpContext = resolveAndRemoveReferenceParameter(parameters, "httpContext", HttpContext.class);
SSLContextParameters sslContextParameters = resolveAndRemoveReferenceParameter(parameters, "sslContextParameters", SSLContextParameters.class);
if (sslContextParameters == null) {
sslContextParameters = getSslContextParameters();
}
String httpMethodRestrict = getAndRemoveParameter(parameters, "httpMethodRestrict", String.class);
HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
UrlRewrite urlRewrite = resolveAndRemoveReferenceParameter(parameters, "urlRewrite", UrlRewrite.class);
boolean secure = HttpHelper.isSecureConnection(uri) || sslContextParameters != null;
// need to set scheme on address uri depending on if its secure or not
String addressUri = (secure ? "https://" : "http://") + remaining;
addressUri = UnsafeUriCharactersEncoder.encodeHttpURI(addressUri);
URI uriHttpUriAddress = new URI(addressUri);
// validate http uri that end-user did not duplicate the http part that can be a common error
int pos = uri.indexOf("//");
if (pos != -1) {
String part = uri.substring(pos + 2);
if (part.startsWith("http:") || part.startsWith("https:")) {
throw new ResolveEndpointFailedException(uri, "The uri part is not configured correctly. You have duplicated the http(s) protocol.");
}
}
// create the configurer to use for this endpoint
HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, secure);
URI endpointUri = URISupport.createRemainingURI(uriHttpUriAddress, httpClientParameters);
// the endpoint uri should use the component name as scheme, so we need to re-create it once more
String scheme = ObjectHelper.before(uri, "://");
endpointUri = URISupport.createRemainingURI(new URI(scheme, endpointUri.getUserInfo(), endpointUri.getHost(), endpointUri.getPort(), endpointUri.getPath(), endpointUri.getQuery(), endpointUri.getFragment()), httpClientParameters);
// create the endpoint and set the http uri to be null
String endpointUriString = endpointUri.toString();
LOG.debug("Creating endpoint uri {}", endpointUriString);
final HttpClientConnectionManager localConnectionManager = createConnectionManager(parameters, sslContextParameters);
HttpEndpoint endpoint = new HttpEndpoint(endpointUriString, this, clientBuilder, localConnectionManager, configurer);
// configure the endpoint with the common configuration from the component
if (getHttpConfiguration() != null) {
Map<String, Object> properties = new HashMap<>();
IntrospectionSupport.getProperties(getHttpConfiguration(), properties, null);
setProperties(endpoint, properties);
}
if (urlRewrite != null) {
// let CamelContext deal with the lifecycle of the url rewrite
// this ensures its being shutdown when Camel shutdown etc.
getCamelContext().addService(urlRewrite);
endpoint.setUrlRewrite(urlRewrite);
}
// configure the endpoint
setProperties(endpoint, parameters);
// determine the portnumber (special case: default portnumber)
//int port = getPort(uriHttpUriAddress);
// we can not change the port of an URI, we must create a new one with an explicit port value
URI httpUri = URISupport.createRemainingURI(new URI(uriHttpUriAddress.getScheme(), uriHttpUriAddress.getUserInfo(), uriHttpUriAddress.getHost(), uriHttpUriAddress.getPort(), uriHttpUriAddress.getPath(), uriHttpUriAddress.getQuery(), uriHttpUriAddress.getFragment()), parameters);
endpoint.setHttpUri(httpUri);
if (headerFilterStrategy != null) {
endpoint.setHeaderFilterStrategy(headerFilterStrategy);
} else {
setEndpointHeaderFilterStrategy(endpoint);
}
endpoint.setBinding(getHttpBinding());
if (httpBinding != null) {
endpoint.setBinding(httpBinding);
}
if (httpMethodRestrict != null) {
endpoint.setHttpMethodRestrict(httpMethodRestrict);
}
endpoint.setHttpContext(getHttpContext());
if (httpContext != null) {
endpoint.setHttpContext(httpContext);
}
if (endpoint.getCookieStore() == null) {
endpoint.setCookieStore(getCookieStore());
}
endpoint.setHttpClientOptions(httpClientOptions);
return endpoint;
}
use of org.apache.camel.ResolveEndpointFailedException in project camel by apache.
the class EndpointHelper method matchEndpoint.
/**
* Matches the endpoint with the given pattern.
* <p/>
* The endpoint will first resolve property placeholders using {@link CamelContext#resolvePropertyPlaceholders(String)}.
* <p/>
* The match rules are applied in this order:
* <ul>
* <li>exact match, returns true</li>
* <li>wildcard match (pattern ends with a * and the uri starts with the pattern), returns true</li>
* <li>regular expression match, returns true</li>
* <li>otherwise returns false</li>
* </ul>
*
* @param context the Camel context, if <tt>null</tt> then property placeholder resolution is skipped.
* @param uri the endpoint uri
* @param pattern a pattern to match
* @return <tt>true</tt> if match, <tt>false</tt> otherwise.
*/
public static boolean matchEndpoint(CamelContext context, String uri, String pattern) {
if (context != null) {
try {
uri = context.resolvePropertyPlaceholders(uri);
} catch (Exception e) {
throw new ResolveEndpointFailedException(uri, e);
}
}
// 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);
}
// we need to test with and without scheme separators (//)
if (uri.contains("://")) {
// try without :// also
String scheme = ObjectHelper.before(uri, "://");
String path = after(uri, "://");
if (matchPattern(scheme + ":" + path, pattern)) {
return true;
}
} else {
// try with :// also
String scheme = ObjectHelper.before(uri, ":");
String path = after(uri, ":");
if (matchPattern(scheme + "://" + path, pattern)) {
return true;
}
}
// and fallback to test with the uri as is
return matchPattern(uri, pattern);
}
Aggregations