private EnricherSpec headerExpression(String name, Expression expression, Boolean overwrite) { AbstractHeaderValueMessageProcessor<?> headerValueMessageProcessor = new ExpressionEvaluatingHeaderValueMessageProcessor<>(expression, null); headerValueMessageProcessor.setOverwrite(overwrite); return header(name, headerValueMessageProcessor); }
/** * @param name the header name. * @param value the value. * @param overwrite true to overwrite the header if already present. * @param <V> the value type. * @return the enricher spec. * @see ContentEnricher#setHeaderExpressions(Map) */ public <V> EnricherSpec header(String name, V value, Boolean overwrite) { AbstractHeaderValueMessageProcessor<V> headerValueMessageProcessor = new StaticHeaderValueMessageProcessor<V>(value); headerValueMessageProcessor.setOverwrite(overwrite); return header(name, headerValueMessageProcessor); }
@Bean @Transformer(inputChannel = "invalidRoutingSlipChannel", outputChannel = "processChannel") public HeaderEnricher headerEnricher2() { return new HeaderEnricher(Collections.singletonMap(IntegrationMessageHeaderAccessor.ROUTING_SLIP, new RoutingSlipHeaderValueMessageProcessor((RoutingSlipRouteStrategy) (message, r) -> new Date()))); }
@Test public void overwriteId() { HeaderEnricher enricher = new HeaderEnricher(Collections.singletonMap(MessageHeaders.ID, new StaticHeaderValueMessageProcessor<>("foo"))); try { enricher.afterPropertiesSet(); fail("BeanInitializationException expected"); } catch (Exception e) { assertThat(e, instanceOf(BeanInitializationException.class)); assertThat(e.getMessage(), containsString("HeaderEnricher cannot override 'id' and 'timestamp' read-only headers.")); } }
@Override public Message<?> transform(Message<?> message) { MessageHeaders messageHeaders = message.getHeaders(); AbstractIntegrationMessageBuilder<?> messageBuilder = getMessageBuilderFactory() .fromMessage(message); addHeadersFromMessageProcessor(message, messageBuilder); for (Map.Entry<String, ? extends HeaderValueMessageProcessor<?>> entry : this.headersToAdd.entrySet()) { String key = entry.getKey(); HeaderValueMessageProcessor<?> valueProcessor = entry.getValue(); Boolean shouldOverwrite = valueProcessor.isOverwrite(); if (shouldOverwrite == null) { shouldOverwrite = this.defaultOverwrite; } boolean headerDoesNotExist = messageHeaders.get(key) == null; /* * Only evaluate value expression if necessary */ if (headerDoesNotExist || shouldOverwrite) { Object value = valueProcessor.processMessage(message); if (value != null || !this.shouldSkipNulls) { messageBuilder.setHeader(key, value); } } } return messageBuilder.build(); }
((BeanFactoryAware) processor).setBeanFactory(getBeanFactory()); Boolean processorOverwrite = processor.isOverwrite(); if (processorOverwrite != null) { shouldOverwrite |= processorOverwrite;
private HeaderEnricherSpec headerExpression(String name, Expression expression, Boolean overwrite) { AbstractHeaderValueMessageProcessor<?> headerValueMessageProcessor = new ExpressionEvaluatingHeaderValueMessageProcessor<>(expression, null); headerValueMessageProcessor.setOverwrite(overwrite); return header(name, headerValueMessageProcessor); }
/** * Add a single header specification. * @param name the header name. * @param value the header value (not an {@link Expression}). * @param overwrite true to overwrite an existing header. * @param <V> the value type. * @return the header enricher spec. */ public <V> HeaderEnricherSpec header(String name, V value, Boolean overwrite) { AbstractHeaderValueMessageProcessor<V> headerValueMessageProcessor = new StaticHeaderValueMessageProcessor<>(value); headerValueMessageProcessor.setOverwrite(overwrite); return header(name, headerValueMessageProcessor); }
@Test public void testOverwriteTimestamp() { ContentEnricher contentEnricher = new ContentEnricher(); contentEnricher.setHeaderExpressions( Collections.singletonMap(MessageHeaders.TIMESTAMP, new StaticHeaderValueMessageProcessor<>("foo"))); contentEnricher.setBeanFactory(mock(BeanFactory.class)); try { contentEnricher.afterPropertiesSet(); fail("BeanInitializationException expected"); } catch (Exception e) { assertThat(e, instanceOf(BeanInitializationException.class)); assertThat(e.getMessage(), containsString("ContentEnricher cannot override 'id' and 'timestamp' read-only headers.")); } }
String header = entry.getKey(); HeaderValueMessageProcessor<?> valueProcessor = entry.getValue(); Boolean overwrite = valueProcessor.isOverwrite(); overwrite = overwrite != null ? overwrite : true; if (overwrite || !requestMessage.getHeaders().containsKey(header)) { Object value = valueProcessor.processMessage(requestMessage); targetHeaders.put(header, value); String header = entry.getKey(); HeaderValueMessageProcessor<?> valueProcessor = entry.getValue(); Boolean overwrite = valueProcessor.isOverwrite(); overwrite = overwrite != null ? overwrite : true; if (overwrite || !requestMessage.getHeaders().containsKey(header)) { Object value = valueProcessor.processMessage(replyMessage); targetHeaders.put(header, value);
@Bean @Transformer(inputChannel = "routingSlipHeaderChannel", outputChannel = "processChannel") public HeaderEnricher headerEnricher() { return new HeaderEnricher(Collections.singletonMap(IntegrationMessageHeaderAccessor.ROUTING_SLIP, new RoutingSlipHeaderValueMessageProcessor(routeStrategy()))); }
/** * Add header specifications; the {@link Map} values must be String representations of * SpEL expressions that will be evaluated at run time when the message headers are * enriched. * @param headers the headers. * @param overwrite true to overwrite existing headers. * @return the header enricher spec. */ public HeaderEnricherSpec headerExpressions(Map<String, String> headers, Boolean overwrite) { Assert.notNull(headers, "'headers' must not be null"); for (Entry<String, String> entry : headers.entrySet()) { AbstractHeaderValueMessageProcessor<Object> processor = new ExpressionEvaluatingHeaderValueMessageProcessor<>(entry.getValue(), null); processor.setOverwrite(overwrite); header(entry.getKey(), processor); } return this; }
@Test public void testOverwriteIdNullResult() { ContentEnricher contentEnricher = new ContentEnricher(); contentEnricher.setNullResultHeaderExpressions( Collections.singletonMap(MessageHeaders.ID, new StaticHeaderValueMessageProcessor<>("foo"))); contentEnricher.setBeanFactory(mock(BeanFactory.class)); try { contentEnricher.afterPropertiesSet(); fail("BeanInitializationException expected"); } catch (Exception e) { assertThat(e, instanceOf(BeanInitializationException.class)); assertThat(e.getMessage(), containsString("ContentEnricher cannot override 'id' and 'timestamp' read-only headers.")); } }
@Test public void testInvalidRoutingSlipRoutStrategy() { try { new RoutingSlipHeaderValueMessageProcessor(new Date()); fail("IllegalArgumentException expected"); } catch (Exception e) { assertThat(e, instanceOf(IllegalArgumentException.class)); assertThat(e.getMessage(), containsString("The RoutingSlip can contain " + "only bean names of MessageChannel or RoutingSlipRouteStrategy, " + "or MessageChannel and RoutingSlipRouteStrategy instances")); } try { this.invalidRoutingSlipChannel.send(new GenericMessage<>("foo")); fail("MessagingException expected"); } catch (Exception e) { assertThat(e, instanceOf(MessagingException.class)); assertThat(e.getMessage(), containsString("replyChannel must be a MessageChannel or String")); } }
/** * Add header specifications from the {@link Map}; if a map value is an * {@link Expression}, it will be evaluated at run time when the message headers are * enriched. Otherwise the value is simply added to the headers. * @param headers The header builder. * @param overwrite true to overwrite existing headers. * @return the header enricher spec. */ public HeaderEnricherSpec headers(Map<String, Object> headers, Boolean overwrite) { Assert.notNull(headers, "'headers' must not be null"); for (Entry<String, Object> entry : headers.entrySet()) { String name = entry.getKey(); Object value = entry.getValue(); if (value instanceof Expression) { AbstractHeaderValueMessageProcessor<Object> processor = new ExpressionEvaluatingHeaderValueMessageProcessor<>((Expression) value, null); processor.setOverwrite(overwrite); header(name, processor); } else { header(name, value, overwrite); } } return this; }
private EnricherSpec headerExpression(String name, Expression expression, Boolean overwrite) { AbstractHeaderValueMessageProcessor<?> headerValueMessageProcessor = new ExpressionEvaluatingHeaderValueMessageProcessor<>(expression, null); headerValueMessageProcessor.setOverwrite(overwrite); return header(name, headerValueMessageProcessor); }
private EnricherSpec headerExpression(String name, Expression expression, Boolean overwrite) { AbstractHeaderValueMessageProcessor<?> headerValueMessageProcessor = new ExpressionEvaluatingHeaderValueMessageProcessor<Object>(expression, null); headerValueMessageProcessor.setOverwrite(overwrite); return header(name, headerValueMessageProcessor); }
private EnricherSpec headerExpression(String name, Expression expression, Boolean overwrite) { AbstractHeaderValueMessageProcessor<?> headerValueMessageProcessor = new ExpressionEvaluatingHeaderValueMessageProcessor<Object>(expression, null); headerValueMessageProcessor.setOverwrite(overwrite); return header(name, headerValueMessageProcessor); }
private HeaderEnricherSpec headerExpression(String name, Expression expression, Boolean overwrite) { AbstractHeaderValueMessageProcessor<?> headerValueMessageProcessor = new ExpressionEvaluatingHeaderValueMessageProcessor<Object>(expression, null); headerValueMessageProcessor.setOverwrite(overwrite); return header(name, headerValueMessageProcessor); }
private HeaderEnricherSpec headerExpression(String name, Expression expression, Boolean overwrite) { AbstractHeaderValueMessageProcessor<?> headerValueMessageProcessor = new ExpressionEvaluatingHeaderValueMessageProcessor<>(expression, null); headerValueMessageProcessor.setOverwrite(overwrite); return header(name, headerValueMessageProcessor); }