Search in sources :

Example 16 with URI

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

the class DefaultComponent method createEndpoint.

public Endpoint createEndpoint(String uri) throws Exception {
    ObjectHelper.notNull(getCamelContext(), "camelContext");
    // check URI string to the unsafe URI characters
    String encodedUri = preProcessUri(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;
    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=[{}]", new Object[] { URISupport.sanitizeUri(uri), URISupport.sanitizePath(path) });
    }
    Endpoint endpoint = createEndpoint(uri, path, parameters);
    if (endpoint == null) {
        return null;
    }
    endpoint.configureProperties(parameters);
    if (useIntrospectionOnEndpoint()) {
        setProperties(endpoint, parameters);
    }
    // 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;
}
Also used : Endpoint(org.apache.camel.Endpoint) URI(java.net.URI) Endpoint(org.apache.camel.Endpoint)

Example 17 with URI

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

the class DefaultCamelContext method explainEndpointJson.

// CHECKSTYLE:OFF
public String explainEndpointJson(String uri, boolean includeAllOptions) {
    try {
        URI u = new URI(uri);
        String json = getComponentParameterJsonSchema(u.getScheme());
        if (json == null) {
            return null;
        }
        List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("properties", json, true);
        // selected rows to use for answer
        Map<String, String[]> selected = new LinkedHashMap<String, String[]>();
        Map<String, String[]> uriOptions = new LinkedHashMap<String, String[]>();
        // insert values from uri
        Map<String, Object> options = EndpointHelper.endpointProperties(this, uri);
        // extract consumer. prefix options
        Map<String, Object> consumerOptions = IntrospectionSupport.extractProperties(options, "consumer.");
        // and add back again without the consumer. prefix as that json schema omits that
        options.putAll(consumerOptions);
        for (Map.Entry<String, Object> entry : options.entrySet()) {
            String name = entry.getKey();
            String value = "";
            if (entry.getValue() != null) {
                value = entry.getValue().toString();
            }
            value = URISupport.sanitizePath(value);
            // find type and description from the json schema
            String type = null;
            String kind = null;
            String group = null;
            String label = null;
            String required = null;
            String javaType = null;
            String deprecated = null;
            String secret = null;
            String defaultValue = null;
            String description = null;
            for (Map<String, String> row : rows) {
                if (name.equals(row.get("name"))) {
                    type = row.get("type");
                    kind = row.get("kind");
                    group = row.get("group");
                    label = row.get("label");
                    required = row.get("required");
                    javaType = row.get("javaType");
                    deprecated = row.get("deprecated");
                    secret = row.get("secret");
                    defaultValue = row.get("defaultValue");
                    description = row.get("description");
                    break;
                }
            }
            // remember this option from the uri
            uriOptions.put(name, new String[] { name, kind, group, label, required, type, javaType, deprecated, secret, value, defaultValue, description });
        }
        // include other rows
        for (Map<String, String> row : rows) {
            String name = row.get("name");
            String kind = row.get("kind");
            String group = row.get("group");
            String label = row.get("label");
            String required = row.get("required");
            String value = row.get("value");
            String defaultValue = row.get("defaultValue");
            String type = row.get("type");
            String javaType = row.get("javaType");
            String deprecated = row.get("deprecated");
            String secret = row.get("secret");
            value = URISupport.sanitizePath(value);
            String description = row.get("description");
            boolean isUriOption = uriOptions.containsKey(name);
            // always include from uri or path options
            if (includeAllOptions || isUriOption || "path".equals(kind)) {
                if (!selected.containsKey(name)) {
                    // add as selected row, but take the value from uri options if it was from there
                    if (isUriOption) {
                        selected.put(name, uriOptions.get(name));
                    } else {
                        selected.put(name, new String[] { name, kind, group, label, required, type, javaType, deprecated, secret, value, defaultValue, description });
                    }
                }
            }
        }
        // skip component properties
        json = ObjectHelper.before(json, "  \"componentProperties\": {");
        // and rewrite properties
        StringBuilder buffer = new StringBuilder("  \"properties\": {");
        boolean first = true;
        for (String[] row : selected.values()) {
            if (first) {
                first = false;
            } else {
                buffer.append(",");
            }
            buffer.append("\n    ");
            String name = row[0];
            String kind = row[1];
            String group = row[2];
            String label = row[3];
            String required = row[4];
            String type = row[5];
            String javaType = row[6];
            String deprecated = row[7];
            String secret = row[8];
            String value = row[9];
            String defaultValue = row[10];
            String description = row[11];
            // add json of the option
            buffer.append(StringQuoteHelper.doubleQuote(name)).append(": { ");
            CollectionStringBuffer csb = new CollectionStringBuffer();
            if (kind != null) {
                csb.append("\"kind\": \"" + kind + "\"");
            }
            if (group != null) {
                csb.append("\"group\": \"" + group + "\"");
            }
            if (label != null) {
                csb.append("\"label\": \"" + label + "\"");
            }
            if (required != null) {
                csb.append("\"required\": \"" + required + "\"");
            }
            if (type != null) {
                csb.append("\"type\": \"" + type + "\"");
            }
            if (javaType != null) {
                csb.append("\"javaType\": \"" + javaType + "\"");
            }
            if (deprecated != null) {
                csb.append("\"deprecated\": \"" + deprecated + "\"");
            }
            if (secret != null) {
                csb.append("\"secret\": \"" + secret + "\"");
            }
            if (value != null) {
                csb.append("\"value\": \"" + value + "\"");
            }
            if (defaultValue != null) {
                csb.append("\"defaultValue\": \"" + defaultValue + "\"");
            }
            if (description != null) {
                csb.append("\"description\": \"" + description + "\"");
            }
            if (!csb.isEmpty()) {
                buffer.append(csb.toString());
            }
            buffer.append(" }");
        }
        buffer.append("\n  }\n}\n");
        // insert the original first part of the json into the start of the buffer
        buffer.insert(0, json);
        return buffer.toString();
    } catch (Exception e) {
        // ignore and return empty response
        return null;
    }
}
Also used : CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer) URI(java.net.URI) RuntimeCamelException(org.apache.camel.RuntimeCamelException) MalformedObjectNameException(javax.management.MalformedObjectNameException) VetoCamelContextStartException(org.apache.camel.VetoCamelContextStartException) IOException(java.io.IOException) LoadPropertiesException(org.apache.camel.util.LoadPropertiesException) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) ResolveEndpointFailedException(org.apache.camel.ResolveEndpointFailedException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) FailedToStartRouteException(org.apache.camel.FailedToStartRouteException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 18 with URI

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

the class AhcHelper 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, AhcEndpoint endpoint) throws URISyntaxException {
    URI uri = new URI(url);
    // 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);
    if (queryString == null) {
        queryString = endpoint.getHttpUri().getRawQuery();
    }
    // We should user the query string from the HTTP_URI header
    if (queryString == null) {
        queryString = uri.getQuery();
    }
    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 19 with URI

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

the class ConfigurationHelper method populateFromURI.

public static void populateFromURI(CamelContext camelContext, EndpointConfiguration config, ParameterSetter setter) {
    URI uri = config.getURI();
    setter.set(camelContext, config, EndpointConfiguration.URI_SCHEME, uri.getScheme());
    setter.set(camelContext, config, EndpointConfiguration.URI_SCHEME_SPECIFIC_PART, uri.getSchemeSpecificPart());
    setter.set(camelContext, config, EndpointConfiguration.URI_AUTHORITY, uri.getAuthority());
    setter.set(camelContext, config, EndpointConfiguration.URI_USER_INFO, uri.getUserInfo());
    setter.set(camelContext, config, EndpointConfiguration.URI_HOST, uri.getHost());
    setter.set(camelContext, config, EndpointConfiguration.URI_PORT, Integer.toString(uri.getPort()));
    setter.set(camelContext, config, EndpointConfiguration.URI_PATH, uri.getPath());
    setter.set(camelContext, config, EndpointConfiguration.URI_QUERY, uri.getQuery());
    setter.set(camelContext, config, EndpointConfiguration.URI_FRAGMENT, uri.getFragment());
    // now parse query and set custom parameters
    Map<String, Object> parameters;
    try {
        parameters = URISupport.parseParameters(uri);
        for (Map.Entry<String, Object> pair : parameters.entrySet()) {
            setter.set(camelContext, config, pair.getKey(), pair.getValue());
        }
    } catch (URISyntaxException e) {
        throw new RuntimeCamelException(e);
    }
}
Also used : RuntimeCamelException(org.apache.camel.RuntimeCamelException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Map(java.util.Map)

Example 20 with URI

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

the class ResourceHelper method appendParameters.

/**
     * Appends the parameters to the given uri
     *
     * @param uri the uri
     * @param parameters the additional parameters (will clear the map)
     * @return a new uri with the additional parameters appended
     * @throws URISyntaxException is thrown if the uri is invalid
     */
public static String appendParameters(String uri, Map<String, Object> parameters) throws URISyntaxException {
    // add additional parameters to the resource uri
    if (!parameters.isEmpty()) {
        String query = URISupport.createQueryString(parameters);
        URI u = new URI(uri);
        u = URISupport.createURIWithQuery(u, query);
        parameters.clear();
        return u.toString();
    } else {
        return uri;
    }
}
Also used : URI(java.net.URI)

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