-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add possibility to add default headers to request.
- Loading branch information
Showing
9 changed files
with
374 additions
and
35 deletions.
There are no files selected for viewing
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
64 changes: 64 additions & 0 deletions
64
src/main/java/io/github/nstdio/http/ext/HeadersAddingInterceptor.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,64 @@ | ||
/* | ||
* Copyright (C) 2022 Edgar Asatryan | ||
* | ||
* 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.github.nstdio.http.ext; | ||
|
||
import java.net.http.HttpHeaders; | ||
import java.net.http.HttpRequest; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
class HeadersAddingInterceptor implements Interceptor { | ||
private final Map<String, String> headers; | ||
private final Map<String, Supplier<String>> resolvableHeaders; | ||
|
||
HeadersAddingInterceptor(Map<String, String> headers, Map<String, Supplier<String>> resolvableHeaders) { | ||
this.headers = headers; | ||
this.resolvableHeaders = resolvableHeaders; | ||
} | ||
|
||
@Override | ||
public <T> Chain<T> intercept(Chain<T> in) { | ||
if (in.response().isPresent() || !hasHeaders()) { | ||
return in; | ||
} | ||
|
||
return in.withRequest(apply(in.request())); | ||
} | ||
|
||
private HttpRequest apply(HttpRequest request) { | ||
var headers = addHeaders(request.headers()); | ||
|
||
var builder = HttpRequests.toBuilderOmitHeaders(request); | ||
headers.forEach((name, values) -> values.forEach(v -> builder.header(name, v))); | ||
|
||
return builder.build(); | ||
} | ||
|
||
private Map<String, List<String>> addHeaders(HttpHeaders h) { | ||
var headersBuilder = new HttpHeadersBuilder(h); | ||
|
||
headers.forEach(headersBuilder::add); | ||
resolvableHeaders.forEach((name, valueSupplier) -> headersBuilder.add(name, valueSupplier.get())); | ||
|
||
return headersBuilder.map(); | ||
} | ||
|
||
private boolean hasHeaders() { | ||
return !headers.isEmpty() || !resolvableHeaders.isEmpty(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (C) 2022 Edgar Asatryan | ||
* | ||
* 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.github.nstdio.http.ext; | ||
|
||
import java.net.http.HttpRequest; | ||
|
||
class HttpRequests { | ||
static HttpRequest.Builder toBuilderOmitHeaders(HttpRequest r) { | ||
var builder = HttpRequest.newBuilder(); | ||
builder | ||
.uri(r.uri()) | ||
.method(r.method(), r.bodyPublisher().orElseGet(HttpRequest.BodyPublishers::noBody)) | ||
.expectContinue(r.expectContinue()); | ||
|
||
r.version().ifPresent(builder::version); | ||
r.timeout().ifPresent(builder::timeout); | ||
|
||
return builder; | ||
} | ||
|
||
static HttpRequest.Builder toBuilder(HttpRequest r) { | ||
var builder = toBuilderOmitHeaders(r); | ||
r.headers().map().forEach((name, values) -> values.forEach(value -> builder.header(name, value))); | ||
|
||
return builder; | ||
} | ||
} |
Oops, something went wrong.