Search in sources :

Example 6 with VirtualHostUnavailableException

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

the class ServerSessionDelegate method messageTransfer.

@Override
public void messageTransfer(ServerSession ssn, final MessageTransfer xfr) {
    try {
        if (ssn.blockingTimeoutExceeded()) {
            getEventLogger(ssn).message(ChannelMessages.FLOW_CONTROL_IGNORED());
            ssn.close(ErrorCodes.MESSAGE_TOO_LARGE, "Session flow control was requested, but not enforced by sender");
        } else if (xfr.getBodySize() > ssn.getConnection().getMaxMessageSize()) {
            exception(ssn, xfr, ExecutionErrorCode.RESOURCE_LIMIT_EXCEEDED, "Message size of " + xfr.getBodySize() + " greater than allowed maximum of " + ssn.getConnection().getMaxMessageSize());
        } else {
            final MessageDestination destination = getDestinationForMessage(ssn, xfr);
            final DeliveryProperties delvProps = xfr.getHeader() == null ? null : xfr.getHeader().getDeliveryProperties();
            if (delvProps != null && delvProps.hasTtl() && !delvProps.hasExpiration()) {
                delvProps.setExpiration(System.currentTimeMillis() + delvProps.getTtl());
            }
            final MessageMetaData_0_10 messageMetaData = new MessageMetaData_0_10(xfr);
            final NamedAddressSpace virtualHost = getAddressSpace(ssn);
            try {
                ssn.getAMQPConnection().checkAuthorizedMessagePrincipal(getMessageUserId(xfr));
                ssn.authorisePublish(destination, messageMetaData.getRoutingKey(), messageMetaData.isImmediate(), ssn.getAMQPConnection().getLastReadTime());
            } catch (AccessControlException e) {
                ExecutionErrorCode errorCode = ExecutionErrorCode.UNAUTHORIZED_ACCESS;
                exception(ssn, xfr, errorCode, e.getMessage());
                return;
            }
            final MessageStore store = virtualHost.getMessageStore();
            final StoredMessage<MessageMetaData_0_10> storeMessage = createStoreMessage(xfr, messageMetaData, store);
            final MessageTransferMessage message = new MessageTransferMessage(storeMessage, ssn.getReference());
            MessageReference<MessageTransferMessage> reference = message.newReference();
            try {
                final InstanceProperties instanceProperties = new InstanceProperties() {

                    @Override
                    public Object getProperty(final Property prop) {
                        switch(prop) {
                            case EXPIRATION:
                                return message.getExpiration();
                            case IMMEDIATE:
                                return message.isImmediate();
                            case MANDATORY:
                                return (delvProps == null || !delvProps.getDiscardUnroutable()) && xfr.getAcceptMode() == MessageAcceptMode.EXPLICIT;
                            case PERSISTENT:
                                return message.isPersistent();
                            case REDELIVERED:
                                return delvProps.getRedelivered();
                        }
                        return null;
                    }
                };
                RoutingResult<MessageTransferMessage> routingResult = ssn.enqueue(message, instanceProperties, destination);
                boolean explictlyRejected = routingResult.containsReject(RejectType.LIMIT_EXCEEDED);
                if (!routingResult.hasRoutes() || explictlyRejected) {
                    boolean closeWhenNoRoute = ssn.getAMQPConnection().getPort().getCloseWhenNoRoute();
                    boolean discardUnroutable = delvProps != null && delvProps.getDiscardUnroutable();
                    if (!discardUnroutable && xfr.getAcceptMode() == MessageAcceptMode.EXPLICIT) {
                        RangeSet rejects = RangeSetFactory.createRangeSet();
                        rejects.add(xfr.getId());
                        MessageReject reject = new MessageReject(rejects, MessageRejectCode.UNROUTABLE, "Unroutable");
                        ssn.invoke(reject);
                    } else if (!discardUnroutable && closeWhenNoRoute && explictlyRejected) {
                        ExecutionErrorCode code = ExecutionErrorCode.RESOURCE_LIMIT_EXCEEDED;
                        String errorMessage = String.format("No route for message with destination '%s' and routing key '%s' : %s", xfr.getDestination(), message.getInitialRoutingAddress(), routingResult.getRejectReason());
                        ExecutionException ex = new ExecutionException();
                        ex.setErrorCode(code);
                        ex.setDescription(errorMessage);
                        ssn.invoke(ex);
                        ssn.close(ErrorCodes.RESOURCE_ERROR, errorMessage);
                        return;
                    } else {
                        getEventLogger(ssn).message(ExchangeMessages.DISCARDMSG(destination.getName(), messageMetaData.getRoutingKey()));
                    }
                }
                // TODO: we currently do not send MessageAccept when AcceptMode is EXPLICIT
                if (ssn.isTransactional()) {
                    ssn.processed(xfr);
                } else {
                    ssn.recordFuture(Futures.immediateFuture(null), new CommandProcessedAction(ssn, xfr));
                }
            } catch (VirtualHostUnavailableException e) {
                getServerConnection(ssn).sendConnectionCloseAsync(ConnectionCloseCode.CONNECTION_FORCED, e.getMessage());
            } finally {
                reference.release();
            }
        }
    } finally {
        xfr.dispose();
    }
}
Also used : MessageStore(org.apache.qpid.server.store.MessageStore) MessageDestination(org.apache.qpid.server.message.MessageDestination) InstanceProperties(org.apache.qpid.server.message.InstanceProperties) NamedAddressSpace(org.apache.qpid.server.model.NamedAddressSpace) AccessControlException(java.security.AccessControlException) MessageReference(org.apache.qpid.server.message.MessageReference) RoutingResult(org.apache.qpid.server.message.RoutingResult) VirtualHostUnavailableException(org.apache.qpid.server.virtualhost.VirtualHostUnavailableException) StoredMessage(org.apache.qpid.server.store.StoredMessage) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject)

Aggregations

VirtualHostUnavailableException (org.apache.qpid.server.virtualhost.VirtualHostUnavailableException)6 AccessControlException (java.security.AccessControlException)4 NamedAddressSpace (org.apache.qpid.server.model.NamedAddressSpace)4 AmqpPort (org.apache.qpid.server.model.port.AmqpPort)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 MessageDestination (org.apache.qpid.server.message.MessageDestination)2 RoutingResult (org.apache.qpid.server.message.RoutingResult)2 Iterators (com.google.common.collect.Iterators)1 PeekingIterator (com.google.common.collect.PeekingIterator)1 Sets (com.google.common.collect.Sets)1 Futures.allAsList (com.google.common.util.concurrent.Futures.allAsList)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 SocketAddress (java.net.SocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 AccessControlContext (java.security.AccessControlContext)1 AccessController (java.security.AccessController)1 Principal (java.security.Principal)1 PrivilegedAction (java.security.PrivilegedAction)1 Arrays (java.util.Arrays)1