Search in sources :

Example 1 with ReservedExchangeNameException

use of org.apache.qpid.server.virtualhost.ReservedExchangeNameException in project qpid-broker-j by apache.

the class ServerSessionDelegate method exchangeDeclare.

@Override
public void exchangeDeclare(ServerSession session, ExchangeDeclare method) {
    String exchangeName = method.getExchange();
    NamedAddressSpace addressSpace = getAddressSpace(session);
    // we must check for any unsupported arguments present and throw not-implemented
    if (method.hasArguments()) {
        Map<String, Object> args = method.getArguments();
        // QPID-3392: currently we don't support any!
        if (!args.isEmpty()) {
            exception(session, method, ExecutionErrorCode.NOT_IMPLEMENTED, "Unsupported exchange argument(s) found " + args.keySet().toString());
            return;
        }
    }
    String alternateExchangeName = method.getAlternateExchange();
    if (nameNullOrEmpty(method.getExchange())) {
        // special case handling to fake the existence of the default exchange for 0-10
        if (!ExchangeDefaults.DIRECT_EXCHANGE_CLASS.equals(method.getType())) {
            exception(session, method, ExecutionErrorCode.NOT_ALLOWED, "Attempt to redeclare default exchange " + " of type " + ExchangeDefaults.DIRECT_EXCHANGE_CLASS + " to " + method.getType() + ".");
        }
        if (!nameNullOrEmpty(alternateExchangeName)) {
            exception(session, method, ExecutionErrorCode.NOT_ALLOWED, "Attempt to set alternate exchange of the default exchange " + " to " + alternateExchangeName + ".");
        }
    } else {
        if (method.getPassive()) {
            Exchange<?> exchange = getExchange(session, exchangeName);
            if (exchange == null) {
                exception(session, method, ExecutionErrorCode.NOT_FOUND, "not-found: exchange-name '" + exchangeName + "'");
            } else {
                if (!exchange.getType().equals(method.getType()) && (method.getType() != null && method.getType().length() > 0)) {
                    exception(session, method, ExecutionErrorCode.NOT_ALLOWED, "Attempt to redeclare exchange: " + exchangeName + " of type " + exchange.getType() + " to " + method.getType() + ".");
                }
            }
        } else {
            try {
                Map<String, Object> attributes = new HashMap<String, Object>();
                attributes.put(org.apache.qpid.server.model.Exchange.NAME, method.getExchange());
                attributes.put(org.apache.qpid.server.model.Exchange.TYPE, method.getType());
                attributes.put(org.apache.qpid.server.model.Exchange.DURABLE, method.getDurable());
                attributes.put(org.apache.qpid.server.model.Exchange.LIFETIME_POLICY, method.getAutoDelete() ? LifetimePolicy.DELETE_ON_NO_LINKS : LifetimePolicy.PERMANENT);
                if (method.hasAlternateExchange() && !nameNullOrEmpty(alternateExchangeName)) {
                    validateAlternateExchangeIsNotQueue(addressSpace, alternateExchangeName);
                    attributes.put(org.apache.qpid.server.model.Exchange.ALTERNATE_BINDING, Collections.singletonMap(AlternateBinding.DESTINATION, alternateExchangeName));
                }
                addressSpace.createMessageDestination(Exchange.class, attributes);
                ;
            } catch (ReservedExchangeNameException e) {
                Exchange<?> existingExchange = getExchange(session, exchangeName);
                if (existingExchange == null || !existingExchange.getType().equals(method.getType()) || (method.hasAlternateExchange() && (existingExchange.getAlternateBinding() == null || !alternateExchangeName.equals(existingExchange.getAlternateBinding().getDestination())))) {
                    exception(session, method, ExecutionErrorCode.NOT_ALLOWED, "Attempt to declare exchange: " + exchangeName + " which begins with reserved name or prefix.");
                }
            } catch (UnknownAlternateBindingException e) {
                exception(session, method, ExecutionErrorCode.NOT_FOUND, String.format("Unknown alternate exchange '%s'", e.getAlternateBindingName()));
            } catch (NoFactoryForTypeException e) {
                exception(session, method, ExecutionErrorCode.NOT_FOUND, "Unknown Exchange Type: " + method.getType());
            } catch (AbstractConfiguredObject.DuplicateNameException e) {
                Exchange<?> exchange = (Exchange<?>) e.getExisting();
                if (!exchange.getType().equals(method.getType())) {
                    exception(session, method, ExecutionErrorCode.NOT_ALLOWED, "Attempt to redeclare exchange: " + exchangeName + " of type " + exchange.getType() + " to " + method.getType() + ".");
                } else if (method.hasAlternateExchange() && (exchange.getAlternateBinding() == null || !alternateExchangeName.equals(exchange.getAlternateBinding().getDestination()))) {
                    exception(session, method, ExecutionErrorCode.NOT_ALLOWED, "Attempt to change alternate exchange of: " + exchangeName + " from " + exchange.getAlternateBinding() + " to " + alternateExchangeName + ".");
                }
            } catch (AccessControlException e) {
                exception(session, method, ExecutionErrorCode.UNAUTHORIZED_ACCESS, e.getMessage());
            } catch (IllegalArgumentException | IllegalConfigurationException e) {
                exception(session, method, ExecutionErrorCode.ILLEGAL_ARGUMENT, e.getMessage());
            }
        }
    }
}
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) NoFactoryForTypeException(org.apache.qpid.server.model.NoFactoryForTypeException) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) ReservedExchangeNameException(org.apache.qpid.server.virtualhost.ReservedExchangeNameException) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) UnknownAlternateBindingException(org.apache.qpid.server.virtualhost.UnknownAlternateBindingException)

Example 2 with ReservedExchangeNameException

use of org.apache.qpid.server.virtualhost.ReservedExchangeNameException 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)

Aggregations

AccessControlException (java.security.AccessControlException)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)2 AbstractConfiguredObject (org.apache.qpid.server.model.AbstractConfiguredObject)2 Exchange (org.apache.qpid.server.model.Exchange)2 NamedAddressSpace (org.apache.qpid.server.model.NamedAddressSpace)2 NoFactoryForTypeException (org.apache.qpid.server.model.NoFactoryForTypeException)2 ReservedExchangeNameException (org.apache.qpid.server.virtualhost.ReservedExchangeNameException)2 UnknownAlternateBindingException (org.apache.qpid.server.virtualhost.UnknownAlternateBindingException)2