use of org.apache.qpid.server.virtualhost.VirtualHostUnavailableException in project qpid-broker-j by apache.
the class VirtualHostTest method testRegisterConnectionOnStoppedVirtualhost.
public void testRegisterConnectionOnStoppedVirtualhost() {
QueueManagingVirtualHost<?> vhost = createVirtualHost("sdf");
AMQPConnection<?> connection = getMockConnection();
((AbstractConfiguredObject<?>) vhost).stop();
try {
vhost.registerConnection(connection, new NoopConnectionEstablishmentPolicy());
fail("exception not thrown");
} catch (VirtualHostUnavailableException e) {
// pass
}
assertEquals("unexpected number of connections", 0, vhost.getConnectionCount());
((AbstractConfiguredObject<?>) vhost).start();
vhost.registerConnection(connection, new NoopConnectionEstablishmentPolicy());
assertEquals("unexpected number of connections", 1, vhost.getConnectionCount());
}
use of org.apache.qpid.server.virtualhost.VirtualHostUnavailableException in project qpid-broker-j by apache.
the class AbstractExchange method route.
@Override
public <M extends ServerMessage<? extends StorableMessageMetaData>> RoutingResult<M> route(final M message, final String routingAddress, final InstanceProperties instanceProperties) {
if (_virtualHost.getState() != State.ACTIVE) {
throw new VirtualHostUnavailableException(this._virtualHost);
}
final RoutingResult<M> routingResult = new RoutingResult<>(message);
Map<AbstractExchange<?>, Set<String>> currentThreadMap = CURRENT_ROUTING.get();
boolean topLevel = currentThreadMap == null;
try {
if (topLevel) {
currentThreadMap = new HashMap<>();
CURRENT_ROUTING.set(currentThreadMap);
}
Set<String> existingRoutes = currentThreadMap.get(this);
if (existingRoutes == null) {
currentThreadMap.put(this, Collections.singleton(routingAddress));
} else if (existingRoutes.contains(routingAddress)) {
return routingResult;
} else {
existingRoutes = new HashSet<>(existingRoutes);
existingRoutes.add(routingAddress);
currentThreadMap.put(this, existingRoutes);
}
_receivedMessageCount.incrementAndGet();
_receivedMessageSize.addAndGet(message.getSizeIncludingHeader());
doRoute(message, routingAddress, instanceProperties, routingResult);
if (!routingResult.hasRoutes()) {
MessageDestination alternateBindingDestination = getAlternateBindingDestination();
if (alternateBindingDestination != null) {
routingResult.add(alternateBindingDestination.route(message, routingAddress, instanceProperties));
}
}
if (routingResult.hasRoutes()) {
_routedMessageCount.incrementAndGet();
_routedMessageSize.addAndGet(message.getSizeIncludingHeader());
} else {
_droppedMessageCount.incrementAndGet();
_droppedMessageSize.addAndGet(message.getSizeIncludingHeader());
}
return routingResult;
} finally {
if (topLevel) {
CURRENT_ROUTING.set(null);
}
}
}
use of org.apache.qpid.server.virtualhost.VirtualHostUnavailableException in project qpid-broker-j by apache.
the class ServerConnectionDelegate method connectionOpen.
@Override
public void connectionOpen(ServerConnection sconn, ConnectionOpen open) {
assertState(sconn, ConnectionState.AWAIT_OPEN);
NamedAddressSpace addressSpace;
String vhostName;
if (open.hasVirtualHost()) {
vhostName = open.getVirtualHost();
} else {
vhostName = "";
}
AmqpPort port = sconn.getPort();
addressSpace = port.getAddressSpace(vhostName);
if (addressSpace != null) {
if (!addressSpace.isActive()) {
sconn.setState(ServerConnection.State.CLOSING);
final String redirectHost = addressSpace.getRedirectHost(port);
if (redirectHost == null) {
sconn.sendConnectionClose(ConnectionCloseCode.CONNECTION_FORCED, "Virtual host '" + vhostName + "' is not active");
} else {
sconn.invoke(new ConnectionRedirect(redirectHost, new ArrayList<Object>()));
}
return;
}
try {
sconn.setVirtualHost(addressSpace);
if (!addressSpace.authoriseCreateConnection(sconn.getAmqpConnection())) {
sconn.setState(ServerConnection.State.CLOSING);
sconn.sendConnectionClose(ConnectionCloseCode.CONNECTION_FORCED, "Connection not authorized");
return;
}
} catch (AccessControlException | VirtualHostUnavailableException e) {
sconn.setState(ServerConnection.State.CLOSING);
sconn.sendConnectionClose(ConnectionCloseCode.CONNECTION_FORCED, e.getMessage());
return;
}
sconn.setState(ServerConnection.State.OPEN);
_state = ConnectionState.OPEN;
sconn.invoke(new ConnectionOpenOk(Collections.emptyList()));
} else {
sconn.setState(ServerConnection.State.CLOSING);
sconn.sendConnectionClose(ConnectionCloseCode.INVALID_PATH, "Unknown virtualhost '" + vhostName + "'");
}
}
use of org.apache.qpid.server.virtualhost.VirtualHostUnavailableException 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.virtualhost.VirtualHostUnavailableException in project qpid-broker-j by apache.
the class AMQPConnection_0_8Impl method receiveConnectionOpen.
@Override
public void receiveConnectionOpen(AMQShortString virtualHostName, AMQShortString capabilities, boolean insist) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("RECV ConnectionOpen[" + " virtualHost: " + virtualHostName + " capabilities: " + capabilities + " insist: " + insist + " ]");
}
assertState(ConnectionState.AWAIT_OPEN);
String virtualHostStr = AMQShortString.toString(virtualHostName);
if ((virtualHostStr != null) && virtualHostStr.charAt(0) == '/') {
virtualHostStr = virtualHostStr.substring(1);
}
NamedAddressSpace addressSpace = ((AmqpPort) getPort()).getAddressSpace(virtualHostStr);
if (addressSpace == null) {
sendConnectionClose(ErrorCodes.NOT_FOUND, "Unknown virtual host: '" + virtualHostName + "'", 0);
} else {
// Check virtualhost access
if (!addressSpace.isActive()) {
String redirectHost = addressSpace.getRedirectHost(getPort());
if (redirectHost != null) {
sendConnectionClose(0, new AMQFrame(0, new ConnectionRedirectBody(getProtocolVersion(), AMQShortString.valueOf(redirectHost), null)));
} else {
sendConnectionClose(ErrorCodes.CONNECTION_FORCED, "Virtual host '" + addressSpace.getName() + "' is not active", 0);
}
} else {
try {
addressSpace.registerConnection(this, new NoopConnectionEstablishmentPolicy());
setAddressSpace(addressSpace);
if (addressSpace.authoriseCreateConnection(this)) {
MethodRegistry methodRegistry = getMethodRegistry();
AMQMethodBody responseBody = methodRegistry.createConnectionOpenOkBody(virtualHostName);
writeFrame(responseBody.generateFrame(0));
_state = ConnectionState.OPEN;
} else {
sendConnectionClose(ErrorCodes.ACCESS_REFUSED, "Connection refused", 0);
}
} catch (AccessControlException | VirtualHostUnavailableException e) {
sendConnectionClose(ErrorCodes.ACCESS_REFUSED, e.getMessage(), 0);
}
}
}
}
Aggregations