use of org.apache.qpid.server.model.NamedAddressSpace 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());
}
}
}
}
use of org.apache.qpid.server.model.NamedAddressSpace in project qpid-broker-j by apache.
the class AMQPConnection_1_0Impl method endpointClosed.
private void endpointClosed() {
try {
performDeleteTasks();
closeReceived();
} finally {
NamedAddressSpace virtualHost = getAddressSpace();
if (virtualHost != null) {
virtualHost.deregisterConnection(this);
}
}
}
use of org.apache.qpid.server.model.NamedAddressSpace in project qpid-broker-j by apache.
the class AMQPConnection_1_0Impl method receiveOpen.
@Override
public void receiveOpen(final int channel, final Open open) {
assertState(ConnectionState.AWAIT_OPEN);
int channelMax = getPort().getSessionCountLimit() - 1;
_channelMax = open.getChannelMax() == null ? channelMax : open.getChannelMax().intValue() < channelMax ? open.getChannelMax().intValue() : channelMax;
if (_receivingSessions == null) {
_receivingSessions = new Session_1_0[_channelMax + 1];
_sendingSessions = new Session_1_0[_channelMax + 1];
}
_maxFrameSize = open.getMaxFrameSize() == null || open.getMaxFrameSize().longValue() > getBroker().getNetworkBufferSize() ? getBroker().getNetworkBufferSize() : open.getMaxFrameSize().intValue();
_remoteContainerId = open.getContainerId();
if (open.getHostname() != null && !"".equals(open.getHostname().trim())) {
_localHostname = open.getHostname();
}
if (_localHostname == null || "".equals(_localHostname.trim()) && getNetwork().getSelectedHost() != null) {
_localHostname = getNetwork().getSelectedHost();
}
if (open.getIdleTimeOut() != null) {
_outgoingIdleTimeout = open.getIdleTimeOut().longValue();
}
final Map<Symbol, Object> remoteProperties = open.getProperties() == null ? Collections.emptyMap() : Collections.unmodifiableMap(new LinkedHashMap<>(open.getProperties()));
_remoteDesiredCapabilities = open.getDesiredCapabilities() == null ? Collections.emptySet() : Sets.newHashSet(open.getDesiredCapabilities());
if (remoteProperties.containsKey(Symbol.valueOf("product"))) {
setClientProduct(remoteProperties.get(Symbol.valueOf("product")).toString());
}
if (remoteProperties.containsKey(Symbol.valueOf("version"))) {
setClientVersion(remoteProperties.get(Symbol.valueOf("version")).toString());
}
setClientId(_remoteContainerId);
if (_remoteDesiredCapabilities.contains(SoleConnectionConnectionProperties.SOLE_CONNECTION_FOR_CONTAINER)) {
if (remoteProperties != null && remoteProperties.containsKey(SOLE_CONNECTION_ENFORCEMENT_POLICY)) {
try {
_soleConnectionEnforcementPolicy = SoleConnectionEnforcementPolicy.valueOf(remoteProperties.get(SOLE_CONNECTION_ENFORCEMENT_POLICY));
} catch (IllegalArgumentException e) {
closeConnection(AmqpError.INVALID_FIELD, e.getMessage());
return;
}
} else {
_soleConnectionEnforcementPolicy = SoleConnectionEnforcementPolicy.REFUSE_CONNECTION;
}
}
if (_outgoingIdleTimeout != 0L && _outgoingIdleTimeout < MINIMUM_SUPPORTED_IDLE_TIMEOUT) {
closeConnection(ConnectionError.CONNECTION_FORCED, "Requested idle timeout of " + _outgoingIdleTimeout + " is too low. The minimum supported timeout is" + MINIMUM_SUPPORTED_IDLE_TIMEOUT);
} else {
initialiseHeartbeating(_outgoingIdleTimeout / 2L, _incomingIdleTimeout);
final NamedAddressSpace addressSpace = getPort().getAddressSpace(_localHostname);
if (addressSpace == null) {
closeConnection(AmqpError.NOT_FOUND, "Unknown hostname in connection open: '" + _localHostname + "'");
} else {
receiveOpenInternal(addressSpace);
}
}
}
use of org.apache.qpid.server.model.NamedAddressSpace in project qpid-broker-j by apache.
the class AMQPConnection_1_0Impl method receiveOpenInternal.
private void receiveOpenInternal(final NamedAddressSpace addressSpace) {
if (!addressSpace.isActive()) {
final Error err = new Error();
err.setCondition(AmqpError.NOT_FOUND);
populateConnectionRedirect(addressSpace, err);
closeConnection(err);
} else {
if (AuthenticatedPrincipal.getOptionalAuthenticatedPrincipalFromSubject(getSubject()) == null) {
closeConnection(AmqpError.NOT_ALLOWED, "Connection has not been authenticated");
} else {
try {
boolean registerSucceeded = addressSpace.registerConnection(this, (existingConnections, newConnection) -> {
boolean proceedWithRegistration = true;
if (newConnection instanceof AMQPConnection_1_0Impl && !newConnection.isClosing()) {
List<ListenableFuture<Void>> rescheduleFutures = new ArrayList<>();
for (AMQPConnection<?> existingConnection : StreamSupport.stream(existingConnections.spliterator(), false).filter(con -> con instanceof AMQPConnection_1_0).filter(con -> !con.isClosing()).filter(con -> con.getRemoteContainerName().equals(newConnection.getRemoteContainerName())).collect(Collectors.toList())) {
SoleConnectionEnforcementPolicy soleConnectionEnforcementPolicy = null;
if (((AMQPConnection_1_0Impl) existingConnection)._soleConnectionEnforcementPolicy != null) {
soleConnectionEnforcementPolicy = ((AMQPConnection_1_0Impl) existingConnection)._soleConnectionEnforcementPolicy;
} else if (((AMQPConnection_1_0Impl) newConnection)._soleConnectionEnforcementPolicy != null) {
soleConnectionEnforcementPolicy = ((AMQPConnection_1_0Impl) newConnection)._soleConnectionEnforcementPolicy;
}
if (SoleConnectionEnforcementPolicy.REFUSE_CONNECTION.equals(soleConnectionEnforcementPolicy)) {
_properties.put(Symbol.valueOf("amqp:connection-establishment-failed"), true);
Error error = new Error(AmqpError.INVALID_FIELD, String.format("Connection closed due to sole-connection-enforcement-policy '%s'", soleConnectionEnforcementPolicy.toString()));
error.setInfo(Collections.singletonMap(Symbol.valueOf("invalid-field"), Symbol.valueOf("container-id")));
newConnection.doOnIOThreadAsync(() -> ((AMQPConnection_1_0Impl) newConnection).closeConnection(error));
proceedWithRegistration = false;
break;
} else if (SoleConnectionEnforcementPolicy.CLOSE_EXISTING.equals(soleConnectionEnforcementPolicy)) {
final Error error = new Error(AmqpError.RESOURCE_LOCKED, String.format("Connection closed due to sole-connection-enforcement-policy '%s'", soleConnectionEnforcementPolicy.toString()));
error.setInfo(Collections.singletonMap(Symbol.valueOf("sole-connection-enforcement"), true));
rescheduleFutures.add(existingConnection.doOnIOThreadAsync(() -> ((AMQPConnection_1_0Impl) existingConnection).closeConnection(error)));
proceedWithRegistration = false;
}
}
if (!rescheduleFutures.isEmpty()) {
doAfter(allAsList(rescheduleFutures), () -> newConnection.doOnIOThreadAsync(() -> receiveOpenInternal(addressSpace)));
}
}
return proceedWithRegistration;
});
if (registerSucceeded) {
setAddressSpace(addressSpace);
if (!addressSpace.authoriseCreateConnection(this)) {
closeConnection(AmqpError.NOT_ALLOWED, "Connection refused");
} else {
switch(_connectionState) {
case AWAIT_OPEN:
sendOpen(_channelMax, _maxFrameSize);
_connectionState = ConnectionState.OPENED;
break;
case CLOSE_SENT:
case CLOSED:
// already sent our close - probably due to an error
break;
default:
throw new ConnectionScopedRuntimeException(String.format("Unexpected state %s during connection open.", _connectionState));
}
}
}
} catch (VirtualHostUnavailableException | AccessControlException e) {
closeConnection(AmqpError.NOT_ALLOWED, e.getMessage());
}
}
}
}
use of org.apache.qpid.server.model.NamedAddressSpace in project qpid-broker-j by apache.
the class AMQChannel method receiveQueuePurge.
@Override
public void receiveQueuePurge(final AMQShortString queueName, final boolean nowait) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("RECV[" + _channelId + "] QueuePurge[" + " queue: " + queueName + " nowait: " + nowait + " ]");
}
NamedAddressSpace virtualHost = _connection.getAddressSpace();
Queue<?> queue = null;
if (queueName == null && (queue = getDefaultQueue()) == null) {
_connection.sendConnectionClose(ErrorCodes.NOT_ALLOWED, "No queue specified.", getChannelId());
} else if ((queueName != null) && (queue = getQueue(queueName.toString())) == null) {
closeChannel(ErrorCodes.NOT_FOUND, "Queue '" + queueName + "' does not exist.");
} else if (!queue.verifySessionAccess(this)) {
_connection.sendConnectionClose(ErrorCodes.NOT_ALLOWED, "Queue is exclusive, but not created on this Connection.", getChannelId());
} else {
try {
long purged = queue.clearQueue();
if (!nowait) {
sync();
MethodRegistry methodRegistry = _connection.getMethodRegistry();
AMQMethodBody responseBody = methodRegistry.createQueuePurgeOkBody(purged);
_connection.writeFrame(responseBody.generateFrame(getChannelId()));
}
} catch (AccessControlException e) {
_connection.sendConnectionClose(ErrorCodes.ACCESS_REFUSED, e.getMessage(), getChannelId());
}
}
}
Aggregations