use of java.util.Map in project camel by apache.
the class RestProducer method prepareExchange.
protected void prepareExchange(Exchange exchange) throws Exception {
boolean hasPath = false;
// uri template with path parameters resolved
// uri template may be optional and the user have entered the uri template in the path instead
String resolvedUriTemplate = getEndpoint().getUriTemplate() != null ? getEndpoint().getUriTemplate() : getEndpoint().getPath();
Message inMessage = exchange.getIn();
if (prepareUriTemplate) {
if (resolvedUriTemplate.contains("{")) {
// resolve template and replace {key} with the values form the exchange
// each {} is a parameter (url templating)
String[] arr = resolvedUriTemplate.split("\\/");
CollectionStringBuffer csb = new CollectionStringBuffer("/");
for (String a : arr) {
if (a.startsWith("{") && a.endsWith("}")) {
String key = a.substring(1, a.length() - 1);
String value = inMessage.getHeader(key, String.class);
if (value != null) {
hasPath = true;
csb.append(value);
} else {
csb.append(a);
}
} else {
csb.append(a);
}
}
resolvedUriTemplate = csb.toString();
}
}
// resolve uri parameters
String query = getEndpoint().getQueryParameters();
if (query != null) {
Map<String, Object> params = URISupport.parseQuery(query);
for (Map.Entry<String, Object> entry : params.entrySet()) {
Object v = entry.getValue();
if (v != null) {
String a = v.toString();
// decode the key as { may be decoded to %NN
a = URLDecoder.decode(a, "UTF-8");
if (a.startsWith("{") && a.endsWith("}")) {
String key = a.substring(1, a.length() - 1);
String value = inMessage.getHeader(key, String.class);
if (value != null) {
params.put(key, value);
} else {
params.put(entry.getKey(), entry.getValue());
}
} else {
params.put(entry.getKey(), entry.getValue());
}
}
}
query = URISupport.createQueryString(params);
}
if (query != null) {
// the query parameters for the rest call to be used
inMessage.setHeader(Exchange.REST_HTTP_QUERY, query);
}
if (hasPath) {
String host = getEndpoint().getHost();
String basePath = getEndpoint().getUriTemplate() != null ? getEndpoint().getPath() : null;
basePath = FileUtil.stripLeadingSeparator(basePath);
resolvedUriTemplate = FileUtil.stripLeadingSeparator(resolvedUriTemplate);
// if so us a header for the dynamic uri template so we reuse same endpoint but the header overrides the actual url to use
String overrideUri = host;
if (!ObjectHelper.isEmpty(basePath)) {
overrideUri += "/" + basePath;
}
if (!ObjectHelper.isEmpty(resolvedUriTemplate)) {
overrideUri += "/" + resolvedUriTemplate;
}
// the http uri for the rest call to be used
inMessage.setHeader(Exchange.REST_HTTP_URI, overrideUri);
}
final String produces = getEndpoint().getProduces();
if (isEmpty(inMessage.getHeader(Exchange.CONTENT_TYPE)) && isNotEmpty(produces)) {
inMessage.setHeader(Exchange.CONTENT_TYPE, produces);
}
final String consumes = getEndpoint().getConsumes();
if (isEmpty(inMessage.getHeader(ACCEPT)) && isNotEmpty(consumes)) {
inMessage.setHeader(ACCEPT, consumes);
}
}
use of java.util.Map 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);
}
}
use of java.util.Map in project camel by apache.
the class HostUtils method getAddresses.
/**
* Returns a {@link Set} of {@link InetAddress} that are non-loopback or mac.
*/
public static Set<InetAddress> getAddresses() {
Set<InetAddress> allAddresses = new LinkedHashSet<InetAddress>();
Map<String, Set<InetAddress>> interfaceAddressMap = getNetworkInterfaceAddresses();
for (Map.Entry<String, Set<InetAddress>> entry : interfaceAddressMap.entrySet()) {
Set<InetAddress> addresses = entry.getValue();
if (!addresses.isEmpty()) {
for (InetAddress address : addresses) {
allAddresses.add(address);
}
}
}
return allAddresses;
}
use of java.util.Map in project camel by apache.
the class ArgumentSubstitutionParser method processResults.
@Override
public List<ApiMethodModel> processResults(List<ApiMethodModel> parseResult) {
final List<ApiMethodModel> result = new ArrayList<ApiMethodModel>();
for (ApiMethodModel model : parseResult) {
// look for method name matches
for (Map.Entry<Pattern, Map<Pattern, List<NameReplacement>>> methodEntry : methodMap.entrySet()) {
// match the whole method name
if (methodEntry.getKey().matcher(model.getName()).matches()) {
// look for arg name matches
final List<ApiMethodArg> updatedArguments = new ArrayList<ApiMethodArg>();
final Map<Pattern, List<NameReplacement>> argMap = methodEntry.getValue();
for (ApiMethodArg argument : model.getArguments()) {
final Class<?> argType = argument.getType();
final String typeArgs = argument.getTypeArgs();
final String argTypeName = argType.getCanonicalName();
for (Map.Entry<Pattern, List<NameReplacement>> argEntry : argMap.entrySet()) {
final Matcher matcher = argEntry.getKey().matcher(argument.getName());
// match argument name substring
if (matcher.find()) {
final List<NameReplacement> adapters = argEntry.getValue();
for (NameReplacement adapter : adapters) {
if (adapter.typePattern == null) {
// no type pattern
final String newName = getJavaArgName(matcher.replaceAll(adapter.replacement));
argument = new ApiMethodArg(newName, argType, typeArgs);
} else {
final Matcher typeMatcher = adapter.typePattern.matcher(argTypeName);
if (typeMatcher.find()) {
if (!adapter.replaceWithType) {
// replace argument name
final String newName = getJavaArgName(matcher.replaceAll(adapter.replacement));
argument = new ApiMethodArg(newName, argType, typeArgs);
} else {
// replace name with argument type name
final String newName = getJavaArgName(typeMatcher.replaceAll(adapter.replacement));
argument = new ApiMethodArg(newName, argType, typeArgs);
}
}
}
}
}
}
updatedArguments.add(argument);
}
model = new ApiMethodModel(model.getUniqueName(), model.getName(), model.getResultType(), updatedArguments, model.getMethod());
}
}
result.add(model);
}
return result;
}
use of java.util.Map in project camel by apache.
the class URISupport method resolveRawParameterValues.
/**
* Traverses the given parameters, and resolve any parameter values which uses the RAW token
* syntax: <tt>key=RAW(value)</tt>. This method will then remove the RAW tokens, and replace
* the content of the value, with just the value.
*
* @param parameters the uri parameters
* @see #parseQuery(String)
* @see #RAW_TOKEN_START
* @see #RAW_TOKEN_END
*/
@SuppressWarnings("unchecked")
public static void resolveRawParameterValues(Map<String, Object> parameters) {
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
if (entry.getValue() != null) {
// if the value is a list then we need to iterate
Object value = entry.getValue();
if (value instanceof List) {
List list = (List) value;
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
if (obj != null) {
String str = obj.toString();
if (str.startsWith(RAW_TOKEN_START) && str.endsWith(RAW_TOKEN_END)) {
str = str.substring(4, str.length() - 1);
// update the string in the list
list.set(i, str);
}
}
}
} else {
String str = entry.getValue().toString();
if (str.startsWith(RAW_TOKEN_START) && str.endsWith(RAW_TOKEN_END)) {
str = str.substring(4, str.length() - 1);
entry.setValue(str);
}
}
}
}
}
Aggregations