70 * BodyHandler} provided when sending the {@code HttpRequest}. |
70 * BodyHandler} provided when sending the {@code HttpRequest}. |
71 * |
71 * |
72 * <p> This class provides methods for accessing the response status code, |
72 * <p> This class provides methods for accessing the response status code, |
73 * headers, the response body, and the {@code HttpRequest} corresponding |
73 * headers, the response body, and the {@code HttpRequest} corresponding |
74 * to this response. |
74 * to this response. |
75 ** |
75 * |
76 * @param <T> the response body type |
76 * @param <T> the response body type |
77 * |
77 * |
78 * @since 11 |
78 * @since 11 |
79 */ |
79 */ |
80 public interface HttpResponse<T> { |
80 public interface HttpResponse<T> { |
386 * @param finisher a function to be applied after the subscriber has completed |
386 * @param finisher a function to be applied after the subscriber has completed |
387 * @param lineSeparator an optional line separator: can be {@code null}, |
387 * @param lineSeparator an optional line separator: can be {@code null}, |
388 * in which case lines will be delimited in the manner of |
388 * in which case lines will be delimited in the manner of |
389 * {@link BufferedReader#readLine()}. |
389 * {@link BufferedReader#readLine()}. |
390 * @return a response body handler |
390 * @return a response body handler |
391 * @throws IllegalArgumentException if the supplied {@code lineSeparator} is the empty string. |
391 * @throws IllegalArgumentException if the supplied {@code lineSeparator} |
|
392 * is the empty string |
392 */ |
393 */ |
393 public static <S extends Subscriber<? super String>,T> BodyHandler<T> |
394 public static <S extends Subscriber<? super String>,T> BodyHandler<T> |
394 fromLineSubscriber(S subscriber, Function<S,T> finisher, String lineSeparator) { |
395 fromLineSubscriber(S subscriber, |
|
396 Function<S,T> finisher, |
|
397 String lineSeparator) { |
395 Objects.requireNonNull(subscriber); |
398 Objects.requireNonNull(subscriber); |
396 Objects.requireNonNull(finisher); |
399 Objects.requireNonNull(finisher); |
397 // implicit null check |
400 // implicit null check |
398 if (lineSeparator != null && lineSeparator.isEmpty()) |
401 if (lineSeparator != null && lineSeparator.isEmpty()) |
399 throw new IllegalArgumentException("empty line separator"); |
402 throw new IllegalArgumentException("empty line separator"); |
904 * @param charset a {@link Charset} to decode the bytes |
907 * @param charset a {@link Charset} to decode the bytes |
905 * @param lineSeparator an optional line separator: can be {@code null}, |
908 * @param lineSeparator an optional line separator: can be {@code null}, |
906 * in which case lines will be delimited in the manner of |
909 * in which case lines will be delimited in the manner of |
907 * {@link BufferedReader#readLine()}. |
910 * {@link BufferedReader#readLine()}. |
908 * @return a body subscriber |
911 * @return a body subscriber |
909 * @throws IllegalArgumentException if the supplied {@code lineSeparator} is the empty string. |
912 * @throws IllegalArgumentException if the supplied {@code lineSeparator} |
|
913 * is the empty string |
910 */ |
914 */ |
911 public static <S extends Subscriber<? super String>,T> BodySubscriber<T> |
915 public static <S extends Subscriber<? super String>,T> BodySubscriber<T> |
912 fromLineSubscriber(S subscriber, |
916 fromLineSubscriber(S subscriber, |
913 Function<S,T> finisher, |
917 Function<S,T> finisher, |
914 Charset charset, |
918 Charset charset, |
1088 /** |
1092 /** |
1089 * Returns a response subscriber which discards the response body. The |
1093 * Returns a response subscriber which discards the response body. The |
1090 * supplied value is the value that will be returned from |
1094 * supplied value is the value that will be returned from |
1091 * {@link HttpResponse#body()}. |
1095 * {@link HttpResponse#body()}. |
1092 * |
1096 * |
1093 * @param <U> The type of the response body |
1097 * @param <U> the type of the response body |
1094 * @param value the value to return from HttpResponse.body(), may be {@code null} |
1098 * @param value the value to return from HttpResponse.body(), may be {@code null} |
1095 * @return a {@code BodySubscriber} |
1099 * @return a {@code BodySubscriber} |
1096 */ |
1100 */ |
1097 public static <U> BodySubscriber<U> replace(U value) { |
1101 public static <U> BodySubscriber<U> replace(U value) { |
1098 return new ResponseSubscribers.NullSubscriber<>(Optional.ofNullable(value)); |
1102 return new ResponseSubscribers.NullSubscriber<>(Optional.ofNullable(value)); |
1157 * } |
1161 * } |
1158 * }); |
1162 * }); |
1159 * return downstream; |
1163 * return downstream; |
1160 * } }</pre> |
1164 * } }</pre> |
1161 * |
1165 * |
1162 * @param <T> the upstream both type |
1166 * @param <T> the upstream body type |
1163 * @param <U> the type of the body subscriber returned |
1167 * @param <U> the type of the body subscriber returned |
1164 * @param upstream the body subscriber to be mapped |
1168 * @param upstream the body subscriber to be mapped |
1165 * @param mapper the mapping function |
1169 * @param mapper the mapping function |
1166 * @return a mapped body subscriber |
1170 * @return a mapping body subscriber |
1167 */ |
1171 */ |
1168 public static <T,U> BodySubscriber<U> mapping(BodySubscriber<T> upstream, |
1172 public static <T,U> BodySubscriber<U> mapping(BodySubscriber<T> upstream, |
1169 Function<T, U> mapper) |
1173 Function<T, U> mapper) |
1170 { |
1174 { |
1171 return new ResponseSubscribers.MappingSubscriber<T, U>(upstream, mapper); |
1175 return new ResponseSubscribers.MappingSubscriber<T, U>(upstream, mapper); |