Search in sources :

Example 1 with RuleDescription

use of com.microsoft.azure.servicebus.rules.RuleDescription in project azure-service-bus-java by Azure.

the class MiscRequestResponseOperationHandler method getRulesAsync.

public CompletableFuture<Collection<RuleDescription>> getRulesAsync(int skip, int top) {
    TRACE_LOGGER.debug("Fetching rules for entity '{}'", this.entityPath);
    return this.createRequestResponseLink().thenComposeAsync((v) -> {
        HashMap requestBodyMap = new HashMap();
        requestBodyMap.put(ClientConstants.REQUEST_RESPONSE_SKIP, skip);
        requestBodyMap.put(ClientConstants.REQUEST_RESPONSE_TOP, top);
        Message requestMessage = RequestResponseUtils.createRequestMessageFromPropertyBag(ClientConstants.REQUEST_RESPONSE_GET_RULES_OPERATION, requestBodyMap, Util.adjustServerTimeout(this.underlyingFactory.getOperationTimeout()));
        CompletableFuture<Message> responseFuture = this.requestResponseLink.requestAysnc(requestMessage, this.underlyingFactory.getOperationTimeout());
        return responseFuture.thenComposeAsync((responseMessage) -> {
            CompletableFuture<Collection<RuleDescription>> returningFuture = new CompletableFuture<>();
            Collection<RuleDescription> rules = new ArrayList<RuleDescription>();
            int statusCode = RequestResponseUtils.getResponseStatusCode(responseMessage);
            if (statusCode == ClientConstants.REQUEST_RESPONSE_OK_STATUS_CODE) {
                Map responseBodyMap = RequestResponseUtils.getResponseBody(responseMessage);
                ArrayList<Map> rulesMap = (ArrayList<Map>) responseBodyMap.get(ClientConstants.REQUEST_RESPONSE_RULES);
                for (Map ruleMap : rulesMap) {
                    DescribedType ruleDescription = (DescribedType) ruleMap.getOrDefault("rule-description", null);
                    rules.add(RequestResponseUtils.decodeRuleDescriptionMap(ruleDescription));
                }
                TRACE_LOGGER.debug("Fetched {} rules from entity '{}'", rules.size(), this.entityPath);
                returningFuture.complete(rules);
            } else if (statusCode == ClientConstants.REQUEST_RESPONSE_NOCONTENT_STATUS_CODE) {
                returningFuture.complete(rules);
            } else {
                // error response
                TRACE_LOGGER.error("Fetching rules for entity '{}' failed with status code '{}'", this.entityPath, statusCode);
                returningFuture.completeExceptionally(RequestResponseUtils.genereateExceptionFromResponse(responseMessage));
            }
            return returningFuture;
        });
    });
}
Also used : RuleDescription(com.microsoft.azure.servicebus.rules.RuleDescription) Message(org.apache.qpid.proton.message.Message) CompletableFuture(java.util.concurrent.CompletableFuture) DescribedType(org.apache.qpid.proton.amqp.DescribedType)

Example 2 with RuleDescription

use of com.microsoft.azure.servicebus.rules.RuleDescription in project azure-service-bus-java by Azure.

the class SubscriptionClientTests method testGetAddRemoveRules.

@Test
public void testGetAddRemoveRules() throws InterruptedException, ServiceBusException {
    this.createClients(ReceiveMode.PEEKLOCK);
    SubscriptionClient subscriptionClient = (SubscriptionClient) this.receiveClient;
    subscriptionClient.removeRule(SubscriptionClient.DEFAULT_RULE_NAME);
    RuleDescription[] rules = subscriptionClient.getRules().toArray(new RuleDescription[0]);
    Assert.assertEquals(0, rules.length);
    // Simple rule
    RuleDescription trueFilterRule = new RuleDescription("customRule1", TrueFilter.DEFAULT);
    subscriptionClient.addRule(trueFilterRule);
    try {
        subscriptionClient.addRule(trueFilterRule);
        Assert.fail("A rule with duplicate name is added.");
    } catch (MessagingEntityAlreadyExistsException e) {
    // Expected
    }
    rules = subscriptionClient.getRules().toArray(new RuleDescription[0]);
    Assert.assertEquals("More than one rules are present", 1, rules.length);
    Assert.assertEquals("Returned rule name doesn't match", trueFilterRule.getName(), rules[0].getName());
    Assert.assertTrue(rules[0].getFilter() instanceof SqlFilter);
    subscriptionClient.removeRule(trueFilterRule.getName());
    // Custom SQL Filter rule
    SqlFilter sqlFilter = new SqlFilter("1=1");
    SqlRuleAction action = new SqlRuleAction("set FilterTag = 'true'");
    RuleDescription sqlRule = new RuleDescription("customRule2", sqlFilter);
    sqlRule.setAction(action);
    subscriptionClient.addRule(sqlRule);
    rules = subscriptionClient.getRules().toArray(new RuleDescription[0]);
    Assert.assertEquals("More than one rules are present", 1, rules.length);
    RuleDescription returnedRule = rules[0];
    Assert.assertEquals("Returned rule name doesn't match", sqlRule.getName(), returnedRule.getName());
    Assert.assertTrue(returnedRule.getFilter() instanceof SqlFilter);
    Assert.assertEquals(sqlFilter.getSqlExpression(), ((SqlFilter) returnedRule.getFilter()).getSqlExpression());
    Assert.assertTrue(returnedRule.getAction() instanceof SqlRuleAction);
    Assert.assertEquals(action.getSqlExpression(), ((SqlRuleAction) returnedRule.getAction()).getSqlExpression());
    subscriptionClient.removeRule(sqlRule.getName());
    // Correlation Filter rule
    CorrelationFilter correlationFilter = new CorrelationFilter();
    correlationFilter.setCorrelationId("TestCorrelationId");
    correlationFilter.setMessageId("TestMessageId");
    correlationFilter.setReplyTo("testReplyTo");
    correlationFilter.setLabel("testLabel");
    correlationFilter.setTo("testTo");
    correlationFilter.setReplyTo("testReplyTo");
    HashMap<String, Object> properties = new HashMap<>();
    properties.put("testKey1", "testValue1");
    properties.put("testKey2", null);
    correlationFilter.setProperties(properties);
    RuleDescription correlationRule = new RuleDescription("customRule3", correlationFilter);
    correlationRule.setAction(action);
    subscriptionClient.addRule(correlationRule);
    rules = subscriptionClient.getRules().toArray(new RuleDescription[0]);
    Assert.assertEquals("More than one rules are present", 1, rules.length);
    returnedRule = rules[0];
    Assert.assertEquals("Returned rule name doesn't match", correlationRule.getName(), returnedRule.getName());
    Assert.assertTrue(returnedRule.getAction() instanceof SqlRuleAction);
    Assert.assertEquals(action.getSqlExpression(), ((SqlRuleAction) returnedRule.getAction()).getSqlExpression());
    Assert.assertTrue(returnedRule.getFilter() instanceof CorrelationFilter);
    CorrelationFilter returnedFilter = (CorrelationFilter) returnedRule.getFilter();
    Assert.assertEquals(correlationFilter.getCorrelationId(), returnedFilter.getCorrelationId());
    Assert.assertEquals(correlationFilter.getMessageId(), returnedFilter.getMessageId());
    Assert.assertEquals(correlationFilter.getReplyTo(), returnedFilter.getReplyTo());
    Assert.assertEquals(correlationFilter.getLabel(), returnedFilter.getLabel());
    Assert.assertEquals(correlationFilter.getTo(), returnedFilter.getTo());
    Assert.assertEquals(correlationFilter.getReplyTo(), returnedFilter.getReplyTo());
    for (Map.Entry<String, Object> entry : properties.entrySet()) {
        Assert.assertTrue(returnedFilter.getProperties().containsKey(entry.getKey()));
        Assert.assertEquals(entry.getValue(), returnedFilter.getProperties().get(entry.getKey()));
    }
    subscriptionClient.removeRule(correlationRule.getName());
    subscriptionClient.addRule(SubscriptionClient.DEFAULT_RULE_NAME, TrueFilter.DEFAULT);
}
Also used : MessagingEntityAlreadyExistsException(com.microsoft.azure.servicebus.primitives.MessagingEntityAlreadyExistsException) RuleDescription(com.microsoft.azure.servicebus.rules.RuleDescription) SqlFilter(com.microsoft.azure.servicebus.rules.SqlFilter) HashMap(java.util.HashMap) CorrelationFilter(com.microsoft.azure.servicebus.rules.CorrelationFilter) SqlRuleAction(com.microsoft.azure.servicebus.rules.SqlRuleAction) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 3 with RuleDescription

use of com.microsoft.azure.servicebus.rules.RuleDescription in project azure-service-bus-java by Azure.

the class SubscriptionClientTests method testGetRulesForMultipleRules.

@Test
public void testGetRulesForMultipleRules() throws InterruptedException, ServiceBusException {
    this.createClients(ReceiveMode.PEEKLOCK);
    SubscriptionClient subscriptionClient = (SubscriptionClient) this.receiveClient;
    RuleDescription trueFilterRule = new RuleDescription("getRules1", TrueFilter.DEFAULT);
    subscriptionClient.addRule(trueFilterRule);
    RuleDescription trueFilterRule2 = new RuleDescription("getRules2", TrueFilter.DEFAULT);
    subscriptionClient.addRule(trueFilterRule2);
    SqlFilter sqlFilter = new SqlFilter("1=1");
    RuleDescription sqlRule = new RuleDescription("getRules3", sqlFilter);
    subscriptionClient.addRule(sqlRule);
    CorrelationFilter correlationFilter = new CorrelationFilter();
    correlationFilter.setCorrelationId("TestCorrelationId");
    RuleDescription correlationRule = new RuleDescription("getRules4", correlationFilter);
    subscriptionClient.addRule(correlationRule);
    Collection<RuleDescription> rules = subscriptionClient.getRules();
    Assert.assertEquals(5, rules.size());
    subscriptionClient.removeRule(trueFilterRule.getName());
    subscriptionClient.removeRule(trueFilterRule2.getName());
    subscriptionClient.removeRule(sqlRule.getName());
    subscriptionClient.removeRule(correlationRule.getName());
}
Also used : RuleDescription(com.microsoft.azure.servicebus.rules.RuleDescription) CorrelationFilter(com.microsoft.azure.servicebus.rules.CorrelationFilter) SqlFilter(com.microsoft.azure.servicebus.rules.SqlFilter) Test(org.junit.Test)

Aggregations

RuleDescription (com.microsoft.azure.servicebus.rules.RuleDescription)3 CorrelationFilter (com.microsoft.azure.servicebus.rules.CorrelationFilter)2 SqlFilter (com.microsoft.azure.servicebus.rules.SqlFilter)2 Test (org.junit.Test)2 MessagingEntityAlreadyExistsException (com.microsoft.azure.servicebus.primitives.MessagingEntityAlreadyExistsException)1 SqlRuleAction (com.microsoft.azure.servicebus.rules.SqlRuleAction)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 DescribedType (org.apache.qpid.proton.amqp.DescribedType)1 Message (org.apache.qpid.proton.message.Message)1