コード例 #1
0
 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;
 }
コード例 #2
0
  @Override
  public ActiveMQMessage convertFrame(ProtocolConverter converter, StompFrame command)
      throws JMSException, ProtocolException {
    Map<String, String> headers = command.getHeaders();
    ActiveMQMessage msg;
    String transformation = headers.get(Headers.TRANSFORMATION);
    if (headers.containsKey(Headers.CONTENT_LENGTH)
        || transformation.equals(Transformations.JMS_BYTE.toString())) {
      msg = super.convertFrame(converter, command);
    } else {
      HierarchicalStreamReader in;

      try {
        String text = new String(command.getContent(), "UTF-8");
        switch (Transformations.getValue(transformation)) {
          case JMS_OBJECT_XML:
            in = new XppReader(new StringReader(text), XppFactory.createDefaultParser());
            msg = createObjectMessage(in);
            break;
          case JMS_OBJECT_JSON:
            in = new JettisonMappedXmlDriver().createReader(new StringReader(text));
            msg = createObjectMessage(in);
            break;
          case JMS_MAP_XML:
            in = new XppReader(new StringReader(text), XppFactory.createDefaultParser());
            msg = createMapMessage(in);
            break;
          case JMS_MAP_JSON:
            in = new JettisonMappedXmlDriver().createReader(new StringReader(text));
            msg = createMapMessage(in);
            break;
          default:
            throw new Exception("Unkown transformation: " + transformation);
        }
      } catch (Throwable e) {
        command.getHeaders().put(Headers.TRANSFORMATION_ERROR, e.getMessage());
        msg = super.convertFrame(converter, command);
      }
    }

    copyStandardHeadersFromFrameToMessage(converter, command, msg, this);
    return msg;
  }