class TokenCertificateService {

    public GeneratedCertRequestInfo regenerateCertRequest(String keyId, String csrId, CertificateRequestFormat format)
            throws KeyNotFoundException, CsrNotFoundException, ActionNotPossibleException {

        // validate key and memberId existence
        TokenInfo tokenInfo = tokenService.getTokenForKeyId(keyId);
        KeyInfo keyInfo = keyService.getKey(tokenInfo, keyId);
        getCsr(keyInfo, csrId);

        // currently regenerate is part of "generate csr" or
        // "combo generate key + csr" operations in the web application.
        // There are no separate permissions or possible action rules for
        // downloading the csr.
        // Downloading a csr means actually re-generating it, so the
        // possible action rules for it would be very close, if not identical,
        // to generate csr.

        // check usage type specific auth in service, since controller does not know usage type
        if (keyInfo.isForSigning()) {
            securityHelper.verifyAuthority("GENERATE_SIGN_CERT_REQ");
        } else {
            securityHelper.verifyAuthority("GENERATE_AUTH_CERT_REQ");
        }

        // validate that regenerate csr is a possible action
        if (keyInfo.isForSigning()) {
            possibleActionsRuleEngine.requirePossibleKeyAction(PossibleActionEnum.GENERATE_SIGN_CSR,
                    tokenInfo, keyInfo);
        } else {
            possibleActionsRuleEngine.requirePossibleKeyAction(PossibleActionEnum.GENERATE_AUTH_CSR,
                    tokenInfo, keyInfo);
        }

        try {
            return signerProxyFacade.regenerateCertRequest(csrId, format);
        } catch (CodedException e) {
            throw e;
        } catch (Exception e) {
            throw new SignerNotReachableException("Regenerate cert request failed", e);
        }
    }

    @Autowired @SuppressWarnings("checkstyle:ParameterNumber") public  TokenCertificateService(SignerProxyFacade signerProxyFacade, ClientService clientService,
            CertificateAuthorityService certificateAuthorityService, KeyService keyService, DnFieldHelper dnFieldHelper,
            GlobalConfService globalConfService, GlobalConfFacade globalConfFacade, ClientRepository clientRepository,
            ManagementRequestSenderService managementRequestSenderService, ServerConfService serverConfService,
            PossibleActionsRuleEngine possibleActionsRuleEngine, TokenService tokenService,
            SecurityHelper securityHelper, AuditDataHelper auditDataHelper,
            AuditEventHelper auditEventHelper);

}