# HG changeset patch # User dfuchs # Date 1518532440 0 # Node ID 33436f5e3b9d8c13b193cc6934983914b63c4dcf # Parent 3420c1bdd254c3e5cbdcbc611de68801df01b736 http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber diff -r 3420c1bdd254 -r 33436f5e3b9d src/java.net.http/share/classes/java/net/http/HttpResponse.java --- a/src/java.net.http/share/classes/java/net/http/HttpResponse.java Mon Feb 12 18:45:17 2018 +0000 +++ b/src/java.net.http/share/classes/java/net/http/HttpResponse.java Tue Feb 13 14:34:00 2018 +0000 @@ -323,14 +323,21 @@ * appropriate, through some other mechanism, e.g. an entry in a * database, etc. * - * @apiNote This method can be used as an adapter between {@code - * BodySubscriber} and {@code Flow.Subscriber}. + * @apiNote This method can be used as an adapter between a {@code + * BodySubscriber} and a text based {@code Flow.Subscriber} that parses + * text line by line. * *

For example: - *

 {@code  TextSubscriber subscriber = new TextSubscriber();
-         *  HttpResponse response = client.sendAsync(request,
-         *      BodyHandler.fromLineSubscriber(subscriber, "\n")).join();
-         *  System.out.println(response.statusCode()); }
+ *
 {@code  // A PrintSubscriber that implements Flow.Subscriber
+         *  // and print lines received by onNext() on System.out
+         *  PrintSubscriber subscriber = new PrintSubscriber(System.out);
+         *  client.sendAsync(request, BodyHandler.fromLineSubscriber(subscriber))
+         *      .thenApply(HttpResponse::statusCode)
+         *      .thenAccept((status) -> {
+         *          if (status != 200) {
+         *              System.err.printf("ERROR: %d status received%n", status);
+         *          }
+         *      }); }
* * @param subscriber the subscriber * @return a response body handler @@ -357,14 +364,21 @@ * function is invoked with the given subscriber, and returns a value * that is set as the response's body. * - * @apiNote This method can be used as an adapter between {@code - * BodySubscriber} and {@code Flow.Subscriber}. + * @apiNote This method can be used as an adapter between a {@code + * BodySubscriber} and a text based {@code Flow.Subscriber} that parses + * text line by line. * *

For example: - *

 {@code  TextSubscriber subscriber = ...;  // accumulates bytes and transforms them into a String
-         *  HttpResponse response = client.sendAsync(request,
-         *      BodyHandler.fromSubscriber(subscriber, TextSubscriber::getTextResult, "\n")).join();
-         *  String text = response.body();  }
+ *
 {@code  // A LineParserSubscriber that implements Flow.Subscriber
+         *  // and accumulates lines that match a particular pattern
+         *  Pattern pattern = ...;
+         *  LineParserSubscriber subscriber = new LineParserSubscriber(pattern);
+         *  HttpResponse> response = client.sendAsync(request,
+         *      BodyHandler.fromLineSubscriber(subscriber, (s) -> s.getMatchingLines(), "\n")).join();
+         *  if (response.statusCode() != 200) {
+         *      System.err.printf("ERROR: %d status received%n", response.statusCode());
+         *  } }
+ * * * @param the type of the Subscriber * @param the type of the response body diff -r 3420c1bdd254 -r 33436f5e3b9d test/jdk/java/net/httpclient/examples/JavadocExamples.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/jdk/java/net/httpclient/examples/JavadocExamples.java Tue Feb 13 14:34:00 2018 +0000 @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.IOException; +import java.io.PrintStream; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.net.http.HttpResponse.BodyHandler; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.Flow; +import java.util.regex.Pattern; + +/* + * THE CONTENTS OF THIS FILE HAVE TO BE IN SYNC WITH THE EXAMPLES USED IN THE + * JAVADOC. + * + * @test + * @compile JavadocExamples.java + */ +public class JavadocExamples { + HttpRequest request = null; + HttpClient client = null; + Pattern p = null; + + /** + * @apiNote This method can be used as an adapter between a {@code + * BodySubscriber} and a text based {@code Flow.Subscriber} that parses + * text line by line. + * + *

For example: + *

 {@code  // A PrintSubscriber that implements Flow.Subscriber
+     *  // and print lines received by onNext() on System.out
+     *  PrintSubscriber subscriber = new PrintSubscriber(System.out);
+     *  client.sendAsync(request, BodyHandler.fromLineSubscriber(subscriber))
+     *      .thenApply(HttpResponse::statusCode)
+     *      .thenAccept((status) -> {
+     *          if (status != 200) {
+     *              System.err.printf("ERROR: %d status received%n", status);
+     *          }
+     *      }); }
+ */ + void fromLineSubscriber1() { + // A PrintSubscriber that implements Flow.Subscriber + // and print lines received by onNext() on System.out + PrintSubscriber subscriber = new PrintSubscriber(System.out); + client.sendAsync(request, BodyHandler.fromLineSubscriber(subscriber)) + .thenApply(HttpResponse::statusCode) + .thenAccept((status) -> { + if (status != 200) { + System.err.printf("ERROR: %d status received%n", status); + } + }); + } + + /** + * @apiNote This method can be used as an adapter between a {@code + * BodySubscriber} and a text based {@code Flow.Subscriber} that parses + * text line by line. + * + *

For example: + *

 {@code  // A LineParserSubscriber that implements Flow.Subscriber
+     *  // and accumulates lines that match a particular pattern
+     *  Pattern pattern = ...;
+     *  LineParserSubscriber subscriber = new LineParserSubscriber(pattern);
+     *  HttpResponse> response = client.send(request,
+     *  BodyHandler.fromLineSubscriber(subscriber, s -> s.getMatchingLines(), "\n"));
+     *  if (response.statusCode() != 200) {
+     *      System.err.printf("ERROR: %d status received%n", response.statusCode());
+     *  } }
+ * + */ + void fromLineSubscriber2() throws IOException, InterruptedException { + // A LineParserSubscriber that implements Flow.Subscriber + // and accumulates lines that match a particular pattern + Pattern pattern = p; + LineParserSubscriber subscriber = new LineParserSubscriber(pattern); + HttpResponse> response = client.send(request, + BodyHandler.fromLineSubscriber(subscriber, s -> s.getMatchingLines(), "\n")); + if (response.statusCode() != 200) { + System.err.printf("ERROR: %d status received%n", response.statusCode()); + } + } + + static final class PrintSubscriber implements Flow.Subscriber { + final PrintStream out; + PrintSubscriber(PrintStream out) { + this.out = out; + } + @Override + public void onSubscribe(Flow.Subscription subscription) { + subscription.request(Long.MAX_VALUE); + } + @Override + public void onNext(String item) { + out.println(item); + } + + @Override + public void onError(Throwable throwable) { + throwable.printStackTrace(); + } + @Override + public void onComplete() { + } + } + + static final class LineParserSubscriber implements Flow.Subscriber { + final Pattern pattern; + final CopyOnWriteArrayList matches = new CopyOnWriteArrayList<>(); + LineParserSubscriber(Pattern pattern) { + this.pattern = pattern; + } + @Override + public void onSubscribe(Flow.Subscription subscription) { + subscription.request(Long.MAX_VALUE); + } + @Override + public void onNext(String item) { + if (pattern.matcher(item).matches()) { + matches.add(item); + } + } + @Override + public void onError(Throwable throwable) { + throwable.printStackTrace(); + } + @Override + public void onComplete() { + } + + public List getMatchingLines() { + return Collections.unmodifiableList(matches); + } + } + +}