Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Custom Header - In AddHeaderPolicy and RequestIDPolicy #6602

Merged
merged 10 commits into from
Dec 9, 2019
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.azure.core.http.policy;

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
* {@link Context} with key 'azure-request-override-http-headers-key'. The value for this key should be of type
* {@link HttpHeaders} for it to be added in {@link HttpRequest}.
*/
public class OverrideHeaderPolicy implements HttpPipelinePolicy {

@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) {
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
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.
* 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) {
this.requestIdHeaderName = Objects.requireNonNull(requestIdHeaderName,
"messageIdHeaderName can not be null.");
}

/**
* 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";

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