Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -0,0 +1,39 @@
package com.azure.core.http.policy;
Comment thread
hemanttanwar marked this conversation as resolved.
Outdated

import com.azure.core.http.HttpHeader;
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpPipelineCallContext;
import com.azure.core.http.HttpPipelineNextPolicy;
import com.azure.core.http.HttpResponse;
import com.azure.core.util.Context;
import com.azure.core.http.HttpRequest;
import static com.azure.core.util.Context.AZURE_REQUEST_HTTP_HEADERS_KEY;
import reactor.core.publisher.Mono;

import java.util.Objects;
import java.util.Optional;

/**
* The pipeline policy that override or add {@link HttpHeader} in {@link HttpRequest} by reading values from
Comment thread
hemanttanwar marked this conversation as resolved.
Outdated
* {@link Context} with key 'azure-request-override-http-headers-key'. The value for this key should be of type
Comment thread
hemanttanwar marked this conversation as resolved.
Outdated
* {@link HttpHeaders} for it to be added in {@link HttpRequest}.
*/
public class OverrideHeaderPolicy implements HttpPipelinePolicy {
Comment thread
hemanttanwar marked this conversation as resolved.
Outdated

@Override
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {

Optional<Object> customHttpHeadersObject = context.getData(AZURE_REQUEST_HTTP_HEADERS_KEY);
if (!Objects.isNull(customHttpHeadersObject) && customHttpHeadersObject.get() instanceof HttpHeaders) {
Comment thread
hemanttanwar marked this conversation as resolved.
Outdated
HttpHeaders customHttpHeaders = (HttpHeaders) customHttpHeadersObject.get();
// loop through customHttpHeaders and add header in HttpRequest
for (HttpHeader httpHeader : customHttpHeaders) {
if (!Objects.isNull(httpHeader.getName()) && !Objects.isNull(httpHeader.getValue())) {
context.getHttpRequest().getHeaders().put(httpHeader.getName(), httpHeader.getValue());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a put overload into HttpHeaders which accepts a HttpHeader?

}
}
}
return next.process();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,43 @@
import com.azure.core.http.HttpPipelineCallContext;
import com.azure.core.http.HttpPipelineNextPolicy;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.HttpHeader;
import reactor.core.publisher.Mono;

import java.util.Objects;
import java.util.UUID;

/**
* The pipeline policy that puts a UUID in the request header. Azure uses the request id as
* the unique identifier for the request.
* The pipeline policy that puts a UUID in the request header. The default {@link HttpHeader} name can be overwritten.
Comment thread
hemanttanwar marked this conversation as resolved.
Outdated
* Azure uses the request id as the unique identifier for the request.
*/
public class RequestIdPolicy implements HttpPipelinePolicy {

private static final String REQUEST_ID_HEADER = "x-ms-client-request-id";
private final String requestIdHeaderName;

/**
* Creates {@link RequestIdPolicy} with provided {@code requestIdHeaderName}.
* @param requestIdHeaderName to be used to set in {@link HttpRequest}.
*/
public RequestIdPolicy(String requestIdHeaderName) {
Comment thread
hemanttanwar marked this conversation as resolved.
this.requestIdHeaderName = Objects.requireNonNull(requestIdHeaderName,
"messageIdHeaderName can not be null.");
Comment thread
hemanttanwar marked this conversation as resolved.
Outdated
}

/**
* Creates default {@link RequestIdPolicy} with default header name 'x-ms-client-request-id'.
*/
public RequestIdPolicy() {
requestIdHeaderName = REQUEST_ID_HEADER;
}

@Override
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
String requestId = context.getHttpRequest().getHeaders().getValue(REQUEST_ID_HEADER);
String requestId = context.getHttpRequest().getHeaders().getValue(requestIdHeaderName);
if (requestId == null) {
context.getHttpRequest().getHeaders().put(REQUEST_ID_HEADER, UUID.randomUUID().toString());
context.getHttpRequest().getHeaders().put(requestIdHeaderName, UUID.randomUUID().toString());
}
return next.process();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.azure.core.annotation.Immutable;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.http.HttpHeaders;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -23,6 +24,9 @@
public class Context {
private final ClientLogger logger = new ClientLogger(Context.class);

/**Key used to override headers in HttpRequest. The Value for this key should be {@link HttpHeaders}.*/
public static String AZURE_REQUEST_HTTP_HEADERS_KEY = "azure-request-override-http-headers-key";
Comment thread
alzimmermsft marked this conversation as resolved.
Outdated

// All fields must be immutable.
//
/**
Expand Down