Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.exception.AzureException;
import com.azure.core.exception.ClientAuthenticationException;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.exception.ResourceExistsException;
Expand All @@ -31,6 +32,7 @@
import com.azure.messaging.servicebus.implementation.models.CreateSubscriptionBodyContent;
import com.azure.messaging.servicebus.implementation.models.CreateTopicBody;
import com.azure.messaging.servicebus.implementation.models.CreateTopicBodyContent;
import com.azure.messaging.servicebus.implementation.models.NamespacePropertiesEntry;
import com.azure.messaging.servicebus.implementation.models.QueueDescriptionEntry;
import com.azure.messaging.servicebus.implementation.models.QueueDescriptionFeed;
import com.azure.messaging.servicebus.implementation.models.ResponseLink;
Expand All @@ -40,6 +42,7 @@
import com.azure.messaging.servicebus.implementation.models.SubscriptionDescriptionFeed;
import com.azure.messaging.servicebus.implementation.models.TopicDescriptionEntry;
import com.azure.messaging.servicebus.implementation.models.TopicDescriptionFeed;
import com.azure.messaging.servicebus.models.NamespaceProperties;
import com.azure.messaging.servicebus.models.QueueDescription;
import com.azure.messaging.servicebus.models.QueueRuntimeInfo;
import com.azure.messaging.servicebus.models.SubscriptionDescription;
Expand Down Expand Up @@ -457,6 +460,40 @@ public Mono<Response<QueueDescription>> getQueueWithResponse(String queueName) {
return withContext(context -> getQueueWithResponse(queueName, context, Function.identity()));
}

/**
* Gets whether or not a queue with {@code queueName} exists in the Service Bus namespace.
*
* @param queueName Name of the queue.
*
* @return A Mono that completes indicating whether or not the queue exists.
* @throws ClientAuthenticationException if the client's credentials do not have access to modify the
* namespace.
* @throws HttpResponseException If error occurred processing the request.
* @throws IllegalArgumentException if {@code queueName} is an empty string.
* @throws NullPointerException if {@code queueName} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Boolean> getQueueExists(String queueName) {
return getQueueExistsWithResponse(queueName).map(Response::getValue);
}

/**
* Gets whether or not a queue with {@code queueName} exists in the Service Bus namespace.
*
* @param queueName Name of the queue.
*
* @return A Mono that completes indicating whether or not the queue exists along with its HTTP response.
* @throws ClientAuthenticationException if the client's credentials do not have access to modify the
* namespace.
* @throws HttpResponseException If error occurred processing the request.
* @throws IllegalArgumentException if {@code queueName} is an empty string.
* @throws NullPointerException if {@code queueName} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Boolean>> getQueueExistsWithResponse(String queueName) {
return getEntityExistsWithResponse(getQueueWithResponse(queueName));
}

/**
* Gets runtime information about the queue.
*
Expand Down Expand Up @@ -495,6 +532,31 @@ public Mono<Response<QueueRuntimeInfo>> getQueueRuntimeInfoWithResponse(String q
return withContext(context -> getQueueWithResponse(queueName, context, QueueRuntimeInfo::new));
}

/**
* Gets information about the Service Bus namespace.
*
* @return A Mono that completes with information about the Service Bus namespace.
* @throws ClientAuthenticationException if the client's credentials do not have access to the namespace.
* @throws HttpResponseException If error occurred processing the request.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<NamespaceProperties> getNamespaceProperties() {
return getNamespacePropertiesWithResponse().map(Response::getValue);
}

/**
* Gets information about the Service Bus namespace along with its HTTP response.
*
* @return A Mono that completes with information about the namespace and the associated HTTP response.
* @throws ClientAuthenticationException if the client's credentials do not have access to modify the
* namespace.
* @throws HttpResponseException If error occurred processing the request.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<NamespaceProperties>> getNamespacePropertiesWithResponse() {
return withContext(this::getNamespacePropertiesWithResponse);
}

/**
* Gets information about the queue.
*
Expand Down Expand Up @@ -537,6 +599,42 @@ public Mono<Response<SubscriptionDescription>> getSubscriptionWithResponse(Strin
Function.identity()));
}

/**
* Gets whether or not a subscription within a topic exists.
*
* @param topicName Name of topic associated with subscription.
* @param subscriptionName Name of the subscription.
*
* @return A Mono that completes indicating whether or not the subscription exists.
* @throws ClientAuthenticationException if the client's credentials do not have access to modify the
* namespace.
* @throws HttpResponseException If error occurred processing the request.
* @throws IllegalArgumentException if {@code subscriptionName} is an empty string.
* @throws NullPointerException if {@code subscriptionName} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Boolean> getSubscriptionExists(String topicName, String subscriptionName) {
return getSubscriptionExistsWithResponse(topicName, subscriptionName).map(Response::getValue);
}

/**
* Gets whether or not a subscription within a topic exists.
*
* @param topicName Name of topic associated with subscription.
* @param subscriptionName Name of the subscription.
*
* @return A Mono that completes indicating whether or not the subscription exists along with its HTTP response.
* @throws ClientAuthenticationException if the client's credentials do not have access to modify the
* namespace.
* @throws HttpResponseException If error occurred processing the request.
* @throws IllegalArgumentException if {@code subscriptionName} is an empty string.
* @throws NullPointerException if {@code subscriptionName} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Boolean>> getSubscriptionExistsWithResponse(String topicName, String subscriptionName) {
return getEntityExistsWithResponse(getSubscriptionWithResponse(topicName, subscriptionName));
}

/**
* Gets runtime information about the subscription.
*
Expand Down Expand Up @@ -619,6 +717,40 @@ public Mono<Response<TopicDescription>> getTopicWithResponse(String topicName) {
return withContext(context -> getTopicWithResponse(topicName, context, Function.identity()));
}

/**
* Gets whether or not a topic with {@code topicName} exists in the Service Bus namespace.
*
* @param topicName Name of the topic.
*
* @return A Mono that completes indicating whether or not the topic exists.
* @throws ClientAuthenticationException if the client's credentials do not have access to modify the
* namespace.
* @throws HttpResponseException If error occurred processing the request.
* @throws IllegalArgumentException if {@code topicName} is an empty string.
* @throws NullPointerException if {@code topicName} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Boolean> getTopicExists(String topicName) {
return getTopicExistsWithResponse(topicName).map(Response::getValue);
}

/**
* Gets whether or not a topic with {@code topicName} exists in the Service Bus namespace.
*
* @param topicName Name of the topic.
*
* @return A Mono that completes indicating whether or not the topic exists along with its HTTP response.
* @throws ClientAuthenticationException if the client's credentials do not have access to modify the
* namespace.
* @throws HttpResponseException If error occurred processing the request.
* @throws IllegalArgumentException if {@code topicName} is an empty string.
* @throws NullPointerException if {@code topicName} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Boolean>> getTopicExistsWithResponse(String topicName) {
return getEntityExistsWithResponse(getTopicWithResponse(topicName));
}

/**
* Gets runtime information about the topic.
*
Expand Down Expand Up @@ -1135,6 +1267,31 @@ Mono<Response<Void>> deleteTopicWithResponse(String topicName, Context context)
}
}

/**
* Gets whether an entity exists.
*
* @param getEntityOperation Operation to get information about entity. If {@link ResourceNotFoundException} is
* thrown, then it is mapped to false.
* @param <T> Entity type.
*
* @return True if the entity exists, false otherwise.
*/
<T> Mono<Response<Boolean>> getEntityExistsWithResponse(Mono<Response<T>> getEntityOperation) {
return getEntityOperation.map(response -> {
// When an entity does not exist, it does not have any description object in it.
final boolean exists = response.getValue() != null;
return (Response<Boolean>) new SimpleResponse<>(response.getRequest(), response.getStatusCode(),
response.getHeaders(), exists);
})
.onErrorResume(ResourceNotFoundException.class, exception -> {
final HttpResponse response = exception.getResponse();
final Response<Boolean> result = new SimpleResponse<>(response.getRequest(), response.getStatusCode(),
response.getHeaders(), false);

return Mono.just(result);
});
}

/**
* Gets a queue with its context.
*
Expand Down Expand Up @@ -1226,6 +1383,30 @@ <T> Mono<Response<T>> getSubscriptionWithResponse(String topicName, String subsc
}
}

/**
* Gets the namespace properties with its context.
*
* @param context Context to pass into request.
*
* @return A Mono that completes with the {@link NamespaceProperties}.
*/
Mono<Response<NamespaceProperties>> getNamespacePropertiesWithResponse(Context context) {
return managementClient.getNamespaces().getWithResponseAsync(context).handle((response, sink) -> {
final NamespacePropertiesEntry entry = response.getValue();
if (entry == null || entry.getContent() == null) {
sink.error(new AzureException(
"There was no content inside namespace response. Entry: " + response));
return;
}

final NamespaceProperties namespaceProperties = entry.getContent().getNamespaceProperties();
final Response<NamespaceProperties> result = new SimpleResponse<>(response.getRequest(),
response.getStatusCode(), response.getHeaders(), namespaceProperties);

sink.next(result);
});
}

/**
* Gets a topic with its context.
*
Expand Down Expand Up @@ -1541,7 +1722,7 @@ private Response<QueueDescription> deserializeQueue(Response<Object> response) {
if (entry == null) {
return new SimpleResponse<>(response.getRequest(), response.getStatusCode(), response.getHeaders(), null);
} else if (entry.getContent() == null) {
logger.info("entry.getContent() is null. There should have been content returned. Entry: {}", entry);
logger.info("entry.getContent() is null. The entity may not exist. {}", entry);
return new SimpleResponse<>(response.getRequest(), response.getStatusCode(), response.getHeaders(), null);
}

Expand Down
Loading