Package org.apache.qpid.server.protocol

Examples of org.apache.qpid.server.protocol.AMQProtocolSession


        AMQCodecFactory codecFactory = new AMQCodecFactory(true);

        TestIoSession iosession = new TestIoSession();
        iosession.setAddress("127.0.0.1");
       
        AMQProtocolSession session = new AMQMinaProtocolSession(iosession, virtualHostRegistry, codecFactory);
        assertFalse(reg.getAccessManager().authoriseConnect(session, virtualHost));
    }
View Full Code Here


        TestIoSession iosession = new TestIoSession();
        iosession.setAddress("127.0.0.1");
        VirtualHostRegistry virtualHostRegistry = reg.getVirtualHostRegistry();
        VirtualHost virtualHost = virtualHostRegistry.getVirtualHost("test");
        AMQCodecFactory codecFactory = new AMQCodecFactory(true);
        AMQProtocolSession session = new AMQMinaProtocolSession(iosession, virtualHostRegistry, codecFactory);
        assertFalse(reg.getAccessManager().authoriseConnect(session, virtualHost));

        RandomAccessFile fileBRandom = new RandomAccessFile(fileB, "rw");
        fileBRandom.setLength(0);
        fileBRandom.seek(0);
View Full Code Here

    {
    }

    public void methodReceived(AMQStateManager stateManager, BasicConsumeBody body, int channelId) throws AMQException
    {
        AMQProtocolSession session = stateManager.getProtocolSession();




        AMQChannel channel = session.getChannel(channelId);

        VirtualHost vHost = session.getVirtualHost();

        if (channel == null)
        {
            throw body.getChannelNotFoundException(channelId);
        }
        else
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("BasicConsume: from '" + body.getQueue() +
                              "' for:" + body.getConsumerTag() +
                              " nowait:" + body.getNowait() +
                              " args:" + body.getArguments());
            }

            AMQQueue queue = body.getQueue() == null ? channel.getDefaultQueue() : vHost.getQueueRegistry().getQueue(body.getQueue().intern());

            if (queue == null)
            {
                if (_logger.isDebugEnabled())
                {
                    _logger.debug("No queue for '" + body.getQueue() + "'");
                }
                if (body.getQueue() != null)
                {
                    String msg = "No such queue, '" + body.getQueue() + "'";
                    throw body.getChannelException(AMQConstant.NOT_FOUND, msg);
                }
                else
                {
                    String msg = "No queue name provided, no default queue defined.";
                    throw body.getConnectionException(AMQConstant.NOT_ALLOWED, msg);
                }
            }
            else
            {

                final AMQShortString consumerTagName;

                // Check authz
                if (!vHost.getAccessManager().authoriseConsume(session,
                        body.getExclusive(), body.getNoAck(),
                        body.getNoLocal(), body.getNowait(), queue))
                {
                    throw body.getConnectionException(AMQConstant.ACCESS_REFUSED, "Permission denied");
                }

                if (body.getConsumerTag() != null)
                {
                    consumerTagName = body.getConsumerTag().intern();
                }
                else
                {
                    consumerTagName = null;
                }

                try
                {
                    AMQShortString consumerTag = channel.subscribeToQueue(consumerTagName, queue, !body.getNoAck(),
                                                                          body.getArguments(), body.getNoLocal(), body.getExclusive());
                    if (!body.getNowait())
                    {
                        MethodRegistry methodRegistry = session.getMethodRegistry();
                        AMQMethodBody responseBody = methodRegistry.createBasicConsumeOkBody(consumerTag);
                        session.writeFrame(responseBody.generateFrame(channelId));

                    }

                   
                }
                catch (org.apache.qpid.AMQInvalidArgumentException ise)
                {
                    _logger.debug("Closing connection due to invalid selector");

                    MethodRegistry methodRegistry = session.getMethodRegistry();
                    AMQMethodBody responseBody = methodRegistry.createChannelCloseBody(AMQConstant.INVALID_ARGUMENT.getCode(),
                                                                                       new AMQShortString(ise.getMessage()),
                                                                                       body.getClazz(),
                                                                                       body.getMethod());
                    session.writeFrame(responseBody.generateFrame(channelId));


                }
                catch (ConsumerTagNotUniqueException e)
                {
                    AMQShortString msg = new AMQShortString("Non-unique consumer tag, '" + body.getConsumerTag() + "'");

                    MethodRegistry methodRegistry = session.getMethodRegistry();
                    AMQMethodBody responseBody = methodRegistry.createConnectionCloseBody(AMQConstant.NOT_ALLOWED.getCode(),    // replyCode
                                                             msg,               // replytext
                                                             body.getClazz(),
                                                             body.getMethod());
                    session.writeFrame(responseBody.generateFrame(0));
                }
                catch (AMQQueue.ExistingExclusiveSubscription e)
                {
                    throw body.getChannelException(AMQConstant.ACCESS_REFUSED,
                                                   "Cannot subscribe to queue "
View Full Code Here

    {
    }

    public void methodReceived(AMQStateManager stateManager, QueueUnbindBody body, int channelId) throws AMQException
    {
        AMQProtocolSession session = stateManager.getProtocolSession();
        VirtualHost virtualHost = session.getVirtualHost();
        ExchangeRegistry exchangeRegistry = virtualHost.getExchangeRegistry();
        QueueRegistry queueRegistry = virtualHost.getQueueRegistry();


        final AMQQueue queue;
        final AMQShortString routingKey;              

        if (body.getQueue() == null)
        {
            AMQChannel channel = session.getChannel(channelId);

            if (channel == null)
            {
                throw body.getChannelNotFoundException(channelId);
            }

            queue = channel.getDefaultQueue();

            if (queue == null)
            {
                throw body.getConnectionException(AMQConstant.NOT_FOUND, "No default queue defined on channel and queue was null");
            }

            routingKey = body.getRoutingKey() == null ? null : body.getRoutingKey().intern();

        }
        else
        {
            queue = queueRegistry.getQueue(body.getQueue());
            routingKey = body.getRoutingKey() == null ? null : body.getRoutingKey().intern();
        }

        if (queue == null)
        {
            throw body.getConnectionException(AMQConstant.NOT_FOUND, "Queue " + body.getQueue() + " does not exist.");
        }
        final Exchange exch = exchangeRegistry.getExchange(body.getExchange());
        if (exch == null)
        {
            throw body.getChannelException(AMQConstant.NOT_FOUND, "Exchange " + body.getExchange() + " does not exist.");
        }

        //Perform ACLs
        if (!virtualHost.getAccessManager().authoriseUnbind(session, exch, routingKey, queue))
        {
            throw body.getConnectionException(AMQConstant.ACCESS_REFUSED, "Permission denied");
        }

        try
        {
            queue.unBind(exch, routingKey, body.getArguments());
        }
        catch (AMQInvalidRoutingKeyException rke)
        {
            throw body.getChannelException(AMQConstant.INVALID_ROUTING_KEY, routingKey.toString());
        }
        catch (AMQException e)
        {
            if(e.getErrorCode() == AMQConstant.NOT_FOUND)
            {
                throw body.getConnectionException(AMQConstant.NOT_FOUND,e.getMessage(),e);
            }
            throw body.getChannelException(AMQConstant.CHANNEL_ERROR, e.toString());
        }

        if (_log.isInfoEnabled())
        {
            _log.info("Binding queue " + queue + " to exchange " + exch + " with routing key " + routingKey);
        }

        MethodRegistry_0_9 methodRegistry = (MethodRegistry_0_9) session.getMethodRegistry();
        AMQMethodBody responseBody = methodRegistry.createQueueUnbindOkBody();
        session.writeFrame(responseBody.generateFrame(channelId));


    }
View Full Code Here

    private final AtomicInteger _counter = new AtomicInteger();

    public void methodReceived(AMQStateManager stateManager, QueueDeclareBody body, int channelId) throws AMQException
    {
        AMQProtocolSession session = stateManager.getProtocolSession();
        VirtualHost virtualHost = session.getVirtualHost();
        ExchangeRegistry exchangeRegistry = virtualHost.getExchangeRegistry();
        QueueRegistry queueRegistry = virtualHost.getQueueRegistry();
        MessageStore store = virtualHost.getMessageStore();


        if (!body.getPassive())
        {
            // Perform ACL if request is not passive
            if (!virtualHost.getAccessManager().authoriseCreateQueue(session, body.getAutoDelete(), body.getDurable(),
                    body.getExclusive(), body.getNowait(), body.getPassive(), body.getQueue()))
            {
                throw body.getConnectionException(AMQConstant.ACCESS_REFUSED, "Permission denied");
            }
        }

        final AMQShortString queueName;

        // if we aren't given a queue name, we create one which we return to the client

        if ((body.getQueue() == null) || (body.getQueue().length() == 0))
        {
            queueName = createName();
        }
        else
        {
            queueName = body.getQueue().intern();
        }

        AMQQueue queue;
        //TODO: do we need to check that the queue already exists with exactly the same "configuration"?

        synchronized (queueRegistry)
        {



            if (((queue = queueRegistry.getQueue(queueName)) == null))
            {

                if (body.getPassive())
                {
                    String msg = "Queue: " + queueName + " not found on VirtualHost(" + virtualHost + ").";
                    throw body.getChannelException(AMQConstant.NOT_FOUND, msg);
                }
                else
                {
                    queue = createQueue(queueName, body, virtualHost, session);
                    if (queue.isDurable() && !queue.isAutoDelete())
                    {
                        store.createQueue(queue, body.getArguments());
                    }
                    queueRegistry.registerQueue(queue);
                    if (autoRegister)
                    {
                        Exchange defaultExchange = exchangeRegistry.getDefaultExchange();

                        queue.bind(defaultExchange, queueName, null);
                        _logger.info("Queue " + queueName + " bound to default exchange(" + defaultExchange.getName() + ")");
                    }
                }
            }
            else if (queue.getOwner() != null && !session.getContextKey().equals(queue.getOwner()))
            {
                throw body.getChannelException(AMQConstant.ALREADY_EXISTS, "Cannot declare queue('" + queueName + "'),"
                                                                           + " as exclusive queue with same name "
                                                                           + "declared on another client ID('"
                                                                           + queue.getOwner() + "')");
            }

            AMQChannel channel = session.getChannel(channelId);

            if (channel == null)
            {
                throw body.getChannelNotFoundException(channelId);
            }

            //set this as the default queue on the channel:
            channel.setDefaultQueue(queue);
        }

        if (!body.getNowait())
        {
            MethodRegistry methodRegistry = session.getMethodRegistry();
            QueueDeclareOkBody responseBody =
                    methodRegistry.createQueueDeclareOkBody(queueName,
                                                            queue.getMessageCount(),
                                                            queue.getConsumerCount());
            session.writeFrame(responseBody.generateFrame(channelId));

            _logger.info("Queue " + queueName + " declared successfully");
        }
    }
View Full Code Here

    {
    }

    public void methodReceived(AMQStateManager stateManager, ExchangeDeclareBody body, int channelId) throws AMQException
    {
        AMQProtocolSession session = stateManager.getProtocolSession();
        VirtualHost virtualHost = session.getVirtualHost();
        ExchangeRegistry exchangeRegistry = virtualHost.getExchangeRegistry();
        ExchangeFactory exchangeFactory = virtualHost.getExchangeFactory();

        if (!body.getPassive())
        {
            // Perform ACL if request is not passive
            if (!virtualHost.getAccessManager().authoriseCreateExchange(session, body.getAutoDelete(),
                    body.getDurable(), body.getExchange(), body.getInternal(), body.getNowait(), body.getPassive(),
                    body.getType()))
            {
                throw body.getConnectionException(AMQConstant.ACCESS_REFUSED, "Permission denied");
            }

        }

        if (_logger.isDebugEnabled())
        {
            _logger.debug("Request to declare exchange of type " + body.getType() + " with name " + body.getExchange());
        }
        synchronized(exchangeRegistry)
        {
            Exchange exchange = exchangeRegistry.getExchange(body.getExchange());



            if (exchange == null)
            {
                if(body.getPassive() && ((body.getType() == null) || body.getType().length() ==0))
                {
                    throw body.getChannelException(AMQConstant.NOT_FOUND, "Unknown exchange: " + body.getExchange());
                }
                else
                {
                    try
                    {

                    exchange = exchangeFactory.createExchange(body.getExchange() == null ? null : body.getExchange().intern(),
                                                              body.getType() == null ? null : body.getType().intern(),
                                                              body.getDurable(),
                                                              body.getPassive(), body.getTicket());
                    exchangeRegistry.registerExchange(exchange);
                    }
                    catch(AMQUnknownExchangeType e)
                    {
                        throw body.getConnectionException(AMQConstant.COMMAND_INVALID, "Unknown exchange: " + body.getExchange(),e);
                    }
                }
            }
            else if (!exchange.getType().equals(body.getType()))
            {

                throw new AMQConnectionException(AMQConstant.NOT_ALLOWED, "Attempt to redeclare exchange: " + body.getExchange() + " of type " + exchange.getType() + " to " + body.getType() +".",body.getClazz(), body.getMethod(),body.getMajor(),body.getMinor(),null);
            }

        }
        if(!body.getNowait())
        {
            MethodRegistry methodRegistry = session.getMethodRegistry();
            AMQMethodBody responseBody = methodRegistry.createExchangeDeclareOkBody();
            session.writeFrame(responseBody.generateFrame(channelId));

        }
    }
View Full Code Here

    }

    public void methodReceived(AMQStateManager stateManager, QueueDeleteBody body, int channelId) throws AMQException
    {
        AMQProtocolSession session = stateManager.getProtocolSession();
        VirtualHost virtualHost = session.getVirtualHost();
        QueueRegistry queueRegistry = virtualHost.getQueueRegistry();
        MessageStore store = virtualHost.getMessageStore();

        AMQQueue queue;
        if (body.getQueue() == null)
        {
            AMQChannel channel = session.getChannel(channelId);

            if (channel == null)
            {
                throw body.getChannelNotFoundException(channelId);
            }

            //get the default queue on the channel:           
            queue = channel.getDefaultQueue();
        }
        else
        {
            queue = queueRegistry.getQueue(body.getQueue());
        }

        if (queue == null)
        {
            if (_failIfNotFound)
            {
                throw body.getChannelException(AMQConstant.NOT_FOUND, "Queue " + body.getQueue() + " does not exist.");
            }
        }
        else
        {
            if (body.getIfEmpty() && !queue.isEmpty())
            {
                throw body.getChannelException(AMQConstant.IN_USE, "Queue: " + body.getQueue() + " is not empty.");
            }
            else if (body.getIfUnused() && !queue.isUnused())
            {
                // TODO - Error code
                throw body.getChannelException(AMQConstant.IN_USE, "Queue: " + body.getQueue() + " is still used.");

            }
            else
            {
               
                //Perform ACLs
                if (!virtualHost.getAccessManager().authoriseDelete(session, queue))
                {
                    throw body.getConnectionException(AMQConstant.ACCESS_REFUSED, "Permission denied");
                }

                int purged = queue.delete();


                if (queue.isDurable())
                {
                    store.removeQueue(queue);
                }

                MethodRegistry methodRegistry = session.getMethodRegistry();
                QueueDeleteOkBody responseBody = methodRegistry.createQueueDeleteOkBody(purged);
                session.writeFrame(responseBody.generateFrame(channelId));
            }
        }
    }
View Full Code Here

    {
    }

    public void methodReceived(AMQStateManager stateManager, ExchangeDeleteBody body, int channelId) throws AMQException
    {
        AMQProtocolSession session = stateManager.getProtocolSession();
        VirtualHost virtualHost = session.getVirtualHost();
        ExchangeRegistry exchangeRegistry = virtualHost.getExchangeRegistry();

        //Perform ACLs
        if (!virtualHost.getAccessManager().authoriseDelete(session,
                exchangeRegistry.getExchange(body.getExchange())))
        {
            throw body.getConnectionException(AMQConstant.ACCESS_REFUSED, "Permission denied");
        }

        try
        {
            exchangeRegistry.unregisterExchange(body.getExchange(), body.getIfUnused());

            ExchangeDeleteOkBody responseBody = session.getMethodRegistry().createExchangeDeleteOkBody();
                       
            session.writeFrame(responseBody.generateFrame(channelId));
        }
        catch (ExchangeInUseException e)
        {
            // TODO: sort out consistent channel close mechanism that does all clean up etc.
        }
View Full Code Here

        return new AMQShortString(Long.toString(System.currentTimeMillis()));
    }

    public void methodReceived(AMQStateManager stateManager, ConnectionOpenBody body, int channelId) throws AMQException
    {
        AMQProtocolSession session = stateManager.getProtocolSession();


        //ignore leading '/'
        String virtualHostName;
        if ((body.getVirtualHost() != null) && body.getVirtualHost().charAt(0) == '/')
        {
            virtualHostName = new StringBuilder(body.getVirtualHost().subSequence(1, body.getVirtualHost().length())).toString();
        }
        else
        {
            virtualHostName = body.getVirtualHost() == null ? null : String.valueOf(body.getVirtualHost());
        }

        VirtualHost virtualHost = stateManager.getVirtualHostRegistry().getVirtualHost(virtualHostName);

        if (virtualHost == null)
        {
            throw body.getConnectionException(AMQConstant.NOT_FOUND, "Unknown virtual host: '" + virtualHostName + "'");
        }
        else
        {
            session.setVirtualHost(virtualHost);

            //Perform ACL
            if (!virtualHost.getAccessManager().authoriseConnect(session, virtualHost))
            {
                throw body.getConnectionException(AMQConstant.ACCESS_REFUSED, "Permission denied");
            }

            // See Spec (0.8.2). Section  3.1.2 Virtual Hosts
            if (session.getContextKey() == null)
            {
                session.setContextKey(generateClientID());
            }

            MethodRegistry methodRegistry = session.getMethodRegistry();
            AMQMethodBody responseBody = methodRegistry.createConnectionOpenOkBody(body.getVirtualHost());

            stateManager.changeState(AMQState.CONNECTION_OPEN);

            session.writeFrame(responseBody.generateFrame(channelId));

           
        }
    }
View Full Code Here

    {
    }

    public void methodReceived(AMQStateManager stateManager, BasicGetBody body, int channelId) throws AMQException
    {
        AMQProtocolSession session = stateManager.getProtocolSession();


        VirtualHost vHost = session.getVirtualHost();

        AMQChannel channel = session.getChannel(channelId);
        if (channel == null)
        {
            throw body.getChannelNotFoundException(channelId);
        }
        else
        {
            AMQQueue queue = body.getQueue() == null ? channel.getDefaultQueue() : vHost.getQueueRegistry().getQueue(body.getQueue());
            if (queue == null)
            {
                _log.info("No queue for '" + body.getQueue() + "'");
                if(body.getQueue()!=null)
                {
                    throw body.getConnectionException(AMQConstant.NOT_FOUND,
                                                      "No such queue, '" + body.getQueue()+ "'");
                }
                else
                {
                    throw body.getConnectionException(AMQConstant.NOT_ALLOWED,
                                                      "No queue name provided, no default queue defined.");
                }
            }
            else
            {

                //Perform ACLs
                if (!vHost.getAccessManager().authoriseConsume(session, body.getNoAck(), queue))
                {
                    throw body.getConnectionException(AMQConstant.ACCESS_REFUSED, "Permission denied");
                }

                if (!performGet(queue,session, channel, !body.getNoAck()))
                {
                    MethodRegistry methodRegistry = session.getMethodRegistry();
                    // TODO - set clusterId
                    BasicGetEmptyBody responseBody = methodRegistry.createBasicGetEmptyBody(null);


                    session.writeFrame(responseBody.generateFrame(channelId));
                }
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.qpid.server.protocol.AMQProtocolSession

Copyright © 2018 www.massapicom. 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.