use of org.apache.qpid.server.message.MessageDestination in project qpid-broker-j by apache.
the class MessageConverter_1_0_to_v0_10 method setExchangeAndRoutingKeyOnDeliveryProperties.
private void setExchangeAndRoutingKeyOnDeliveryProperties(final DeliveryProperties deliveryProps, final MessageMetaData_1_0.MessageHeader_1_0 origHeader, final NamedAddressSpace addressSpace) {
final String to = origHeader.getTo();
final String subject = origHeader.getSubject() == null ? "" : origHeader.getSubject();
final String exchangeName;
final String routingKey;
if (to != null && !"".equals(to)) {
DestinationAddress destinationAddress = new DestinationAddress(addressSpace, to);
MessageDestination messageDestination = destinationAddress.getMessageDestination();
if (messageDestination instanceof Queue) {
exchangeName = "";
routingKey = messageDestination.getName();
} else if (messageDestination instanceof Exchange) {
exchangeName = messageDestination.getName();
routingKey = "".equals(destinationAddress.getRoutingKey()) ? subject : destinationAddress.getRoutingKey();
} else {
exchangeName = "";
routingKey = to;
}
} else {
exchangeName = "";
routingKey = subject;
}
deliveryProps.setRoutingKey(ensureStr8("to' or 'subject", routingKey));
deliveryProps.setExchange(ensureStr8("to", exchangeName));
}
use of org.apache.qpid.server.message.MessageDestination in project qpid-broker-j by apache.
the class AbstractVirtualHost method publishMessage.
@Override
public int publishMessage(@Param(name = "message") final ManageableMessage message) {
final String address = message.getAddress();
MessageDestination destination = address == null ? getDefaultDestination() : getAttainedMessageDestination(address);
if (destination == null) {
destination = getDefaultDestination();
}
final AMQMessageHeader header = new MessageHeaderImpl(message);
Serializable body = null;
Object messageContent = message.getContent();
if (messageContent != null) {
if (messageContent instanceof Map || messageContent instanceof List) {
if (message.getMimeType() != null || message.getEncoding() != null) {
throw new IllegalArgumentException("If the message content is provided as map or list, the mime type and encoding must be left unset");
}
body = (Serializable) messageContent;
} else if (messageContent instanceof String) {
String contentTransferEncoding = message.getContentTransferEncoding();
if ("base64".equalsIgnoreCase(contentTransferEncoding)) {
body = Strings.decodeBase64((String) messageContent);
} else if (contentTransferEncoding == null || contentTransferEncoding.trim().equals("") || contentTransferEncoding.trim().equalsIgnoreCase("identity")) {
String mimeType = message.getMimeType();
if (mimeType != null && !(mimeType = mimeType.trim().toLowerCase()).equals("")) {
if (!(mimeType.startsWith("text/") || Arrays.asList("application/json", "application/xml").contains(mimeType))) {
throw new IllegalArgumentException(message.getMimeType() + " is invalid as a MIME type for this message. " + "Only MIME types of the text type can be used if a string is supplied as the content");
} else if (mimeType.matches(".*;\\s*charset\\s*=.*")) {
throw new IllegalArgumentException(message.getMimeType() + " is invalid as a MIME type for this message. " + "If a string is supplied as the content, the MIME type must not include a charset parameter");
}
}
body = (String) messageContent;
} else {
throw new IllegalArgumentException("contentTransferEncoding value '" + contentTransferEncoding + "' is invalid. The only valid values are base64 and identity");
}
} else {
throw new IllegalArgumentException("The message content (if present) can only be a string, map or list");
}
}
InternalMessage internalMessage = InternalMessage.createMessage(getMessageStore(), header, body, message.isPersistent(), address);
AutoCommitTransaction txn = new AutoCommitTransaction(getMessageStore());
final InstanceProperties instanceProperties = new InstanceProperties() {
@Override
public Object getProperty(final Property prop) {
switch(prop) {
case EXPIRATION:
Date expiration = message.getExpiration();
return expiration == null ? 0 : expiration.getTime();
case IMMEDIATE:
return false;
case PERSISTENT:
return message.isPersistent();
case MANDATORY:
return false;
case REDELIVERED:
return false;
default:
return null;
}
}
};
final RoutingResult<InternalMessage> result = destination.route(internalMessage, address, instanceProperties);
return result.send(txn, null);
}
use of org.apache.qpid.server.message.MessageDestination in project qpid-broker-j by apache.
the class AbstractExchange method performDelete.
private void performDelete() {
if (_closed.compareAndSet(false, true)) {
performDeleteTasks();
for (Binding b : _bindings) {
final MessageDestination messageDestination = getAttainedMessageDestination(b.getDestination());
if (messageDestination != null) {
messageDestination.linkRemoved(this, b);
}
}
for (MessageSender sender : _linkedSenders.keySet()) {
sender.destinationRemoved(this);
}
if (_alternateBindingDestination != null) {
_alternateBindingDestination.removeReference(AbstractExchange.this);
}
getEventLogger().message(_logSubject, ExchangeMessages.DELETED());
}
}
use of org.apache.qpid.server.message.MessageDestination 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.message.MessageDestination in project qpid-broker-j by apache.
the class AbstractExchange method onOpen.
@Override
protected void onOpen() {
super.onOpen();
final ConfiguredDerivedMethodAttribute<Exchange<?>, Collection<Binding>> durableBindingsAttribute = (ConfiguredDerivedMethodAttribute<Exchange<?>, Collection<Binding>>) getModel().getTypeRegistry().getAttributeTypes(getTypeClass()).get(DURABLE_BINDINGS);
final Collection<Binding> bindings = durableBindingsAttribute.convertValue(getActualAttributes().get(DURABLE_BINDINGS), this);
if (bindings != null) {
_bindings.addAll(bindings);
for (Binding b : _bindings) {
final MessageDestination messageDestination = getOpenedMessageDestination(b.getDestination());
if (messageDestination != null) {
Map<String, Object> arguments = b.getArguments() == null ? Collections.emptyMap() : b.getArguments();
onBind(new BindingIdentifier(b.getBindingKey(), messageDestination), arguments);
messageDestination.linkAdded(this, b);
}
}
}
if (getLifetimePolicy() == LifetimePolicy.DELETE_ON_CREATING_LINK_CLOSE) {
if (_creatingLinkInfo != null) {
final LinkModel link;
if (_creatingLinkInfo.isSendingLink()) {
link = _virtualHost.getSendingLink(_creatingLinkInfo.getRemoteContainerId(), _creatingLinkInfo.getLinkName());
} else {
link = _virtualHost.getReceivingLink(_creatingLinkInfo.getRemoteContainerId(), _creatingLinkInfo.getLinkName());
}
addLifetimeConstraint(link);
} else {
throw new IllegalArgumentException("Exchanges created with a lifetime policy of " + getLifetimePolicy() + " must be created from a AMQP 1.0 link.");
}
}
if (getAlternateBinding() != null) {
String alternateDestination = getAlternateBinding().getDestination();
_alternateBindingDestination = getOpenedMessageDestination(alternateDestination);
if (_alternateBindingDestination != null) {
_alternateBindingDestination.addReference(this);
} else {
LOGGER.warn("Cannot find alternate binding destination '{}' for exchange '{}'", alternateDestination, toString());
}
}
getEventLogger().message(ExchangeMessages.CREATED(getType(), getName(), isDurable()));
}
Aggregations