package org.radarcns.management.web.rest;


import java.net.MalformedURLException;

import com.codahale.metrics.annotation.Timed;
import org.radarcns.auth.config.Constants;
import org.radarcns.management.service.MetaTokenService;
import org.radarcns.management.service.dto.ClientPairInfoDTO;
import org.radarcns.management.service.dto.TokenDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class MetaTokenResource {

    private final Logger log = LoggerFactory.getLogger(OAuthClientsResource.class);

    @Autowired
    private MetaTokenService metaTokenService;

    /**
     * GET /api/meta-token/:tokenName.
     *
     * <p>Get refresh-token available under this tokenName.</p>
     *
     * @param tokenName the tokenName given after pairing the subject with client
     * @return the client as a {@link ClientPairInfoDTO}
     */
    @GetMapping("/meta-token/{tokenName:" + Constants.TOKEN_NAME_REGEX + "}")
    @Timed
    public ResponseEntity<TokenDTO> getTokenByTokenName(@PathVariable("tokenName") String tokenName)
            throws MalformedURLException {
        log.info("Requesting token with tokenName {}", tokenName);
        // check whether token.read permission is available
        return ResponseEntity.ok().body(metaTokenService.fetchToken(tokenName));
    }
}

@@ -75,16 +75,54 @@
 ception;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aimport
@@ -1597,59 +1597,229 @@
     
-// check whether token.read permission is available
+TokenDTO retrievedToken = metaTokenService.fetchToken(tokenName);%0A%0A        try %7B%0A            TimeUnit.SECONDS.sleep(1);%0A        %7D catch (InterruptedException e) %7B%0A            Thread.currentThread().interrupt();%0A        %7D%0A
 %0A   
@@ -1859,46 +1859,22 @@
 ody(
-metaTokenService.fetchToken(tokenName)
+retrievedToken
 );%0A 

add sleep before returning the response