Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ public MessageExchange<HttpRequest, HttpResponse> messageExchange() {
@Override
public HttpRequest setServiceEndpoint(HttpRequest request, Endpoint endpoint) {
var merged = request.uri().withEndpoint(endpoint.uri());
var requestBuilder = request.toBuilder();
var modifiableRequest = request.toModifiable();
modifiableRequest.setUri(merged);

// Merge in any HTTP headers found on the endpoint.
if (endpoint.property(HttpContext.ENDPOINT_RESOLVER_HTTP_HEADERS) != null) {
requestBuilder.withAddedHeaders(endpoint.property(HttpContext.ENDPOINT_RESOLVER_HTTP_HEADERS));
modifiableRequest.headers().addHeaders(endpoint.property(HttpContext.ENDPOINT_RESOLVER_HTTP_HEADERS));
}

return requestBuilder.uri(merged).build();
return modifiableRequest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package software.amazon.smithy.java.client.http.plugins;

import java.util.List;
import java.util.Locale;
import software.amazon.smithy.java.client.core.AutoClientPlugin;
import software.amazon.smithy.java.client.core.CallContext;
Expand Down Expand Up @@ -78,10 +77,9 @@ static final class UserAgentInterceptor implements ClientInterceptor {
@Override
public <RequestT> RequestT modifyBeforeSigning(RequestHook<?, ?, RequestT> hook) {
if (hook.request() instanceof HttpRequest req && !req.headers().hasHeader("user-agent")) {
return hook.asRequestType(
req.toBuilder()
.withReplacedHeader("user-agent", List.of(createUa(hook.context())))
.build());
var updated = req.toModifiable();
updated.headers().setHeader("user-agent", createUa(hook.context()));
return hook.asRequestType(updated);
}
return hook.request();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public HttpResponse send(Context context, HttpRequest request) {
// Track the request here rather than when the request is created because this method is called
// more often than the request creation method when retries happen.
if (trackRequests) {
requests.add(currentRequest.request());
requests.add(currentRequest.request().withRequest(request.toUnmodifiable()));
}

MockedResult result = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ default B body(Flow.Publisher<ByteBuffer> publisher) {
* @param headers Headers to add and merge in.
* @return the builder.
*/
@SuppressWarnings("unchecked")
default B withAddedHeaders(HttpHeaders headers) {
return withAddedHeaders(headers.map());
headers.forEachEntry(this::withAddedHeader);
return (B) this;
}

/**
Expand All @@ -168,15 +170,26 @@ default B withAddedHeaders(HttpHeaders headers) {
* @param headers Headers to put.
* @return the builder.
*/
@SuppressWarnings("unchecked")
default B withReplacedHeaders(HttpHeaders headers) {
return withReplacedHeaders(headers.map());
headers.forEachEntry(this::withReplacedHeader);
return (B) this;
}

/**
* Replaces a header.
*
* @param name Header name to replace.
* @param values Header value to replace.
* @param value Header value to replace.
* @return the builder.
*/
B withReplacedHeader(String name, String value);

/**
* Replaces a header.
*
* @param name Header name to replace.
* @param values Header values to replace.
* @return the builder.
*/
default B withReplacedHeader(String name, List<String> values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public Builder withReplacedHeaders(Map<String, List<String>> headers) {
return this;
}

@Override
public HttpRequest.Builder withReplacedHeader(String name, String value) {
modifiableHttpRequest.headers().setHeader(name, value);
return this;
}

private void beforeBuild() {
Objects.requireNonNull(modifiableHttpRequest.method(), "method not set");
Objects.requireNonNull(modifiableHttpRequest.uri(), "uri not set");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public Builder withReplacedHeaders(Map<String, List<String>> headers) {
return this;
}

@Override
public HttpResponse.Builder withReplacedHeader(String name, String value) {
modifiableResponse.headers().setHeader(name, value);
return this;
}

@Override
public HttpResponse build() {
return modifiableResponse.toUnmodifiable();
Expand Down
Loading