Example usage for org.springframework.messaging Message getPayload

List of usage examples for org.springframework.messaging Message getPayload

Introduction

In this page you can find the example usage for org.springframework.messaging Message getPayload.

Prototype

T getPayload();

Source Link

Document

Return the message payload.

Usage

From source file:com.samples.platform.util.AggregatorGetReferenceData.java

/**
 * Aggregate the messages./*from  w w w.  j  ava 2s.com*/
 *
 * @param messages
 *            The list of {@link GetReferenceDataResponseType} containing
 *            messages.
 * @return One Message with the content of all messages.
 */
public Message<?> aggregate(final Collection<Message<JAXBElement<GetReferenceDataResponseType>>> messages) {
    this.logger.trace("+aggregate {}", messages != null ? messages.size() : " collection of messages is null");
    JAXBElement<GetReferenceDataResponseType> response = null;
    if (messages != null) {
        for (Message<JAXBElement<GetReferenceDataResponseType>> message : messages) {
            if (response == null) {
                response = message.getPayload();
            } else {
                JAXBElement<GetReferenceDataResponseType> payload = message.getPayload();
                response.getValue().getReferenceData().addAll(payload.getValue().getReferenceData());
                response.getValue().getFailure().addAll(payload.getValue().getFailure());
            }
        }
    }
    MessageBuilder<JAXBElement<GetReferenceDataResponseType>> m = MessageBuilder.withPayload(response);
    this.logger.trace("-aggregate {}", messages != null ? messages.size() : " collection of messages is null");
    return m.build();
}

From source file:io.spring.TaskProcessorApplicationTests.java

@Test
public void test() throws InterruptedException, IOException {
    channels.input().send(new GenericMessage<Object>(DEFAULT_PAYLOAD));
    Map<String, String> properties = new HashMap();
    properties.put("payload", DEFAULT_PAYLOAD);
    TaskLaunchRequest expectedRequest = new TaskLaunchRequest(
            "maven://org.springframework.cloud.task.app:" + "timestamp-task:jar:1.0.1.RELEASE", null,
            properties, null, null);/*from   w  w w .j a v  a2  s .c  o m*/
    Message<String> result = (Message<String>) collector.forChannel(channels.output()).take();
    TaskLaunchRequest tlq = mapper.readValue(result.getPayload(), TaskLaunchRequest.class);
    assertThat(tlq, is(expectedRequest));
}

From source file:apiserver.services.images.services.coldfusion.ImageRotateCFService.java

public Object execute(Message<?> message) throws ColdFusionException {
    FileRotateJob props = (FileRotateJob) message.getPayload();

    try {// ww  w  .  ja v a  2s .  co  m
        cfcPath = imageConfigMBean.getImageRotatePath();
        String method = imageConfigMBean.getImageRotateMethod();

        // extract properties
        Map<String, Object> methodArgs = props.toMap();

        // execute
        Object cfcResult = coldFusionBridge.invoke(cfcPath, method, methodArgs);

        // strip out the base64 string from the json packet
        //ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
        //mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //String img = mapper.readValue((String)cfcResult, String.class);

        if (cfcResult instanceof byte[]) {
            // convert base64 back to buffered image
            byte[] bytes = Base64.decodeBase64(new String((byte[]) cfcResult));
            BufferedImage bi = ImageIO.read(new ByteArrayInputStream(bytes));
            props.setBufferedImage(bi);
        } else {
            throw new NotImplementedException();
        }

        return props;
    } catch (Throwable e) {
        e.printStackTrace(); //todo use logging library
        throw new RuntimeException(e);
    } finally {

    }
}

From source file:org.thingsplode.server.bus.ThingsplodeServiceLocator.java

public <SRV extends AbstractExecutor> SRV getService(Message<?> message, Class<SRV> type)
        throws SrvExecutionException {
    if (message == null || message.getPayload() == null) {
        throw new SrvExecutionException(ExecutionStatus.DECLINED, ResponseCode.INTERNAL_SYSTEM_ERROR,
                "the message or the message payload cannot be null.");
    }//from  ww w  .  j  a  va2  s.  c o  m
    Object request = message.getPayload();
    if (request instanceof AbstractRequest) {
        AbstractRequest req = (AbstractRequest) request;
        if (req.getServiceProviderName() != null && !req.getServiceProviderName().isEmpty()) {
            try {
                return (SRV) ctx.getBean(req.getServiceProviderName(), type);
            } catch (BeansException ex) {
                throw new SrvExecutionException(((AbstractRequest) request).getMessageId(),
                        ExecutionStatus.DECLINED, ResponseCode.INTERNAL_SYSTEM_ERROR,
                        "Service of name: " + req.getServiceProviderName()
                                + " is not installed! Try to request a different service provider name. "
                                + ex.getMessage(),
                        ex);
            }
        } else {
            String requestBeanName = request.getClass().getSimpleName();
            requestBeanName = StringUtils.uncapitalize(requestBeanName);
            requestBeanName += "Executor";
            try {
                return (SRV) ctx.getBean(requestBeanName, type);
            } catch (BeansException ex) {
                throw new SrvExecutionException(((AbstractRequest) request).getMessageId(),
                        ExecutionStatus.DECLINED, ResponseCode.INTERNAL_SYSTEM_ERROR,
                        "Service of name: " + requestBeanName
                                + " is not installed! Try to request a different service provider name. "
                                + ex.getMessage(),
                        ex);
            }
        }
    } else {
        throw new SrvExecutionException(ExecutionStatus.DECLINED, ResponseCode.INTERNAL_SYSTEM_ERROR,
                "This locator does not support the request of type: " + request.getClass().getSimpleName());
    }
}

From source file:apiserver.services.images.services.coldfusion.ImageDrawingCFService.java

public Object imageDrawTextHandler(Message<?> message) throws ColdFusionException {
    FileTextJob props = (FileTextJob) message.getPayload();

    try {//  ww  w .j a  v  a  2  s  . c o m
        cfcPath = imageConfigMBean.getImageTextPath();
        String method = imageConfigMBean.getImageTextMethod();
        Map<String, Object> methodArgs = props.toMap();

        Object cfcResult = coldFusionBridge.invoke(cfcPath, method, methodArgs);

        if (cfcResult instanceof byte[]) {
            // convert base64 back to buffered image
            byte[] bytes = Base64.decodeBase64(new String((byte[]) cfcResult));
            BufferedImage bi = ImageIO.read(new ByteArrayInputStream(bytes));
            //BufferedImage bi = ImageIO.read(new ByteArrayInputStream( (byte[])cfcResult ));
            props.setBufferedImage(bi);
        } else {
            throw new NotImplementedException();
        }

        return props;
    } catch (Throwable e) {
        e.printStackTrace(); //todo use logging library
        throw new RuntimeException(e);
    }
}

From source file:com.acme.ModuleConfigurationTest.java

@Test
public void test() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    Properties properties = new Properties();
    properties.put("prefix", "foo");
    properties.put("suffix", "bar");
    context.getEnvironment().getPropertySources().addLast(new PropertiesPropertySource("options", properties));
    context.register(TestConfiguration.class);
    context.refresh();//from  w  w  w .jav  a  2s .  co  m

    MessageChannel input = context.getBean("input", MessageChannel.class);
    SubscribableChannel output = context.getBean("output", SubscribableChannel.class);

    final AtomicBoolean handled = new AtomicBoolean();
    output.subscribe(new MessageHandler() {
        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            handled.set(true);
            assertEquals("foohellobar", message.getPayload());
        }
    });
    input.send(new GenericMessage<String>("hello"));
    assertTrue(handled.get());
}

From source file:com.kinglcc.spring.jms.core.Jackson2PayloadArgumentResolver.java

private Object convertFromMessage(MethodParameter parameter, Message<?> message) {
    Object payload = message.getPayload();
    Class<?> targetClass = parameter.getParameterType();
    if (targetClass.isInterface() || Modifier.isAbstract(targetClass.getModifiers())) {
        return payload;
    }/* w w w . ja  va2 s  .  co  m*/

    if (this.converter instanceof GenericMessageAdapterConverter) {
        payload = convertJavaTypeFromMessage(message, parameter);
        validate(message, parameter, payload);
        return payload;
    }

    payload = convertClassFromMessage(message, targetClass);
    validate(message, parameter, payload);
    return payload;
}

From source file:biz.c24.io.spring.integration.transformer.IoUnmarshallingTransformerIUTests.java

@Test
public void canUnmarshalTextFromBytearray() throws Exception {

    byte[] valid1 = loadCsvBytes();
    C24UnmarshallingTransformer transformer = new C24UnmarshallingTransformer(model);
    transformer.setSourceFactory(new TextualSourceFactory());

    Message message = MessageBuilder.withPayload(valid1).build();

    Message<?> outputMessage = transformer.transform(message);

    assertThat(outputMessage.getPayload(), notNullValue());
    assertThat(outputMessage.getPayload(), is(Employees.class));

    Employees employees = (Employees) outputMessage.getPayload();

}

From source file:biz.c24.io.spring.integration.transformer.IoUnmarshallingTransformerIUTests.java

@Test
public void canUnmarshalXmlFromString() throws Exception {

    String validString = loadXmlString();

    C24UnmarshallingTransformer transformer = new C24UnmarshallingTransformer(model, new XmlSourceFactory());

    Message message = MessageBuilder.withPayload(validString).build();

    Message<?> outputMessage = transformer.transform(message);

    assertThat(outputMessage.getPayload(), notNullValue());
    assertThat(outputMessage.getPayload(), is(Employees.class));

    Employees employees = (Employees) outputMessage.getPayload();

}

From source file:biz.c24.io.spring.integration.transformer.IoUnmarshallingTransformerIUTests.java

@Test
public void canUnmarshalXmlFromBytearray() throws Exception {

    byte[] valid1 = loadXmlBytes();

    C24UnmarshallingTransformer transformer = new C24UnmarshallingTransformer(model, new XmlSourceFactory());

    Message message = MessageBuilder.withPayload(valid1).build();

    Message<?> outputMessage = transformer.transform(message);

    assertThat(outputMessage.getPayload(), notNullValue());
    assertThat(outputMessage.getPayload(), is(Employees.class));

    Employees employees = (Employees) outputMessage.getPayload();

}