Search in sources :

Example 1 with MutableMessageHeaders

use of org.springframework.integration.support.MutableMessageHeaders in project spring-integration by spring-projects.

the class MessageConvertingTcpMessageMapper method toMessage.

@Override
public Message<?> toMessage(TcpConnection connection, @Nullable Map<String, Object> headers) throws Exception {
    Object data = connection.getPayload();
    if (data != null) {
        MessageHeaders messageHeaders = new MutableMessageHeaders(null, MessageHeaders.ID_VALUE_NONE, -1L) {

            private static final long serialVersionUID = 3084692953798643018L;
        };
        addStandardHeaders(connection, messageHeaders);
        addCustomHeaders(connection, messageHeaders);
        if (headers != null) {
            headers.forEach(messageHeaders::putIfAbsent);
        }
        return this.messageConverter.toMessage(data, messageHeaders);
    } else {
        if (logger.isWarnEnabled()) {
            logger.warn("Null payload from connection " + connection.getConnectionId());
        }
        return null;
    }
}
Also used : MutableMessageHeaders(org.springframework.integration.support.MutableMessageHeaders) MessageHeaders(org.springframework.messaging.MessageHeaders) MutableMessageHeaders(org.springframework.integration.support.MutableMessageHeaders)

Example 2 with MutableMessageHeaders

use of org.springframework.integration.support.MutableMessageHeaders in project spring-integration by spring-projects.

the class TcpMessageMapper method toMessage.

@SuppressWarnings("unchecked")
@Override
public Message<?> toMessage(TcpConnection connection, @Nullable Map<String, Object> headers) throws Exception {
    Message<Object> message = null;
    Object payload = connection.getPayload();
    if (payload != null) {
        AbstractIntegrationMessageBuilder<Object> messageBuilder;
        if (this.bytesMessageMapper != null && payload instanceof byte[]) {
            messageBuilder = (AbstractIntegrationMessageBuilder<Object>) getMessageBuilderFactory().fromMessage(this.bytesMessageMapper.toMessage((byte[]) payload));
        } else {
            messageBuilder = getMessageBuilderFactory().withPayload(payload);
        }
        MessageHeaders messageHeaders = new MutableMessageHeaders(null);
        addStandardHeaders(connection, messageHeaders);
        addCustomHeaders(connection, messageHeaders);
        message = messageBuilder.copyHeaders(messageHeaders).copyHeadersIfAbsent(headers).build();
    } else {
        if (this.logger.isWarnEnabled()) {
            this.logger.warn("Null payload from connection " + connection.getConnectionId());
        }
    }
    return message;
}
Also used : MutableMessageHeaders(org.springframework.integration.support.MutableMessageHeaders) MessageHeaders(org.springframework.messaging.MessageHeaders) MutableMessageHeaders(org.springframework.integration.support.MutableMessageHeaders)

Example 3 with MutableMessageHeaders

use of org.springframework.integration.support.MutableMessageHeaders in project spring-cloud-stream by spring-cloud.

the class AvroMessageConverterSerializationTests method sourceWriteSameVersion.

@Test
public void sourceWriteSameVersion() throws Exception {
    User specificRecord = new User();
    specificRecord.setName("joe");
    Schema v1 = new Schema.Parser().parse(AvroMessageConverterSerializationTests.class.getClassLoader().getResourceAsStream("schemas/user.avsc"));
    GenericRecord genericRecord = new GenericData.Record(v1);
    genericRecord.put("name", "joe");
    SchemaRegistryClient client = new DefaultSchemaRegistryClient();
    AvroSchemaRegistryClientMessageConverter converter = new AvroSchemaRegistryClientMessageConverter(client, new NoOpCacheManager());
    converter.setSubjectNamingStrategy(new DefaultSubjectNamingStrategy());
    converter.setDynamicSchemaGenerationEnabled(false);
    converter.afterPropertiesSet();
    Message specificMessage = converter.toMessage(specificRecord, new MutableMessageHeaders(Collections.<String, Object>emptyMap()), MimeTypeUtils.parseMimeType("application/*+avro"));
    SchemaReference specificRef = extractSchemaReference(MimeTypeUtils.parseMimeType(specificMessage.getHeaders().get("contentType").toString()));
    Message genericMessage = converter.toMessage(genericRecord, new MutableMessageHeaders(Collections.<String, Object>emptyMap()), MimeTypeUtils.parseMimeType("application/*+avro"));
    SchemaReference genericRef = extractSchemaReference(MimeTypeUtils.parseMimeType(genericMessage.getHeaders().get("contentType").toString()));
    Assert.assertEquals(genericRef, specificRef);
    Assert.assertEquals(1, genericRef.getVersion());
}
Also used : User(example.avro.User) Message(org.springframework.messaging.Message) Schema(org.apache.avro.Schema) NoOpCacheManager(org.springframework.cache.support.NoOpCacheManager) SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) DefaultSubjectNamingStrategy(org.springframework.cloud.stream.schema.avro.DefaultSubjectNamingStrategy) AvroSchemaRegistryClientMessageConverter(org.springframework.cloud.stream.schema.avro.AvroSchemaRegistryClientMessageConverter) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) MutableMessageHeaders(org.springframework.integration.support.MutableMessageHeaders) DefaultSchemaRegistryClient(org.springframework.cloud.stream.schema.client.DefaultSchemaRegistryClient) DefaultSchemaRegistryClient(org.springframework.cloud.stream.schema.client.DefaultSchemaRegistryClient) SchemaRegistryClient(org.springframework.cloud.stream.schema.client.SchemaRegistryClient) Test(org.junit.Test)

Example 4 with MutableMessageHeaders

use of org.springframework.integration.support.MutableMessageHeaders in project spring-integration by spring-projects.

the class EmbeddedJsonHeadersMessageMapper method decodeNativeFormat.

private Message<?> decodeNativeFormat(byte[] bytes, Map<String, Object> headersToAdd) throws Exception {
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    if (buffer.remaining() > 4) {
        int headersLen = buffer.getInt();
        if (headersLen >= 0 && headersLen < buffer.remaining() - 4) {
            buffer.position(headersLen + 4);
            int payloadLen = buffer.getInt();
            if (payloadLen != buffer.remaining()) {
                return null;
            } else {
                buffer.position(4);
                @SuppressWarnings("unchecked") Map<String, Object> headers = this.objectMapper.readValue(bytes, buffer.position(), headersLen, HashMap.class);
                buffer.position(buffer.position() + headersLen);
                buffer.getInt();
                Object payload;
                byte[] payloadBytes = new byte[payloadLen];
                buffer.get(payloadBytes);
                payload = payloadBytes;
                if (headersToAdd != null) {
                    headersToAdd.forEach(headers::putIfAbsent);
                }
                return new GenericMessage<>(payload, new MutableMessageHeaders(headers));
            }
        }
    }
    return null;
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MutableMessageHeaders(org.springframework.integration.support.MutableMessageHeaders) ByteBuffer(java.nio.ByteBuffer)

Example 5 with MutableMessageHeaders

use of org.springframework.integration.support.MutableMessageHeaders in project spring-integration by spring-projects.

the class MessageJacksonDeserializer method convert.

@Override
public T convert(JsonNode root, DeserializationContext ctxt) throws IOException {
    Map<String, Object> headers = this.mapper.readValue(root.get("headers").traverse(), TypeFactory.defaultInstance().constructMapType(HashMap.class, String.class, Object.class));
    Object payload = this.mapper.readValue(root.get("payload").traverse(), this.payloadType);
    return buildMessage(new MutableMessageHeaders(headers), payload, root, ctxt);
}
Also used : HashMap(java.util.HashMap) MutableMessageHeaders(org.springframework.integration.support.MutableMessageHeaders)

Aggregations

MutableMessageHeaders (org.springframework.integration.support.MutableMessageHeaders)5 MessageHeaders (org.springframework.messaging.MessageHeaders)2 User (example.avro.User)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Schema (org.apache.avro.Schema)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 Test (org.junit.Test)1 NoOpCacheManager (org.springframework.cache.support.NoOpCacheManager)1 SchemaReference (org.springframework.cloud.stream.schema.SchemaReference)1 AvroSchemaRegistryClientMessageConverter (org.springframework.cloud.stream.schema.avro.AvroSchemaRegistryClientMessageConverter)1 DefaultSubjectNamingStrategy (org.springframework.cloud.stream.schema.avro.DefaultSubjectNamingStrategy)1 DefaultSchemaRegistryClient (org.springframework.cloud.stream.schema.client.DefaultSchemaRegistryClient)1 SchemaRegistryClient (org.springframework.cloud.stream.schema.client.SchemaRegistryClient)1 Message (org.springframework.messaging.Message)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1