/*
 * Copyright 2016 Red Hat Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.enmasse.amqp;

import io.vertx.core.Vertx;
import io.vertx.proton.ProtonClient;
import io.vertx.proton.ProtonConnection;
import io.vertx.proton.ProtonReceiver;
import io.vertx.proton.ProtonSender;
import org.apache.qpid.proton.amqp.messaging.Source;
import org.apache.qpid.proton.message.Message;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * A simple client for doing request-response over AMQP.
 */
public class SyncRequestClient implements AutoCloseable {
    private final String host;
    private final int port;
    private final Vertx vertx;

    public SyncRequestClient(String host, int port) {
        this(host, port, Vertx.vertx());
    }

    public SyncRequestClient(String host, int port, Vertx vertx) {
        this.host = host;
        this.port = port;
        this.vertx = vertx;
    }

    public Message request(Message message, long timeout, TimeUnit timeUnit) throws InterruptedException, TimeoutException, ExecutionException {
        String address = message.getAddress();
        CompletableFuture<Message> response = new CompletableFuture<>();

        ProtonClient client = ProtonClient.create(vertx);
        client.connect(host, port, connectEvent -> {
            if (connectEvent.succeeded()) {
                ProtonConnection connection = connectEvent.result();
                connection.open();

                ProtonSender sender = connection.createSender(address);
                sender.openHandler(senderOpenEvent -> {
                    if (senderOpenEvent.succeeded()) {
                        ProtonReceiver receiver = connection.createReceiver(address);
                        Source source = new Source();
                        source.setDynamic(true);
                        receiver.setSource(source);
                        receiver.setPrefetch(1);
                        receiver.handler(((delivery, msg) -> {
                            response.complete(msg);

                            receiver.close();
                            sender.close();
                            connection.close();
                        }));

                        receiver.openHandler(receiverOpenEvent -> {
                            if (receiverOpenEvent.succeeded()) {
                                if (receiver.getRemoteSource() != null) {
                                    message.setReplyTo(receiver.getRemoteSource().getAddress());
                                }
                                sender.send(message);
                            } else {
                                response.completeExceptionally(receiverOpenEvent.cause());
                            }
                        });
                        receiver.open();
                    }
                });
                sender.open();
            } else {
                response.completeExceptionally(connectEvent.cause());
            }
        });
        return response.get(timeout, timeUnit);
    }

    @Override
    public void close() throws Exception {
        vertx.close();
    }
}

@@ -664,68 +664,34 @@
 rtx.
-proton.ProtonClient;%0Aimport io.vertx.proton.ProtonConnec
+core.net.PemKeyCertOp
 tion
+s
 ;%0Aim
@@ -708,29 +708,32 @@
 rtx.
-proton.ProtonReceiver
+core.net.PemTrustOptions
 ;%0Aim
@@ -753,28 +753,17 @@
 .proton.
-ProtonSender
+*
 ;%0Aimport
@@ -857,16 +857,37 @@
 ssage;%0A%0A
+import java.io.File;%0A
 import j
@@ -1270,16 +1270,69 @@
 x vertx;
+%0A    private final ProtonClientOptions clientOptions;
 %0A%0A    pu
@@ -1490,24 +1490,193 @@
 tx vertx) %7B%0A
+        this(host, port, vertx, new ProtonClientOptions());%0A    %7D%0A%0A    public SyncRequestClient(String host, int port, Vertx vertx, ProtonClientOptions clientOptions) %7B%0A
         this
@@ -1739,24 +1739,68 @@
 tx = vertx;%0A
+        this.clientOptions = clientOptions;%0A
     %7D%0A%0A    p
@@ -2118,16 +2118,16 @@
 vertx);%0A
-
         
@@ -2141,16 +2141,31 @@
 connect(
+clientOptions, 
 host, po

Support custom proton client options for client