99 */ |
99 */ |
100 public abstract int statusCode(); |
100 public abstract int statusCode(); |
101 |
101 |
102 /** |
102 /** |
103 * Returns the {@link HttpRequest} corresponding to this response. |
103 * Returns the {@link HttpRequest} corresponding to this response. |
104 * <p> |
104 * |
105 * This may not be the original request provided by the caller, |
105 * <p> This may not be the original request provided by the caller, |
106 * for example, if that request was redirected. |
106 * for example, if that request was redirected. |
107 * |
107 * |
108 * @see #previousResponse() |
108 * @see #previousResponse() |
109 * |
109 * |
110 * @return the request |
110 * @return the request |
111 */ |
111 */ |
112 public abstract HttpRequest request(); |
112 public abstract HttpRequest request(); |
113 |
113 |
114 /** |
114 /** |
115 * Returns an {@code Optional} containing the previous intermediate response if |
115 * Returns an {@code Optional} containing the previous intermediate response |
116 * one was received. An intermediate response is one that is received |
116 * if one was received. An intermediate response is one that is received |
117 * as a result of redirection or authentication. If no previous response |
117 * as a result of redirection or authentication. If no previous response |
118 * was received then an empty {@code Optional} is returned. |
118 * was received then an empty {@code Optional} is returned. |
119 * |
119 * |
120 * @return an Optional containing the HttpResponse, if any. |
120 * @return an Optional containing the HttpResponse, if any. |
121 */ |
121 */ |
131 /** |
131 /** |
132 * Returns the body. Depending on the type of {@code T}, the returned body |
132 * Returns the body. Depending on the type of {@code T}, the returned body |
133 * may represent the body after it was read (such as {@code byte[]}, or |
133 * may represent the body after it was read (such as {@code byte[]}, or |
134 * {@code String}, or {@code Path}) or it may represent an object with |
134 * {@code String}, or {@code Path}) or it may represent an object with |
135 * which the body is read, such as an {@link java.io.InputStream}. |
135 * which the body is read, such as an {@link java.io.InputStream}. |
136 * <p> |
136 * |
137 * If this {@code HttpResponse} was returned from an invocation of |
137 * <p> If this {@code HttpResponse} was returned from an invocation of |
138 * {@link #previousResponse()} then this method returns {@code null} |
138 * {@link #previousResponse()} then this method returns {@code null} |
139 * |
139 * |
140 * @return the body |
140 * @return the body |
141 */ |
141 */ |
142 public abstract T body(); |
142 public abstract T body(); |
780 * |
780 * |
781 * @since 9 |
781 * @since 9 |
782 */ |
782 */ |
783 public interface MultiSubscriber<U,T> { |
783 public interface MultiSubscriber<U,T> { |
784 /** |
784 /** |
785 * Called for the main request from the user. This {@link HttpRequest} parameter |
785 * Called for the main request from the user. This {@link HttpRequest} |
786 * is the request that was supplied to {@link HttpClient#sendAsync(HttpRequest, MultiSubscriber)}. The |
786 * parameter is the request that was supplied to {@link |
787 * implementation must return an {@link BodyHandler} for |
787 * HttpClient#sendAsync(HttpRequest, MultiSubscriber)}. The |
788 * the response body. |
788 * implementation must return an {@link BodyHandler} for the response |
|
789 * body. |
789 * |
790 * |
790 * @param request the request |
791 * @param request the request |
791 * |
792 * |
792 * @return an optional body handler |
793 * @return an optional body handler |
793 */ |
794 */ |
794 BodyHandler<T> onRequest(HttpRequest request); |
795 BodyHandler<T> onRequest(HttpRequest request); |
795 |
796 |
796 /** |
797 /** |
797 * Called for each push promise that is received. The {@link HttpRequest} parameter |
798 * Called for each push promise that is received. The {@link HttpRequest} |
798 * represents the PUSH_PROMISE. The implementation must return an {@code Optional} of |
799 * parameter represents the PUSH_PROMISE. The implementation must return |
799 * {@link BodyHandler} for the response body. Different handlers (of the same type) can be returned |
800 * an {@code Optional} of {@link BodyHandler} for the response body. |
800 * for different pushes within the same multi send. If no handler |
801 * Different handlers (of the same type) can be returned for different |
801 * (an empty {@code Optional}) is returned, then the push will be canceled. |
802 * pushes within the same multi send. If no handler (an empty {@code |
802 * If required, the {@code CompletableFuture<Void>} supplied to the {@code onFinalPushPromise} parameter |
803 * Optional}) is returned, then the push will be canceled. If required, |
803 * of {@link #completion(CompletableFuture, CompletableFuture)} can be used to determine |
804 * the {@code CompletableFuture<Void>} supplied to the {@code |
804 * when the final PUSH_PROMISE is received. |
805 * onFinalPushPromise} parameter of {@link |
|
806 * #completion(CompletableFuture, CompletableFuture)} can be used to |
|
807 * determine when the final PUSH_PROMISE is received. |
805 * |
808 * |
806 * @param pushPromise the push promise |
809 * @param pushPromise the push promise |
807 * |
810 * |
808 * @return an optional body handler |
811 * @return an optional body handler |
809 */ |
812 */ |
839 |
842 |
840 /** |
843 /** |
841 * Returns a {@link java.util.concurrent.CompletableFuture}{@code <U>} |
844 * Returns a {@link java.util.concurrent.CompletableFuture}{@code <U>} |
842 * which completes when the aggregate result object itself is available. |
845 * which completes when the aggregate result object itself is available. |
843 * It is expected that the returned {@code CompletableFuture} will depend |
846 * It is expected that the returned {@code CompletableFuture} will depend |
844 * on one of the given {@code CompletableFuture<Void}s which themselves complete |
847 * on one of the given {@code CompletableFuture<Void}s which themselves |
845 * after all individual responses associated with the multi response |
848 * complete after all individual responses associated with the multi |
846 * have completed, or after all push promises have been received. |
849 * response have completed, or after all push promises have been received. |
847 * This method is called after {@link #onRequest(HttpRequest)} but |
850 * This method is called after {@link #onRequest(HttpRequest)} but |
848 * before any other methods. |
851 * before any other methods. |
849 * |
852 * |
850 * @implNote Implementations might follow the pattern shown below |
853 * @implNote Implementations might follow the pattern shown below |
851 * <pre> |
854 * <pre> |
900 * |
903 * |
901 * <p> See {@link #asMap(Function, boolean)} for a code sample of using |
904 * <p> See {@link #asMap(Function, boolean)} for a code sample of using |
902 * this interface. |
905 * this interface. |
903 * |
906 * |
904 * @param <V> the body type used for all responses |
907 * @param <V> the body type used for all responses |
905 * @param reqHandler a function invoked for the user's request and each push promise |
908 * @param reqHandler a function invoked for the user's request and each |
|
909 * push promise |
906 * @param completion {@code true} if the aggregate CompletableFuture |
910 * @param completion {@code true} if the aggregate CompletableFuture |
907 * completes after all responses have been received, |
911 * completes after all responses have been received, |
908 * or {@code false} after all push promises received |
912 * or {@code false} after all push promises received |
909 * |
913 * |
910 * @return a MultiSubscriber |
914 * @return a MultiSubscriber |
911 */ |
915 */ |
912 public static <V> MultiSubscriber<MultiMapResult<V>,V> asMap( |
916 public static <V> MultiSubscriber<MultiMapResult<V>,V> asMap( |
913 Function<HttpRequest, Optional<HttpResponse.BodyHandler<V>>> reqHandler, |
917 Function<HttpRequest, Optional<HttpResponse.BodyHandler<V>>> reqHandler, |
914 boolean completion) { |
918 boolean completion) { |
915 return new MultiSubscriberImpl<V>(reqHandler.andThen(optv -> optv.get()), reqHandler, completion); |
919 return new MultiSubscriberImpl<V>(reqHandler.andThen(optv -> optv.get()), |
|
920 reqHandler, |
|
921 completion); |
916 } |
922 } |
917 |
923 |
918 /** |
924 /** |
919 * Returns a general purpose handler for multi responses. This is a |
925 * Returns a general purpose handler for multi responses. This is a |
920 * convenience method which invokes {@link #asMap(Function,boolean) |
926 * convenience method which invokes {@link #asMap(Function,boolean) |
943 * headers, and every push that the server sends is accepted. When the |
949 * headers, and every push that the server sends is accepted. When the |
944 * join() call returns, all {@code HttpResponse}s and their associated |
950 * join() call returns, all {@code HttpResponse}s and their associated |
945 * body objects are available. |
951 * body objects are available. |
946 * |
952 * |
947 * @param <V> the body type used for all responses |
953 * @param <V> the body type used for all responses |
948 * @param reqHandler a function invoked for each push promise and the main request |
954 * @param reqHandler a function invoked for each push promise and the |
|
955 * main request |
949 * @return a MultiSubscriber |
956 * @return a MultiSubscriber |
950 */ |
957 */ |
951 public static <V> MultiSubscriber<MultiMapResult<V>,V> asMap( |
958 public static <V> MultiSubscriber<MultiMapResult<V>,V> asMap( |
952 Function<HttpRequest, Optional<HttpResponse.BodyHandler<V>>> reqHandler) { |
959 Function<HttpRequest, Optional<HttpResponse.BodyHandler<V>>> reqHandler) { |
953 |
960 |