Search in sources :

Example 21 with URI

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();
}
Also used : URI(java.net.URI)

Example 22 with URI

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;
}
Also used : Builder(org.asynchttpclient.Realm.Builder) DefaultAsyncHttpClientConfig(org.asynchttpclient.DefaultAsyncHttpClientConfig) Builder(org.asynchttpclient.Realm.Builder) Realm(org.asynchttpclient.Realm) URI(java.net.URI)

Example 23 with URI

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);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) URI(java.net.URI)

Example 24 with URI

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());
}
Also used : URI(java.net.URI)

Example 25 with URI

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) UrlRewrite(org.apache.camel.http.common.UrlRewrite) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) HttpRestHeaderFilterStrategy(org.apache.camel.http.common.HttpRestHeaderFilterStrategy) URI(java.net.URI) ResolveEndpointFailedException(org.apache.camel.ResolveEndpointFailedException) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HttpBinding(org.apache.camel.http.common.HttpBinding) HttpConnectionManager(org.apache.commons.httpclient.HttpConnectionManager) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)

Aggregations

URI (java.net.URI)5680 Test (org.junit.Test)1852 URISyntaxException (java.net.URISyntaxException)1016 IOException (java.io.IOException)749 File (java.io.File)531 HashMap (java.util.HashMap)458 ArrayList (java.util.ArrayList)452 Test (org.testng.annotations.Test)394 Configuration (org.apache.hadoop.conf.Configuration)321 Path (org.apache.hadoop.fs.Path)267 URL (java.net.URL)266 Map (java.util.Map)262 Response (javax.ws.rs.core.Response)218 List (java.util.List)184 InputStream (java.io.InputStream)154 HashSet (java.util.HashSet)136 FileSystem (org.apache.hadoop.fs.FileSystem)135 RequestContext (com.linkedin.r2.message.RequestContext)129 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)128 RestRequest (com.linkedin.r2.message.rest.RestRequest)112