-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add Custom Header - In AddHeaderPolicy and RequestIDPolicy #6602
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
Merged
hemanttanwar
merged 10 commits into
Azure:master
from
hemanttanwar:6217_RequestID_AddHeaderPolicy_customHeader
Dec 9, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
eccfbf6
initial design
5e650a6
Adding OverrideHeaderPolicy as discussed.
bf58b5d
Adding more documentation and validations.
f7d4359
Renamed OverrideHeaderPolicy to AddHeadersFromContextPolicy and reso…
04c34a2
Adding unit test and incorporating review comments.
ecbc5dc
fix checkstyle issue in Java snippet
5c87630
Incorporating review comments
231a5da
Adding code anipper for new policy and incorporating review comments.
1bc9a6a
Adding code sample in new policy
5ac3436
Removed changes in ConfigurationClientTest .
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...core/azure-core/src/main/java/com/azure/core/http/policy/AddHeadersFromContextPolicy.java
This file contains hidden or 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,61 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| 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 reactor.core.publisher.Mono; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * The pipeline policy that override or add {@link HttpHeaders} in {@link HttpRequest} by reading values from | ||
| * {@link Context} with key 'azure-http-headers-key'. The value for this key should be of type {@link HttpHeaders} for | ||
| * it to be added in {@link HttpRequest}. | ||
| * | ||
| * <p><strong>Code Sample: Add multiple HttpHeader in Context and call client</strong></p> | ||
| * <pre> | ||
| * // Create ConfigurationClient for example | ||
| * ConfigurationClient configurationClient = new ConfigurationClientBuilder() | ||
| * .connectionString("endpoint={endpoint_value};id={id_value};secret={secret_value}") | ||
| * .buildClient(); | ||
| * // Add your headers | ||
| * HttpHeaders headers = new HttpHeaders(); | ||
| * headers.put("my-header1", "my-header1-value"); | ||
| * headers.put("my-header2", "my-header2-value"); | ||
| * headers.put("my-header3", "my-header3-value"); | ||
| * // Call API by passing headers in Context. | ||
| * configurationClient.addConfigurationSettingWithResponse( | ||
| * new ConfigurationSetting().setKey("key").setValue("value"), | ||
| * new Context(AddHeadersFromContextPolicy.AZURE_REQUEST_HTTP_HEADERS_KEY, headers)); | ||
| * // Above three HttpHeader will be added in outgoing HttpRequest. | ||
| * </pre> | ||
| */ | ||
| public class AddHeadersFromContextPolicy implements HttpPipelinePolicy { | ||
hemanttanwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /**Key used to override headers in HttpRequest. The Value for this key should be {@link HttpHeaders}.*/ | ||
| public static final String AZURE_REQUEST_HTTP_HEADERS_KEY = "azure-http-headers-key"; | ||
|
|
||
| @Override | ||
| public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { | ||
| context.getData(AZURE_REQUEST_HTTP_HEADERS_KEY).ifPresent(headers -> { | ||
| if (headers instanceof HttpHeaders) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am checking for |
||
| HttpHeaders customHttpHeaders = (HttpHeaders) headers; | ||
| // loop through customHttpHeaders and add headers in HttpRequest | ||
| for (HttpHeader httpHeader : customHttpHeaders) { | ||
| if (!Objects.isNull(httpHeader.getName()) && !Objects.isNull(httpHeader.getValue())) { | ||
| context.getHttpRequest().getHeaders().put(httpHeader.getName(), httpHeader.getValue()); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| return next.process(); | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or 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
20 changes: 20 additions & 0 deletions
20
...e-core/src/samples/java/com/azure/core/http/policy/RequestIdPolicyJavaDocCodeSnippet.java
This file contains hidden or 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,20 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.core.http.policy; | ||
|
|
||
| /** | ||
| * Code snippets for {@link RequestIdPolicy} | ||
| */ | ||
| public class RequestIdPolicyJavaDocCodeSnippet { | ||
|
|
||
| /** | ||
| * Code snippets for using {@link RequestIdPolicy#RequestIdPolicy(String)} } | ||
| */ | ||
| public void overrideRequestIdHeaderName() { | ||
|
|
||
| // BEGIN: com.azure.core.http.policy.RequestIdPolicy.constructor.overrideRequestIdHeaderName | ||
| new RequestIdPolicy("x-ms-my-custom-request-id"); | ||
| // END: com.azure.core.http.policy.RequestIdPolicy.constructor.overrideRequestIdHeaderName | ||
| } | ||
| } |
87 changes: 87 additions & 0 deletions
87
.../azure-core/src/test/java/com/azure/core/http/policy/AddHeadersFromContextPolicyTest.java
This file contains hidden or 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,87 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.core.http.policy; | ||
|
|
||
| import com.azure.core.http.HttpHeaders; | ||
| import com.azure.core.http.HttpMethod; | ||
| import com.azure.core.http.HttpPipeline; | ||
| import com.azure.core.http.HttpPipelineBuilder; | ||
| import com.azure.core.http.HttpRequest; | ||
| import com.azure.core.http.HttpResponse; | ||
| import com.azure.core.http.clients.NoOpHttpClient; | ||
| import com.azure.core.util.Context; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import reactor.core.publisher.Flux; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.net.URL; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.Charset; | ||
|
|
||
| public class AddHeadersFromContextPolicyTest { | ||
|
|
||
| private final HttpResponse mockResponse = new HttpResponse(null) { | ||
| @Override | ||
| public int getStatusCode() { | ||
| return 500; | ||
| } | ||
|
|
||
| @Override | ||
| public String getHeaderValue(String name) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public HttpHeaders getHeaders() { | ||
| return new HttpHeaders(); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<byte[]> getBodyAsByteArray() { | ||
| return Mono.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Flux<ByteBuffer> getBody() { | ||
| return Flux.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<String> getBodyAsString() { | ||
| return Mono.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<String> getBodyAsString(Charset charset) { | ||
| return Mono.empty(); | ||
| } | ||
| }; | ||
|
|
||
| @Test | ||
| public void clientProvidedMultipleHeaderTest() throws Exception { | ||
| // Create custom Headers | ||
| String customRequestId = "request-id-value"; | ||
| final HttpHeaders headers = new HttpHeaders(); | ||
| headers.put("x-ms-client-request-id", customRequestId); | ||
| headers.put("my-header1", "my-header1-value"); | ||
| headers.put("my-header2", "my-header2-value"); | ||
|
|
||
| final HttpPipeline pipeline = new HttpPipelineBuilder() | ||
| .httpClient(new NoOpHttpClient() { | ||
| @Override | ||
| public Mono<HttpResponse> send(HttpRequest request) { | ||
| Assertions.assertEquals(request.getHeaders().getValue("x-ms-client-request-id"), customRequestId); | ||
| Assertions.assertEquals(request.getHeaders().getValue("my-header1"), "my-header1-value"); | ||
| Assertions.assertEquals(request.getHeaders().getValue("my-header2"), "my-header2-value"); | ||
| return Mono.just(mockResponse); | ||
| } | ||
| }) | ||
| .policies(new RequestIdPolicy()) | ||
| .policies(new AddHeadersFromContextPolicy()) | ||
| .build(); | ||
|
|
||
| pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost/")), new Context(AddHeadersFromContextPolicy.AZURE_REQUEST_HTTP_HEADERS_KEY, headers)).block(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.