use of java.util.Map in project camel by apache.
the class AlbertoAggregatorTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
AggregationStrategy surnameAggregator = new AggregationStrategy() {
@SuppressWarnings("unchecked")
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
debugIn("Surname Aggregator", oldExchange, newExchange);
Exchange answer = newExchange;
if (oldExchange != null) {
List<String> brothers = oldExchange.getIn().getBody(List.class);
brothers.add(newExchange.getIn().getBody(String.class));
answer = oldExchange;
} else {
List<String> brothers = new ArrayList<String>();
brothers.add(newExchange.getIn().getBody(String.class));
newExchange.getIn().setBody(brothers);
}
debugOut("Surname Aggregator", answer);
return answer;
}
};
@SuppressWarnings("unchecked")
AggregationStrategy brothersAggregator = new AggregationStrategy() {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
debugIn("Brothers Aggregator", oldExchange, newExchange);
Exchange answer = newExchange;
if (oldExchange != null) {
Map<String, List<?>> brothers = oldExchange.getIn().getBody(Map.class);
brothers.put(newExchange.getIn().getHeader(SURNAME_HEADER, String.class), newExchange.getIn().getBody(List.class));
answer = oldExchange;
} else {
Map<String, List<?>> brothers = new HashMap<String, List<?>>();
brothers.put(newExchange.getIn().getHeader(SURNAME_HEADER, String.class), newExchange.getIn().getBody(List.class));
newExchange.getIn().setBody(brothers);
}
debugOut("Brothers Aggregator", answer);
return answer;
}
};
private void debugIn(String stringId, Exchange oldExchange, Exchange newExchange) {
if (oldExchange != null) {
log.debug(stringId + " old headers in: " + oldExchange.getIn().getHeaders());
log.debug(stringId + " old body in: " + oldExchange.getIn().getBody());
}
log.debug(stringId + " new headers in: " + newExchange.getIn().getHeaders());
log.debug(stringId + " new body in: " + newExchange.getIn().getBody());
}
private void debugOut(String stringId, Exchange exchange) {
log.debug(stringId + " old headers out: " + exchange.getIn().getHeaders());
log.debug(stringId + " old body out: " + exchange.getIn().getBody());
}
@Override
public void configure() throws Exception {
from("direct:start").split(bodyAs(String.class).tokenize(",")).process(// header
new Processor() {
public void process(Exchange exchange) throws Exception {
String[] parts = exchange.getIn().getBody(String.class).split(" ");
exchange.getIn().setBody(parts[0]);
exchange.getIn().setHeader(SURNAME_HEADER, parts[1]);
}
}).to("direct:joinSurnames");
from("direct:joinSurnames").aggregate(header(SURNAME_HEADER), surnameAggregator).completionTimeout(2000L).setHeader(TYPE_HEADER, constant(BROTHERS_TYPE)).to("direct:joinBrothers");
// Join all brothers lists and remove surname and type headers
AggregateDefinition agg = from("direct:joinBrothers").aggregate(header(TYPE_HEADER), brothersAggregator);
agg.setCompletionTimeout(2000L);
agg.removeHeader(SURNAME_HEADER).removeHeader(TYPE_HEADER).to("mock:result");
}
};
}
use of java.util.Map in project camel by apache.
the class BatchGetItemsCommand method execute.
@Override
public void execute() {
BatchGetItemResult result = ddbClient.batchGetItem(new BatchGetItemRequest().withRequestItems(determineBatchItems()));
Map tmp = new HashMap<>();
tmp.put(DdbConstants.BATCH_RESPONSE, result.getResponses());
tmp.put(DdbConstants.UNPROCESSED_KEYS, result.getUnprocessedKeys());
addToResults(tmp);
}
use of java.util.Map in project camel by apache.
the class CwProducer method setDimension.
private void setDimension(MetricDatum metricDatum, Exchange exchange) {
String name = exchange.getIn().getHeader(CwConstants.METRIC_DIMENSION_NAME, String.class);
String value = exchange.getIn().getHeader(CwConstants.METRIC_DIMENSION_VALUE, String.class);
if (name != null && value != null) {
metricDatum.withDimensions(new Dimension().withName(name).withValue(value));
} else {
Map<String, String> dimensions = exchange.getIn().getHeader(CwConstants.METRIC_DIMENSIONS, Map.class);
if (dimensions != null) {
Collection<Dimension> dimensionCollection = new ArrayList<Dimension>();
for (Map.Entry<String, String> dimensionEntry : dimensions.entrySet()) {
Dimension dimension = new Dimension().withName(dimensionEntry.getKey()).withValue(dimensionEntry.getValue());
dimensionCollection.add(dimension);
}
metricDatum.withDimensions(dimensionCollection);
}
}
}
use of java.util.Map in project camel by apache.
the class DefaultCamelContext method doStartOrResumeRoutes.
/**
* Starts or resumes the routes
*
* @param routeServices the routes to start (will only start a route if its not already started)
* @param checkClash whether to check for startup ordering clash
* @param startConsumer whether the route consumer should be started. Can be used to warmup the route without starting the consumer.
* @param resumeConsumer whether the route consumer should be resumed.
* @param addingRoutes whether we are adding new routes
* @throws Exception is thrown if error starting routes
*/
protected void doStartOrResumeRoutes(Map<String, RouteService> routeServices, boolean checkClash, boolean startConsumer, boolean resumeConsumer, boolean addingRoutes) throws Exception {
isStartingRoutes.set(true);
try {
// filter out already started routes
Map<String, RouteService> filtered = new LinkedHashMap<String, RouteService>();
for (Map.Entry<String, RouteService> entry : routeServices.entrySet()) {
boolean startable = false;
Consumer consumer = entry.getValue().getRoutes().iterator().next().getConsumer();
if (consumer instanceof SuspendableService) {
// consumer could be suspended, which is not reflected in the RouteService status
startable = ((SuspendableService) consumer).isSuspended();
}
if (!startable && consumer instanceof StatefulService) {
// consumer could be stopped, which is not reflected in the RouteService status
startable = ((StatefulService) consumer).getStatus().isStartable();
} else if (!startable) {
// no consumer so use state from route service
startable = entry.getValue().getStatus().isStartable();
}
if (startable) {
filtered.put(entry.getKey(), entry.getValue());
}
}
// the context is in last phase of staring, so lets start the routes
safelyStartRouteServices(checkClash, startConsumer, resumeConsumer, addingRoutes, filtered.values());
} finally {
isStartingRoutes.remove();
}
}
use of java.util.Map in project camel by apache.
the class DefaultCamelContext method explainDataFormatJson.
public String explainDataFormatJson(String dataFormatName, DataFormat dataFormat, boolean includeAllOptions) {
try {
String json = getDataFormatParameterJsonSchema(dataFormatName);
if (json == null) {
// the model may be shared for multiple data formats such as bindy, json (xstream, jackson, gson)
if (dataFormatName.contains("-")) {
dataFormatName = ObjectHelper.before(dataFormatName, "-");
json = getDataFormatParameterJsonSchema(dataFormatName);
}
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[]> dataFormatOptions = new LinkedHashMap<String, String[]>();
// extract options from the data format
Map<String, Object> options = new LinkedHashMap<String, Object>();
IntrospectionSupport.getProperties(dataFormat, options, "", false);
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 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");
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
dataFormatOptions.put(name, new String[] { name, kind, 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 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 isDataFormatOption = dataFormatOptions.containsKey(name);
// always include from uri or path options
if (includeAllOptions || isDataFormatOption) {
if (!selected.containsKey(name)) {
// add as selected row, but take the value from uri options if it was from there
if (isDataFormatOption) {
selected.put(name, dataFormatOptions.get(name));
} else {
selected.put(name, new String[] { name, kind, label, required, type, javaType, deprecated, secret, value, defaultValue, description });
}
}
}
}
json = ObjectHelper.before(json, " \"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 label = row[2];
String required = row[3];
String type = row[4];
String javaType = row[5];
String deprecated = row[6];
String secret = row[7];
String value = row[8];
String defaultValue = row[9];
String description = row[10];
// add json of the option
buffer.append(StringQuoteHelper.doubleQuote(name)).append(": { ");
CollectionStringBuffer csb = new CollectionStringBuffer();
if (kind != null) {
csb.append("\"kind\": \"" + kind + "\"");
}
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;
}
}
Aggregations