use of org.springframework.integration.transformer.support.HeaderValueMessageProcessor in project spring-integration by spring-projects.
the class ContentEnricher method handleRequestMessage.
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
final Object requestPayload = requestMessage.getPayload();
final Object targetPayload;
if (requestPayload instanceof Cloneable && this.shouldClonePayload) {
try {
Method cloneMethod = requestPayload.getClass().getMethod("clone");
targetPayload = ReflectionUtils.invokeMethod(cloneMethod, requestPayload);
} catch (Exception e) {
throw new MessageHandlingException(requestMessage, "Failed to clone payload object", e);
}
} else {
targetPayload = requestPayload;
}
final Message<?> actualRequestMessage;
if (this.requestPayloadExpression == null) {
actualRequestMessage = requestMessage;
} else {
final Object requestMessagePayload = this.requestPayloadExpression.getValue(this.sourceEvaluationContext, requestMessage);
actualRequestMessage = this.getMessageBuilderFactory().withPayload(requestMessagePayload).copyHeaders(requestMessage.getHeaders()).build();
}
final Message<?> replyMessage;
if (this.gateway == null) {
replyMessage = actualRequestMessage;
} else {
replyMessage = this.gateway.sendAndReceiveMessage(actualRequestMessage);
if (replyMessage == null) {
if (this.nullResultPropertyExpressions.isEmpty() && this.nullResultHeaderExpressions.isEmpty()) {
return null;
}
for (Map.Entry<Expression, Expression> entry : this.nullResultPropertyExpressions.entrySet()) {
Expression propertyExpression = entry.getKey();
Expression valueExpression = entry.getValue();
Object value = valueExpression.getValue(this.sourceEvaluationContext, requestMessage);
propertyExpression.setValue(this.targetEvaluationContext, targetPayload, value);
}
if (this.nullResultHeaderExpressions.isEmpty()) {
return targetPayload;
} else {
Map<String, Object> targetHeaders = new HashMap<String, Object>(this.nullResultHeaderExpressions.size());
for (Map.Entry<String, HeaderValueMessageProcessor<?>> entry : this.nullResultHeaderExpressions.entrySet()) {
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);
}
}
return this.getMessageBuilderFactory().withPayload(targetPayload).copyHeaders(targetHeaders).build();
}
}
}
for (Map.Entry<Expression, Expression> entry : this.propertyExpressions.entrySet()) {
Expression propertyExpression = entry.getKey();
Expression valueExpression = entry.getValue();
Object value = valueExpression.getValue(this.sourceEvaluationContext, replyMessage);
propertyExpression.setValue(this.targetEvaluationContext, targetPayload, value);
}
if (this.headerExpressions.isEmpty()) {
return targetPayload;
} else {
Map<String, Object> targetHeaders = new HashMap<String, Object>(this.headerExpressions.size());
for (Map.Entry<String, HeaderValueMessageProcessor<?>> entry : this.headerExpressions.entrySet()) {
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);
}
}
return getMessageBuilderFactory().withPayload(targetPayload).copyHeaders(targetHeaders);
}
}
use of org.springframework.integration.transformer.support.HeaderValueMessageProcessor in project spring-integration by spring-projects.
the class ContentEnricher method doInit.
/**
* Initializes the Content Enricher. Will instantiate an internal Gateway if the
* requestChannel is set.
*/
@Override
protected void doInit() {
Assert.state(!(this.requestChannelName != null && this.requestChannel != null), "'requestChannelName' and 'requestChannel' are mutually exclusive.");
Assert.state(!(this.replyChannelName != null && this.replyChannel != null), "'replyChannelName' and 'replyChannel' are mutually exclusive.");
Assert.state(!(this.errorChannelName != null && this.errorChannel != null), "'errorChannelName' and 'errorChannel' are mutually exclusive.");
if (this.replyChannel != null || this.replyChannelName != null) {
Assert.state(this.requestChannel != null || this.requestChannelName != null, "If the replyChannel is set, then the requestChannel must not be null");
}
if (this.errorChannel != null || this.errorChannelName != null) {
Assert.state(this.requestChannel != null || this.requestChannelName != null, "If the errorChannel is set, then the requestChannel must not be null");
}
if (this.requestChannel != null || this.requestChannelName != null) {
this.gateway = new Gateway();
this.gateway.setRequestChannel(this.requestChannel);
if (this.requestChannelName != null) {
this.gateway.setRequestChannelName(this.requestChannelName);
}
if (this.requestTimeout != null) {
this.gateway.setRequestTimeout(this.requestTimeout);
}
if (this.replyTimeout != null) {
this.gateway.setReplyTimeout(this.replyTimeout);
}
this.gateway.setReplyChannel(this.replyChannel);
if (this.replyChannelName != null) {
this.gateway.setReplyChannelName(this.replyChannelName);
}
this.gateway.setErrorChannel(this.errorChannel);
if (this.errorChannelName != null) {
this.gateway.setErrorChannelName(this.errorChannelName);
}
if (this.getBeanFactory() != null) {
this.gateway.setBeanFactory(this.getBeanFactory());
}
this.gateway.afterPropertiesSet();
}
if (this.sourceEvaluationContext == null) {
this.sourceEvaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
}
StandardEvaluationContext targetContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
// bean resolution is NOT allowed for the target of the enrichment
targetContext.setBeanResolver(null);
this.targetEvaluationContext = targetContext;
if (getBeanFactory() != null) {
boolean checkReadOnlyHeaders = getMessageBuilderFactory() instanceof DefaultMessageBuilderFactory;
for (Map.Entry<String, HeaderValueMessageProcessor<?>> entry : this.headerExpressions.entrySet()) {
if (checkReadOnlyHeaders && (MessageHeaders.ID.equals(entry.getKey()) || MessageHeaders.TIMESTAMP.equals(entry.getKey()))) {
throw new BeanInitializationException("ContentEnricher cannot override 'id' and 'timestamp' read-only headers.\n" + "Wrong 'headerExpressions' [" + this.headerExpressions + "] configuration for " + getComponentName());
}
if (entry.getValue() instanceof BeanFactoryAware) {
((BeanFactoryAware) entry.getValue()).setBeanFactory(getBeanFactory());
}
}
for (Map.Entry<String, HeaderValueMessageProcessor<?>> entry : this.nullResultHeaderExpressions.entrySet()) {
if (checkReadOnlyHeaders && (MessageHeaders.ID.equals(entry.getKey()) || MessageHeaders.TIMESTAMP.equals(entry.getKey()))) {
throw new BeanInitializationException("ContentEnricher cannot override 'id' and 'timestamp' read-only headers.\n" + "Wrong 'nullResultHeaderExpressions' [" + this.nullResultHeaderExpressions + "] configuration for " + getComponentName());
}
if (entry.getValue() instanceof BeanFactoryAware) {
((BeanFactoryAware) entry.getValue()).setBeanFactory(getBeanFactory());
}
}
}
}
Aggregations