test/jdk/java/net/httpclient/examples/JavadocExamples.java
branchhttp-client-branch
changeset 56119 33436f5e3b9d
child 56120 5db317238575
equal deleted inserted replaced
56104:3420c1bdd254 56119:33436f5e3b9d
       
     1 /*
       
     2  * Copyright (c) 2018, 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.io.IOException;
       
    25 import java.io.PrintStream;
       
    26 import java.net.http.HttpClient;
       
    27 import java.net.http.HttpRequest;
       
    28 import java.net.http.HttpResponse;
       
    29 import java.net.http.HttpResponse.BodyHandler;
       
    30 import java.util.Collections;
       
    31 import java.util.List;
       
    32 import java.util.concurrent.CopyOnWriteArrayList;
       
    33 import java.util.concurrent.Flow;
       
    34 import java.util.regex.Pattern;
       
    35 
       
    36 /*
       
    37  * THE CONTENTS OF THIS FILE HAVE TO BE IN SYNC WITH THE EXAMPLES USED IN THE
       
    38  * JAVADOC.
       
    39  *
       
    40  * @test
       
    41  * @compile JavadocExamples.java
       
    42  */
       
    43 public class JavadocExamples {
       
    44     HttpRequest request = null;
       
    45     HttpClient client = null;
       
    46     Pattern p = null;
       
    47 
       
    48     /**
       
    49      * @apiNote This method can be used as an adapter between a {@code
       
    50      * BodySubscriber} and a text based {@code Flow.Subscriber} that parses
       
    51      * text line by line.
       
    52      *
       
    53      * <p> For example:
       
    54      * <pre> {@code  // A PrintSubscriber that implements Flow.Subscriber<String>
       
    55      *  // and print lines received by onNext() on System.out
       
    56      *  PrintSubscriber subscriber = new PrintSubscriber(System.out);
       
    57      *  client.sendAsync(request, BodyHandler.fromLineSubscriber(subscriber))
       
    58      *      .thenApply(HttpResponse::statusCode)
       
    59      *      .thenAccept((status) -> {
       
    60      *          if (status != 200) {
       
    61      *              System.err.printf("ERROR: %d status received%n", status);
       
    62      *          }
       
    63      *      }); }</pre>
       
    64      */
       
    65     void fromLineSubscriber1() {
       
    66          // A PrintSubscriber that implements Flow.Subscriber<String>
       
    67          // and print lines received by onNext() on System.out
       
    68          PrintSubscriber subscriber = new PrintSubscriber(System.out);
       
    69          client.sendAsync(request, BodyHandler.fromLineSubscriber(subscriber))
       
    70                  .thenApply(HttpResponse::statusCode)
       
    71                  .thenAccept((status) -> {
       
    72                      if (status != 200) {
       
    73                          System.err.printf("ERROR: %d status received%n", status);
       
    74                      }
       
    75                  });
       
    76     }
       
    77 
       
    78     /**
       
    79      * @apiNote This method can be used as an adapter between a {@code
       
    80      * BodySubscriber} and a text based {@code Flow.Subscriber} that parses
       
    81      * text line by line.
       
    82      *
       
    83      * <p> For example:
       
    84      * <pre> {@code  // A LineParserSubscriber that implements Flow.Subscriber<String>
       
    85      *  // and accumulates lines that match a particular pattern
       
    86      *  Pattern pattern = ...;
       
    87      *  LineParserSubscriber subscriber = new LineParserSubscriber(pattern);
       
    88      *  HttpResponse<List<String>> response = client.send(request,
       
    89      *  BodyHandler.fromLineSubscriber(subscriber, s -> s.getMatchingLines(), "\n"));
       
    90      *  if (response.statusCode() != 200) {
       
    91      *      System.err.printf("ERROR: %d status received%n", response.statusCode());
       
    92      *  } }</pre>
       
    93      *
       
    94      */
       
    95     void fromLineSubscriber2() throws IOException, InterruptedException {
       
    96         // A LineParserSubscriber that implements Flow.Subscriber<String>
       
    97         // and accumulates lines that match a particular pattern
       
    98         Pattern pattern = p;
       
    99         LineParserSubscriber subscriber = new LineParserSubscriber(pattern);
       
   100         HttpResponse<List<String>> response = client.send(request,
       
   101                 BodyHandler.fromLineSubscriber(subscriber, s -> s.getMatchingLines(), "\n"));
       
   102         if (response.statusCode() != 200) {
       
   103             System.err.printf("ERROR: %d status received%n", response.statusCode());
       
   104         }
       
   105     }
       
   106 
       
   107     static final class PrintSubscriber implements Flow.Subscriber<String> {
       
   108         final PrintStream out;
       
   109         PrintSubscriber(PrintStream out) {
       
   110             this.out = out;
       
   111         }
       
   112         @Override
       
   113         public void onSubscribe(Flow.Subscription subscription) {
       
   114             subscription.request(Long.MAX_VALUE);
       
   115         }
       
   116         @Override
       
   117         public void onNext(String item) {
       
   118             out.println(item);
       
   119         }
       
   120 
       
   121         @Override
       
   122         public void onError(Throwable throwable) {
       
   123             throwable.printStackTrace();
       
   124         }
       
   125         @Override
       
   126         public void onComplete() {
       
   127         }
       
   128     }
       
   129 
       
   130     static final class LineParserSubscriber implements Flow.Subscriber<String> {
       
   131         final Pattern pattern;
       
   132         final CopyOnWriteArrayList<String> matches = new CopyOnWriteArrayList<>();
       
   133         LineParserSubscriber(Pattern pattern) {
       
   134             this.pattern = pattern;
       
   135         }
       
   136         @Override
       
   137         public void onSubscribe(Flow.Subscription subscription) {
       
   138             subscription.request(Long.MAX_VALUE);
       
   139         }
       
   140         @Override
       
   141         public void onNext(String item) {
       
   142             if (pattern.matcher(item).matches()) {
       
   143                 matches.add(item);
       
   144             }
       
   145         }
       
   146         @Override
       
   147         public void onError(Throwable throwable) {
       
   148             throwable.printStackTrace();
       
   149         }
       
   150         @Override
       
   151         public void onComplete() {
       
   152         }
       
   153 
       
   154         public List<String> getMatchingLines() {
       
   155             return Collections.unmodifiableList(matches);
       
   156         }
       
   157     }
       
   158 
       
   159 }