Search in sources :

Example 26 with URI

use of java.net.URI in project camel by apache.

the class HttpProducer method createMethod.

/**
     * Creates the HttpMethod to use to call the remote server, either its GET or POST.
     *
     * @param exchange the exchange
     * @return the created method as either GET or POST
     * @throws CamelExchangeException is thrown if error creating RequestEntity
     */
@SuppressWarnings("deprecation")
protected HttpMethod createMethod(Exchange exchange) throws Exception {
    // creating the url to use takes 2-steps
    String url = HttpHelper.createURL(exchange, getEndpoint());
    URI uri = HttpHelper.createURI(exchange, url, getEndpoint());
    // get the url and query string from the uri
    url = uri.toASCIIString();
    String queryString = uri.getRawQuery();
    // execute any custom url rewrite
    String rewriteUrl = HttpHelper.urlRewrite(exchange, url, getEndpoint(), this);
    if (rewriteUrl != null) {
        // update url and query string from the rewritten url
        url = rewriteUrl;
        uri = new URI(url);
        // use raw query to have uri decimal encoded which http client requires
        queryString = uri.getRawQuery();
    }
    // remove query string as http client does not accept that
    if (url.indexOf('?') != -1) {
        url = url.substring(0, url.indexOf('?'));
    }
    // create http holder objects for the request
    RequestEntity requestEntity = createRequestEntity(exchange);
    String methodName = HttpHelper.createMethod(exchange, getEndpoint(), requestEntity != null).name();
    HttpMethods methodsToUse = HttpMethods.valueOf(methodName);
    HttpMethod method = methodsToUse.createMethod(url);
    if (queryString != null) {
        // need to encode query string
        queryString = UnsafeUriCharactersEncoder.encode(queryString);
        method.setQueryString(queryString);
    }
    LOG.trace("Using URL: {} with method: {}", url, method);
    if (methodsToUse.isEntityEnclosing()) {
        ((EntityEnclosingMethod) method).setRequestEntity(requestEntity);
        if (requestEntity != null && requestEntity.getContentType() == null) {
            LOG.debug("No Content-Type provided for URL: {} with exchange: {}", url, exchange);
        }
    }
    // there must be a host on the method
    if (method.getHostConfiguration().getHost() == null) {
        throw new IllegalArgumentException("Invalid uri: " + url + ". If you are forwarding/bridging http endpoints, then enable the bridgeEndpoint option on the endpoint: " + getEndpoint());
    }
    return method;
}
Also used : EntityEnclosingMethod(org.apache.commons.httpclient.methods.EntityEnclosingMethod) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity) InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) URI(java.net.URI) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 27 with URI

use of java.net.URI in project camel by apache.

the class HttpHelper method createMethod.

/**
     * Creates the HttpMethod to use to call the remote server, often either its GET or POST.
     *
     * @param exchange  the exchange
     * @return the created method
     * @throws URISyntaxException
     */
public static HttpMethods createMethod(Exchange exchange, HttpCommonEndpoint endpoint, boolean hasPayload) throws URISyntaxException {
    // is a query string provided in the endpoint URI or in a header (header overrules endpoint)
    String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
    // We need also check the HTTP_URI header query part
    String uriString = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
    // resolve placeholders in uriString
    try {
        uriString = exchange.getContext().resolvePropertyPlaceholders(uriString);
    } catch (Exception e) {
        throw new RuntimeExchangeException("Cannot resolve property placeholders with uri: " + uriString, exchange, e);
    }
    if (uriString != null) {
        // in case the URI string contains unsafe characters
        uriString = UnsafeUriCharactersEncoder.encodeHttpURI(uriString);
        URI uri = new URI(uriString);
        queryString = uri.getQuery();
    }
    if (queryString == null) {
        queryString = endpoint.getHttpUri().getRawQuery();
    }
    HttpMethods answer;
    if (endpoint.getHttpMethod() != null) {
        // endpoint configured take precedence
        answer = endpoint.getHttpMethod();
    } else {
        // compute what method to use either GET or POST (header take precedence)
        HttpMethods m = exchange.getIn().getHeader(Exchange.HTTP_METHOD, HttpMethods.class);
        if (m != null) {
            // always use what end-user provides in a header
            answer = m;
        } else if (queryString != null) {
            // if a query string is provided then use GET
            answer = HttpMethods.GET;
        } else {
            // fallback to POST if we have payload, otherwise GET
            answer = hasPayload ? HttpMethods.POST : HttpMethods.GET;
        }
    }
    return answer;
}
Also used : RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException)

Example 28 with URI

use of java.net.URI in project camel by apache.

the class HttpHelper method createURI.

/**
     * Creates the URI to invoke.
     *
     * @param exchange the exchange
     * @param url      the url to invoke
     * @param endpoint the endpoint
     * @return the URI to invoke
     */
public static URI createURI(Exchange exchange, String url, HttpCommonEndpoint endpoint) throws URISyntaxException {
    URI uri = new URI(url);
    // rest producer may provide an override query string to be used which we should discard if using (hence the remove)
    String queryString = (String) exchange.getIn().removeHeader(Exchange.REST_HTTP_QUERY);
    // (header overrules endpoint, raw query header overrules query header)
    if (queryString == null) {
        queryString = exchange.getIn().getHeader(Exchange.HTTP_RAW_QUERY, String.class);
    }
    if (queryString == null) {
        queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
    }
    if (queryString == null) {
        queryString = endpoint.getHttpUri().getRawQuery();
    }
    // We should use the query string from the HTTP_URI header
    if (queryString == null) {
        queryString = uri.getRawQuery();
    }
    if (queryString != null) {
        // need to encode query string
        queryString = UnsafeUriCharactersEncoder.encodeHttpURI(queryString);
        uri = URISupport.createURIWithQuery(uri, queryString);
    }
    return uri;
}
Also used : URI(java.net.URI)

Example 29 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 {
    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;
}
Also used : HashMap(java.util.HashMap) HttpContext(org.apache.http.protocol.HttpContext) UrlRewrite(org.apache.camel.http.common.UrlRewrite) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) HttpRestHeaderFilterStrategy(org.apache.camel.http.common.HttpRestHeaderFilterStrategy) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URI(java.net.URI) Endpoint(org.apache.camel.Endpoint) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters) ResolveEndpointFailedException(org.apache.camel.ResolveEndpointFailedException) HttpBinding(org.apache.camel.http.common.HttpBinding) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager)

Example 30 with URI

use of java.net.URI in project camel by apache.

the class HttpMethodHelper method createMethod.

/**
     * Creates the HttpMethod to use to call the remote server, often either its GET or POST.
     *
     * @param exchange the exchange
     * @return the created method
     * @throws URISyntaxException 
     */
public static HttpMethods createMethod(Exchange exchange, HttpEndpoint endpoint, boolean hasPayload) throws URISyntaxException {
    // is a query string provided in the endpoint URI or in a header (header
    // overrules endpoint)
    String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
    // We need also check the HTTP_URI header query part
    String uriString = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
    // resolve placeholders in uriString
    try {
        uriString = exchange.getContext().resolvePropertyPlaceholders(uriString);
    } catch (Exception e) {
        throw new RuntimeExchangeException("Cannot resolve property placeholders with uri: " + uriString, exchange, e);
    }
    if (uriString != null) {
        // in case the URI string contains unsafe characters
        uriString = UnsafeUriCharactersEncoder.encodeHttpURI(uriString);
        URI uri = new URI(uriString);
        queryString = uri.getQuery();
    }
    if (queryString == null) {
        queryString = endpoint.getHttpUri().getRawQuery();
    }
    // compute what method to use either GET or POST
    HttpMethods answer;
    if (endpoint.getHttpMethod() != null) {
        // endpoint configured take precedence
        answer = HttpMethods.valueOf(endpoint.getHttpMethod().name());
    } else {
        // compute what method to use either GET or POST (header take precedence)
        HttpMethods m = exchange.getIn().getHeader(Exchange.HTTP_METHOD, HttpMethods.class);
        if (m != null) {
            // always use what end-user provides in a header
            answer = m;
        } else if (queryString != null) {
            // if a query string is provided then use GET
            answer = HttpMethods.GET;
        } else {
            // fallback to POST if we have payload, otherwise GET
            answer = hasPayload ? HttpMethods.POST : HttpMethods.GET;
        }
    }
    return answer;
}
Also used : RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) HttpMethods(org.apache.camel.component.http4.HttpMethods) URI(java.net.URI) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) URISyntaxException(java.net.URISyntaxException)

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