Search in sources :

Example 21 with NamedAddressSpace

use of org.apache.qpid.server.model.NamedAddressSpace in project qpid-broker-j by apache.

the class AMQChannel method receiveExchangeDeclare.

@Override
public void receiveExchangeDeclare(final AMQShortString exchangeName, final AMQShortString type, final boolean passive, final boolean durable, final boolean autoDelete, final boolean internal, final boolean nowait, final FieldTable arguments) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("RECV[" + _channelId + "] ExchangeDeclare[" + " exchange: " + exchangeName + " type: " + type + " passive: " + passive + " durable: " + durable + " autoDelete: " + autoDelete + " internal: " + internal + " nowait: " + nowait + " arguments: " + arguments + " ]");
    }
    final MethodRegistry methodRegistry = _connection.getMethodRegistry();
    final AMQMethodBody declareOkBody = methodRegistry.createExchangeDeclareOkBody();
    Exchange<?> exchange;
    NamedAddressSpace virtualHost = _connection.getAddressSpace();
    if (isDefaultExchange(exchangeName)) {
        if (!new AMQShortString(ExchangeDefaults.DIRECT_EXCHANGE_CLASS).equals(type)) {
            _connection.sendConnectionClose(ErrorCodes.NOT_ALLOWED, "Attempt to redeclare default exchange: " + " of type " + ExchangeDefaults.DIRECT_EXCHANGE_CLASS + " to " + type + ".", getChannelId());
        } else if (!nowait) {
            sync();
            _connection.writeFrame(declareOkBody.generateFrame(getChannelId()));
        }
    } else {
        if (passive) {
            exchange = getExchange(exchangeName.toString());
            if (exchange == null) {
                closeChannel(ErrorCodes.NOT_FOUND, "Unknown exchange: '" + exchangeName + "'");
            } else if (!(type == null || type.length() == 0) && !exchange.getType().equals(type.toString())) {
                _connection.sendConnectionClose(ErrorCodes.NOT_ALLOWED, "Attempt to redeclare exchange: '" + exchangeName + "' of type " + exchange.getType() + " to " + type + ".", getChannelId());
            } else if (!nowait) {
                sync();
                _connection.writeFrame(declareOkBody.generateFrame(getChannelId()));
            }
        } else {
            String name = exchangeName.toString();
            String typeString = type == null ? null : type.toString();
            try {
                Map<String, Object> attributes = new HashMap<String, Object>();
                if (arguments != null) {
                    attributes.putAll(FieldTable.convertToMap(arguments));
                }
                attributes.put(Exchange.NAME, name);
                attributes.put(Exchange.TYPE, typeString);
                attributes.put(Exchange.DURABLE, durable);
                attributes.put(Exchange.LIFETIME_POLICY, autoDelete ? LifetimePolicy.DELETE_ON_NO_LINKS : LifetimePolicy.PERMANENT);
                Object alternateExchange = attributes.remove(ALTERNATE_EXCHANGE);
                if (alternateExchange != null) {
                    String alternateExchangeName = String.valueOf(alternateExchange);
                    validateAlternateExchangeIsNotQueue(virtualHost, alternateExchangeName);
                    attributes.put(Exchange.ALTERNATE_BINDING, Collections.singletonMap(AlternateBinding.DESTINATION, alternateExchangeName));
                }
                exchange = virtualHost.createMessageDestination(Exchange.class, attributes);
                if (!nowait) {
                    sync();
                    _connection.writeFrame(declareOkBody.generateFrame(getChannelId()));
                }
            } catch (ReservedExchangeNameException e) {
                Exchange existing = getExchange(name);
                if (existing == null || !existing.getType().equals(typeString)) {
                    _connection.sendConnectionClose(ErrorCodes.NOT_ALLOWED, "Attempt to declare exchange: '" + exchangeName + "' which begins with reserved prefix.", getChannelId());
                } else if (!nowait) {
                    sync();
                    _connection.writeFrame(declareOkBody.generateFrame(getChannelId()));
                }
            } catch (AbstractConfiguredObject.DuplicateNameException e) {
                exchange = (Exchange<?>) e.getExisting();
                if (!exchange.getType().equals(typeString)) {
                    _connection.sendConnectionClose(ErrorCodes.NOT_ALLOWED, "Attempt to redeclare exchange: '" + exchangeName + "' of type " + exchange.getType() + " to " + type + ".", getChannelId());
                } else {
                    if (!nowait) {
                        sync();
                        _connection.writeFrame(declareOkBody.generateFrame(getChannelId()));
                    }
                }
            } catch (NoFactoryForTypeException e) {
                _connection.sendConnectionClose(ErrorCodes.COMMAND_INVALID, "Unknown exchange type '" + e.getType() + "' for exchange '" + exchangeName + "'", getChannelId());
            } catch (AccessControlException e) {
                _connection.sendConnectionClose(ErrorCodes.ACCESS_REFUSED, e.getMessage(), getChannelId());
            } catch (UnknownAlternateBindingException e) {
                final String message = String.format("Unknown alternate exchange '%s'", e.getAlternateBindingName());
                _connection.sendConnectionClose(ErrorCodes.NOT_FOUND, message, getChannelId());
            } catch (IllegalArgumentException | IllegalConfigurationException e) {
                _connection.sendConnectionClose(ErrorCodes.COMMAND_INVALID, "Error creating exchange '" + exchangeName + "': " + e.getMessage(), getChannelId());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NamedAddressSpace(org.apache.qpid.server.model.NamedAddressSpace) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) AccessControlException(java.security.AccessControlException) Exchange(org.apache.qpid.server.model.Exchange) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) NoFactoryForTypeException(org.apache.qpid.server.model.NoFactoryForTypeException) ReservedExchangeNameException(org.apache.qpid.server.virtualhost.ReservedExchangeNameException) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) UnknownAlternateBindingException(org.apache.qpid.server.virtualhost.UnknownAlternateBindingException)

Example 22 with NamedAddressSpace

use of org.apache.qpid.server.model.NamedAddressSpace in project qpid-broker-j by apache.

the class AMQChannel method receiveBasicPublish.

@Override
public void receiveBasicPublish(final AMQShortString exchangeName, final AMQShortString routingKey, final boolean mandatory, final boolean immediate) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("RECV[" + _channelId + "] BasicPublish[" + " exchange: " + exchangeName + " routingKey: " + routingKey + " mandatory: " + mandatory + " immediate: " + immediate + " ]");
    }
    NamedAddressSpace vHost = _connection.getAddressSpace();
    if (blockingTimeoutExceeded()) {
        message(ChannelMessages.FLOW_CONTROL_IGNORED());
        closeChannel(ErrorCodes.MESSAGE_TOO_LARGE, "Channel flow control was requested, but not enforced by sender");
    } else {
        MessageDestination destination;
        if (isDefaultExchange(exchangeName)) {
            destination = vHost.getDefaultDestination();
        } else {
            destination = vHost.getAttainedMessageDestination(exchangeName.toString());
        }
        // if the exchange does not exist we raise a channel exception
        if (destination == null) {
            closeChannel(ErrorCodes.NOT_FOUND, "Unknown exchange name: '" + exchangeName + "'");
        } else {
            MessagePublishInfo info = new MessagePublishInfo(exchangeName, immediate, mandatory, routingKey);
            try {
                setPublishFrame(info, destination);
            } catch (AccessControlException e) {
                _connection.sendConnectionClose(ErrorCodes.ACCESS_REFUSED, e.getMessage(), getChannelId());
            }
        }
    }
}
Also used : MessageDestination(org.apache.qpid.server.message.MessageDestination) NamedAddressSpace(org.apache.qpid.server.model.NamedAddressSpace) AccessControlException(java.security.AccessControlException)

Example 23 with NamedAddressSpace

use of org.apache.qpid.server.model.NamedAddressSpace in project qpid-broker-j by apache.

the class AMQChannel method receiveQueueDeclare.

@Override
public void receiveQueueDeclare(final AMQShortString queueStr, final boolean passive, final boolean durable, final boolean exclusive, final boolean autoDelete, final boolean nowait, final FieldTable arguments) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("RECV[" + _channelId + "] QueueDeclare[" + " queue: " + queueStr + " passive: " + passive + " durable: " + durable + " exclusive: " + exclusive + " autoDelete: " + autoDelete + " nowait: " + nowait + " arguments: " + arguments + " ]");
    }
    NamedAddressSpace virtualHost = _connection.getAddressSpace();
    final AMQShortString queueName;
    // if we aren't given a queue name, we create one which we return to the client
    if ((queueStr == null) || (queueStr.length() == 0)) {
        queueName = new AMQShortString("tmp_" + UUID.randomUUID());
    } else {
        queueName = queueStr;
    }
    Queue<?> queue;
    if (passive) {
        queue = getQueue(queueName.toString());
        if (queue == null) {
            closeChannel(ErrorCodes.NOT_FOUND, "Queue: '" + queueName + "' not found on VirtualHost '" + virtualHost.getName() + "'.");
        } else {
            if (!queue.verifySessionAccess(this)) {
                _connection.sendConnectionClose(ErrorCodes.NOT_ALLOWED, "Queue '" + queue.getName() + "' is exclusive, but not created on this Connection.", getChannelId());
            } else {
                // set this as the default queue on the channel:
                setDefaultQueue(queue);
                if (!nowait) {
                    sync();
                    MethodRegistry methodRegistry = _connection.getMethodRegistry();
                    QueueDeclareOkBody responseBody = methodRegistry.createQueueDeclareOkBody(queueName, queue.getQueueDepthMessages(), queue.getConsumerCount());
                    _connection.writeFrame(responseBody.generateFrame(getChannelId()));
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("Queue " + queueName + " declared successfully");
                    }
                }
            }
        }
    } else {
        try {
            final String queueNameString = AMQShortString.toString(queueName);
            Map<String, Object> wireArguments = FieldTable.convertToMap(arguments);
            Object alternateExchange = wireArguments.get(ALTERNATE_EXCHANGE);
            if (alternateExchange != null) {
                String alternateExchangeName = String.valueOf(alternateExchange);
                validateAlternateExchangeIsNotQueue(virtualHost, alternateExchangeName);
            }
            Map<String, Object> attributes = QueueArgumentsConverter.convertWireArgsToModel(queueNameString, wireArguments);
            attributes.put(Queue.NAME, queueNameString);
            attributes.put(Queue.DURABLE, durable);
            LifetimePolicy lifetimePolicy;
            ExclusivityPolicy exclusivityPolicy;
            if (exclusive) {
                lifetimePolicy = autoDelete ? LifetimePolicy.DELETE_ON_NO_OUTBOUND_LINKS : durable ? LifetimePolicy.PERMANENT : LifetimePolicy.DELETE_ON_CONNECTION_CLOSE;
                exclusivityPolicy = durable ? ExclusivityPolicy.CONTAINER : ExclusivityPolicy.CONNECTION;
            } else {
                lifetimePolicy = autoDelete ? LifetimePolicy.DELETE_ON_NO_OUTBOUND_LINKS : LifetimePolicy.PERMANENT;
                exclusivityPolicy = ExclusivityPolicy.NONE;
            }
            if (!attributes.containsKey(Queue.EXCLUSIVE)) {
                attributes.put(Queue.EXCLUSIVE, exclusivityPolicy);
            }
            if (!attributes.containsKey(Queue.LIFETIME_POLICY)) {
                attributes.put(Queue.LIFETIME_POLICY, lifetimePolicy);
            }
            queue = virtualHost.createMessageSource(Queue.class, attributes);
            setDefaultQueue(queue);
            if (!nowait) {
                sync();
                MethodRegistry methodRegistry = _connection.getMethodRegistry();
                QueueDeclareOkBody responseBody = methodRegistry.createQueueDeclareOkBody(queueName, queue.getQueueDepthMessages(), queue.getConsumerCount());
                _connection.writeFrame(responseBody.generateFrame(getChannelId()));
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Queue " + queueName + " declared successfully");
                }
            }
        } catch (AbstractConfiguredObject.DuplicateNameException qe) {
            queue = (Queue<?>) qe.getExisting();
            if (!queue.verifySessionAccess(this)) {
                _connection.sendConnectionClose(ErrorCodes.NOT_ALLOWED, "Queue '" + queue.getName() + "' is exclusive, but not created on this Connection.", getChannelId());
            } else if (queue.isExclusive() != exclusive) {
                closeChannel(ErrorCodes.ALREADY_EXISTS, "Cannot re-declare queue '" + queue.getName() + "' with different exclusivity (was: " + queue.isExclusive() + " requested " + exclusive + ")");
            } else if ((autoDelete && queue.getLifetimePolicy() == LifetimePolicy.PERMANENT) || (!autoDelete && queue.getLifetimePolicy() != ((exclusive && !durable) ? LifetimePolicy.DELETE_ON_CONNECTION_CLOSE : LifetimePolicy.PERMANENT))) {
                closeChannel(ErrorCodes.ALREADY_EXISTS, "Cannot re-declare queue '" + queue.getName() + "' with different lifetime policy (was: " + queue.getLifetimePolicy() + " requested autodelete: " + autoDelete + ")");
            } else {
                setDefaultQueue(queue);
                if (!nowait) {
                    sync();
                    MethodRegistry methodRegistry = _connection.getMethodRegistry();
                    QueueDeclareOkBody responseBody = methodRegistry.createQueueDeclareOkBody(queueName, queue.getQueueDepthMessages(), queue.getConsumerCount());
                    _connection.writeFrame(responseBody.generateFrame(getChannelId()));
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("Queue " + queueName + " declared successfully");
                    }
                }
            }
        } catch (AccessControlException e) {
            _connection.sendConnectionClose(ErrorCodes.ACCESS_REFUSED, e.getMessage(), getChannelId());
        } catch (UnknownAlternateBindingException e) {
            final String message = String.format("Unknown alternate exchange: '%s'", e.getAlternateBindingName());
            _connection.sendConnectionClose(ErrorCodes.NOT_FOUND, message, getChannelId());
        } catch (IllegalArgumentException | IllegalConfigurationException e) {
            String message = String.format("Error creating queue '%s': %s", queueName, e.getMessage());
            _connection.sendConnectionClose(ErrorCodes.COMMAND_INVALID, message, getChannelId());
        }
    }
}
Also used : LifetimePolicy(org.apache.qpid.server.model.LifetimePolicy) NamedAddressSpace(org.apache.qpid.server.model.NamedAddressSpace) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) AccessControlException(java.security.AccessControlException) ExclusivityPolicy(org.apache.qpid.server.model.ExclusivityPolicy) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) UnknownAlternateBindingException(org.apache.qpid.server.virtualhost.UnknownAlternateBindingException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Queue(org.apache.qpid.server.model.Queue)

Example 24 with NamedAddressSpace

use of org.apache.qpid.server.model.NamedAddressSpace in project qpid-broker-j by apache.

the class AMQChannel method receiveQueueDelete.

@Override
public void receiveQueueDelete(final AMQShortString queueName, final boolean ifUnused, final boolean ifEmpty, final boolean nowait) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("RECV[" + _channelId + "] QueueDelete[" + " queue: " + queueName + " ifUnused: " + ifUnused + " ifEmpty: " + ifEmpty + " nowait: " + nowait + " ]");
    }
    NamedAddressSpace virtualHost = _connection.getAddressSpace();
    sync();
    Queue<?> queue;
    if (queueName == null) {
        // get the default queue on the channel:
        queue = getDefaultQueue();
    } else {
        queue = getQueue(queueName.toString());
    }
    if (queue == null) {
        closeChannel(ErrorCodes.NOT_FOUND, "Queue '" + queueName + "' does not exist.");
    } else {
        if (ifEmpty && !queue.isEmpty()) {
            closeChannel(ErrorCodes.IN_USE, "Queue: '" + queueName + "' is not empty.");
        } else if (ifUnused && !queue.isUnused()) {
            // TODO - Error code
            closeChannel(ErrorCodes.IN_USE, "Queue: '" + queueName + "' is still used.");
        } else {
            if (!queue.verifySessionAccess(this)) {
                _connection.sendConnectionClose(ErrorCodes.NOT_ALLOWED, "Queue '" + queue.getName() + "' is exclusive, but not created on this Connection.", getChannelId());
            } else {
                try {
                    int purged = queue.deleteAndReturnCount();
                    if (!nowait || _connection.isSendQueueDeleteOkRegardless()) {
                        MethodRegistry methodRegistry = _connection.getMethodRegistry();
                        QueueDeleteOkBody responseBody = methodRegistry.createQueueDeleteOkBody(purged);
                        _connection.writeFrame(responseBody.generateFrame(getChannelId()));
                    }
                } catch (AccessControlException e) {
                    _connection.sendConnectionClose(ErrorCodes.ACCESS_REFUSED, e.getMessage(), getChannelId());
                }
            }
        }
    }
}
Also used : NamedAddressSpace(org.apache.qpid.server.model.NamedAddressSpace) AccessControlException(java.security.AccessControlException)

Example 25 with NamedAddressSpace

use of org.apache.qpid.server.model.NamedAddressSpace in project qpid-broker-j by apache.

the class AMQChannel method receiveExchangeDelete.

@Override
public void receiveExchangeDelete(final AMQShortString exchangeStr, final boolean ifUnused, final boolean nowait) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("RECV[" + _channelId + "] ExchangeDelete[" + " exchange: " + exchangeStr + " ifUnused: " + ifUnused + " nowait: " + nowait + " ]");
    }
    NamedAddressSpace virtualHost = _connection.getAddressSpace();
    sync();
    if (isDefaultExchange(exchangeStr)) {
        _connection.sendConnectionClose(ErrorCodes.NOT_ALLOWED, "Default Exchange cannot be deleted", getChannelId());
    } else {
        final String exchangeName = exchangeStr.toString();
        final Exchange<?> exchange = getExchange(exchangeName);
        if (exchange == null) {
            closeChannel(ErrorCodes.NOT_FOUND, "No such exchange: '" + exchangeStr + "'");
        } else {
            if (ifUnused && exchange.hasBindings()) {
                closeChannel(ErrorCodes.IN_USE, "Exchange has bindings");
            } else {
                try {
                    exchange.delete();
                    if (!nowait) {
                        ExchangeDeleteOkBody responseBody = _connection.getMethodRegistry().createExchangeDeleteOkBody();
                        _connection.writeFrame(responseBody.generateFrame(getChannelId()));
                    }
                } catch (MessageDestinationIsAlternateException e) {
                    closeChannel(ErrorCodes.NOT_ALLOWED, "Exchange in use as an alternate binding destination");
                } catch (RequiredExchangeException e) {
                    closeChannel(ErrorCodes.NOT_ALLOWED, "Exchange '" + exchangeStr + "' cannot be deleted");
                } catch (AccessControlException e) {
                    _connection.sendConnectionClose(ErrorCodes.ACCESS_REFUSED, e.getMessage(), getChannelId());
                }
            }
        }
    }
}
Also used : NamedAddressSpace(org.apache.qpid.server.model.NamedAddressSpace) MessageDestinationIsAlternateException(org.apache.qpid.server.virtualhost.MessageDestinationIsAlternateException) AccessControlException(java.security.AccessControlException) RequiredExchangeException(org.apache.qpid.server.virtualhost.RequiredExchangeException)

Aggregations

NamedAddressSpace (org.apache.qpid.server.model.NamedAddressSpace)42 AccessControlException (java.security.AccessControlException)22 HashMap (java.util.HashMap)12 MessageSource (org.apache.qpid.server.message.MessageSource)8 AbstractConfiguredObject (org.apache.qpid.server.model.AbstractConfiguredObject)8 Queue (org.apache.qpid.server.model.Queue)6 Collection (java.util.Collection)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 Symbol (org.apache.qpid.server.protocol.v1_0.type.Symbol)5 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)4 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)4 MessageDestination (org.apache.qpid.server.message.MessageDestination)4 AmqpPort (org.apache.qpid.server.model.port.AmqpPort)4 ArrayList (java.util.ArrayList)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Exchange (org.apache.qpid.server.model.Exchange)3 BaseSource (org.apache.qpid.server.protocol.v1_0.type.BaseSource)3 Source (org.apache.qpid.server.protocol.v1_0.type.messaging.Source)3 AmqpError (org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError)3