Search in sources :

Example 6 with ForbiddenException

use of org.sonar.server.exceptions.ForbiddenException in project sonarqube by SonarSource.

the class QualityGateAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    response.setHeader("Cache-Control", "no-cache");
    response.stream().setMediaType(SVG);
    try (DbSession dbSession = dbClient.openSession(false)) {
        support.validateToken(request);
        BranchDto branch = support.getBranch(dbSession, request);
        Level qualityGateStatus = getQualityGate(dbSession, branch);
        String result = svgGenerator.generateQualityGate(qualityGateStatus);
        String eTag = getETag(result);
        Optional<String> requestedETag = request.header("If-None-Match");
        if (requestedETag.filter(eTag::equals).isPresent()) {
            response.stream().setStatus(304);
            return;
        }
        response.setHeader("ETag", eTag);
        write(result, response.stream().output(), UTF_8);
    } catch (ProjectBadgesException | ForbiddenException | NotFoundException e) {
        // There is an issue, so do not return any ETag but make this response expire now
        SimpleDateFormat sdf = new SimpleDateFormat(RFC1123_DATE, Locale.US);
        response.setHeader("Expires", sdf.format(new Date()));
        write(svgGenerator.generateError(e.getMessage()), response.stream().output(), UTF_8);
    }
}
Also used : DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) NotFoundException(org.sonar.server.exceptions.NotFoundException) Level(org.sonar.api.measures.Metric.Level) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

ForbiddenException (org.sonar.server.exceptions.ForbiddenException)6 DbSession (org.sonar.db.DbSession)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 RuleSetChangedEvent (org.sonar.core.util.RuleSetChangedEvent)2 BranchDto (org.sonar.db.component.BranchDto)2 NotFoundException (org.sonar.server.exceptions.NotFoundException)2 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 Level (org.sonar.api.measures.Metric.Level)1 RuleChange (org.sonar.core.util.RuleChange)1 LiveMeasureDto (org.sonar.db.measure.LiveMeasureDto)1 MetricDto (org.sonar.db.metric.MetricDto)1 UserDto (org.sonar.db.user.UserDto)1