All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.azure.servicebus.ManagementChannel Maven / Gradle / Ivy

/*
 * Copyright (c) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
 */
package com.microsoft.azure.servicebus;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.microsoft.azure.servicebus.ClientConstants;
import com.microsoft.azure.servicebus.FaultTolerantObject;
import com.microsoft.azure.servicebus.amqp.AmqpResponseCode;
import com.microsoft.azure.servicebus.amqp.IAmqpConnection;
import com.microsoft.azure.servicebus.amqp.IOperationResult;
import com.microsoft.azure.servicebus.amqp.ISessionProvider;
import com.microsoft.azure.servicebus.amqp.ReactorDispatcher;
import com.microsoft.azure.servicebus.amqp.RequestResponseChannel;
import com.microsoft.azure.servicebus.amqp.RequestResponseCloser;
import com.microsoft.azure.servicebus.amqp.RequestResponseOpener;

import org.apache.qpid.proton.Proton;
import org.apache.qpid.proton.amqp.messaging.AmqpValue;
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
import org.apache.qpid.proton.message.Message;

public class ManagementChannel {
    private static final Logger TRACE_LOGGER = Logger.getLogger(ClientConstants.SERVICEBUS_CLIENT_TRACE);
    
    final FaultTolerantObject innerChannel;
    final ISessionProvider sessionProvider;
    final IAmqpConnection connectionEventDispatcher;

    public ManagementChannel(final ISessionProvider sessionProvider, final IAmqpConnection connection,
            final String linkName) {
        this.sessionProvider = sessionProvider;
        this.connectionEventDispatcher = connection;

        RequestResponseCloser closer = new RequestResponseCloser();
        this.innerChannel = new FaultTolerantObject<>(
        		new RequestResponseOpener(sessionProvider, "path", "mgmt", ClientConstants.MANAGEMENT_ADDRESS, connection),
                closer);
        closer.setInnerChannel(this.innerChannel);
    }
	
	public CompletableFuture> request(final ReactorDispatcher dispatcher, final Map request)
	{
		final Message requestMessage = Proton.message();
        final ApplicationProperties applicationProperties = new ApplicationProperties(request);
        requestMessage.setApplicationProperties(applicationProperties);
        // no body required
        
        CompletableFuture> resultFuture = new CompletableFuture>();
        
        this.innerChannel.runOnOpenedObject(dispatcher,
                new IOperationResult() {
                    @Override
                    public void onComplete(final RequestResponseChannel result) {
                        result.request(dispatcher, requestMessage,
                                new IOperationResult() {
                                    @Override
                                    public void onComplete(final Message response) {
                                        final int statusCode = (int)response.getApplicationProperties().getValue().get(ClientConstants.PUT_TOKEN_STATUS_CODE);
                                        final String statusDescription = (String)response.getApplicationProperties().getValue().get(ClientConstants.PUT_TOKEN_STATUS_DESCRIPTION);

                                        if (statusCode == AmqpResponseCode.ACCEPTED.getValue() || statusCode == AmqpResponseCode.OK.getValue()) {
                                            if (response.getBody() != null) {
                                                resultFuture.complete((Map)((AmqpValue)response.getBody()).getValue());
                                            }
                                        } 
                                        else {
                                            this.onError(ExceptionUtil.amqpResponseCodeToException(statusCode, statusDescription));
                                        }
                                    }

                                    @Override
                                    public void onError(final Exception error) {
                                    	resultFuture.completeExceptionally(error);
                                    }
                                });
                    }

                    @Override
                    public void onError(Exception error) {
                        resultFuture.completeExceptionally(error);
                    }
                });
        
		return resultFuture;
	}
	
    public void close(final ReactorDispatcher reactorDispatcher, final IOperationResult closeCallback) {
        this.innerChannel.close(reactorDispatcher, closeCallback);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy