Search in sources :

Example 6 with AMQInvalidArgumentException

use of org.apache.qpid.server.filter.AMQInvalidArgumentException in project qpid-broker-j by apache.

the class AbstractExchange method onOpen.

@Override
protected void onOpen() {
    super.onOpen();
    final ConfiguredDerivedMethodAttribute<Exchange<?>, Collection<Binding>> durableBindingsAttribute = (ConfiguredDerivedMethodAttribute<Exchange<?>, Collection<Binding>>) getModel().getTypeRegistry().getAttributeTypes(getTypeClass()).get(DURABLE_BINDINGS);
    final Collection<Binding> bindings = durableBindingsAttribute.convertValue(getActualAttributes().get(DURABLE_BINDINGS), this);
    if (bindings != null) {
        _bindings.addAll(bindings);
        for (Binding b : _bindings) {
            final MessageDestination messageDestination = getOpenedMessageDestination(b.getDestination());
            if (messageDestination != null) {
                Map<String, Object> arguments = b.getArguments() == null ? Collections.emptyMap() : b.getArguments();
                try {
                    onBind(new BindingIdentifier(b.getBindingKey(), messageDestination), arguments);
                } catch (AMQInvalidArgumentException e) {
                    throw new IllegalConfigurationException("Unexpected bind argument : " + e.getMessage(), e);
                }
                messageDestination.linkAdded(this, b);
            }
        }
    }
    if (getLifetimePolicy() == LifetimePolicy.DELETE_ON_CREATING_LINK_CLOSE) {
        if (_creatingLinkInfo != null) {
            final LinkModel link;
            if (_creatingLinkInfo.isSendingLink()) {
                link = _virtualHost.getSendingLink(_creatingLinkInfo.getRemoteContainerId(), _creatingLinkInfo.getLinkName());
            } else {
                link = _virtualHost.getReceivingLink(_creatingLinkInfo.getRemoteContainerId(), _creatingLinkInfo.getLinkName());
            }
            addLifetimeConstraint(link);
        } else {
            throw new IllegalArgumentException("Exchanges created with a lifetime policy of " + getLifetimePolicy() + " must be created from a AMQP 1.0 link.");
        }
    }
    if (getAlternateBinding() != null) {
        String alternateDestination = getAlternateBinding().getDestination();
        _alternateBindingDestination = getOpenedMessageDestination(alternateDestination);
        if (_alternateBindingDestination != null) {
            _alternateBindingDestination.addReference(this);
        } else {
            LOGGER.warn("Cannot find alternate binding destination '{}' for exchange '{}'", alternateDestination, toString());
        }
    }
}
Also used : Binding(org.apache.qpid.server.model.Binding) AlternateBinding(org.apache.qpid.server.model.AlternateBinding) MessageDestination(org.apache.qpid.server.message.MessageDestination) AMQInvalidArgumentException(org.apache.qpid.server.filter.AMQInvalidArgumentException) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) LinkModel(org.apache.qpid.server.protocol.LinkModel) Exchange(org.apache.qpid.server.model.Exchange) ConfiguredDerivedMethodAttribute(org.apache.qpid.server.model.ConfiguredDerivedMethodAttribute) Collection(java.util.Collection) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject)

Example 7 with AMQInvalidArgumentException

use of org.apache.qpid.server.filter.AMQInvalidArgumentException in project qpid-broker-j by apache.

the class AbstractVirtualHost method getSubscriptionQueue.

@Override
@DoOnConfigThread
public Queue<?> getSubscriptionQueue(@Param(name = "exchangeName", mandatory = true) final String exchangeName, @Param(name = "attributes", mandatory = true) final Map<String, Object> attributes, @Param(name = "bindings", mandatory = true) final Map<String, Map<String, Object>> bindings) {
    Queue queue;
    Object exclusivityPolicy = attributes.get(Queue.EXCLUSIVE);
    if (exclusivityPolicy == null) {
        exclusivityPolicy = getContextValue(ExclusivityPolicy.class, Queue.QUEUE_DEFAULT_EXCLUSIVITY_POLICY);
    }
    if (!(exclusivityPolicy instanceof ExclusivityPolicy)) {
        throw new IllegalArgumentException("Exclusivity policy is required");
    }
    Exchange<?> exchange = findConfiguredObject(Exchange.class, exchangeName);
    if (exchange == null) {
        throw new NotFoundException(String.format("Exchange '%s' was not found", exchangeName));
    }
    try {
        queue = createMessageDestination(Queue.class, attributes);
        for (String binding : bindings.keySet()) {
            exchange.addBinding(binding, queue, bindings.get(binding));
        }
    } catch (AbstractConfiguredObject.DuplicateNameException e) {
        Queue<?> existingQueue = (Queue) e.getExisting();
        if (existingQueue.getExclusive() == exclusivityPolicy) {
            if (hasDifferentBindings(exchange, existingQueue, bindings)) {
                if (existingQueue.getConsumers().isEmpty()) {
                    existingQueue.delete();
                    queue = createMessageDestination(Queue.class, attributes);
                    for (String binding : bindings.keySet()) {
                        try {
                            exchange.addBinding(binding, queue, bindings.get(binding));
                        } catch (AMQInvalidArgumentException ia) {
                            throw new IllegalArgumentException("Unexpected bind argument : " + ia.getMessage(), ia);
                        }
                    }
                } else {
                    throw new IllegalStateException("subscription already in use");
                }
            } else {
                queue = existingQueue;
            }
        } else {
            throw new IllegalStateException("subscription already in use");
        }
    } catch (AMQInvalidArgumentException e) {
        throw new IllegalArgumentException("Unexpected bind argument : " + e.getMessage(), e);
    }
    return queue;
}
Also used : ExclusivityPolicy(org.apache.qpid.server.model.ExclusivityPolicy) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) AMQInvalidArgumentException(org.apache.qpid.server.filter.AMQInvalidArgumentException) NotFoundException(org.apache.qpid.server.model.NotFoundException) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) Queue(org.apache.qpid.server.model.Queue) DoOnConfigThread(org.apache.qpid.server.model.DoOnConfigThread)

Example 8 with AMQInvalidArgumentException

use of org.apache.qpid.server.filter.AMQInvalidArgumentException in project qpid-broker-j by apache.

the class AMQChannel method consumeFromSource.

/**
 * Subscribe to a queue. We register all subscriptions in the channel so that if the channel is closed we can clean
 * up all subscriptions, even if the client does not explicitly unsubscribe from all queues.
 *
 * @param tag       the tag chosen by the client (if null, server will generate one)
 * @param sources     the queues to subscribe to
 * @param acks      Are acks enabled for this subscriber
 * @param arguments   Filters to apply to this subscriber
 *
 * @param exclusive Flag requesting exclusive access to the queue
 * @return the consumer tag. This is returned to the subscriber and used in subsequent unsubscribe requests
 */
private AMQShortString consumeFromSource(AMQShortString tag, Collection<MessageSource> sources, boolean acks, FieldTable arguments, boolean exclusive, boolean noLocal) throws MessageSource.ExistingConsumerPreventsExclusive, MessageSource.ExistingExclusiveConsumer, AMQInvalidArgumentException, MessageSource.ConsumerAccessRefused, ConsumerTagInUseException, MessageSource.QueueDeleted {
    if (tag == null) {
        tag = AMQShortString.createAMQShortString("sgen_" + getNextConsumerTag());
    }
    if (_tag2SubscriptionTargetMap.containsKey(tag)) {
        throw new ConsumerTagInUseException("Consumer already exists with same tag: " + tag);
    }
    ConsumerTarget_0_8 target;
    EnumSet<ConsumerOption> options = EnumSet.noneOf(ConsumerOption.class);
    final boolean multiQueue = sources.size() > 1;
    if (arguments != null && Boolean.TRUE.equals(arguments.get(AMQPFilterTypes.NO_CONSUME.getValue()))) {
        target = ConsumerTarget_0_8.createBrowserTarget(this, tag, arguments, INFINITE_CREDIT_CREDIT_MANAGER, multiQueue);
    } else if (acks) {
        target = ConsumerTarget_0_8.createAckTarget(this, tag, arguments, _creditManager, multiQueue);
        options.add(ConsumerOption.ACQUIRES);
        options.add(ConsumerOption.SEES_REQUEUES);
    } else {
        target = ConsumerTarget_0_8.createNoAckTarget(this, tag, arguments, INFINITE_CREDIT_CREDIT_MANAGER, multiQueue);
        options.add(ConsumerOption.ACQUIRES);
        options.add(ConsumerOption.SEES_REQUEUES);
    }
    if (exclusive) {
        options.add(ConsumerOption.EXCLUSIVE);
    }
    // So to keep things straight we put before the call and catch all exceptions from the register and tidy up.
    // We add before we register as the Async Delivery process may AutoClose the subscriber
    // so calling _cT2QM.remove before we have done put which was after the register succeeded.
    // So to keep things straight we put before the call and catch all exceptions from the register and tidy up.
    _tag2SubscriptionTargetMap.put(tag, target);
    try {
        FilterManager filterManager = FilterManagerFactory.createManager(FieldTable.convertToMap(arguments));
        if (noLocal) {
            if (filterManager == null) {
                filterManager = new FilterManager();
            }
            MessageFilter filter = new NoLocalFilter();
            filterManager.add(filter.getName(), filter);
        }
        if (arguments != null && arguments.containsKey(AMQPFilterTypes.REPLAY_PERIOD.toString())) {
            Object value = arguments.get(AMQPFilterTypes.REPLAY_PERIOD.toString());
            final long period;
            if (value instanceof Number) {
                period = ((Number) value).longValue();
            } else if (value instanceof String) {
                try {
                    period = Long.parseLong(value.toString());
                } catch (NumberFormatException e) {
                    throw new AMQInvalidArgumentException("Cannot parse value " + value + " as a number for filter " + AMQPFilterTypes.REPLAY_PERIOD.toString());
                }
            } else {
                throw new AMQInvalidArgumentException("Cannot parse value " + value + " as a number for filter " + AMQPFilterTypes.REPLAY_PERIOD.toString());
            }
            final long startingFrom = System.currentTimeMillis() - (1000l * period);
            if (filterManager == null) {
                filterManager = new FilterManager();
            }
            MessageFilter filter = new ArrivalTimeFilter(startingFrom, period == 0);
            filterManager.add(filter.getName(), filter);
        }
        Integer priority = null;
        if (arguments != null && arguments.containsKey("x-priority")) {
            Object value = arguments.get("x-priority");
            if (value instanceof Number) {
                priority = ((Number) value).intValue();
            } else if (value instanceof String || value instanceof AMQShortString) {
                try {
                    priority = Integer.parseInt(value.toString());
                } catch (NumberFormatException e) {
                // use default vlaue
                }
            }
        }
        for (MessageSource source : sources) {
            source.addConsumer(target, filterManager, AMQMessage.class, AMQShortString.toString(tag), options, priority);
        }
        target.updateNotifyWorkDesired();
    } catch (AccessControlException | MessageSource.ExistingExclusiveConsumer | MessageSource.ExistingConsumerPreventsExclusive | MessageSource.QueueDeleted | AMQInvalidArgumentException | MessageSource.ConsumerAccessRefused e) {
        _tag2SubscriptionTargetMap.remove(tag);
        throw e;
    }
    return tag;
}
Also used : AMQInvalidArgumentException(org.apache.qpid.server.filter.AMQInvalidArgumentException) ConsumerOption(org.apache.qpid.server.consumer.ConsumerOption) MessageSource(org.apache.qpid.server.message.MessageSource) AccessControlException(java.security.AccessControlException) FilterManager(org.apache.qpid.server.filter.FilterManager) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) MessageFilter(org.apache.qpid.server.filter.MessageFilter) ArrivalTimeFilter(org.apache.qpid.server.filter.ArrivalTimeFilter)

Aggregations

AMQInvalidArgumentException (org.apache.qpid.server.filter.AMQInvalidArgumentException)8 AccessControlException (java.security.AccessControlException)5 AbstractConfiguredObject (org.apache.qpid.server.model.AbstractConfiguredObject)5 NamedAddressSpace (org.apache.qpid.server.model.NamedAddressSpace)4 Collection (java.util.Collection)3 MessageSource (org.apache.qpid.server.message.MessageSource)3 Queue (org.apache.qpid.server.model.Queue)3 ConsumerOption (org.apache.qpid.server.consumer.ConsumerOption)2 ArrivalTimeFilter (org.apache.qpid.server.filter.ArrivalTimeFilter)2 FilterManager (org.apache.qpid.server.filter.FilterManager)2 MessageFilter (org.apache.qpid.server.filter.MessageFilter)2 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)2 Exchange (org.apache.qpid.server.model.Exchange)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)1 TopicExchangeResult (org.apache.qpid.server.exchange.topic.TopicExchangeResult)1