public List<Data> list() throws SQLException { List<Data> list = new ArrayList<Data>(); try ( Connection connection = database.getConnection(); Statement statement = connection.createStatement("SELECT id, name, value FROM data"); ResultSet resultSet = statement.executeQuery(); ) { while (resultSet.next()) { list.add(map(resultSet)); } } return list; } private Data map(ResultSet resultSet) throws SQLException { Data data = new Data(); data.setId(resultSet.getLong("id")); data.setName(resultSet.getString("name")); data.setValue(resultSet.getInteger("value")); return data; }
@Override public String getText() throws JMSException { Section body = getBody(); if (body == null) { return null; } else if (body instanceof Data) { Data data = (Data) body; if (data.getValue() == null || data.getValue().getLength() == 0) { return ""; } else { Binary b = data.getValue(); ByteBuffer buf = ByteBuffer.wrap(b.getArray(), b.getArrayOffset(), b.getLength()); try { CharBuffer chars = charset.newDecoder().decode(buf); return String.valueOf(chars); } catch (CharacterCodingException e) { throw JmsExceptionSupport.create("Cannot decode String in " + charset.displayName(), e); } } } else if (body instanceof AmqpValue) { Object value = ((AmqpValue) body).getValue(); if (value == null || value instanceof String) { return (String) value; } else { throw new IllegalStateException("Unexpected amqp-value body content type: " + value.getClass().getSimpleName()); } } else { throw new IllegalStateException("Unexpected message body type: " + body.getClass().getSimpleName()); } }
/** * Return a raw AMQP message * * @return */ public Message toAmqp() { Message message = ProtonHelper.message(); message.setMessageId(this.messageId); Map<Symbol, Object> map = new HashMap<>(); map.put(Symbol.valueOf(AMQP_RETAIN_ANNOTATION), this.isRetain); map.put(Symbol.valueOf(AMQP_QOS_ANNOTATION), this.qos.value()); MessageAnnotations messageAnnotations = new MessageAnnotations(map); message.setMessageAnnotations(messageAnnotations); message.setAddress(this.topic); Header header = new Header(); header.setDurable(this.qos != MqttQoS.AT_MOST_ONCE); message.setHeader(header); message.setDeliveryCount(this.isDup ? 1 : 0); // the payload could be null (or empty) if (this.payload != null) message.setBody(new Data(new Binary(this.payload.getBytes()))); return message; }
/** * Return a raw AMQP message * * @return */ public Message toAmqp() { Message message = ProtonHelper.message(); message.setSubject(AMQP_SUBJECT); Map<Symbol, Object> map = new HashMap<>(); map.put(Symbol.valueOf(AMQP_RETAIN_ANNOTATION), this.isRetain); map.put(Symbol.valueOf(AMQP_QOS_ANNOTATION), this.qos.value()); MessageAnnotations messageAnnotations = new MessageAnnotations(map); message.setMessageAnnotations(messageAnnotations); message.setAddress(this.topic); Header header = new Header(); header.setDurable(this.qos != MqttQoS.AT_MOST_ONCE); message.setHeader(header); // the payload could be null (or empty) if (this.payload != null) message.setBody(new Data(new Binary(this.payload.getBytes()))); return message; }
message.getWrappedMessage().setContentType("text/plain"); message.getWrappedMessage().setBody(new Data(new Binary(messageText.getBytes(StandardCharsets.UTF_8)))); assertTrue(body.getValue() instanceof Binary); Binary payload = (Binary) body.getValue(); String reconstitutedString = new String( payload.getArray(), payload.getArrayOffset(), payload.getLength(), StandardCharsets.UTF_8);
/** * Set the payload of the message using a {@link Data} section. * <p> * If the payload is {@code null}, then neither the payload, nor content type will be set. * </p> * * @param message The message to update. * @param contentType An optional content type. * @param payload The optional message payload. * * @throws NullPointerException If the parameter {@code message} was {@code null}. */ public static void setPayload(final Message message, final String contentType, final byte[] payload) { Objects.requireNonNull(message); if (contentType != null) { message.setContentType(contentType); } if (payload != null) { message.setBody(new Data(new Binary(payload))); } }
/** * Gets a message's body as Buffer object. * * @param msg The AMQP 1.0 message to parse the body of. * @return The message body as a Buffer or {@code null} if the message does not have a <em>Data</em> nor an * <em>AmqpValue</em> section. * @throws NullPointerException if the message is {@code null}. */ public static Buffer getPayload(final Message msg) { Objects.requireNonNull(msg); if (msg.getBody() == null) { LOG.trace("message has no body"); return null; } if (msg.getBody() instanceof Data) { final Data body = (Data) msg.getBody(); return Buffer.buffer(body.getValue().getArray()); } else if (msg.getBody() instanceof AmqpValue) { final AmqpValue body = (AmqpValue) msg.getBody(); if (body.getValue() instanceof byte[]) { return Buffer.buffer((byte[]) body.getValue()); } else if (body.getValue() instanceof String) { return Buffer.buffer((String) body.getValue()); } } LOG.debug("unsupported body type [{}]", msg.getBody().getClass().getName()); return null; }
/** * Gets a message's body as String. * * @param message The AMQP 1.0 message to parse the body of. * @return The message body as a {@link String} or {@code null} if the message does not have a <em>Data</em> nor an * <em>AmqpValue</em> section. * * @throws NullPointerException if the message is {@code null}. */ public static String getPayloadAsString(final Message message) { Objects.requireNonNull(message); if (message.getBody() == null) { LOG.trace("message has no body"); return null; } if (message.getBody() instanceof Data) { final Data body = (Data) message.getBody(); return StandardCharsets.UTF_8.decode(body.getValue().asByteBuffer()).toString(); } else if (message.getBody() instanceof AmqpValue) { final AmqpValue body = (AmqpValue) message.getBody(); if (body.getValue() instanceof byte[]) { return StandardCharsets.UTF_8.decode(ByteBuffer.wrap((byte[]) body.getValue())).toString(); } else if (body.getValue() instanceof String) { return (String) body.getValue(); } } LOG.debug("unsupported body type [{}]", message.getBody().getClass().getName()); return null; }
@Test(timeout = 60000) public void testMessageWithDataAndEmptyBinaryPreservesBody() throws Exception { server.getAddressSettingsRepository().addMatch("#", new AddressSettings().setDefaultAddressRoutingType(RoutingType.ANYCAST)); AmqpClient client = createAmqpClient(); AmqpConnection connection = addConnection(client.connect()); try { AmqpSession session = connection.createSession(); AmqpSender sender = session.createSender(getTestName()); AmqpMessage message = createAmqpLargeMessageWithNoBody(); message.getWrappedMessage().setBody(new Data(new Binary(new byte[0]))); sender.send(message); sender.close(); AmqpReceiver receiver = session.createReceiver(getTestName()); receiver.flow(1); AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS); assertNotNull("failed to read large AMQP message", received); MessageImpl wrapped = (MessageImpl) received.getWrappedMessage(); assertTrue(wrapped.getBody() instanceof Data); Data body = (Data) wrapped.getBody(); assertTrue(body.getValue() instanceof Binary); Binary payload = (Binary) body.getValue(); assertEquals(0, payload.getLength()); received.accept(); session.close(); } finally { connection.close(); } }
@Test public void testJSON_to_AMQP_VerifyDataBody() { String testContent = "myTestContent"; JsonObject jsonObject = new JsonObject(); jsonObject.put(AmqpConstants.BODY, testContent.getBytes(StandardCharsets.UTF_8)); jsonObject.put(AmqpConstants.BODY_TYPE, AmqpConstants.BODY_TYPE_DATA); Message protonMsg = translator.convertToAmqpMessage(jsonObject); assertNotNull("Expected converted msg", protonMsg); Section body = protonMsg.getBody(); assertTrue("Unexpected body type", body instanceof Data); assertNotNull("Unexpected body content", body); assertEquals("Unexpected message body value", new Binary(testContent.getBytes(StandardCharsets.UTF_8)), ((Data) body).getValue()); }
@Override public Message toMessage(String address, KafkaConsumerRecord<String, byte[]> record) { Message message = Proton.message(); message.setAddress(address); // put message annotations about partition, offset and key (if not null) Map<Symbol, Object> map = new HashMap<>(); map.put(Symbol.valueOf(AmqpBridge.AMQP_PARTITION_ANNOTATION), record.partition()); map.put(Symbol.valueOf(AmqpBridge.AMQP_OFFSET_ANNOTATION), record.offset()); map.put(Symbol.valueOf(AmqpBridge.AMQP_KEY_ANNOTATION), record.key()); map.put(Symbol.valueOf(AmqpBridge.AMQP_TOPIC_ANNOTATION), record.topic()); MessageAnnotations messageAnnotations = new MessageAnnotations(map); message.setMessageAnnotations(messageAnnotations); message.setBody(new Data(new Binary(record.value()))); return message; }
private static int getPayloadSize(Message msg) { if (msg == null || msg.getBody() == null) { return 0; } if (msg.getBody() instanceof Data) { final Data payloadSection = (Data) msg.getBody(); if (payloadSection == null) { return 0; } final Binary payloadBytes = payloadSection.getValue(); if (payloadBytes == null) { return 0; } return payloadBytes.getLength(); } if (msg.getBody() instanceof AmqpValue) { final AmqpValue amqpValue = (AmqpValue) msg.getBody(); if (amqpValue == null) { return 0; } return amqpValue.getValue().toString().length() * 2; } return 0; }
@Override protected Binary wrap(Data val) { return val.getValue(); }
private static int getPayloadSize(Message msg) { if (msg == null || msg.getBody() == null) { return 0; } Section bodySection = msg.getBody(); if(bodySection instanceof AmqpValue) { return Util.sizeof(((AmqpValue)bodySection).getValue()); } else if(bodySection instanceof AmqpSequence) { return Util.sizeof(((AmqpSequence)bodySection).getValue()); } else if (bodySection instanceof Data) { Data payloadSection = (Data) bodySection; Binary payloadBytes = payloadSection.getValue(); return Util.sizeof(payloadBytes); } else { return 0; } }