forked from helidon-io/helidon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTTP/2.0 Client trailers support helidon-io#6544
- Loading branch information
Showing
18 changed files
with
492 additions
and
69 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
http/http/src/main/java/io/helidon/http/ClientResponseTrailers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2023 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.http; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* HTTP Trailer headers of a client response. | ||
*/ | ||
public interface ClientResponseTrailers extends io.helidon.http.Headers { | ||
|
||
/** | ||
* Create new trailers from headers future. | ||
* | ||
* @param trailerFuture trailer headers future completed when trailers are received. | ||
* @param timeout timeout for blocking till trailers are received | ||
* @return new client trailers from headers future | ||
*/ | ||
static ClientResponseTrailers create(CompletableFuture<io.helidon.http.Headers> trailerFuture, Duration timeout) { | ||
return new ClientResponseTrailersImpl(trailerFuture, timeout); | ||
} | ||
|
||
/** | ||
* Create new empty trailers. | ||
* | ||
* @return new empty client trailers | ||
*/ | ||
static ClientResponseTrailers create() { | ||
return new ClientResponseTrailersImpl(); | ||
} | ||
|
||
/** | ||
* Returns true when trailer headers have been fully received. | ||
* | ||
* @return true if received | ||
*/ | ||
boolean received(); | ||
|
||
/** | ||
* Returns true if trailer headers can be expected. | ||
* | ||
* @return true if expected | ||
*/ | ||
boolean expected(); | ||
} |
95 changes: 95 additions & 0 deletions
95
http/http/src/main/java/io/helidon/http/ClientResponseTrailersImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright (c) 2023 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.http; | ||
|
||
import java.time.Duration; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Supplier; | ||
|
||
class ClientResponseTrailersImpl implements ClientResponseTrailers { | ||
private static final CompletableFuture<Headers> EMPTY_TRAILERS = CompletableFuture.completedFuture(WritableHeaders.create()); | ||
private final CompletableFuture<Headers> trailerFuture; | ||
private final Duration timeout; | ||
|
||
ClientResponseTrailersImpl(CompletableFuture<Headers> trailerFuture, Duration timeout) { | ||
this.trailerFuture = trailerFuture; | ||
this.timeout = timeout; | ||
} | ||
|
||
ClientResponseTrailersImpl() { | ||
this.trailerFuture = EMPTY_TRAILERS; | ||
this.timeout = Duration.ZERO; | ||
} | ||
|
||
@Override | ||
public boolean received() { | ||
return trailerFuture.isDone() && expected(); | ||
} | ||
|
||
@Override | ||
public boolean expected() { | ||
return trailerFuture != EMPTY_TRAILERS; | ||
} | ||
|
||
@Override | ||
public List<String> all(Http.HeaderName name, Supplier<List<String>> defaultSupplier) { | ||
return getWithTimeout().all(name, defaultSupplier); | ||
} | ||
|
||
@Override | ||
public boolean contains(Http.HeaderName name) { | ||
return getWithTimeout().contains(name); | ||
} | ||
|
||
@Override | ||
public boolean contains(Http.Header value) { | ||
return getWithTimeout().contains(value); | ||
} | ||
|
||
@Override | ||
public Http.Header get(Http.HeaderName name) { | ||
return getWithTimeout().get(name); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return getWithTimeout().size(); | ||
} | ||
|
||
@Override | ||
public List<HttpMediaType> acceptedTypes() { | ||
return getWithTimeout().acceptedTypes(); | ||
} | ||
|
||
@Override | ||
public Iterator<Http.Header> iterator() { | ||
return getWithTimeout().iterator(); | ||
} | ||
|
||
private Headers getWithTimeout() { | ||
try { | ||
return trailerFuture.get(timeout.toMillis(), TimeUnit.MILLISECONDS); | ||
} catch (InterruptedException | ExecutionException | TimeoutException e) { | ||
throw new IllegalStateException("Timeout " + timeout + " reached while waiting for trailers.", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
webclient/api/src/main/java/io/helidon/webclient/api/WebClientServiceResponseDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2023 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.webclient.api; | ||
|
||
import io.helidon.builder.api.Prototype; | ||
import io.helidon.http.ClientResponseTrailers; | ||
|
||
class WebClientServiceResponseDecorator implements Prototype.BuilderDecorator<WebClientServiceResponse.BuilderBase<?, ?>> { | ||
@Override | ||
public void decorate(WebClientServiceResponse.BuilderBase<?, ?> target) { | ||
if (target.trailers().isEmpty()) { | ||
// Empty trailers by default | ||
target.trailers(ClientResponseTrailers.create()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.