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());
}
}
}
}
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());
}
}
}
}
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());
}
}
}
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());
}
}
}
}
}
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());
}
}
}
}
}
Aggregations