com.microsoft.azure.servicebus.primitives.RequestResponseUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-servicebus Show documentation
Show all versions of azure-servicebus Show documentation
Java library for Azure Service Bus
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.azure.servicebus.primitives;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.microsoft.azure.servicebus.rules.CorrelationFilter;
import com.microsoft.azure.servicebus.rules.FalseFilter;
import com.microsoft.azure.servicebus.rules.Filter;
import com.microsoft.azure.servicebus.rules.RuleAction;
import com.microsoft.azure.servicebus.rules.RuleDescription;
import com.microsoft.azure.servicebus.rules.SqlFilter;
import com.microsoft.azure.servicebus.rules.SqlRuleAction;
import com.microsoft.azure.servicebus.rules.TrueFilter;
import org.apache.qpid.proton.amqp.DescribedType;
import org.apache.qpid.proton.amqp.Symbol;
import org.apache.qpid.proton.amqp.messaging.AmqpValue;
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
import org.apache.qpid.proton.amqp.transport.ErrorCondition;
import org.apache.qpid.proton.message.Message;
public class RequestResponseUtils {
public static Message createRequestMessageFromPropertyBag(String operation, Map propertyBag, Duration timeout) {
return createRequestMessageFromPropertyBag(operation, propertyBag, timeout, null);
}
public static Message createRequestMessageFromPropertyBag(String operation, Map propertyBag, Duration timeout, String associatedLinkName) {
return createRequestMessageFromValueBody(operation, propertyBag, timeout, associatedLinkName);
}
public static Message createRequestMessageFromValueBody(String operation, Object valueBody, Duration timeout) {
return createRequestMessageFromValueBody(operation, valueBody, timeout, null);
}
private static Message createRequestMessageFromValueBody(String operation, Object valueBody, Duration timeout, String associatedLinkName) {
Message requestMessage = Message.Factory.create();
requestMessage.setBody(new AmqpValue(valueBody));
HashMap applicationPropertiesMap = new HashMap();
applicationPropertiesMap.put(ClientConstants.REQUEST_RESPONSE_OPERATION_NAME, operation);
applicationPropertiesMap.put(ClientConstants.REQUEST_RESPONSE_TIMEOUT, timeout.toMillis());
if (!StringUtil.isNullOrEmpty(associatedLinkName)) {
applicationPropertiesMap.put(ClientConstants.REQUEST_RESPONSE_ASSOCIATED_LINK_NAME, associatedLinkName);
}
requestMessage.setApplicationProperties(new ApplicationProperties(applicationPropertiesMap));
return requestMessage;
}
public static int getResponseStatusCode(Message responseMessage) {
int statusCode = ClientConstants.REQUEST_RESPONSE_UNDEFINED_STATUS_CODE;
Object codeObject = responseMessage.getApplicationProperties().getValue().get(ClientConstants.REQUEST_RESPONSE_STATUS_CODE);
if (codeObject == null) {
codeObject = responseMessage.getApplicationProperties().getValue().get(ClientConstants.REQUEST_RESPONSE_LEGACY_STATUS_CODE);
}
if (codeObject != null) {
statusCode = (int) codeObject;
}
return statusCode;
}
public static Symbol getResponseErrorCondition(Message responseMessage) {
Symbol errorCondition = (Symbol) responseMessage.getApplicationProperties().getValue().get(ClientConstants.REQUEST_RESPONSE_ERROR_CONDITION);
if (errorCondition == null) {
errorCondition = (Symbol) responseMessage.getApplicationProperties().getValue().get(ClientConstants.REQUEST_RESPONSE_LEGACY_ERROR_CONDITION);
}
return errorCondition;
}
public static String getResponseStatusDescription(Message responseMessage) {
String statusDescription = (String) responseMessage.getApplicationProperties().getValue().get(ClientConstants.REQUEST_RESPONSE_STATUS_DESCRIPTION);
if (statusDescription == null) {
statusDescription = (String) responseMessage.getApplicationProperties().getValue().get(ClientConstants.REQUEST_RESPONSE_LEGACY_STATUS_DESCRIPTION);
}
return statusDescription;
}
public static Map getResponseBody(Message responseMessage) {
return (Map) ((AmqpValue) responseMessage.getBody()).getValue();
}
public static Exception genereateExceptionFromResponse(Message responseMessage) {
Symbol errorCondition = getResponseErrorCondition(responseMessage);
Object statusDescription = getResponseStatusDescription(responseMessage);
return generateExceptionFromError(errorCondition, statusDescription == null ? errorCondition.toString() : (String) statusDescription);
}
public static Exception generateExceptionFromError(Symbol errorCondition, String exceptionMessage) {
return ExceptionUtil.toException(new ErrorCondition(errorCondition, exceptionMessage));
}
public static Map encodeRuleDescriptionToMap(RuleDescription ruleDescription) {
HashMap descriptionMap = new HashMap<>();
if (ruleDescription.getFilter() instanceof SqlFilter) {
HashMap filterMap = new HashMap<>();
filterMap.put(ClientConstants.REQUEST_RESPONSE_EXPRESSION, ((SqlFilter) ruleDescription.getFilter()).getSqlExpression());
descriptionMap.put(ClientConstants.REQUEST_RESPONSE_SQLFILTER, filterMap);
} else if (ruleDescription.getFilter() instanceof CorrelationFilter) {
CorrelationFilter correlationFilter = (CorrelationFilter) ruleDescription.getFilter();
HashMap filterMap = new HashMap<>();
filterMap.put(ClientConstants.REQUEST_RESPONSE_CORRELATION_ID, correlationFilter.getCorrelationId());
filterMap.put(ClientConstants.REQUEST_RESPONSE_MESSAGE_ID, correlationFilter.getMessageId());
filterMap.put(ClientConstants.REQUEST_RESPONSE_TO, correlationFilter.getTo());
filterMap.put(ClientConstants.REQUEST_RESPONSE_REPLY_TO, correlationFilter.getReplyTo());
filterMap.put(ClientConstants.REQUEST_RESPONSE_LABEL, correlationFilter.getLabel());
filterMap.put(ClientConstants.REQUEST_RESPONSE_SESSION_ID, correlationFilter.getSessionId());
filterMap.put(ClientConstants.REQUEST_RESPONSE_REPLY_TO_SESSION_ID, correlationFilter.getReplyToSessionId());
filterMap.put(ClientConstants.REQUEST_RESPONSE_CONTENT_TYPE, correlationFilter.getContentType());
filterMap.put(ClientConstants.REQUEST_RESPONSE_CORRELATION_FILTER_PROPERTIES, correlationFilter.getProperties());
descriptionMap.put(ClientConstants.REQUEST_RESPONSE_CORRELATION_FILTER, filterMap);
} else {
throw new IllegalArgumentException("This API supports the addition of only SQLFilters and CorrelationFilters.");
}
if (ruleDescription.getAction() == null) {
descriptionMap.put(ClientConstants.REQUEST_RESPONSE_SQLRULEACTION, null);
} else if (ruleDescription.getAction() instanceof SqlRuleAction) {
HashMap sqlActionMap = new HashMap<>();
sqlActionMap.put(ClientConstants.REQUEST_RESPONSE_EXPRESSION, ((SqlRuleAction) ruleDescription.getAction()).getSqlExpression());
descriptionMap.put(ClientConstants.REQUEST_RESPONSE_SQLRULEACTION, sqlActionMap);
} else {
throw new IllegalArgumentException("This API supports the addition of only filters with SqlRuleActions.");
}
descriptionMap.put(ClientConstants.REQUEST_RESPONSE_RULENAME, ruleDescription.getName());
return descriptionMap;
}
static RuleDescription decodeRuleDescriptionMap(DescribedType ruleDescribedType) {
if (ruleDescribedType == null) {
return null;
}
if (!(ruleDescribedType.getDescriptor()).equals(ClientConstants.RULE_DESCRIPTION_DESCRIPTOR)) {
return null;
}
RuleDescription ruleDescription = new RuleDescription();
if (ruleDescribedType.getDescribed() instanceof ArrayList) {
ArrayList
© 2015 - 2024 Weber Informatics LLC | Privacy Policy