Search in sources :

Example 36 with ApiResponse

use of com.searchcode.app.dto.api.ApiResponse in project searchcode-server by boyter.

the class ApiRouteService method repoDelete.

public ApiResponse repoDelete(Request request, Response response) {
    if (!apiEnabled) {
        return new ApiResponse(false, "API not enabled");
    }
    String publicKey = request.queryParams(Values.PUB);
    String signedKey = request.queryParams(Values.SIG);
    String reponames = request.queryParams(Values.REPONAME);
    String hmacTypeString = request.queryParams(Values.HMAC);
    hmacTypeString = hmacTypeString == null ? Values.EMPTYSTRING : hmacTypeString;
    if (reponames == null || reponames.trim().equals(Values.EMPTYSTRING)) {
        return new ApiResponse(false, "reponame is a required parameter");
    }
    if (apiAuth) {
        if (publicKey == null || publicKey.trim().equals(Values.EMPTYSTRING)) {
            return new ApiResponse(false, "pub is a required parameter");
        }
        if (signedKey == null || signedKey.trim().equals(Values.EMPTYSTRING)) {
            return new ApiResponse(false, "sig is a required parameter");
        }
        String toValidate = null;
        try {
            toValidate = String.format("pub=%s&reponame=%s", URLEncoder.encode(publicKey, java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(reponames, java.nio.charset.StandardCharsets.UTF_8.toString()));
        } catch (UnsupportedEncodingException ex) {
            this.logger.severe(String.format("940323e9::error in class %s exception %s", ex.getClass(), ex.getMessage()));
            return new ApiResponse(false, "invalid signed url");
        }
        ApiService.HmacType hmacType = hmacTypeString.toLowerCase().equals("sha512") ? ApiService.HmacType.SHA512 : ApiService.HmacType.SHA1;
        boolean validRequest = apiService.validateRequest(publicKey, signedKey, toValidate, hmacType);
        if (!validRequest) {
            this.logger.apiLog("09353e0e::invalid signed repodelete call using publickey=" + publicKey);
            return new ApiResponse(false, "invalid signed url");
        }
    }
    Optional<RepoResult> repoResult = this.repo.getRepoByName(reponames);
    if (!repoResult.isPresent()) {
        return new ApiResponse(false, "repository already deleted");
    }
    repoResult.ifPresent(x -> this.dataService.addToPersistentDelete(x.getName()));
    this.logger.apiLog("bafbae4f::valid signed repodelete call using publickey=" + publicKey);
    return new ApiResponse(true, "repository queued for deletion");
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) ApiResponse(com.searchcode.app.dto.api.ApiResponse) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse) RepoResult(com.searchcode.app.model.RepoResult)

Aggregations

ApiResponse (com.searchcode.app.dto.api.ApiResponse)36 RepoResultApiResponse (com.searchcode.app.dto.api.RepoResultApiResponse)36 ApiRouteService (com.searchcode.app.service.route.ApiRouteService)32 Request (spark.Request)28 Helpers (com.searchcode.app.util.Helpers)26 LoggerWrapper (com.searchcode.app.util.LoggerWrapper)26 SQLiteRepo (com.searchcode.app.dao.SQLiteRepo)20 RepoResult (com.searchcode.app.model.RepoResult)9 ValidatorResult (com.searchcode.app.model.ValidatorResult)3 IndexService (com.searchcode.app.service.index.IndexService)3 UniqueRepoQueue (com.searchcode.app.util.UniqueRepoQueue)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Repo (com.searchcode.app.dao.Repo)1