Esempio n. 1
0
  /**
   * Debug Helper!!!!
   *
   * <p>I'm leaving this message here without any callers for a reason: During debugs it's important
   * eventually to identify what's on the bodies, and this method will give you a good idea about
   * them. Add the message.bodyToString() to the Watch variables on the debugger view and this will
   * show up like a charm!!!
   *
   * @return
   */
  public String bodyToString() {
    getEndOfBodyPosition();
    int readerIndex1 = this.buffer.readerIndex();
    buffer.readerIndex(0);
    byte[] buffer1 = new byte[buffer.writerIndex()];
    buffer.readBytes(buffer1);
    buffer.readerIndex(readerIndex1);

    byte[] buffer2 = null;
    if (bodyBuffer != null) {
      int readerIndex2 = this.bodyBuffer.readerIndex();
      bodyBuffer.readerIndex(0);
      buffer2 = new byte[bodyBuffer.writerIndex() - bodyBuffer.readerIndex()];
      bodyBuffer.readBytes(buffer2);
      bodyBuffer.readerIndex(readerIndex2);
    }

    return "ServerMessage@"
        + Integer.toHexString(System.identityHashCode(this))
        + "["
        + ",bodyStart="
        + getEndOfBodyPosition()
        + " buffer="
        + ByteUtil.bytesToHex(buffer1, 1)
        + ", bodyBuffer="
        + ByteUtil.bytesToHex(buffer2, 1);
  }
Esempio n. 2
0
 public void decodeHeadersAndProperties(final HornetQBuffer buffer) {
   messageID = buffer.readLong();
   address = buffer.readNullableSimpleString();
   if (buffer.readByte() == DataConstants.NOT_NULL) {
     byte[] bytes = new byte[16];
     buffer.readBytes(bytes);
     userID = new UUID(UUID.TYPE_TIME_BASED, bytes);
   } else {
     userID = null;
   }
   type = buffer.readByte();
   durable = buffer.readBoolean();
   expiration = buffer.readLong();
   timestamp = buffer.readLong();
   priority = buffer.readByte();
   properties.decode(buffer);
 }
Esempio n. 3
0
  public StompFrame unmarshal(HornetQBuffer in) throws IOException {

    try {
      String action = null;

      // skip white space to next real action line
      while (true) {
        action = readLine(in, MAX_COMMAND_LENGTH, "The maximum command length was exceeded");
        if (action == null) {
          throw new IOException("connection was closed");
        } else {
          action = action.trim();
          if (action.length() > 0) {
            break;
          }
        }
      }

      // Parse the headers
      HashMap headers = new HashMap(25);
      while (true) {
        String line = readLine(in, MAX_HEADER_LENGTH, "The maximum header length was exceeded");
        if (line != null && line.trim().length() > 0) {

          if (headers.size() > MAX_HEADERS) {
            throw new StompException("The maximum number of headers was exceeded", true);
          }

          try {
            int seperator_index = line.indexOf(Stomp.Headers.SEPERATOR);
            String name = line.substring(0, seperator_index).trim();
            String value = line.substring(seperator_index + 1, line.length()).trim();
            headers.put(name, value);
          } catch (Exception e) {
            throw new StompException("Unable to parser header line [" + line + "]", true);
          }
        } else {
          break;
        }
      }

      // Read in the data part.
      byte[] data = NO_DATA;
      String contentLength = (String) headers.get(Stomp.Headers.CONTENT_LENGTH);
      if (contentLength != null) {

        // Bless the client, he's telling us how much data to read in.
        int length;
        try {
          length = Integer.parseInt(contentLength.trim());
        } catch (NumberFormatException e) {
          throw new StompException("Specified content-length is not a valid integer", true);
        }

        if (length > MAX_DATA_LENGTH) {
          throw new StompException("The maximum data length was exceeded", true);
        }

        data = new byte[length];
        in.readBytes(data);

        if (in.readByte() != 0) {
          throw new StompException(
              Stomp.Headers.CONTENT_LENGTH
                  + " bytes were read and "
                  + "there was no trailing null byte",
              true);
        }
      } else {

        // We don't know how much to read.. data ends when we hit a 0
        byte b;
        ByteArrayOutputStream baos = null;
        while (in.readableBytes() > 0 && (b = in.readByte()) != 0) {

          if (baos == null) {
            baos = new ByteArrayOutputStream();
          } else if (baos.size() > MAX_DATA_LENGTH) {
            throw new StompException("The maximum data length was exceeded", true);
          }

          baos.write(b);
        }

        if (baos != null) {
          baos.close();
          data = baos.toByteArray();
        }
      }

      return new StompFrame(action, headers, data);
    } catch (StompException e) {
      return new StompFrameError(e);
    }
  }
Esempio n. 4
0
  public int sendMessage(ServerMessage serverMessage, long consumerID, int deliveryCount) {
    LargeServerMessageImpl largeMessage = null;
    ServerMessage newServerMessage = serverMessage;
    try {
      StompSubscription subscription = subscriptions.get(consumerID);
      StompFrame frame = null;
      if (serverMessage.isLargeMessage()) {
        newServerMessage = serverMessage.copy();

        largeMessage = (LargeServerMessageImpl) serverMessage;
        BodyEncoder encoder = largeMessage.getBodyEncoder();
        encoder.open();
        int bodySize = (int) encoder.getLargeBodySize();

        // large message doesn't have a body.
        ((ServerMessageImpl) newServerMessage).createBody(bodySize);
        encoder.encode(newServerMessage.getBodyBuffer(), bodySize);
        encoder.close();
      }

      if (serverMessage.getBooleanProperty(Message.HDR_LARGE_COMPRESSED)) {
        // decompress
        HornetQBuffer qbuff = newServerMessage.getBodyBuffer();
        int bytesToRead = qbuff.writerIndex() - MessageImpl.BODY_OFFSET;
        Inflater inflater = new Inflater();
        inflater.setInput(qbuff.readBytes(bytesToRead).toByteBuffer().array());

        // get the real size of large message
        long sizeBody = newServerMessage.getLongProperty(Message.HDR_LARGE_BODY_SIZE);

        byte[] data = new byte[(int) sizeBody];
        inflater.inflate(data);
        inflater.end();
        qbuff.resetReaderIndex();
        qbuff.resetWriterIndex();
        qbuff.writeBytes(data);
      }

      frame = connection.createStompMessage(newServerMessage, subscription, deliveryCount);

      int length = frame.getEncodedSize();

      if (subscription.getAck().equals(Stomp.Headers.Subscribe.AckModeValues.AUTO)) {
        if (manager.send(connection, frame)) {
          // we ack and commit only if the send is successful
          session.acknowledge(consumerID, newServerMessage.getMessageID());
          session.commit();
        }
      } else {
        messagesToAck.put(
            newServerMessage.getMessageID(), new Pair<Long, Integer>(consumerID, length));
        // Must send AFTER adding to messagesToAck - or could get acked from client BEFORE it's been
        // added!
        manager.send(connection, frame);
      }

      return length;
    } catch (Exception e) {
      return 0;
    } finally {
      if (largeMessage != null) {
        largeMessage.releaseResources();
        largeMessage = null;
      }
    }
  }