headers.put(AmqpHeaders.CLUSTER_ID, clusterId); String contentEncoding = amqpMessageProperties.getContentEncoding(); if (StringUtils.hasText(contentEncoding)) { headers.put(AmqpHeaders.CONTENT_ENCODING, contentEncoding);
message1 -> { MessageProperties messageProperties = message1.getMessageProperties(); String contentEncoding = messageProperties.getContentEncoding(); long contentLength = messageProperties.getContentLength(); String contentType = messageProperties.getContentType();
assertEquals("test.contentEncoding", amqpProperties.getContentEncoding()); assertEquals(99L, amqpProperties.getContentLength()); assertEquals("test.contentType", amqpProperties.getContentType());
public MessageBuilderSupport<T> setContentEncodingIfAbsent(String contentEncoding) { if (this.properties.getContentEncoding() == null) { this.properties.setContentEncoding(contentEncoding); } return this; }
private Object asString(Message message, MessageProperties properties) { String encoding = properties.getContentEncoding(); if (encoding == null) { encoding = this.defaultCharset; } try { return new String(message.getBody(), encoding); } catch (UnsupportedEncodingException e) { throw new MessageConversionException("failed to convert text-based Message content", e); } }
@Override public Message postProcessMessage(Message message) throws AmqpException { String encoding = message.getMessageProperties().getContentEncoding(); if (encoding == null) { return message; } else { int colonAt = encoding.indexOf(':'); if (colonAt > 0) { encoding = encoding.substring(0, colonAt); } MessagePostProcessor decompressor = this.decompressors.get(encoding); if (decompressor != null) { return decompressor.postProcessMessage(message); } else { return message; } } }
@Override public Object fromMessage(Message message) throws MessageConversionException { MessageProperties messageProperties = message.getMessageProperties(); if(messageProperties == null) throw new MessageConversionException("Cannot decode a message with no properties!"); byte[] body = message.getBody(); if(body == null) return null; String messageEncoding = messageProperties.getContentEncoding(); if(messageEncoding == null) messageEncoding = this.encoding; String messageContentType = messageProperties.getContentType(); if(this.contentType != null && ! this.contentType.equalsIgnoreCase(messageContentType)) throw new MessageConversionException("Cannot understand a message of type "+messageContentType); try { return new String(body, messageEncoding); } catch (UnsupportedEncodingException ex) { LOG.error("Cannot dencode strings as {}", this.encoding, ex); throw new MessageConversionException("Cannot dencode strings as "+this.encoding, ex); } } }
@Override public Object fromMessage(Message message) throws MessageConversionException { MessageProperties messageProperties = message.getMessageProperties(); if(messageProperties == null) throw new MessageConversionException("Cannot decode a message with no properties!"); byte[] body = message.getBody(); if(body == null) return null; String messageEncoding = messageProperties.getContentEncoding(); if(messageEncoding == null) messageEncoding = this.encoding; String messageContentType = messageProperties.getContentType(); if(this.contentType != null && ! this.contentType.equalsIgnoreCase(messageContentType)) throw new MessageConversionException("Cannot understand a message of type "+messageContentType); try { return new String(body, messageEncoding); } catch (UnsupportedEncodingException ex) { LOG.error("Cannot dencode strings as {}", this.encoding, ex); throw new MessageConversionException("Cannot dencode strings as "+this.encoding, ex); } } }
@Override public Message postProcessMessage(Message message) throws AmqpException { ByteArrayOutputStream zipped = new ByteArrayOutputStream(); try { OutputStream zipper = getCompressorStream(zipped); FileCopyUtils.copy(new ByteArrayInputStream(message.getBody()), zipper); MessageProperties messageProperties = message.getMessageProperties(); String currentEncoding = messageProperties.getContentEncoding(); messageProperties .setContentEncoding(getEncoding() + (currentEncoding == null ? "" : ":" + currentEncoding)); if (this.autoDecompress) { messageProperties.setHeader(MessageProperties.SPRING_AUTO_DECOMPRESS, true); } byte[] compressed = zipped.toByteArray(); if (this.logger.isTraceEnabled()) { this.logger.trace("Compressed " + message.getBody().length + " to " + compressed.length); } return new Message(compressed, messageProperties); } catch (IOException e) { throw new AmqpIOException(e); } }
public static SpringAMQPMessage setBasicPropertiesToHeaders(SpringAMQPMessage msg, Message amqpMessage) { msg.getHeaders().put(MESSAGE_ID, amqpMessage.getMessageProperties().getMessageId()); byte[] correlationId = amqpMessage.getMessageProperties().getCorrelationId(); msg.getHeaders().put(CORRELATION_ID, correlationId == null ? null : new String(correlationId)); msg.getHeaders().put(CONTENT_ENCODING, amqpMessage.getMessageProperties().getContentEncoding()); msg.getHeaders().put(CONTENT_TYPE, amqpMessage.getMessageProperties().getContentType()); msg.getHeaders().put(EXPIRATION, amqpMessage.getMessageProperties().getExpiration()); msg.getHeaders().put(PRIORITY, amqpMessage.getMessageProperties().getPriority()); msg.getHeaders().put(REPLY_TO, amqpMessage.getMessageProperties().getReplyTo()); msg.getHeaders().put(DELIVERY_MODE, MessageDeliveryMode.toInt(amqpMessage.getMessageProperties().getDeliveryMode())); msg.getHeaders().put(TYPE, amqpMessage.getMessageProperties().getType()); return msg; }
public static SpringAMQPMessage setBasicPropertiesToHeaders(SpringAMQPMessage msg, Message amqpMessage) { msg.getHeaders().put(MESSAGE_ID, amqpMessage.getMessageProperties().getMessageId()); byte[] correlationId = amqpMessage.getMessageProperties().getCorrelationId(); msg.getHeaders().put(CORRELATION_ID, correlationId == null ? null : new String(correlationId)); msg.getHeaders().put(CONTENT_ENCODING, amqpMessage.getMessageProperties().getContentEncoding()); msg.getHeaders().put(CONTENT_TYPE, amqpMessage.getMessageProperties().getContentType()); msg.getHeaders().put(EXPIRATION, amqpMessage.getMessageProperties().getExpiration()); msg.getHeaders().put(PRIORITY, amqpMessage.getMessageProperties().getPriority()); msg.getHeaders().put(REPLY_TO, amqpMessage.getMessageProperties().getReplyTo()); msg.getHeaders().put(DELIVERY_MODE, MessageDeliveryMode.toInt(amqpMessage.getMessageProperties().getDeliveryMode())); msg.getHeaders().put(TYPE, amqpMessage.getMessageProperties().getType()); return msg; }
@Test public void stringToMessage() throws Exception { SimpleMessageConverter converter = new SimpleMessageConverter(); Message message = converter.toMessage("test", new MessageProperties()); String contentType = message.getMessageProperties().getContentType(); String content = new String(message.getBody(), message.getMessageProperties().getContentEncoding()); assertEquals("text/plain", contentType); assertEquals("test", content); }
@Test public void stringToMessage() throws Exception { SerializerMessageConverter converter = new SerializerMessageConverter(); Message message = converter.toMessage("test", new MessageProperties()); String contentType = message.getMessageProperties().getContentType(); String content = new String(message.getBody(), message.getMessageProperties().getContentEncoding()); assertEquals("text/plain", contentType); assertEquals("test", content); }
@Test public void testSimpleBatchGZippedWithEncoding() throws Exception { BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000); BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler); template.setConnectionFactory(this.connectionFactory); template.setBeforePublishPostProcessors(new GZipPostProcessor()); MessageProperties props = new MessageProperties(); props.setContentEncoding("foo"); Message message = new Message("foo".getBytes(), props); template.send("", ROUTE, message); message = new Message("bar".getBytes(), props); template.send("", ROUTE, message); message = receive(template); assertEquals("gzip:foo", message.getMessageProperties().getContentEncoding()); GUnzipPostProcessor unzipper = new GUnzipPostProcessor(); message = unzipper.postProcessMessage(message); assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", new String(message.getBody())); }
@Test public void testSimpleBatchGZippedConfiguredUnzipper() throws Exception { BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000); BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler); template.setConnectionFactory(this.connectionFactory); GZipPostProcessor gZipPostProcessor = new GZipPostProcessor(); gZipPostProcessor.setLevel(Deflater.BEST_COMPRESSION); assertEquals(Deflater.BEST_COMPRESSION, getStreamLevel(gZipPostProcessor)); template.setBeforePublishPostProcessors(gZipPostProcessor); template.setAfterReceivePostProcessors(new GUnzipPostProcessor()); MessageProperties props = new MessageProperties(); Message message = new Message("foo".getBytes(), props); template.send("", ROUTE, message); message = new Message("bar".getBytes(), props); template.send("", ROUTE, message); message = receive(template); assertNull(message.getMessageProperties().getContentEncoding()); assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", new String(message.getBody())); }
@Test public void testSimpleBatchGZipped() throws Exception { BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000); BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler); template.setConnectionFactory(this.connectionFactory); GZipPostProcessor gZipPostProcessor = new GZipPostProcessor(); assertEquals(Deflater.BEST_SPEED, getStreamLevel(gZipPostProcessor)); template.setBeforePublishPostProcessors(gZipPostProcessor); MessageProperties props = new MessageProperties(); Message message = new Message("foo".getBytes(), props); template.send("", ROUTE, message); message = new Message("bar".getBytes(), props); template.send("", ROUTE, message); message = receive(template); assertEquals("gzip", message.getMessageProperties().getContentEncoding()); GUnzipPostProcessor unzipper = new GUnzipPostProcessor(); message = unzipper.postProcessMessage(message); assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", new String(message.getBody())); }
@Test public void testSimpleBatchZippedBestCompression() throws Exception { BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000); BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler); template.setConnectionFactory(this.connectionFactory); ZipPostProcessor zipPostProcessor = new ZipPostProcessor(); zipPostProcessor.setLevel(Deflater.BEST_COMPRESSION); assertEquals(Deflater.BEST_COMPRESSION, getStreamLevel(zipPostProcessor)); template.setBeforePublishPostProcessors(zipPostProcessor); MessageProperties props = new MessageProperties(); Message message = new Message("foo".getBytes(), props); template.send("", ROUTE, message); message = new Message("bar".getBytes(), props); template.send("", ROUTE, message); message = receive(template); assertEquals("zip", message.getMessageProperties().getContentEncoding()); UnzipPostProcessor unzipper = new UnzipPostProcessor(); message = unzipper.postProcessMessage(message); assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", new String(message.getBody())); }
@Test public void testSimpleBatchZippedWithEncoding() throws Exception { BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000); BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler); template.setConnectionFactory(this.connectionFactory); ZipPostProcessor zipPostProcessor = new ZipPostProcessor(); assertEquals(Deflater.BEST_SPEED, getStreamLevel(zipPostProcessor)); template.setBeforePublishPostProcessors(zipPostProcessor); MessageProperties props = new MessageProperties(); props.setContentEncoding("foo"); Message message = new Message("foo".getBytes(), props); template.send("", ROUTE, message); message = new Message("bar".getBytes(), props); template.send("", ROUTE, message); message = receive(template); assertEquals("zip:foo", message.getMessageProperties().getContentEncoding()); UnzipPostProcessor unzipper = new UnzipPostProcessor(); message = unzipper.postProcessMessage(message); assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", new String(message.getBody())); }
private void assertLower(MessageProperties properties) { assertEquals("appId", properties.getAppId()); assertEquals("clusterId", properties.getClusterId()); assertEquals("contentEncoding", properties.getContentEncoding()); assertEquals(MessageProperties.CONTENT_TYPE_TEXT_PLAIN, properties.getContentType()); assertEquals(1, properties.getContentLength()); assertEquals("correlationId", properties.getCorrelationId()); assertEquals(MessageDeliveryMode.NON_PERSISTENT, properties.getDeliveryMode()); assertEquals(2, properties.getDeliveryTag()); assertEquals("expiration", properties.getExpiration()); assertEquals("bar", properties.getHeaders().get("foo")); assertEquals("fiz", properties.getHeaders().get("qux")); assertEquals("fuz", properties.getHeaders().get("baz")); assertEquals(Integer.valueOf(3), properties.getMessageCount()); assertEquals("messageId", properties.getMessageId()); assertEquals(Integer.valueOf(4), properties.getPriority()); assertEquals("receivedExchange", properties.getReceivedExchange()); assertEquals("receivedRoutingKey", properties.getReceivedRoutingKey()); assertTrue(properties.getRedelivered()); assertTrue(properties.getTimestamp().getTime() > 0); assertEquals("type", properties.getType()); assertEquals("userId", properties.getUserId()); }
private void assertUpper(MessageProperties properties) { assertEquals("APPID", properties.getAppId()); assertEquals("CLUSTERID", properties.getClusterId()); assertEquals("CONTENTENCODING", properties.getContentEncoding()); assertEquals(MessageProperties.CONTENT_TYPE_BYTES, properties.getContentType()); assertEquals(10, properties.getContentLength()); assertEquals("CORRELATIONID", properties.getCorrelationId()); assertEquals(MessageDeliveryMode.PERSISTENT, properties.getDeliveryMode()); assertEquals(20, properties.getDeliveryTag()); assertEquals("EXPIRATION", properties.getExpiration()); assertEquals("BAR", properties.getHeaders().get("foo")); assertEquals("FIZ", properties.getHeaders().get("qux")); assertEquals("FUZ", properties.getHeaders().get("baz")); assertEquals(Integer.valueOf(30), properties.getMessageCount()); assertEquals("MESSAGEID", properties.getMessageId()); assertEquals(Integer.valueOf(40), properties.getPriority()); assertEquals("RECEIVEDEXCHANGE", properties.getReceivedExchange()); assertEquals("RECEIVEDROUTINGKEY", properties.getReceivedRoutingKey()); assertFalse(properties.getRedelivered()); assertTrue(properties.getTimestamp().getTime() == 0); assertEquals("TYPE", properties.getType()); assertEquals("USERID", properties.getUserId()); }