Package org.activemq.transport.stomp

Source Code of org.activemq.transport.stomp.CommandParser

/*
* Copyright (c) 2005 Your Corporation. All Rights Reserved.
*/
package org.activemq.transport.stomp;

import org.activemq.message.Packet;
import org.activemq.message.Receipt;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.net.ProtocolException;

class CommandParser
{
    private short sessionId;
    private String clientId;
    private final StompWireFormat format;

    CommandParser(StompWireFormat wireFormat)
    {
        format = wireFormat;
    }

    Packet parse(DataInput in) throws IOException
    {
        String line;

        // skip white space to next real line
        try
        {
            while ((line = in.readLine()).trim().length() == 0) {}
        }
        catch (NullPointerException e)
        {
            throw new IOException("connection was closed");
        }

        // figure corrent command and return it
        Command command = null;
        if (line.startsWith(Stomp.Commands.SUBSCRIBE)) command = new Subscribe(format);
        if (line.startsWith(Stomp.Commands.SEND)) command = new Send(format);
        if (line.startsWith(Stomp.Commands.DISCONNECT)) command = new Disconnect(clientId);
        if (line.startsWith(Stomp.Commands.BEGIN)) command = new Begin(format);
        if (line.startsWith(Stomp.Commands.COMMIT)) command = new Commit(format);
        if (line.startsWith(Stomp.Commands.ABORT)) command = new Abort(format);
        if (line.startsWith(Stomp.Commands.UNSUBSCRIBE)) command = new Unsubscribe(format);

        if (command == null)
        {
            while (in.readByte() == 0) {}
            throw new ProtocolException("Unknown command [" + line + "]");
        }

        PacketEnvelope envelope = command.build(line, in);
        if (envelope.getHeaders().containsKey(Stomp.Headers.RECEIPT_REQUESTED))
        {
            final short id = StompWireFormat.PACKET_IDS.getNextShortSequence();
            envelope.getPacket().setId(id);
            envelope.getPacket().setReceiptRequired(true);
            final String client_packet_key = envelope.getHeaders().getProperty(Stomp.Headers.RECEIPT_REQUESTED);
            format.addReceiptListener(new ReceiptListener()
            {
                public boolean onReceipt(Receipt receipt, DataOutput out) throws IOException
                {
                    if (receipt.getCorrelationId() != id) return false;

                    out.writeBytes(new FrameBuilder(Stomp.Responses.RECEIPT)
                            .addHeader(Stomp.Headers.Receipt.RECEIPT_ID, client_packet_key)
                            .toFrame());
                    return true;
                }
            });
        }

        return envelope.getPacket();
    }

    void setSessionId(short sessionId)
    {
        this.sessionId = sessionId;
    }

    void setClientId(String clientId)
    {
        this.clientId = clientId;
    }
}
TOP

Related Classes of org.activemq.transport.stomp.CommandParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.