-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
191 additions
and
9 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
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
35 changes: 35 additions & 0 deletions
35
...ial-client/src/main/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwClientException.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,35 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.tractusx.edc.iam.ssi.miw.api; | ||
|
||
import okhttp3.Response; | ||
import org.eclipse.edc.spi.http.EdcHttpClientException; | ||
|
||
public class MiwClientException extends EdcHttpClientException { | ||
private final Response response; | ||
|
||
public MiwClientException(String message) { | ||
this(message, null); | ||
} | ||
|
||
public MiwClientException(String message, Response response) { | ||
super(message); | ||
this.response = response; | ||
} | ||
|
||
public Response getResponse() { | ||
return response; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...l-client/src/main/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwFallbackFactories.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,55 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.tractusx.edc.iam.ssi.miw.api; | ||
|
||
import dev.failsafe.Fallback; | ||
import dev.failsafe.event.ExecutionAttemptedEvent; | ||
import dev.failsafe.function.CheckedFunction; | ||
import okhttp3.Response; | ||
import org.eclipse.edc.spi.http.FallbackFactory; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.lang.String.format; | ||
|
||
public interface MiwFallbackFactories { | ||
|
||
static FallbackFactory retryWhenStatusIsNot(int status) { | ||
return retryWhenStatusIsNotIn(status); | ||
} | ||
|
||
/** | ||
* Verifies that the response has a specific statuses, otherwise it should be retried | ||
* | ||
* @return the {@link FallbackFactory} | ||
*/ | ||
static FallbackFactory retryWhenStatusIsNotIn(int... status) { | ||
var codes = Arrays.stream(status).boxed().collect(Collectors.toSet()); | ||
return request -> { | ||
CheckedFunction<ExecutionAttemptedEvent<? extends Response>, Exception> exceptionSupplier = event -> { | ||
var response = event.getLastResult(); | ||
if (response == null) { | ||
return new MiwClientException(event.getLastException().getMessage()); | ||
} else { | ||
return new MiwClientException(format("Server response to %s was not one of %s but was %s", request, Arrays.toString(status), response.code()), response); | ||
} | ||
}; | ||
return Fallback.builderOfException(exceptionSupplier) | ||
.handleResultIf(r -> !codes.contains(r.code())) | ||
.build(); | ||
}; | ||
} | ||
} |