@Override public void setBytes(final String name, final byte[] value, final int offset, final int length) throws JMSException { checkName(name); if (bodyReadOnly) { throw new MessageNotWriteableException("Message is ReadOnly !"); } if (offset + length > value.length) { throw new JMSException("Array is too small"); } byte[] temp = new byte[length]; System.arraycopy(value, offset, temp, 0, length); content.put(name, temp); }
@Override public void setObject(Serializable object) throws JMSException { if (bodyIsReadOnly) throw new MessageNotWriteableException("Message body is read-only"); assertDeserializationLevel(MessageSerializationLevel.FULL); if (object == null) { body = null; return; } try { body = SerializationTools.toByteArray(object); } catch (Exception e) { throw new FFMQException("Cannot serialize object message body","MESSAGE_ERROR",e); } }
public void testResetMakesReadble() throws Exception { try { JMSBytesMessage bm = TestMessageHelper.newJMSBytesMessage(); bm.writeInt(10); bm.reset(); bm.writeInt(12); fail("expected exception did not occur"); } catch (MessageNotWriteableException m) { // ok } catch (Exception e) { fail("expected MessageNotWriteableException, got " + e); } }
public void testResetMakesReadble() throws Exception { try { JMSStreamMessage bm = TestMessageHelper.newJMSStreamMessage(); bm.writeInt(10); bm.reset(); bm.writeInt(12); fail("expected exception did not occur"); } catch (MessageNotWriteableException m) { // ok } catch (Exception e) { fail("expected MessageNotWriteableException, got " + e); } }
@Test public void testClearBody() throws JMSException, IOException { JmsTextMessage textMessage = factory.createTextMessage(); textMessage.setText("string"); textMessage.clearBody(); assertFalse(textMessage.isReadOnlyBody()); assertNull(textMessage.getText()); try { textMessage.setText("String"); textMessage.getText(); } catch (MessageNotWriteableException mnwe) { fail("should be writeable"); } catch (MessageNotReadableException mnre) { fail("should be readable"); } }
@Test public void testReadOnlyBody() throws JMSException { JmsTextMessage textMessage = factory.createTextMessage(); textMessage.setText("test"); textMessage.setReadOnlyBody(true); try { textMessage.getText(); } catch (MessageNotReadableException e) { fail("should be readable"); } try { textMessage.setText("test"); fail("should throw exception"); } catch (MessageNotWriteableException mnwe) { } }
/** * Test that we are not able to write to a received message without calling * {@link JmsMapMessage#clearBody()} * * @throws Exception if an error occurs during the test. */ @Test public void testReceivedMessageIsReadOnlyAndThrowsMNWE() throws Exception { JmsMapMessageFacade facade = new JmsTestMapMessageFacade(); String myKey1 = "key1"; facade.put(myKey1, "value1"); JmsMapMessage mapMessage = new JmsMapMessage(facade); mapMessage.onDispatch(); try { mapMessage.setObject("name", "value"); fail("expected exception to be thrown"); } catch (MessageNotWriteableException mnwe) { // expected } }
@Test public void testClearPropertiesClearsReadOnly() throws Exception { JmsMessage msg = factory.createMessage(); msg.onDispatch(); try { msg.setObjectProperty("test", "value"); fail("should throw exception"); } catch (MessageNotWriteableException e) { // Expected } assertTrue(msg.isReadOnlyProperties()); msg.clearProperties(); msg.setObjectProperty("test", "value"); assertFalse(msg.isReadOnlyProperties()); }
/** * Test that attempting to write bytes to a received message (without calling {@link ObjectMessage#clearBody()} first) * causes a {@link MessageNotWriteableException} to be thrown due to being read-only. * * @throws Exception if an error occurs during the test. */ @Test public void testReceivedObjectMessageThrowsMessageNotWriteableExceptionOnSetObject() throws Exception { String content = "myStringContent"; JmsObjectMessageFacade facade = new JmsTestObjectMessageFacade(); facade.setObject(content); JmsObjectMessage objectMessage = new JmsObjectMessage(facade); objectMessage.onDispatch(); try { objectMessage.setObject("newObject"); fail("Expected exception to be thrown"); } catch (MessageNotWriteableException mnwe) { // expected } }
public static JMSRuntimeException convertToRuntimeException(JMSException e) { if (e instanceof javax.jms.IllegalStateException) { return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidClientIDException) { return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidDestinationException) { return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidSelectorException) { return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof JMSSecurityException) { return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof MessageFormatException) { return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof MessageNotWriteableException) { return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof ResourceAllocationException) { return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof TransactionInProgressException) { return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof TransactionRolledBackException) { return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e); } return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e); }
@Test public void convertMessageNotWritableException() throws JMSException { Message<String> message = createTextMessage(); MessageConverter messageConverter = mock(MessageConverter.class); willThrow(MessageNotWriteableException.class).given(messageConverter).toMessage(eq(message), anyObject()); messagingTemplate.setJmsMessageConverter(messageConverter); invokeMessageCreator("myQueue"); thrown.expect(org.springframework.messaging.converter.MessageConversionException.class); messagingTemplate.send("myQueue", message); }
private void initializeWriting() throws MessageNotWriteableException { checkReadOnlyBody(); if (this.dataOut == null) { this.bytesOut = new ByteArrayOutputStream(); OutputStream os = bytesOut; ActiveMQConnection connection = getConnection(); if (connection != null && connection.isUseCompression()) { compressed = true; os = new DeflaterOutputStream(os); } this.dataOut = new DataOutputStream(os); } }
protected Message createMessage(ProducerInfo producerInfo, ActiveMQDestination destination) { ActiveMQTextMessage message = new ActiveMQTextMessage(); message.setMessageId(new MessageId(producerInfo, ++msgIdGenerator)); message.setDestination(destination); message.setPersistent(false); try { message.setText("Test Message Payload."); } catch (MessageNotWriteableException e) { } return message; }
protected void assertSendTextMessage(ActiveMQDestination destination, String text) throws MessageNotWriteableException { large = true; ActiveMQTextMessage expected = new ActiveMQTextMessage(); expected.setText(text); expected.setDestination(destination); try { LOG.info("About to send message of type: " + expected.getClass()); producer.oneway(expected); // lets send a dummy command to ensure things don't block if we // discard the last one // keepalive does not have a commandId... // producer.oneway(new KeepAliveInfo()); producer.oneway(new ProducerInfo()); producer.oneway(new ProducerInfo()); Command received = assertCommandReceived(); assertTrue("Should have received an ActiveMQTextMessage but was: " + received, received instanceof ActiveMQTextMessage); ActiveMQTextMessage actual = (ActiveMQTextMessage) received; assertEquals("getDestination", expected.getDestination(), actual.getDestination()); assertEquals("getText", expected.getText(), actual.getText()); LOG.info("Received text message with: " + actual.getText().length() + " character(s)"); } catch (Exception e) { LOG.info("Caught: " + e); e.printStackTrace(); fail("Failed to send to transport: " + e); } }
public void testClearBody() throws JMSException, IOException { ActiveMQTextMessage textMessage = new ActiveMQTextMessage(); textMessage.setText("string"); textMessage.clearBody(); assertFalse(textMessage.isReadOnlyBody()); assertNull(textMessage.getText()); try { textMessage.setText("String"); textMessage.getText(); } catch (MessageNotWriteableException mnwe) { fail("should be writeable"); } catch (MessageNotReadableException mnre) { fail("should be readable"); } }
public void testReadOnlyBody() throws JMSException { ActiveMQTextMessage textMessage = new ActiveMQTextMessage(); textMessage.setText("test"); textMessage.setReadOnlyBody(true); try { textMessage.getText(); } catch (MessageNotReadableException e) { fail("should be readable"); } try { textMessage.setText("test"); fail("should throw exception"); } catch (MessageNotWriteableException mnwe) { } }
public void testClearBody() throws JMSException { ActiveMQObjectMessage objectMessage = new ActiveMQObjectMessage(); try { objectMessage.setObject("String"); objectMessage.clearBody(); assertFalse(objectMessage.isReadOnlyBody()); assertNull(objectMessage.getObject()); objectMessage.setObject("String"); objectMessage.getObject(); } catch (MessageNotWriteableException mnwe) { fail("should be writeable"); } }
public void testSetReadOnly() { ActiveMQMessage msg = new ActiveMQMessage(); msg.setReadOnlyProperties(true); boolean test = false; try { msg.setIntProperty("test", 1); } catch (MessageNotWriteableException me) { test = true; } catch (JMSException e) { e.printStackTrace(System.err); test = false; } assertTrue(test); }
@Override public void writeBoolean(final boolean value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("The message body is readonly"); } content.add(new Boolean(value)); }
@Override public void writeByte(final byte value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("The message body is readonly"); } content.add(new Byte(value)); }
@Override public void writeShort(final short value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("The message body is readonly"); } content.add(new Short(value)); }
@Override public void writeChar(final char value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("The message body is readonly"); } content.add(new Character(value)); }
@Override public void writeInt(final int value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("The message body is readonly"); } content.add(new Integer(value)); }
@Override public void writeLong(final long value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("The message body is readonly"); } content.add(new Long(value)); }
@Override public void writeFloat(final float value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("The message body is readonly"); } content.add(new Float(value)); }
@Override public void writeDouble(final double value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("The message body is readonly"); } content.add(new Double(value)); }
@Override public void writeString(final String value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("The message body is readonly"); } if (value == null) { content.add(null); } else { content.add(value); } }
@Override public void writeBytes(final byte[] value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("The message body is readonly"); } content.add(value.clone()); }
@Override public void writeBytes(final byte[] value, final int offset, final int length) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("The message body is readonly"); } if (offset + length > value.length) { throw new JMSException("Array is too small"); } byte[] temp = new byte[length]; System.arraycopy(value, offset, temp, 0, length); content.add(temp); }
@Override public void writeObject(final Object value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("The message body is readonly"); } if (value == null) { content.add(null); } else if (value instanceof Boolean) { content.add(value); } else if (value instanceof Byte) { content.add(value); } else if (value instanceof Short) { content.add(value); } else if (value instanceof Character) { content.add(value); } else if (value instanceof Integer) { content.add(value); } else if (value instanceof Long) { content.add(value); } else if (value instanceof Float) { content.add(value); } else if (value instanceof Double) { content.add(value); } else if (value instanceof String) { content.add(value); } else if (value instanceof byte[]) { content.add(((byte[]) value).clone()); } else { throw new MessageFormatException("Invalid object type"); } }
@Override public void writeBoolean(final boolean value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("the message body is read-only"); } try { p.writeBoolean(value); } catch (IOException e) { throw new JMSException("IOException"); } }
@Override public void writeByte(final byte value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("the message body is read-only"); } try { p.writeByte(value); } catch (IOException e) { throw new JMSException("IOException"); } }
@Override public void writeShort(final short value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("the message body is read-only"); } try { p.writeShort(value); } catch (IOException e) { throw new JMSException("IOException"); } }
@Override public void writeChar(final char value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("the message body is read-only"); } try { p.writeChar(value); } catch (IOException e) { throw new JMSException("IOException"); } }
@Override public void writeInt(final int value) throws JMSException { if (!bodyWriteOnly) { throw new MessageNotWriteableException("the message body is read-only"); } try { p.writeInt(value); } catch (IOException e) { throw new JMSException("IOException"); } }