use of org.apache.qpid.server.model.Exchange in project qpid-broker-j by apache.
the class PropertyConverter_1_0_to_0_8Test method testReplyToConversionWhenExchangeIsSpecified.
@Test
public void testReplyToConversionWhenExchangeIsSpecified() throws IOException {
final String replyTo = "myTestExchange";
final Exchange exchange = mock(Exchange.class);
when(exchange.getName()).thenReturn(replyTo);
when(exchange.getType()).thenReturn(ExchangeDefaults.FANOUT_EXCHANGE_CLASS);
doReturn(exchange).when(_namedAddressSpace).getAttainedMessageDestination(eq(replyTo), anyBoolean());
Properties properties = new Properties();
properties.setReplyTo(replyTo);
Message_1_0 message = createTestMessage(properties);
final AMQMessage convertedMessage = _messageConverter.convert(message, _namedAddressSpace);
BasicContentHeaderProperties convertedProperties = convertedMessage.getContentHeaderBody().getProperties();
assertEquals("Unexpected reply-to", "fanout://" + replyTo + "//", convertedProperties.getReplyToAsString());
}
use of org.apache.qpid.server.model.Exchange in project qpid-broker-j by apache.
the class PropertyConverter_1_0_to_0_10Test method testReplyToConversionWhenExchangeAndRoutingKeyAreSpecified.
@Test
public void testReplyToConversionWhenExchangeAndRoutingKeyAreSpecified() throws IOException {
final String exchangeName = "testExchnageName";
final String routingKey = "testRoutingKey";
final String replyTo = String.format("%s/%s", exchangeName, routingKey);
final Exchange exchange = mock(Exchange.class);
when(exchange.getName()).thenReturn(exchangeName);
doReturn(exchange).when(_namedAddressSpace).getAttainedMessageDestination(eq(exchangeName), anyBoolean());
Properties properties = new Properties();
properties.setReplyTo(replyTo);
Message_1_0 message = createTestMessage(properties);
MessageTransferMessage convertedMessage = _messageConverter.convert(message, _namedAddressSpace);
final ReplyTo convertedReplyTo = convertedMessage.getHeader().getMessageProperties().getReplyTo();
assertEquals("Unexpected exchange", exchangeName, convertedReplyTo.getExchange());
assertEquals("Unexpected routing key", routingKey, convertedReplyTo.getRoutingKey());
}
use of org.apache.qpid.server.model.Exchange in project qpid-broker-j by apache.
the class MessageConverter_1_0_to_v0_10 method getReplyTo.
private ReplyTo getReplyTo(final NamedAddressSpace addressSpace, final String origReplyTo) {
DestinationAddress destinationAddress = new DestinationAddress(addressSpace, origReplyTo);
MessageDestination messageDestination = destinationAddress.getMessageDestination();
return new ReplyTo(ensureStr8("reply-to[\"exchange\"]", messageDestination instanceof Exchange ? messageDestination.getName() : ""), ensureStr8("reply-to[\"routing-key\"]", messageDestination instanceof Queue ? messageDestination.getName() : destinationAddress.getRoutingKey()));
}
use of org.apache.qpid.server.model.Exchange 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 (!AMQShortString.createAMQShortString(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));
}
validateAndSanitizeExchangeDeclareArguments(attributes);
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 destination '%s'", e.getAlternateBindingName());
_connection.sendConnectionClose(ErrorCodes.NOT_FOUND, message, getChannelId());
} catch (IllegalArgumentException | IllegalConfigurationException e) {
_connection.sendConnectionClose(ErrorCodes.INVALID_ARGUMENT, "Error creating exchange '" + exchangeName + "': " + e.getMessage(), getChannelId());
}
}
}
}
use of org.apache.qpid.server.model.Exchange in project qpid-broker-j by apache.
the class AMQChannel method validateAndSanitizeExchangeDeclareArguments.
private void validateAndSanitizeExchangeDeclareArguments(final Map<String, Object> attributes) {
final ConfiguredObjectTypeRegistry typeRegistry = getModel().getTypeRegistry();
final List<ConfiguredObjectAttribute<?, ?>> types = new ArrayList<>(typeRegistry.getAttributeTypes(Exchange.class).values());
typeRegistry.getTypeSpecialisations(Exchange.class).forEach(type -> types.addAll(typeRegistry.getTypeSpecificAttributes(type)));
final Set<String> unsupported = attributes.keySet().stream().filter(name -> types.stream().noneMatch(a -> Objects.equals(name, a.getName()) && !a.isDerived())).collect(Collectors.toSet());
if (!unsupported.isEmpty()) {
Exchange.BehaviourOnUnknownDeclareArgument unknownArgumentBehaviour = getConnection().getContextValue(Exchange.BehaviourOnUnknownDeclareArgument.class, Exchange.UNKNOWN_EXCHANGE_DECLARE_ARGUMENT_BEHAVIOUR_NAME);
switch(unknownArgumentBehaviour) {
case LOG:
LOGGER.warn("Unsupported exchange declare arguments : {}", String.join(",", unsupported));
// fall through
case IGNORE:
attributes.keySet().removeAll(unsupported);
break;
case FAIL:
default:
throw new IllegalArgumentException(String.format("Unsupported exchange declare arguments : %s", String.join(",", unsupported)));
}
}
}
Aggregations