// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.aad.msal4j;

class AcquireTokenSilentSupplier extends AuthenticationResultSupplier {

    private SilentRequest silentRequest;

    AcquireTokenSilentSupplier(ClientApplicationBase clientApplication, SilentRequest silentRequest) {
        super(clientApplication, silentRequest);

        this.silentRequest = silentRequest;
    }

    @Override
    AuthenticationResult execute() throws Exception {
        Authority requestAuthority = silentRequest.requestAuthority();
        if(requestAuthority.authorityType != AuthorityType.B2C){
            requestAuthority =
                    getAuthorityWithPrefNetworkHost(silentRequest.requestAuthority().authority());
        }

        AuthenticationResult res;

        if(silentRequest.parameters().account() == null){
            res = clientApplication.tokenCache.getCachedAuthenticationResult(
                    requestAuthority,
                    silentRequest.parameters().scopes(),
                    clientApplication.clientId());
        }
        else {
            res = clientApplication.tokenCache.getCachedAuthenticationResult(
                    silentRequest.parameters().account(),
                    requestAuthority,
                    silentRequest.parameters().scopes(),
                    clientApplication.clientId());

            if (silentRequest.parameters().forceRefresh() || StringHelper.isBlank(res.accessToken())) {

                if (!StringHelper.isBlank(res.refreshToken())) {
                    RefreshTokenRequest refreshTokenRequest = new RefreshTokenRequest(
                            RefreshTokenParameters.builder(silentRequest.parameters().scopes(), res.refreshToken()).build(),
                            silentRequest.application(),
                            silentRequest.requestContext());

                    AcquireTokenByAuthorizationGrantSupplier acquireTokenByAuthorisationGrantSupplier =
                            new AcquireTokenByAuthorizationGrantSupplier(clientApplication, refreshTokenRequest, requestAuthority);

                    res = acquireTokenByAuthorisationGrantSupplier.execute();
                }
                else{
                    res = null;
                }
            }
        }
        if(res == null || StringHelper.isBlank(res.accessToken())){
            throw new MsalClientException(AuthenticationErrorMessage.NO_TOKEN_IN_CACHE, AuthenticationErrorCode.CACHE_MISS);
        }

        clientApplication.getServiceBundle().getServerSideTelemetry().incrementSilentSuccessfulCount();
        return res;
    }
}

@@ -593,16 +593,17 @@
       if
+ 
 (request
@@ -647,16 +647,17 @@
 ype.B2C)
+ 
 %7B%0A      
@@ -836,16 +836,17 @@
       if
+ 
 (silentR
@@ -883,16 +883,17 @@
 == null)
+ 
 %7B%0A      
@@ -1119,24 +1119,16 @@
        %7D
-%0A       
  else %7B%0A
@@ -1406,24 +1406,211 @@
 ientId());%0A%0A
+            if (!StringHelper.isBlank(res.accessToken())) %7B%0A                clientApplication.getServiceBundle().getServerSideTelemetry().incrementSilentSuccessfulCount();%0A            %7D%0A%0A
             
@@ -2434,29 +2434,14 @@
    %7D
-%0A               
  else
+ 
 %7B%0A  
@@ -2522,16 +2522,17 @@
       if
+ 
 (res == 
@@ -2579,16 +2579,17 @@
 oken()))
+ 
 %7B%0A      
@@ -2722,112 +2722,8 @@
  %7D%0A%0A
-        clientApplication.getServiceBundle().getServerSideTelemetry().incrementSilentSuccessfulCount();%0A
     

Update how successful count is calculated (#225)