public ActiveMQMessage convertFrame(StompProtocolConverter converter, StompFrame command)
     throws JMSException, ProtocolException {
   final Map<?, ?> headers = command.getHeaders();
   final ActiveMQMessage msg;
   /*
    * To reduce the complexity of this method perhaps a Chain of Responsibility
    * would be a better implementation
    */
   if (headers.containsKey(Stomp.Headers.AMQ_MESSAGE_TYPE)) {
     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) {
         throw new ProtocolException("Text could not bet set: " + e, false, e);
       }
       msg = text;
     } else if (intendedType.equalsIgnoreCase("bytes")) {
       ActiveMQBytesMessage byteMessage = new ActiveMQBytesMessage();
       byteMessage.writeBytes(command.getContent());
       msg = byteMessage;
     } else {
       throw new ProtocolException("Unsupported message type '" + intendedType + "'", false);
     }
   } else if (headers.containsKey(Stomp.Headers.CONTENT_LENGTH)) {
     headers.remove(Stomp.Headers.CONTENT_LENGTH);
     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);
       data.write(command.getContent());
       text.setContent(bytes.toByteSequence());
       data.close();
     } catch (Throwable e) {
       throw new ProtocolException("Text could not bet set: " + e, false, e);
     }
     msg = text;
   }
   Helper.copyStandardHeadersFromFrameToMessage(converter, command, msg, this);
   return msg;
 }
Example #2
0
  @Test(timeout = 60000)
  public void testSendMessageWithStandardHeadersEncoded() throws Exception {

    MessageConsumer consumer = session.createConsumer(queue);

    String frame =
        "CONNECT\n"
            + "login:system\n"
            + "passcode:manager\n"
            + "accept-version:1.1"
            + "\n\n"
            + Stomp.NULL;
    stompConnection.sendFrame(frame);

    frame = stompConnection.receiveFrame();
    assertTrue(frame.startsWith("CONNECTED"));

    frame =
        "SEND\n"
            + "correlation-id:c1\\:\\n\\23\n"
            + "priority:3\n"
            + "type:t34:5\n"
            + "JMSXGroupID:abc\n"
            + "foo:a\\bc\n"
            + "bar:123\n"
            + "destination:/queue/"
            + getQueueName()
            + "\n\n"
            + "Hello World"
            + Stomp.NULL;

    stompConnection.sendFrame(frame);

    TextMessage message = (TextMessage) consumer.receive(2500);
    assertNotNull(message);
    assertEquals("Hello World", message.getText());
    assertEquals("JMSCorrelationID", "c1\\:\n\\23", message.getJMSCorrelationID());
    assertEquals("getJMSType", "t34:5", message.getJMSType());
    assertEquals("getJMSPriority", 3, message.getJMSPriority());
    assertEquals("foo", "a\\bc", message.getStringProperty("foo"));
    assertEquals("bar", "123", message.getStringProperty("bar"));

    assertEquals("JMSXGroupID", "abc", message.getStringProperty("JMSXGroupID"));
    ActiveMQTextMessage amqMessage = (ActiveMQTextMessage) message;
    assertEquals("GroupID", "abc", amqMessage.getGroupID());
  }
 @Override
 public void dispatchMessage(String messageBody) {
   if (messageBody != null) {
     logger.info("dispatch: " + messageBody);
     try {
       ActiveMQTextMessage message = new ActiveMQTextMessage();
       message.setText(messageBody);
       String destination = getDestination(messageBody);
       logger.info("destination:" + destination);
       if (destination != null) {
         pooledSessionProducer.sendQueue(destination, message);
       }
     } catch (MessageNotWriteableException e) {
       logger.error(e.getMessage());
       e
           .printStackTrace(); // To change body of catch statement use File | Settings | File
                               // Templates.
     }
   }
 }
Example #4
0
  public void testXPathSelectors() throws Exception {
    ActiveMQTextMessage message = new ActiveMQTextMessage();

    message.setJMSType("xml");
    message.setText("<root><a key='first' num='1'/><b key='second' num='2'>b</b></root>");

    assertSelector(message, "XPATH 'root/a'", true);
    assertSelector(message, "XPATH '//root/b'", true);
    assertSelector(message, "XPATH 'root/c'", false);
    assertSelector(message, "XPATH '//root/b/text()=\"b\"'", true);
    assertSelector(message, "XPATH '//root/b=\"b\"'", true);
    assertSelector(message, "XPATH '//root/b=\"c\"'", false);
    assertSelector(message, "XPATH '//root/b!=\"c\"'", true);

    assertSelector(message, "XPATH '//root/*[@key=''second'']'", true);
    assertSelector(message, "XPATH '//root/*[@key=''third'']'", false);
    assertSelector(message, "XPATH '//root/a[@key=''first'']'", true);
    assertSelector(message, "XPATH '//root/a[@num=1]'", true);
    assertSelector(message, "XPATH '//root/a[@key=''second'']'", false);

    assertSelector(message, "XPATH '/root/*[@key=''first'' or @key=''third'']'", true);
    assertSelector(message, "XPATH '//root/*[@key=''third'' or @key=''forth'']'", false);

    assertSelector(message, "XPATH '/root/b=''b'' and /root/b[@key=''second'']'", true);
    assertSelector(message, "XPATH '/root/b=''b'' and /root/b[@key=''first'']'", false);

    assertSelector(message, "XPATH 'not(//root/a)'", false);
    assertSelector(message, "XPATH 'not(//root/c)'", true);
    assertSelector(message, "XPATH '//root/a[not(@key=''first'')]'", false);
    assertSelector(message, "XPATH '//root/a[not(not(@key=''first''))]'", true);

    assertSelector(message, "XPATH 'string(//root/b)'", true);
    assertSelector(message, "XPATH 'string(//root/a)'", false);

    assertSelector(message, "XPATH 'sum(//@num) < 10'", true);
    assertSelector(message, "XPATH 'sum(//@num) > 10'", false);

    assertSelector(message, "XPATH '//root/a[@num > 1]'", false);
    assertSelector(message, "XPATH '//root/b[@num > 1]'", true);
  }
Example #5
0
  public void testSendMessageWithStandardHeaders() throws Exception {

    MessageConsumer consumer = session.createConsumer(queue);

    String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL;
    sendFrame(frame);

    frame = receiveFrame(10000);
    Assert.assertTrue(frame.startsWith("CONNECTED"));

    frame =
        "SEND\n"
            + "correlation-id:c123\n"
            + "priority:3\n"
            + "type:t345\n"
            + "JMSXGroupID:abc\n"
            + "foo:abc\n"
            + "bar:123\n"
            + "destination:/queue/"
            + getQueueName()
            + "\n\n"
            + "Hello World"
            + Stomp.NULL;

    sendFrame(frame);

    TextMessage message = (TextMessage) consumer.receive(1000);
    Assert.assertNotNull(message);
    Assert.assertEquals("Hello World", message.getText());
    Assert.assertEquals("JMSCorrelationID", "c123", message.getJMSCorrelationID());
    Assert.assertEquals("getJMSType", "t345", message.getJMSType());
    Assert.assertEquals("getJMSPriority", 3, message.getJMSPriority());
    Assert.assertEquals(DeliveryMode.PERSISTENT, message.getJMSDeliveryMode());
    Assert.assertEquals("foo", "abc", message.getStringProperty("foo"));
    Assert.assertEquals("bar", "123", message.getStringProperty("bar"));

    Assert.assertEquals("JMSXGroupID", "abc", message.getStringProperty("JMSXGroupID"));
    ActiveMQTextMessage amqMessage = (ActiveMQTextMessage) message;
    Assert.assertEquals("GroupID", "abc", amqMessage.getGroupID());
  }
  public StompFrame convertMessage(StompProtocolConverter converter, ActiveMQMessage message)
      throws IOException, JMSException {
    StompFrame command = new StompFrame();
    command.setAction(Stomp.Responses.MESSAGE);
    Map<String, String> headers = new HashMap<>(25);
    command.setHeaders(headers);

    Helper.copyStandardHeadersFromMessageToFrame(converter, message, command, this);

    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);
        }
      } else {
        ActiveMQTextMessage msg = (ActiveMQTextMessage) message.copy();
        String messageText = msg.getText();
        if (messageText != null) {
          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, Integer.toString(data.length));
      command.setContent(data);
    }

    return command;
  }