-1

I am calling below API using jersey

JsonObject response = ConnectionUtil.getwebTarget()
                                        .request().
                 header("Authorization", "Bearer "+ access_token)
                                        .get(JsonObject.class);

Here accessToken is variable which got value by calling another API. Now if response.getStatus() returnd 401 I want to call the API to get token and will call the above commnad again. how can I stop myself from duplication? As of now I am writing below code.

JsonObject response = ConnectionUtil.getwebTarget()
                                            .request().
                     header("Authorization", "Bearer "+ access_token)
                                            .get(JsonObject.class);
  if(response.getStatus()==401)
  {
    accessToken= new AccessToken().getAccessToken();
    JsonObject response = ConnectionUtil.getwebTarget()
                                            .request()
                                            .header("Authorization", "Bearer "+ access_token)
                                            .get(JsonObject.class);
}

how can I reduce writing the code of line JsonObject response twice. I atleast want to check twice 401 code before throwing customized exception

I am new to java programming. can somebody tell me logic. its basic coding but I am still struggling.

Shruti sharma
  • 199
  • 6
  • 21
  • 67

3 Answers3

1

Write a subroutine.

private JsonObject doRequest(AccessToken access_token) {
    return ConnectionUtil.getwebTarget()
             .request()
             .header("Authorization", "Bearer "+ access_token)
             .get(JsonObject.class);
}

and then use it twice

JsonObject response = doRequest(access_token);
if (response.getStatus() == 401) {
    access_token = new AccessToken().getAccessToken();
    response = doRequest(access_token);
}

'doRequest' might not be the best name to choose, but I don't know what the big picture is - i.e., what is it actually doing?

user13784117
  • 1,124
  • 4
  • 4
  • thank you @user13784117 . But if again and again 401 error comes then it will go to infinite loop. – Shruti sharma Jul 31 '20 at 23:36
  • There is no loop. You 'doRequest' once. If that fails, then you 'doRequest' again. That is the end of it. It is an **if** statement, not any of the loop statements (**for**, **while**, or **do**). – user13784117 Aug 01 '20 at 01:10
1

this is a good candidate for a do...while loop. do...while loops guarantee execution at least once, and are a great native java tool to implement this type of retry semantic.

AccessToken accessToken;
JsonObject response;
int retryCount = 0;
final int MAX_RETRIES = 2;

do {
    accessToken= new AccessToken().getAccessToken();
    response = ConnectionUtil.getwebTarget().request().header("Authorization", "Bearer "+ access_token).get(JsonObject.class);
} while (response.getStatus()==401 && retryCount++ < MAX_RETRIES);

if (response.getStatus() == 401) {
    throw new CustomException("");
}

Worth checking out this reference as well if you want to learn more

bluesky33
  • 69
  • 4
0

If you're looking for a policy based retry, you can do something like this.

RetryPolicy retryPolicy = new RetryPolicy()
  .retryIf((ClientResponse response) -> response.getStatus() == 401)
  .withDelay(1, TimeUnit.SECONDS)
  .withMaxRetries(3);

Failsafe.with(retryPolicy).get(() -> webResource.post(ClientResponse.class, input));
stackguy
  • 188
  • 5