List of usage examples for org.springframework.integration.support MessageBuilder copyHeadersIfAbsent
@Override
public MessageBuilder<T> copyHeadersIfAbsent(@Nullable Map<String, ?> headersToCopy)
From source file:com.joshlong.activiti.coordinator.CoordinatorGatewayClient.java
public Message<?> processMessage(Message<?> message) throws MessagingException { try {//from w ww . ja v a 2s.c o m MessageHeaders headers = message.getHeaders(); String procName = (String) headers.get(CoordinatorConstants.PROCESS_NAME); String stateName = (String) headers.get(CoordinatorConstants.STATE_NAME); ActivitiStateHandlerRegistration registration = registry.findRegistrationForProcessAndState(procName, stateName); Object bean = applicationContext.getBean(registration.getBeanName()); Method method = registration.getHandlerMethod(); // int size = method.getParameterTypes().length; ArrayList<Object> argsList = new ArrayList<Object>(); Map<Integer, String> processVariablesMap = registration.getProcessVariablesExpected(); // already has which indexes get which process variables Map<Integer, Object> variables = new HashMap<Integer, Object>(); for (Integer i : processVariablesMap.keySet()) variables.put(i, headers.get(processVariablesMap.get(i))); if (registration.requiresProcessId()) { variables.put(registration.getProcessIdIndex(), headers.get(CoordinatorConstants.PROC_ID)); } // System.out.println(variables.toString()); List<Integer> indices = new ArrayList<Integer>(variables.keySet()); Collections.sort(indices); argsList.clear(); for (Integer idx : indices) argsList.add(variables.get(idx)); Object[] args = argsList.toArray(new Object[argsList.size()]); Object result = (args.length == 0) ? method.invoke(bean) : method.invoke(bean, args); MessageBuilder builder = MessageBuilder.withPayload(message.getPayload()) .copyHeaders(message.getHeaders()); if (result instanceof Map) { // then we update the BPM // by including them in the reply message builder.copyHeadersIfAbsent((Map) result); } return builder.build(); } catch (Throwable e) { throw new RuntimeException(e); } }
From source file:org.springframework.integration.gateway.GatewayMethodInboundMessageMapper.java
private Message<?> mapArgumentsToMessage(Object[] arguments) { Object messageOrPayload = null; boolean foundPayloadAnnotation = false; Map<String, Object> headers = new HashMap<String, Object>(); EvaluationContext methodInvocationEvaluationContext = createMethodInvocationEvaluationContext(arguments); if (this.payloadExpression != null) { messageOrPayload = this.payloadExpression.getValue(methodInvocationEvaluationContext); }//w w w. j a v a2s . c o m for (int i = 0; i < this.parameterList.size(); i++) { Object argumentValue = arguments[i]; MethodParameter methodParameter = this.parameterList.get(i); Annotation annotation = this.findMappingAnnotation(methodParameter.getParameterAnnotations()); if (annotation != null) { if (annotation.annotationType().equals(Payload.class)) { if (messageOrPayload != null) { this.throwExceptionForMultipleMessageOrPayloadParameters(methodParameter); } String expression = ((Payload) annotation).value(); if (!StringUtils.hasText(expression)) { messageOrPayload = argumentValue; } else { messageOrPayload = this.evaluatePayloadExpression(expression, argumentValue); } foundPayloadAnnotation = true; } else if (annotation.annotationType().equals(Header.class)) { Header headerAnnotation = (Header) annotation; String headerName = this.determineHeaderName(headerAnnotation, methodParameter); if (headerAnnotation.required() && argumentValue == null) { throw new IllegalArgumentException( "Received null argument value for required header: '" + headerName + "'"); } headers.put(headerName, argumentValue); } else if (annotation.annotationType().equals(Headers.class)) { if (argumentValue != null) { if (!(argumentValue instanceof Map)) { throw new IllegalArgumentException( "@Headers annotation is only valid for Map-typed parameters"); } for (Object key : ((Map<?, ?>) argumentValue).keySet()) { Assert.isInstanceOf(String.class, key, "Invalid header name [" + key + "], name type must be String."); Object value = ((Map<?, ?>) argumentValue).get(key); headers.put((String) key, value); } } } } else if (messageOrPayload == null) { messageOrPayload = argumentValue; } else if (Map.class.isAssignableFrom(methodParameter.getParameterType())) { if (messageOrPayload instanceof Map && !foundPayloadAnnotation) { if (payloadExpression == null) { throw new MessagingException("Ambiguous method parameters; found more than one " + "Map-typed parameter and neither one contains a @Payload annotation"); } } this.copyHeaders((Map<?, ?>) argumentValue, headers); } else if (this.payloadExpression == null) { this.throwExceptionForMultipleMessageOrPayloadParameters(methodParameter); } } Assert.isTrue(messageOrPayload != null, "unable to determine a Message or payload parameter on method [" + method + "]"); MessageBuilder<?> builder = (messageOrPayload instanceof Message) ? MessageBuilder.fromMessage((Message<?>) messageOrPayload) : MessageBuilder.withPayload(messageOrPayload); builder.copyHeadersIfAbsent(headers); if (!CollectionUtils.isEmpty(this.headerExpressions)) { Map<String, Object> evaluatedHeaders = new HashMap<String, Object>(); for (Map.Entry<String, Expression> entry : this.headerExpressions.entrySet()) { Object value = entry.getValue().getValue(methodInvocationEvaluationContext); if (value != null) { evaluatedHeaders.put(entry.getKey(), value); } } builder.copyHeaders(evaluatedHeaders); } return builder.build(); }
From source file:org.springframework.integration.ip.tcp.connection.TcpMessageMapper.java
protected final void addCustomHeaders(TcpConnection connection, MessageBuilder<?> messageBuilder) { Map<String, ?> customHeaders = this.supplyCustomHeaders(connection); if (customHeaders != null) { messageBuilder.copyHeadersIfAbsent(customHeaders); }// w w w .ja v a 2 s. co m }