-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX: Editing ChangeLog and Readme for CallingServer. (#23167)
Also, Applying new comments received for the RedirectPolicy
- Loading branch information
Showing
10 changed files
with
101 additions
and
92 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
5 changes: 3 additions & 2 deletions
5
sdk/communication/azure-communication-callingserver/CHANGELOG.md
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
70 changes: 70 additions & 0 deletions
70
...er/src/main/java/com/azure/communication/callingserver/implementation/RedirectPolicy.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,70 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
package com.azure.communication.callingserver.implementation; | ||
|
||
import com.azure.core.http.HttpPipelineCallContext; | ||
import com.azure.core.http.HttpPipelineNextPolicy; | ||
import com.azure.core.http.HttpRequest; | ||
import com.azure.core.http.HttpResponse; | ||
import com.azure.core.http.policy.HttpPipelinePolicy; | ||
import com.azure.core.util.logging.ClientLogger; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* HttpPipelinePolicy to redirect requests when a redirect response (Http codes 301 or 302) is received to the | ||
* new location marked by the Location header. | ||
*/ | ||
public final class RedirectPolicy implements HttpPipelinePolicy { | ||
private static final int MAX_REDIRECTS = 10; | ||
private static final String LOCATION_HEADER_NAME = "Location"; | ||
private static final int SC_MOVED_PERMANENTLY = 301; | ||
private static final int SC_MOVED_TEMPORARILY = 302; | ||
|
||
private final ClientLogger logger = new ClientLogger(RedirectPolicy.class); | ||
|
||
@Override | ||
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { | ||
return attemptRedirection(context, next, 0, new HashSet<>()); | ||
} | ||
|
||
private Mono<HttpResponse> attemptRedirection(HttpPipelineCallContext context, HttpPipelineNextPolicy next, | ||
int redirectNumber, Set<String> attemptedRedirectLocations) { | ||
return next.clone().process().flatMap(httpResponse -> { | ||
if (isRedirectResponse(httpResponse) | ||
&& shouldRedirect(httpResponse, context, redirectNumber, attemptedRedirectLocations)) { | ||
String newLocation = httpResponse.getHeaderValue(LOCATION_HEADER_NAME); | ||
attemptedRedirectLocations.add(newLocation); | ||
|
||
HttpRequest newRequest = context.getHttpRequest().copy(); | ||
newRequest.setUrl(newLocation); | ||
context.setHttpRequest(newRequest); | ||
|
||
return attemptRedirection(context, next, redirectNumber + 1, attemptedRedirectLocations); | ||
} | ||
return Mono.just(httpResponse); | ||
}); | ||
} | ||
|
||
private boolean isRedirectResponse(HttpResponse response) { | ||
return response.getStatusCode() == SC_MOVED_TEMPORARILY || response.getStatusCode() == SC_MOVED_PERMANENTLY; | ||
} | ||
|
||
private boolean shouldRedirect(HttpResponse response, HttpPipelineCallContext context, int retryCount, | ||
Set<String> attemptedRedirectLocations) { | ||
if (retryCount > MAX_REDIRECTS) { | ||
logger.error(String.format("Request to %s has been redirected more than %s times.", | ||
context.getHttpRequest().getUrl(), MAX_REDIRECTS)); | ||
return false; | ||
} | ||
if (attemptedRedirectLocations.contains(response.getHeaderValue(LOCATION_HEADER_NAME))) { | ||
logger.error(String.format("Request to %s was redirected more than once to: %s", | ||
context.getHttpRequest().getUrl(), response.getHeaderValue(LOCATION_HEADER_NAME))); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...n/implementation/RedirectPolicyTests.java → ...r/implementation/RedirectPolicyTests.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
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 |
---|---|---|
|
@@ -62,4 +62,4 @@ Check out other client libraries for Azure communication service | |
[cla]: https://cla.microsoft.com | ||
[coc]: https://opensource.microsoft.com/codeofconduct/ | ||
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ | ||
[coc_contact]: mailto:[email protected] | ||
[coc_contact]: mailto:[email protected] |
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
50 changes: 0 additions & 50 deletions
50
...on-common/src/main/java/com/azure/communication/common/implementation/RedirectPolicy.java
This file was deleted.
Oops, something went wrong.