Example usage for org.springframework.messaging MessageHeaders get

List of usage examples for org.springframework.messaging MessageHeaders get

Introduction

In this page you can find the example usage for org.springframework.messaging MessageHeaders get.

Prototype

@SuppressWarnings("unchecked")
    @Nullable
    public <T> T get(Object key, Class<T> type) 

Source Link

Usage

From source file:com.googlecode.msidor.springframework.integration.history.MessageHistoryParser.java

/**
 * Parse message history generated by spring integration and produce output with components names and components processing times
 * //w  w w .  ja  va2s.  co  m
 * @param mh message headers to be parsed
 * @return output with components names and components processing times
 */
public static String parse(MessageHeaders mh) {
    StringBuilder sb = new StringBuilder();

    //get message history
    MessageHistory history = mh.get(MessageHistory.HEADER_NAME, MessageHistory.class);
    String[] names = new String[history.size()];
    long[] times = new long[history.size()];

    //go thought all history entries
    Iterator<Properties> historyIterator = history.iterator();
    int i = 0;
    while (historyIterator.hasNext()) {
        Properties gatewayHistory = historyIterator.next();
        String name = gatewayHistory.getProperty("name");
        String historyTimestampStr = gatewayHistory.getProperty("timestamp");
        Long historyTimestamp = Long.parseLong(historyTimestampStr);

        names[i] = name;
        times[i++] = historyTimestamp;
    }

    //calculates the time deltas between components 
    final long lastTimestamp = mh.getTimestamp();
    for (int j = 0; j < names.length; j++) {
        if (j > 0) {
            sb.append("; ");
        }

        if (j + 1 < names.length)
            sb.append(names[j]).append("=").append(times[j + 1] - times[j]);
        else
            sb.append(names[j]).append("=").append(lastTimestamp - times[j]);
    }

    if (sb.length() > 0) {
        sb.append("; ");
    }

    sb.append("total=").append(lastTimestamp - times[0]);

    return sb.toString();
}

From source file:com.consol.citrus.samples.flightbooking.header.HttpHeaderMapper.java

public Integer getSequenceNumber(MessageHeaders headers) {
    Integer sequenceNumber = headers.get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, Integer.class);
    return (sequenceNumber != null ? sequenceNumber : 0);
}

From source file:com.consol.citrus.samples.flightbooking.header.HttpHeaderMapper.java

public Integer getSequenceSize(MessageHeaders headers) {
    Integer sequenceNumber = headers.get(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE, Integer.class);
    return (sequenceNumber != null ? sequenceNumber : 0);
}

From source file:biz.c24.io.spring.integration.config.ValidatingHeaderEnricherTests.java

@Test
public void testValid() {
    Employee employee = new Employee();
    employee.setSalutation("Mr");
    employee.setFirstName("Andy");
    employee.setLastName("Acheson");
    employee.setJobTitle("Software Developer");

    template.convertAndSend(employee);//from  w w  w.  j a  v a  2 s  .  c  o m

    @SuppressWarnings("unchecked")
    Message<Employee> result = (Message<Employee>) outputChannel.receive(1);

    assertThat(result, is(not(nullValue())));

    MessageHeaders headers = result.getHeaders();
    assertThat(headers.containsKey(C24Headers.VALID), is(true));
    assertThat(headers.get(C24Headers.VALID, Boolean.class), is(true));

    assertThat(headers.containsKey(C24Headers.FAIL_EVENTS), is(true));
    assertThat(headers.get(C24Headers.FAIL_EVENTS, Collection.class).size(), is(0));

    assertThat(headers.containsKey(C24Headers.PASS_EVENTS), is(true));
    assertThat(headers.get(C24Headers.PASS_EVENTS, Collection.class).size(), is(greaterThan(0)));

}

From source file:biz.c24.io.spring.integration.config.ValidatingHeaderEnricherTests.java

@Test
public void testInvalid() {
    Employee employee = new Employee();
    employee.setSalutation("Mr");
    // Invalid as first char not capitalised
    employee.setFirstName("andy");
    employee.setLastName("Acheson");
    // Invalid as no job title
    //employee.setJobTitle("Software Developer");

    template.convertAndSend(employee);//from   ww  w  .j a v  a  2s.c  om

    @SuppressWarnings("unchecked")
    Message<Employee> result = (Message<Employee>) outputChannel.receive(1);

    assertThat(result, is(not(nullValue())));

    MessageHeaders headers = result.getHeaders();
    assertThat(headers.containsKey(C24Headers.VALID), is(true));
    assertThat(headers.get(C24Headers.VALID, Boolean.class), is(false));

    assertThat(headers.containsKey(C24Headers.FAIL_EVENTS), is(true));
    assertThat(headers.get(C24Headers.FAIL_EVENTS, Collection.class).size(), is(2));

    assertThat(headers.containsKey(C24Headers.PASS_EVENTS), is(true));
    assertThat(headers.get(C24Headers.PASS_EVENTS, Collection.class).size(), is(greaterThan(0)));

}

From source file:org.springframework.cloud.iot.integration.coap.support.DefaultCoapHeaderMapper.java

@Override
public void fromHeaders(MessageHeaders headers, CoapHeaders coapHeaders) {
    if (logger.isDebugEnabled()) {
        logger.debug(MessageFormat.format("outboundHeaderNames={0}",
                CollectionUtils.arrayToList(this.outboundHeaderNames)));
    }//ww w. j av a 2 s . co m

    for (Entry<String, Object> entry : headers.entrySet()) {
        String name = entry.getKey();
        String lowerName = name.toLowerCase();
    }

    String gatewayService = headers.get("iotGatewayServiceRoute", String.class);
    if (StringUtils.hasText(gatewayService)) {
        coapHeaders.add(9999, gatewayService.getBytes());
    }
}

From source file:org.springframework.integration.samples.filesplit.Application.java

/**
 * Rename the input file after success/failure.
 *
 * @return the flow./*from w  w w .ja  va  2 s  . c o  m*/
 */
@Bean
public MethodInterceptor afterMailAdvice() {
    return invocation -> {
        Message<?> message = (Message<?>) invocation.getArguments()[0];
        MessageHeaders headers = message.getHeaders();
        File originalFile = headers.get(FileHeaders.ORIGINAL_FILE, File.class);
        try {
            invocation.proceed();
            originalFile.renameTo(new File(originalFile.getAbsolutePath() + headers.get(EMAIL_SUCCESS_SUFFIX)));
        } catch (Exception e) {
            originalFile.renameTo(new File(
                    originalFile.getAbsolutePath() + headers.get(EMAIL_SUCCESS_SUFFIX) + ".email.failed"));
        }
        return null;
    };
}

From source file:org.springframework.integration.stomp.support.StompHeaderMapper.java

@Override
@SuppressWarnings("unchecked")
public void fromHeaders(MessageHeaders headers, StompHeaders target) {
    for (Map.Entry<String, Object> entry : headers.entrySet()) {
        String name = entry.getKey();
        if (shouldMapHeader(name, this.outboundHeaderNames)) {
            Object value = entry.getValue();
            if (value != null) {
                setStompHeader(target, name, value);
            }//ww w.j  a v  a  2  s  . com
        } else if (StompHeaderAccessor.NATIVE_HEADERS.equals(name)) {
            MultiValueMap<String, String> multiValueMap = headers.get(StompHeaderAccessor.NATIVE_HEADERS,
                    MultiValueMap.class);
            for (Map.Entry<String, List<String>> entry1 : multiValueMap.entrySet()) {
                name = entry1.getKey();
                if (shouldMapHeader(name, this.outboundHeaderNames)) {
                    String value = entry1.getValue().get(0);
                    if (StringUtils.hasText(value)) {
                        setStompHeader(target, name, value);
                    }
                }
            }
        }
    }
}