/*
 * RESTHeart Security
 * 
 * Copyright (C) SoftInstigate Srl
 * 
 * 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 com.restheart.security.net;

import io.undertow.client.ClientCallback;
import io.undertow.client.ClientExchange;
import io.undertow.client.ClientResponse;
import io.undertow.client.UndertowClient;
import io.undertow.server.DefaultByteBufferPool;
import io.undertow.util.AttachmentKey;
import io.undertow.util.StringReadChannelListener;
import io.undertow.util.StringWriteChannelListener;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.restheart.security.Bootstrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xnio.ChannelListeners;
import org.xnio.OptionMap;
import org.xnio.Options;
import org.xnio.Xnio;
import org.xnio.channels.StreamSinkChannel;

/**
 *
 * @author Andrea Di Cesare <andrea@softinstigate.com>
 */
public class Client {
    private static final Logger LOGGER
            = LoggerFactory.getLogger(Client.class);

    private static final OptionMap DEFAULT_OPTIONS;

    static {
        final OptionMap.Builder builder = OptionMap.builder()
                .set(Options.WORKER_IO_THREADS, 8)
                .set(Options.TCP_NODELAY, true)
                .set(Options.KEEP_ALIVE, true)
                .set(Options.WORKER_NAME, "Client");

        DEFAULT_OPTIONS = builder.getMap();
    }

    public static final AttachmentKey<String> RESPONSE_BODY_KEY = AttachmentKey.create(String.class);

    private static final DefaultByteBufferPool POOL
            = new DefaultByteBufferPool(true,
                    Bootstrapper.getConfiguration() == null ? 16384
                    : Bootstrapper.getConfiguration().getBufferSize(),
                    1000,
                    10,
                    100);

    private Client() {
    }

    public static Client getInstance() {
        return ClientHolder.INSTANCE;
    }

    /**
     *
     * @param uri
     * @param request
     * @return
     * @throws IOException, ConnectException on connection refused
     */
    public Response execute(Request request) throws IOException {
        final var client = UndertowClient.getInstance();

        // Create xnio worker
        final var xnio = Xnio.getInstance();
        final var xnioWorker = xnio.createWorker(null, DEFAULT_OPTIONS);

        try {
            try ( var connection = request.getProtocol().equalsIgnoreCase("https")
                    ? client.connect(request.getConnectionUri(),
                            xnioWorker,
                            xnio.getSslProvider(OptionMap.EMPTY),
                            POOL,
                            OptionMap.EMPTY).get()
                    : client.connect(request.getConnectionUri(),
                            xnioWorker,
                            POOL,
                            OptionMap.EMPTY).get()) {

                final CountDownLatch latch = new CountDownLatch(1);

                final List<ClientResponse> responses = new CopyOnWriteArrayList<>();

                connection.getIoThread().execute(new Runnable() {
                    @Override
                    public void run() {
                        connection.sendRequest(
                                request.asClientRequest(),
                                createClientCallback(request, responses, latch));
                    }
                });

                latch.await(60, TimeUnit.SECONDS);

                if (responses.size() > 0) {
                    return Response.wrap(responses.get(0));
                } else {
                    throw new IOException("Error, no response");
                }
            } catch (InterruptedException ex) {
                throw new IOException(ex.getMessage(), ex);
            }
        } catch (GeneralSecurityException ex) {
            LOGGER.error("Error with SSL configuration", ex);
            throw new IOException(ex.getMessage(), ex);
        }
    }

    private static class ClientHolder {
        private static final Client INSTANCE = new Client();
    }

    private ClientCallback<ClientExchange> createClientCallback(
            final Request request,
            final List<ClientResponse> responses,
            final CountDownLatch latch) {
        return new ClientCallback<ClientExchange>() {
            @Override
            public void completed(ClientExchange result) {
                if (request.getBody() != null) {
                    new StringWriteChannelListener(request.getBody())
                            .setup(result.getRequestChannel());
                }

                result.setResponseListener(new ClientCallback<ClientExchange>() {
                    @Override
                    public void completed(final ClientExchange result) {
                        responses.add(result.getResponse());

                        new StringReadChannelListener(result.getConnection().getBufferPool()) {

                            @Override
                            protected void stringDone(String string) {
                                result.getResponse().putAttachment(RESPONSE_BODY_KEY, string);
                                latch.countDown();
                            }

                            @Override
                            protected void error(IOException e) {
                                LOGGER.error("Error executing request {}", request, e);

                                latch.countDown();
                            }
                        }.setup(result.getResponseChannel());
                    }

                    @Override
                    public void failed(IOException e) {
                        LOGGER.error("Error executing request {}", request, e);

                        latch.countDown();
                    }
                });

                try {
                    result.getRequestChannel().shutdownWrites();
                    if (!result.getRequestChannel().flush()) {
                        result.getRequestChannel().getWriteSetter()
                                .set(ChannelListeners.<StreamSinkChannel>flushingChannelListener(null, null));
                        result.getRequestChannel().resumeWrites();
                    }
                } catch (IOException e) {
                    LOGGER.error("Error executing request {}", request, e);

                    latch.countDown();
                }
            }

            @Override
            public void failed(IOException e) {
                LOGGER.error("Error executing request {}", request, e);

                latch.countDown();
            }
        };
    }
}

@@ -1422,23 +1422,20 @@
 rg.xnio.
-Options
+Xnio
 ;%0Aimport
@@ -1440,32 +1440,38 @@
 rt org.xnio.Xnio
+Worker
 ;%0Aimport org.xni
@@ -1686,387 +1686,8 @@
 );%0A%0A
-    private static final OptionMap DEFAULT_OPTIONS;%0A%0A    static %7B%0A        final OptionMap.Builder builder = OptionMap.builder()%0A                .set(Options.WORKER_IO_THREADS, 8)%0A                .set(Options.TCP_NODELAY, true)%0A                .set(Options.KEEP_ALIVE, true)%0A                .set(Options.WORKER_NAME, %22Client%22);%0A%0A        DEFAULT_OPTIONS = builder.getMap();%0A    %7D%0A%0A
     
@@ -2211,24 +2211,414 @@
 NCE;%0A    %7D%0A%0A
+    public Response execute(Request request) throws IOException %7B%0A        try %7B%0A            return execute(XnioWorker.getContextManager().get(), %0A                    request);%0A        %7D catch (Throwable t) %7B%0A            LOGGER.error(%22Error executing request %22 + request.toString(), t);%0A            throw new IOException(%22Error executing request %22 + request.toString(), t);%0A        %7D%0A    %7D%0A%0A
     /**%0A    
@@ -2634,19 +2634,22 @@
  @param 
-uri
+worker
 %0A     * 
@@ -2773,32 +2773,51 @@
 esponse execute(
+XnioWorker worker, 
 Request request)
@@ -2900,157 +2900,8 @@
 );%0A%0A
-        // Create xnio worker%0A        final var xnio = Xnio.getInstance();%0A        final var xnioWorker = xnio.createWorker(null, DEFAULT_OPTIONS);%0A%0A
     
@@ -3078,37 +3078,33 @@
                 
-xnioW
+w
 orker,%0A         
@@ -3122,21 +3122,35 @@
         
-x
+X
 nio.
+getInstance().
 getSslPr
@@ -3356,13 +3356,9 @@
     
-xnioW
+w
 orke

Fix XnioWorker resource leak