/** * @return a new object instance */ public DataStructure createObject() { return new ActiveMQBytesMessage(); }
@Override public Map<String, Object> getFields(Object o) throws OpenDataException { ActiveMQBytesMessage m = (ActiveMQBytesMessage)o; m.setReadOnlyBody(true); Map<String, Object> rc = super.getFields(o); long length = 0; try { length = m.getBodyLength(); rc.put(CompositeDataConstants.BODY_LENGTH, Long.valueOf(length)); } catch (JMSException e) { rc.put(CompositeDataConstants.BODY_LENGTH, Long.valueOf(0)); } try { byte preview[] = new byte[(int)Math.min(length, 255)]; m.readBytes(preview); m.reset(); // This is whack! Java 1.5 JMX spec does not support primitive // arrays! // In 1.6 it seems it is supported.. but until then... Byte data[] = new Byte[preview.length]; for (int i = 0; i < data.length; i++) { data[i] = new Byte(preview[i]); } rc.put(CompositeDataConstants.BODY_PREVIEW, data); } catch (JMSException e) { rc.put(CompositeDataConstants.BODY_PREVIEW, new Byte[] {}); } return rc; }
throw new NullPointerException(); initializeWriting(); if (value instanceof Boolean) { writeBoolean(((Boolean)value).booleanValue()); } else if (value instanceof Character) { writeChar(((Character)value).charValue()); } else if (value instanceof Byte) { writeByte(((Byte)value).byteValue()); } else if (value instanceof Short) { writeShort(((Short)value).shortValue()); } else if (value instanceof Integer) { writeInt(((Integer)value).intValue()); } else if (value instanceof Long) { writeLong(((Long)value).longValue()); } else if (value instanceof Float) { writeFloat(((Float)value).floatValue()); } else if (value instanceof Double) { writeDouble(((Double)value).doubleValue()); } else if (value instanceof String) { writeUTF(value.toString()); } else if (value instanceof byte[]) { writeBytes((byte[])value); } else { throw new MessageFormatException("Cannot write non-primitive type:" + value.getClass());
@Override public Message copy() { ActiveMQBytesMessage copy = new ActiveMQBytesMessage(); copy(copy); return copy; }
/** * Puts the message body in read-only mode and repositions the stream of * bytes to the beginning. * * @throws JMSException if an internal error occurs */ @Override public void reset() throws JMSException { storeContent(); setReadOnlyBody(true); try { if (bytesOut != null) { bytesOut.close(); bytesOut = null; } if (dataIn != null) { dataIn.close(); dataIn = null; } if (dataOut != null) { dataOut.close(); dataOut = null; } } catch (IOException ioe) { throw JMSExceptionSupport.create(ioe); } }
String intendedType = (String)headers.get(Stomp.Headers.AMQ_MESSAGE_TYPE); if(intendedType.equalsIgnoreCase("text")){ ActiveMQTextMessage text = new ActiveMQTextMessage(); try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(command.getContent().length + 4); DataOutputStream data = new DataOutputStream(bytes); data.writeInt(command.getContent().length); data.write(command.getContent()); text.setContent(bytes.toByteSequence()); data.close(); } catch (Throwable e) { ActiveMQBytesMessage byteMessage = new ActiveMQBytesMessage(); byteMessage.writeBytes(command.getContent()); msg = byteMessage; } else { ActiveMQBytesMessage bm = new ActiveMQBytesMessage(); bm.writeBytes(command.getContent()); msg = bm; } else { ActiveMQTextMessage text = new ActiveMQTextMessage(); try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(command.getContent().length + 4); DataOutputStream data = new DataOutputStream(bytes); data.writeInt(command.getContent().length);
private void initializeReading() throws JMSException { checkWriteOnlyBody(); if (dataIn == null) { try { ByteSequence data = getContent(); if (data == null) { data = new ByteSequence(new byte[] {}, 0, 0); } InputStream is = new ByteArrayInputStream(data); if (isCompressed()) { if (data.length != 0) { is = new ByteArrayInputStream(decompress(data)); } } else { length = data.getLength(); } dataIn = new DataInputStream(is); } catch (IOException ioe) { throw JMSExceptionSupport.create(ioe); } } }
private void initializeReading() throws JMSException { checkWriteOnlyBody(); if (dataIn == null) { ByteSequence data = getContent(); if (data == null) { data = new ByteSequence(new byte[] {}, 0, 0); } InputStream is = new ByteArrayInputStream(data); if (isCompressed()) { // keep track of the real length of the content if // we are compressed. try { DataInputStream dis = new DataInputStream(is); length = dis.readInt(); dis.close(); } catch (IOException e) { throw JMSExceptionSupport.create(e); } is = new InflaterInputStream(is); } else { length = data.getLength(); } dataIn = new DataInputStream(is); } }
@Override public void storeContent() { if (dataOut != null) { try { dataOut.close(); ByteSequence bs = bytesOut.toByteSequence(); setContent(bs); ActiveMQConnection connection = getConnection(); if (connection != null && connection.isUseCompression()) { doCompress(); } } catch (IOException ioe) { throw new RuntimeException(ioe.getMessage(), ioe); } finally { try { if (bytesOut != null) { bytesOut.close(); bytesOut = null; } if (dataOut != null) { dataOut.close(); dataOut = null; } } catch (IOException ioe) { } } } }
private void initializeWriting() throws JMSException { checkReadOnlyBody(); if (this.dataOut == null) { this.bytesOut = new ByteArrayOutputStream(); OutputStream os = bytesOut; ActiveMQConnection connection = getConnection(); if (connection != null && connection.isUseCompression()) { this.dataOut = new DataOutputStream(os);
if (message.propertyExists(QOS_PROPERTY_NAME)) { int ordinal = message.getIntProperty(QOS_PROPERTY_NAME); qoS = QoS.values()[ordinal]; qoS = message.isPersistent() ? QoS.AT_MOST_ONCE : QoS.AT_LEAST_ONCE; } else if (message.getDataStructureType() == ActiveMQBytesMessage.DATA_STRUCTURE_TYPE) { ActiveMQBytesMessage msg = (ActiveMQBytesMessage) message.copy(); msg.setReadOnlyBody(true); byte[] data = new byte[(int) msg.getBodyLength()]; msg.readBytes(data); result.payload(new Buffer(data)); } else if (message.getDataStructureType() == ActiveMQMapMessage.DATA_STRUCTURE_TYPE) { ByteSequence byteSequence = message.getContent(); if (byteSequence != null && byteSequence.getLength() > 0) { if (message.isCompressed()) { Inflater inflater = new Inflater(); byte[] data = new byte[4096]; int read; ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); while ((read = inflater.inflate(data)) != 0) { bytesOut.write(data, 0, read); byteSequence = bytesOut.toByteSequence(); bytesOut.close();
if (message.getDataStructureType() == ActiveMQTextMessage.DATA_STRUCTURE_TYPE) { if (!message.isCompressed() && message.getContent() != null) { ByteSequence msgContent = message.getContent(); if (msgContent.getLength() > 4) { byte[] content = new byte[msgContent.getLength() - 4]; System.arraycopy(msgContent.data, 4, content, 0, content.length); command.setContent(content); ActiveMQTextMessage msg = (ActiveMQTextMessage) message.copy(); String messageText = msg.getText(); if (messageText != null) { command.setContent(msg.getText().getBytes("UTF-8")); ActiveMQBytesMessage msg = (ActiveMQBytesMessage) message.copy(); msg.setReadOnlyBody(true); byte[] data = new byte[(int) msg.getBodyLength()]; msg.readBytes(data);
@Override protected void doCompress() throws IOException { compressed = true; ByteSequence bytes = getContent(); if (bytes != null) { int length = bytes.getLength(); ByteArrayOutputStream compressed = new ByteArrayOutputStream(); compressed.write(new byte[4]); Deflater deflater = new Deflater(); try { deflater.setInput(bytes.data); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); compressed.write(buffer, 0, count); } bytes = compressed.toByteSequence(); ByteSequenceData.writeIntBig(bytes, length); bytes.offset = 0; setContent(bytes); } finally { deflater.end(); compressed.close(); } } } }
BytesMessage bytesMsg = (BytesMessage)message; bytesMsg.reset(); ActiveMQBytesMessage msg = new ActiveMQBytesMessage(); msg.setConnection(connection); try { for (;;) { msg.writeByte(bytesMsg.readByte()); } else if (message instanceof TextMessage) { TextMessage textMsg = (TextMessage)message; ActiveMQTextMessage msg = new ActiveMQTextMessage(); msg.setConnection(connection); msg.setText(textMsg.getText()); activeMessage = msg; } else if (message instanceof BlobMessage) { activeMessage = new ActiveMQMessage(); activeMessage.setConnection(connection);
ActiveMQMessage convertMessage(PUBLISH command) throws JMSException { ActiveMQBytesMessage msg = new ActiveMQBytesMessage(); msg.setProducerId(producerId); MessageId id = new MessageId(producerId, publisherIdGenerator.getNextSequenceId()); msg.setMessageId(id); LOG.trace("MQTT-->ActiveMQ: MQTT_MSGID:{} client:{} connection:{} ActiveMQ_MSGID:{}", command.messageId(), clientId, connectionInfo.getConnectionId(), msg.getMessageId()); msg.setTimestamp(System.currentTimeMillis()); msg.setPriority((byte) Message.DEFAULT_PRIORITY); msg.setPersistent(command.qos() != QoS.AT_MOST_ONCE && !command.retain()); msg.setIntProperty(QOS_PROPERTY_NAME, command.qos().ordinal()); if (command.retain()) { msg.setBooleanProperty(RETAIN_PROPERTY, true); } ActiveMQDestination destination; synchronized (activeMQDestinationMap) { destination = activeMQDestinationMap.get(command.topicName().toString()); if (destination == null) { String topicName = MQTTProtocolSupport.convertMQTTToActiveMQ(command.topicName().toString()); destination = new ActiveMQTopic(topicName); activeMQDestinationMap.put(command.topicName().toString(), destination); } } msg.setJMSDestination(destination); msg.writeBytes(command.payload().data, command.payload().offset, command.payload().length); return msg; }
/** * Return the encoded form of the BytesMessage as an AMQP Binary instance. * * @param message * the Message whose binary encoded body is needed. * * @return a Binary instance containing the encoded message body. * * @throws JMSException if an error occurs while fetching the binary payload. */ public static Binary getBinaryFromMessageBody(ActiveMQBytesMessage message) throws JMSException { Binary result = null; if (message.getContent() != null) { ByteSequence contents = message.getContent(); if (message.isCompressed()) { int length = (int) message.getBodyLength(); byte[] uncompressed = new byte[length]; message.readBytes(uncompressed); result = new Binary(uncompressed); } else { return new Binary(contents.getData(), contents.getOffset(), contents.getLength()); } } return result; }
public StompFrame convertMessage(ProtocolConverter converter, ActiveMQMessage message) throws IOException, JMSException { StompFrame command = new StompFrame(); command.setAction(Stomp.Responses.MESSAGE); Map<String, String> headers = new HashMap<String, String>(25); command.setHeaders(headers); FrameTranslator.Helper.copyStandardHeadersFromMessageToFrame(converter, message, command, this); if (message.getDataStructureType() == ActiveMQTextMessage.DATA_STRUCTURE_TYPE) { ActiveMQTextMessage msg = (ActiveMQTextMessage)message.copy(); command.setContent(msg.getText().getBytes("UTF-8")); } else if (message.getDataStructureType() == ActiveMQBytesMessage.DATA_STRUCTURE_TYPE) { ActiveMQBytesMessage msg = (ActiveMQBytesMessage)message.copy(); msg.setReadOnlyBody(true); byte[] data = new byte[(int)msg.getBodyLength()]; msg.readBytes(data); headers.put(Stomp.Headers.CONTENT_LENGTH, "" + data.length); command.setContent(data); } else if (message.getDataStructureType() == ActiveMQMessage.DATA_STRUCTURE_TYPE && AdvisorySupport.ADIVSORY_MESSAGE_TYPE.equals(message.getType())) { FrameTranslator.Helper.copyStandardHeadersFromMessageToFrame( converter, message, command, this); String body = marshallAdvisory(message.getDataStructure()); command.setContent(body.getBytes("UTF-8")); } return command; }
@Override protected ActiveMQMessage doTransform(EncodedMessage amqpMessage) throws Exception { ActiveMQBytesMessage result = new ActiveMQBytesMessage(); result.setContent(new ByteSequence(amqpMessage.getArray(), amqpMessage.getArrayOffset(), amqpMessage.getLength())); // We cannot decode the message headers to check so err on the side of caution // and mark all messages as persistent. result.setPersistent(true); result.setPriority((byte) Message.DEFAULT_PRIORITY); final long now = System.currentTimeMillis(); result.setTimestamp(now); if (amqpMessage.getMessageFormat() != 0) { result.setLongProperty(JMS_AMQP_MESSAGE_FORMAT, amqpMessage.getMessageFormat()); } result.setBooleanProperty(JMS_AMQP_NATIVE, true); return result; } }
result = new ActiveMQObjectMessage(); } else if (isContentType(OCTET_STREAM_CONTENT_TYPE, message) || isContentType(null, message)) { result = new ActiveMQBytesMessage(); } else { Charset charset = getCharsetForTextualContent(message.getContentType()); if (charset != null) { result = new ActiveMQTextMessage(); } else { result = new ActiveMQMessage(); result.setShortProperty(JMS_AMQP_ORIGINAL_ENCODING, AMQP_NULL); } else if (body instanceof Data) { Binary payload = ((Data) body).getValue(); result.setShortProperty(JMS_AMQP_ORIGINAL_ENCODING, AMQP_DATA); } else if (body instanceof AmqpSequence) { AmqpSequence sequence = (AmqpSequence) body;
String intendedType = (String)headers.get(Stomp.Headers.AMQ_MESSAGE_TYPE); if(intendedType.equalsIgnoreCase("text")){ ActiveMQTextMessage text = new ActiveMQTextMessage(); try { text.setText(new String(command.getContent(), "UTF-8")); } catch (Throwable e) { throw new ProtocolException("Text could not bet set: " + e, false, e); ActiveMQBytesMessage byteMessage = new ActiveMQBytesMessage(); byteMessage.writeBytes(command.getContent()); msg = byteMessage; } else { ActiveMQBytesMessage bm = new ActiveMQBytesMessage(); bm.writeBytes(command.getContent()); msg = bm; } else { ActiveMQTextMessage text = new ActiveMQTextMessage(); try { text.setText(new String(command.getContent(), "UTF-8"));