/*
 * Copyright (c) 2013-2016 M-Way Solutions GmbH
 *
 * 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 org.jenkinsci.plugins.relution_publisher.net.requests;

import org.apache.http.HttpResponse;
import org.apache.http.nio.client.HttpAsyncClient;
import org.apache.http.nio.protocol.BasicAsyncResponseConsumer;
import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
import org.apache.http.util.Args;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;


public class ZeroCopyFileRequest extends BaseRequest {

    private final List<Item> mFiles = new ArrayList<>();

    public ZeroCopyFileRequest(final String uri) {
        super(Method.POST, uri);
    }

    public void addItem(final String name, final File file) {
        Args.notNull(name, "name");
        Args.notNull(file, "file");

        final Item item = new Item(name, file);
        this.mFiles.add(item);
    }

    public List<Item> getItems() {
        return this.mFiles;
    }

    @Override
    public Future<HttpResponse> execute(final HttpAsyncClient httpClient) throws FileNotFoundException {

        final HttpAsyncResponseConsumer<HttpResponse> consumer = new BasicAsyncResponseConsumer();
        final HttpAsyncRequestProducer producer = new ZeroCopyFileRequestProducer(this);

        return httpClient.execute(producer, consumer, null);
    }

    public static class Item {

        private final String name;
        private final File   file;

        public Item(final String name, final File file) {
            this.name = name;
            this.file = file;
        }

        public String getName() {
            return this.name;
        }

        public File getFile() {
            return this.file;
        }
    }
}

@@ -1211,16 +1211,27 @@
 st%3CItem%3E
+           
  mFiles 
@@ -1250,16 +1250,67 @@
 ist%3C%3E();
+%0A    private ZeroCopyFileRequestProducer mProducer;
 %0A%0A    pu
@@ -1804,17 +1804,16 @@
 ption %7B%0A
-%0A
         
@@ -1957,48 +1957,235 @@
 r = 
-new ZeroCopyFileRequestProducer(this
+this.getProducer();%0A        return httpClient.execute(producer, consumer, null);%0A    %7D%0A%0A    public long getContentLength() throws FileNotFoundException %7B%0A        final ZeroCopyFileRequestProducer producer = this.getProducer(
 );%0A
-%0A
     
@@ -2199,52 +2199,274 @@
 urn 
-httpClient.execute(producer, consumer, null)
+producer.getContentLength();%0A    %7D%0A%0A    private ZeroCopyFileRequestProducer getProducer() throws FileNotFoundException %7B%0A        if (this.mProducer == null) %7B%0A            this.mProducer = new ZeroCopyFileRequestProducer(this);%0A        %7D%0A        return this.mProducer
 ;%0A  

Add method to get content length.