src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/RequestPublishers.java
branchhttp-client-branch
changeset 55763 634d8e14c172
child 55777 e62cbcc08cae
equal deleted inserted replaced
55762:e947a3a50a95 55763:634d8e14c172
       
     1 /*
       
     2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.incubator.http;
       
    27 
       
    28 import java.io.File;
       
    29 import java.io.FileInputStream;
       
    30 import java.io.IOException;
       
    31 import java.io.InputStream;
       
    32 import java.io.UncheckedIOException;
       
    33 import java.nio.ByteBuffer;
       
    34 import java.nio.charset.Charset;
       
    35 import java.nio.file.Path;
       
    36 import java.security.AccessControlContext;
       
    37 import java.security.AccessController;
       
    38 import java.security.PrivilegedAction;
       
    39 import java.security.PrivilegedActionException;
       
    40 import java.security.PrivilegedExceptionAction;
       
    41 import java.util.ArrayList;
       
    42 import java.util.Iterator;
       
    43 import java.util.List;
       
    44 import java.util.NoSuchElementException;
       
    45 import java.util.Objects;
       
    46 import java.util.concurrent.ConcurrentLinkedQueue;
       
    47 import java.util.concurrent.Flow;
       
    48 import java.util.function.Supplier;
       
    49 import jdk.incubator.http.HttpRequest.BodyPublisher;
       
    50 import jdk.incubator.http.internal.common.Utils;
       
    51 
       
    52 class RequestPublishers {
       
    53 
       
    54     static class ByteArrayPublisher implements HttpRequest.BodyPublisher {
       
    55         private volatile Flow.Publisher<ByteBuffer> delegate;
       
    56         private final int length;
       
    57         private final byte[] content;
       
    58         private final int offset;
       
    59         private final int bufSize;
       
    60 
       
    61         ByteArrayPublisher(byte[] content) {
       
    62             this(content, 0, content.length);
       
    63         }
       
    64 
       
    65         ByteArrayPublisher(byte[] content, int offset, int length) {
       
    66             this(content, offset, length, Utils.BUFSIZE);
       
    67         }
       
    68 
       
    69         /* bufSize exposed for testing purposes */
       
    70         ByteArrayPublisher(byte[] content, int offset, int length, int bufSize) {
       
    71             this.content = content;
       
    72             this.offset = offset;
       
    73             this.length = length;
       
    74             this.bufSize = bufSize;
       
    75         }
       
    76 
       
    77         List<ByteBuffer> copy(byte[] content, int offset, int length) {
       
    78             List<ByteBuffer> bufs = new ArrayList<>();
       
    79             while (length > 0) {
       
    80                 ByteBuffer b = ByteBuffer.allocate(Math.min(bufSize, length));
       
    81                 int max = b.capacity();
       
    82                 int tocopy = Math.min(max, length);
       
    83                 b.put(content, offset, tocopy);
       
    84                 offset += tocopy;
       
    85                 length -= tocopy;
       
    86                 b.flip();
       
    87                 bufs.add(b);
       
    88             }
       
    89             return bufs;
       
    90         }
       
    91 
       
    92         @Override
       
    93         public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
       
    94             List<ByteBuffer> copy = copy(content, offset, length);
       
    95             this.delegate = new PullPublisher<>(copy);
       
    96             delegate.subscribe(subscriber);
       
    97         }
       
    98 
       
    99         @Override
       
   100         public long contentLength() {
       
   101             return length;
       
   102         }
       
   103     }
       
   104 
       
   105     // This implementation has lots of room for improvement.
       
   106     static class IterablePublisher implements HttpRequest.BodyPublisher {
       
   107         private volatile Flow.Publisher<ByteBuffer> delegate;
       
   108         private final Iterable<byte[]> content;
       
   109         private volatile long contentLength;
       
   110 
       
   111         IterablePublisher(Iterable<byte[]> content) {
       
   112             this.content = content;
       
   113         }
       
   114 
       
   115         // The ByteBufferIterator will iterate over the byte[] arrays in
       
   116         // the content one at the time.
       
   117         //
       
   118         class ByteBufferIterator implements Iterator<ByteBuffer> {
       
   119             final ConcurrentLinkedQueue<ByteBuffer> buffers = new ConcurrentLinkedQueue<>();
       
   120             final Iterator<byte[]> iterator = content.iterator();
       
   121             @Override
       
   122             public boolean hasNext() {
       
   123                 return !buffers.isEmpty() || iterator.hasNext();
       
   124             }
       
   125 
       
   126             @Override
       
   127             public ByteBuffer next() {
       
   128                 ByteBuffer buffer = buffers.poll();
       
   129                 while (buffer == null) {
       
   130                     copy();
       
   131                     buffer = buffers.poll();
       
   132                 }
       
   133                 return buffer;
       
   134             }
       
   135 
       
   136             ByteBuffer getBuffer() {
       
   137                 return Utils.getBuffer();
       
   138             }
       
   139 
       
   140             void copy() {
       
   141                 byte[] bytes = iterator.next();
       
   142                 int length = bytes.length;
       
   143                 if (length == 0 && iterator.hasNext()) {
       
   144                     // avoid inserting empty buffers, except
       
   145                     // if that's the last.
       
   146                     return;
       
   147                 }
       
   148                 int offset = 0;
       
   149                 do {
       
   150                     ByteBuffer b = getBuffer();
       
   151                     int max = b.capacity();
       
   152 
       
   153                     int tocopy = Math.min(max, length);
       
   154                     b.put(bytes, offset, tocopy);
       
   155                     offset += tocopy;
       
   156                     length -= tocopy;
       
   157                     b.flip();
       
   158                     buffers.add(b);
       
   159                 } while (length > 0);
       
   160             }
       
   161         }
       
   162 
       
   163         public Iterator<ByteBuffer> iterator() {
       
   164             return new ByteBufferIterator();
       
   165         }
       
   166 
       
   167         @Override
       
   168         public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
       
   169             Iterable<ByteBuffer> iterable = this::iterator;
       
   170             this.delegate = new PullPublisher<>(iterable);
       
   171             delegate.subscribe(subscriber);
       
   172         }
       
   173 
       
   174         static long computeLength(Iterable<byte[]> bytes) {
       
   175             long len = 0;
       
   176             for (byte[] b : bytes) {
       
   177                 len = Math.addExact(len, (long)b.length);
       
   178             }
       
   179             return len;
       
   180         }
       
   181 
       
   182         @Override
       
   183         public long contentLength() {
       
   184             if (contentLength == 0) {
       
   185                 synchronized(this) {
       
   186                     if (contentLength == 0) {
       
   187                         contentLength = computeLength(content);
       
   188                     }
       
   189                 }
       
   190             }
       
   191             return contentLength;
       
   192         }
       
   193     }
       
   194 
       
   195     static class StringPublisher extends ByteArrayPublisher {
       
   196         public StringPublisher(String content, Charset charset) {
       
   197             super(content.getBytes(charset));
       
   198         }
       
   199     }
       
   200 
       
   201     static class EmptyPublisher implements HttpRequest.BodyPublisher {
       
   202         private final PseudoPublisher<ByteBuffer> delegate = new PseudoPublisher<>();
       
   203 
       
   204         @Override
       
   205         public long contentLength() {
       
   206             return 0;
       
   207         }
       
   208 
       
   209         @Override
       
   210         public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
       
   211             delegate.subscribe(subscriber);
       
   212         }
       
   213     }
       
   214 
       
   215     static class FilePublisher implements BodyPublisher  {
       
   216         private final File file;
       
   217         private volatile AccessControlContext acc;
       
   218 
       
   219         FilePublisher(Path name) {
       
   220             file = name.toFile();
       
   221         }
       
   222 
       
   223         void setAccessControlContext(AccessControlContext acc) {
       
   224             this.acc = acc;
       
   225         }
       
   226 
       
   227         @Override
       
   228         public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
       
   229             if (System.getSecurityManager() != null && acc == null)
       
   230                 throw new InternalError(
       
   231                         "Unexpected null acc when security manager has been installed");
       
   232 
       
   233             InputStream is;
       
   234             try {
       
   235                 PrivilegedExceptionAction<FileInputStream> pa =
       
   236                         () -> new FileInputStream(file);
       
   237                 is = AccessController.doPrivileged(pa, acc);
       
   238             } catch (PrivilegedActionException pae) {
       
   239                 throw new UncheckedIOException((IOException)pae.getCause());
       
   240             }
       
   241             PullPublisher<ByteBuffer> publisher =
       
   242                     new PullPublisher<>(() -> new StreamIterator(is));
       
   243             publisher.subscribe(subscriber);
       
   244         }
       
   245 
       
   246         @Override
       
   247         public long contentLength() {
       
   248             assert System.getSecurityManager() != null ? acc != null: true;
       
   249             PrivilegedAction<Long> pa = () -> file.length();
       
   250             return AccessController.doPrivileged(pa, acc);
       
   251         }
       
   252     }
       
   253 
       
   254     /**
       
   255      * Reads one buffer ahead all the time, blocking in hasNext()
       
   256      */
       
   257     static class StreamIterator implements Iterator<ByteBuffer> {
       
   258         final InputStream is;
       
   259         final Supplier<? extends ByteBuffer> bufSupplier;
       
   260         volatile ByteBuffer nextBuffer;
       
   261         volatile boolean need2Read = true;
       
   262         volatile boolean haveNext;
       
   263         volatile Throwable error;
       
   264 
       
   265         StreamIterator(InputStream is) {
       
   266             this(is, Utils::getBuffer);
       
   267         }
       
   268 
       
   269         StreamIterator(InputStream is, Supplier<? extends ByteBuffer> bufSupplier) {
       
   270             this.is = is;
       
   271             this.bufSupplier = bufSupplier;
       
   272         }
       
   273 
       
   274         Throwable error() {
       
   275             return error;
       
   276         }
       
   277 
       
   278         private int read() {
       
   279             nextBuffer = bufSupplier.get();
       
   280             nextBuffer.clear();
       
   281             byte[] buf = nextBuffer.array();
       
   282             int offset = nextBuffer.arrayOffset();
       
   283             int cap = nextBuffer.capacity();
       
   284             try {
       
   285                 int n = is.read(buf, offset, cap);
       
   286                 if (n == -1) {
       
   287                     is.close();
       
   288                     return -1;
       
   289                 }
       
   290                 //flip
       
   291                 nextBuffer.limit(n);
       
   292                 nextBuffer.position(0);
       
   293                 return n;
       
   294             } catch (IOException ex) {
       
   295                 error = ex;
       
   296                 return -1;
       
   297             }
       
   298         }
       
   299 
       
   300         @Override
       
   301         public synchronized boolean hasNext() {
       
   302             if (need2Read) {
       
   303                 haveNext = read() != -1;
       
   304                 if (haveNext) {
       
   305                     need2Read = false;
       
   306                 }
       
   307                 return haveNext;
       
   308             }
       
   309             return haveNext;
       
   310         }
       
   311 
       
   312         @Override
       
   313         public synchronized ByteBuffer next() {
       
   314             if (!hasNext()) {
       
   315                 throw new NoSuchElementException();
       
   316             }
       
   317             need2Read = true;
       
   318             return nextBuffer;
       
   319         }
       
   320 
       
   321     }
       
   322 
       
   323     static class InputStreamPublisher implements BodyPublisher {
       
   324         private final Supplier<? extends InputStream> streamSupplier;
       
   325 
       
   326         InputStreamPublisher(Supplier<? extends InputStream> streamSupplier) {
       
   327             this.streamSupplier = streamSupplier;
       
   328         }
       
   329 
       
   330         @Override
       
   331         public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
       
   332 
       
   333             InputStream is = streamSupplier.get();
       
   334             if (is == null) {
       
   335                 throw new UncheckedIOException(new IOException("no inputstream supplied"));
       
   336             }
       
   337             PullPublisher<ByteBuffer> publisher =
       
   338                     new PullPublisher<>(iterableOf(is));
       
   339             publisher.subscribe(subscriber);
       
   340         }
       
   341 
       
   342         protected Iterable<ByteBuffer> iterableOf(InputStream is) {
       
   343             return () -> new StreamIterator(is);
       
   344         }
       
   345 
       
   346         @Override
       
   347         public long contentLength() {
       
   348             return -1;
       
   349         }
       
   350     }
       
   351 }