use of org.springframework.tuple.Tuple in project spring-cloud-stream by spring-cloud.
the class ContentTypeTests method testSendTuple.
@Test
public void testSendTuple() throws Exception {
try (ConfigurableApplicationContext context = SpringApplication.run(SourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=application/x-spring-tuple")) {
MessageCollector collector = context.getBean(MessageCollector.class);
Source source = context.getBean(Source.class);
Tuple tuple = TupleBuilder.tuple().of("foo", "bar");
source.output().send(MessageBuilder.withPayload(tuple).build());
Message<byte[]> message = (Message<byte[]>) collector.forChannel(source.output()).poll(1, TimeUnit.SECONDS);
assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class).includes(MessageConverterUtils.X_SPRING_TUPLE));
assertThat(TupleBuilder.fromString(new String(message.getPayload()))).isEqualTo(tuple);
}
}
use of org.springframework.tuple.Tuple in project spring-cloud-gateway by spring-cloud.
the class RouteDefinitionRouteLocatorTests method testGetTupleWithSpel.
@Test
public void testGetTupleWithSpel() {
parser = new SpelExpressionParser();
ShortcutConfigurable shortcutConfigurable = new ShortcutConfigurable() {
@Override
public List<String> shortcutFieldOrder() {
return Arrays.asList("bean", "arg1");
}
};
Map<String, String> args = new HashMap<>();
args.put("bean", "#{@foo}");
args.put("arg1", "val1");
Tuple tuple = RouteDefinitionRouteLocator.getTuple(shortcutConfigurable, args, parser, this.beanFactory);
assertThat(tuple).isNotNull();
assertThat(tuple.getValue("bean", Integer.class)).isEqualTo(42);
assertThat(tuple.getString("arg1")).isEqualTo("val1");
}
use of org.springframework.tuple.Tuple in project spring-cloud-stream by spring-cloud.
the class TupleJsonMessageConverter method convertToInternal.
@Override
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
Tuple t = (Tuple) payload;
String json;
if (this.prettyPrint) {
try {
Object tmp = this.objectMapper.readValue(t.toString(), Object.class);
json = this.objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(tmp);
} catch (IOException e) {
this.logger.error(e.getMessage(), e);
return null;
}
} else {
json = t.toString();
}
return json.getBytes();
}
use of org.springframework.tuple.Tuple in project spring-cloud-gateway by spring-cloud.
the class RouteDefinitionRouteLocator method getTuple.
@SuppressWarnings("Duplicates")
// TODO: remove after Tuple is removed
@Deprecated
static /* for testing */
Tuple getTuple(ShortcutConfigurable shortcutConf, Map<String, String> args, SpelExpressionParser parser, BeanFactory beanFactory) {
TupleBuilder builder = TupleBuilder.tuple();
List<String> argNames = shortcutConf.shortcutFieldOrder();
if (!argNames.isEmpty()) {
// ensure size is the same for key replacement later
if (shortcutConf.validateFieldsExist() && args.size() != argNames.size()) {
throw new IllegalArgumentException("Wrong number of arguments. Expected " + argNames + " " + argNames + ". Found " + args.size() + " " + args + "'");
}
}
int entryIdx = 0;
for (Map.Entry<String, String> entry : args.entrySet()) {
String key = normalizeKey(entry.getKey(), entryIdx, shortcutConf, args);
Object value = getValue(parser, beanFactory, entry.getValue());
builder.put(key, value);
entryIdx++;
}
Tuple tuple = builder.build();
if (shortcutConf.validateFieldsExist()) {
for (String name : argNames) {
if (!tuple.hasFieldName(name)) {
throw new IllegalArgumentException("Missing argument '" + name + "'. Given " + tuple);
}
}
}
return tuple;
}
use of org.springframework.tuple.Tuple in project spring-cloud-gateway by spring-cloud.
the class RouteDefinitionRouteLocator method loadGatewayFilters.
@SuppressWarnings("unchecked")
private List<GatewayFilter> loadGatewayFilters(String id, List<FilterDefinition> filterDefinitions) {
List<GatewayFilter> filters = filterDefinitions.stream().map(definition -> {
GatewayFilterFactory factory = this.gatewayFilterFactories.get(definition.getName());
if (factory == null) {
throw new IllegalArgumentException("Unable to find GatewayFilterFactory with name " + definition.getName());
}
Map<String, String> args = definition.getArgs();
if (logger.isDebugEnabled()) {
logger.debug("RouteDefinition " + id + " applying filter " + args + " to " + definition.getName());
}
if (factory.isConfigurable()) {
Map<String, Object> properties = factory.shortcutType().normalize(args, factory, this.parser, this.beanFactory);
Object configuration = factory.newConfig();
ConfigurationUtils.bind(configuration, properties, factory.shortcutFieldPrefix(), definition.getName(), validator);
GatewayFilter gatewayFilter = factory.apply(configuration);
if (this.publisher != null) {
this.publisher.publishEvent(new FilterArgsEvent(this, id, properties));
}
return gatewayFilter;
} else {
Tuple tuple = getTuple(factory, args, this.parser, this.beanFactory);
return factory.apply(tuple);
}
}).collect(Collectors.toList());
ArrayList<GatewayFilter> ordered = new ArrayList<>(filters.size());
for (int i = 0; i < filters.size(); i++) {
ordered.add(new OrderedGatewayFilter(filters.get(i), i + 1));
}
return ordered;
}
Aggregations