use of java.net.URI in project camel by apache.
the class URISupport method appendParametersToURI.
/**
* Appends the given parameters to the given URI.
* <p/>
* It keeps the original parameters and if a new parameter is already defined in
* {@code originalURI}, it will be replaced by its value in {@code newParameters}.
*
* @param originalURI the original URI
* @param newParameters the parameters to add
* @return the URI with all the parameters
* @throws URISyntaxException is thrown if the uri syntax is invalid
* @throws UnsupportedEncodingException is thrown if encoding error
*/
public static String appendParametersToURI(String originalURI, Map<String, Object> newParameters) throws URISyntaxException, UnsupportedEncodingException {
URI uri = new URI(normalizeUri(originalURI));
Map<String, Object> parameters = parseParameters(uri);
parameters.putAll(newParameters);
return createRemainingURI(uri, parameters).toString();
}
use of java.net.URI in project camel by apache.
the class AhcComponent method createEndpoint.
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
String addressUri = createAddressUri(uri, remaining);
// Do not set the HTTP URI because we still have all of the Camel internal
// parameters in the URI at this point.
AhcEndpoint endpoint = createAhcEndpoint(uri, this, null);
setEndpointHeaderFilterStrategy(endpoint);
endpoint.setClient(getClient());
endpoint.setClientConfig(getClientConfig());
endpoint.setBinding(getBinding());
endpoint.setSslContextParameters(getSslContextParameters());
setProperties(endpoint, parameters);
if (IntrospectionSupport.hasProperties(parameters, CLIENT_CONFIG_PREFIX)) {
DefaultAsyncHttpClientConfig.Builder builder = endpoint.getClientConfig() == null ? new DefaultAsyncHttpClientConfig.Builder() : AhcComponent.cloneConfig(endpoint.getClientConfig());
if (endpoint.getClient() != null) {
LOG.warn("The user explicitly set an AsyncHttpClient instance on the component or " + "endpoint, but this endpoint URI contains client configuration parameters. " + "Are you sure that this is what was intended? The AsyncHttpClient will be used" + " and the URI parameters will be ignored.");
} else if (endpoint.getClientConfig() != null) {
LOG.warn("The user explicitly set an AsyncHttpClientConfig instance on the component or " + "endpoint, but this endpoint URI contains client configuration parameters. " + "Are you sure that this is what was intended? The URI parameters will be applied" + " to a clone of the supplied AsyncHttpClientConfig in order to prevent unintended modification" + " of the explicitly configured AsyncHttpClientConfig. That is, the URI parameters override the" + " settings on the explicitly configured AsyncHttpClientConfig for this endpoint.");
}
// special for realm builder
Builder realmBuilder = null;
if (IntrospectionSupport.hasProperties(parameters, CLIENT_REALM_CONFIG_PREFIX)) {
// set and validate additional parameters on client config
Map<String, Object> realmParams = IntrospectionSupport.extractProperties(parameters, CLIENT_REALM_CONFIG_PREFIX);
// copy the parameters for the endpoint to have
endpoint.setClientConfigRealmOptions(new LinkedHashMap<>(realmParams));
Object principal = realmParams.remove("principal");
Object password = realmParams.remove("password");
if (ObjectHelper.isEmpty(principal)) {
throw new IllegalArgumentException(CLIENT_REALM_CONFIG_PREFIX + ".principal must be configured");
}
if (password == null) {
password = "";
}
realmBuilder = new Realm.Builder(principal.toString(), password.toString());
setProperties(realmBuilder, realmParams);
validateParameters(uri, realmParams, null);
}
// set and validate additional parameters on client config
Map<String, Object> clientParams = IntrospectionSupport.extractProperties(parameters, CLIENT_CONFIG_PREFIX);
// copy the parameters for the endpoint to have
endpoint.setClientConfigOptions(new LinkedHashMap<>(clientParams));
setProperties(builder, clientParams);
validateParameters(uri, clientParams, null);
if (realmBuilder != null) {
builder.setRealm(realmBuilder.build());
}
endpoint.setClientConfig(builder.build());
}
// restructure uri to be based on the parameters left as we dont want to include the Camel internal options
addressUri = UnsafeUriCharactersEncoder.encodeHttpURI(addressUri);
URI httpUri = URISupport.createRemainingURI(new URI(addressUri), parameters);
endpoint.setHttpUri(httpUri);
return endpoint;
}
use of java.net.URI in project camel by apache.
the class URISupportTest method testCreateRemaingURI.
public void testCreateRemaingURI() throws Exception {
URI original = new URI("http://camel.apache.org");
Map<String, Object> param = new HashMap<String, Object>();
param.put("foo", "123");
URI newUri = URISupport.createRemainingURI(original, param);
assertNotNull(newUri);
String s = newUri.toString();
assertEquals("http://camel.apache.org?foo=123", s);
}
use of java.net.URI in project camel by apache.
the class URISupportTest method testCreateURIWithQueryHasOneFragmentAndQueryParameter.
public void testCreateURIWithQueryHasOneFragmentAndQueryParameter() throws Exception {
URI uri = new URI("smtp://localhost#fragmentOne");
URI resultUri = URISupport.createURIWithQuery(uri, "utm_campaign=launch");
assertNotNull(resultUri);
assertEquals("smtp://localhost?utm_campaign=launch#fragmentOne", resultUri.toString());
}
use of java.net.URI 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;
}
Aggregations