Search in sources :

Example 1 with Response

use of org.sonar.api.server.ws.Response in project sonarqube by SonarSource.

the class SearchHistoryAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    SearchHistoryResponse searchHistoryResponse = Stream.of(request).map(SearchHistoryAction::toWsRequest).map(search()).map(result -> new SearchHistoryResponseFactory(result).apply()).collect(Collectors.toOneElement());
    writeProtobuf(searchHistoryResponse, request, response);
}
Also used : ComponentFinder(org.sonar.server.component.ComponentFinder) Protobuf.setNullable(org.sonar.core.util.Protobuf.setNullable) DEFAULT_PAGE_SIZE(org.sonarqube.ws.client.measure.SearchHistoryRequest.DEFAULT_PAGE_SIZE) STATUS_PROCESSED(org.sonar.db.component.SnapshotDto.STATUS_PROCESSED) SORT_ORDER(org.sonar.db.component.SnapshotQuery.SORT_ORDER) Date(java.util.Date) PARAM_TO(org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_TO) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) MeasureDto(org.sonar.db.measure.MeasureDto) PARAM_COMPONENT(org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_COMPONENT) Request(org.sonar.api.server.ws.Request) MAX_PAGE_SIZE(org.sonarqube.ws.client.measure.SearchHistoryRequest.MAX_PAGE_SIZE) SORT_FIELD(org.sonar.db.component.SnapshotQuery.SORT_FIELD) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) Response(org.sonar.api.server.ws.Response) PastMeasureQuery(org.sonar.db.measure.PastMeasureQuery) PARAM_METRICS(org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_METRICS) SearchHistoryResponse(org.sonarqube.ws.WsMeasures.SearchHistoryResponse) PARAM_FROM(org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_FROM) Set(java.util.Set) Collectors(org.sonar.core.util.stream.Collectors) DateUtils.parseStartingDateOrDateTime(org.sonar.api.utils.DateUtils.parseStartingDateOrDateTime) KeyExamples(org.sonar.server.ws.KeyExamples) DateUtils.parseEndingDateOrDateTime(org.sonar.api.utils.DateUtils.parseEndingDateOrDateTime) Sets(com.google.common.collect.Sets) String.format(java.lang.String.format) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) UserRole(org.sonar.api.web.UserRole) MetricDto(org.sonar.db.metric.MetricDto) SnapshotQuery(org.sonar.db.component.SnapshotQuery) UserSession(org.sonar.server.user.UserSession) SearchHistoryRequest(org.sonarqube.ws.client.measure.SearchHistoryRequest) SnapshotDto(org.sonar.db.component.SnapshotDto) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) ACTION_SEARCH_HISTORY(org.sonarqube.ws.client.measure.MeasuresWsParameters.ACTION_SEARCH_HISTORY) SearchHistoryResponse(org.sonarqube.ws.WsMeasures.SearchHistoryResponse)

Example 2 with Response

use of org.sonar.api.server.ws.Response in project sonarqube by SonarSource.

the class UpdateAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn();
    try (DbSession dbSession = dbClient.openSession(false)) {
        organizationFlags.checkEnabled(dbSession);
        String key = request.mandatoryParam(PARAM_KEY);
        UpdateOrganizationRequest updateRequest = new UpdateOrganizationRequest(request.getParam(PARAM_NAME, (rqt, paramKey) -> wsSupport.getAndCheckName(rqt)), request.getParam(PARAM_DESCRIPTION, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckDescription(rqt))), request.getParam(PARAM_URL, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckUrl(rqt))), request.getParam(PARAM_AVATAR_URL, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckAvatar(rqt))));
        OrganizationDto dto = getDto(dbSession, key);
        userSession.checkPermission(ADMINISTER, dto);
        dto.setName(updateRequest.getName().or(dto::getName)).setDescription(updateRequest.getDescription().or(dto::getDescription)).setUrl(updateRequest.getUrl().or(dto::getUrl)).setAvatarUrl(updateRequest.getAvatar().or(dto::getAvatarUrl));
        dbClient.organizationDao().update(dbSession, dto);
        dbSession.commit();
        writeResponse(request, response, dto);
    }
}
Also used : OrganizationFlags(org.sonar.server.organization.OrganizationFlags) PARAM_AVATAR_URL(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_AVATAR_URL) ADMINISTER(org.sonar.db.permission.OrganizationPermission.ADMINISTER) PARAM_KEY(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_KEY) DbSession(org.sonar.db.DbSession) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) Organizations(org.sonarqube.ws.Organizations) Request(org.sonar.api.server.ws.Request) DbClient(org.sonar.db.DbClient) WebService(org.sonar.api.server.ws.WebService) PARAM_DESCRIPTION(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_DESCRIPTION) PARAM_URL(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_URL) Response(org.sonar.api.server.ws.Response) Optional(java.util.Optional) OrganizationDto(org.sonar.db.organization.OrganizationDto) UserSession(org.sonar.server.user.UserSession) PARAM_NAME(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_NAME) CheckForNull(javax.annotation.CheckForNull) Nullable(javax.annotation.Nullable) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) DbSession(org.sonar.db.DbSession) OrganizationDto(org.sonar.db.organization.OrganizationDto)

Example 3 with Response

use of org.sonar.api.server.ws.Response in project sonarqube by SonarSource.

the class SetAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String projectKey = request.mandatoryParam(PARAM_PROJECT);
    List<String> tags = request.mandatoryParamAsStrings(PARAM_TAGS).stream().filter(StringUtils::isNotBlank).map(t -> t.toLowerCase(Locale.ENGLISH)).map(SetAction::checkTag).distinct().collect(Collectors.toList());
    try (DbSession dbSession = dbClient.openSession(false)) {
        ComponentDto project = componentFinder.getByKey(dbSession, projectKey);
        checkRequest(project.isRootProject(), "Component must be a project");
        userSession.checkComponentUuidPermission(UserRole.ADMIN, project.uuid());
        project.setTags(tags);
        dbClient.componentDao().updateTags(dbSession, project);
        dbSession.commit();
        indexers.forEach(i -> i.indexProject(project.uuid(), PROJECT_TAGS_UPDATE));
    }
    response.noContent();
}
Also used : PROJECT_TAGS_UPDATE(org.sonar.server.es.ProjectIndexer.Cause.PROJECT_TAGS_UPDATE) ComponentFinder(org.sonar.server.component.ComponentFinder) StringUtils(org.apache.commons.lang.StringUtils) Collectors(org.sonar.core.util.stream.Collectors) DbSession(org.sonar.db.DbSession) Request(org.sonar.api.server.ws.Request) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) UserRole(org.sonar.api.web.UserRole) WebService(org.sonar.api.server.ws.WebService) WsUtils.checkRequest(org.sonar.server.ws.WsUtils.checkRequest) Locale(java.util.Locale) Response(org.sonar.api.server.ws.Response) ProjectIndexer(org.sonar.server.es.ProjectIndexer) KEY_PROJECT_EXAMPLE_001(org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001) UserSession(org.sonar.server.user.UserSession) DbSession(org.sonar.db.DbSession) StringUtils(org.apache.commons.lang.StringUtils) ComponentDto(org.sonar.db.component.ComponentDto)

Example 4 with Response

use of org.sonar.api.server.ws.Response in project sonarqube by SonarSource.

the class BulkDeleteAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn();
    List<String> uuids = request.paramAsStrings(PARAM_PROJECT_IDS);
    List<String> keys = request.paramAsStrings(PARAM_PROJECTS);
    String orgKey = request.param(ProjectsWsSupport.PARAM_ORGANIZATION);
    try (DbSession dbSession = dbClient.openSession(false)) {
        Optional<OrganizationDto> org = loadOrganizationByKey(dbSession, orgKey);
        List<ComponentDto> projects = searchProjects(dbSession, uuids, keys);
        projects.stream().filter(p -> !org.isPresent() || org.get().getUuid().equals(p.getOrganizationUuid())).forEach(p -> componentCleanerService.delete(dbSession, p));
    }
    response.noContent();
}
Also used : ADMINISTER(org.sonar.db.permission.OrganizationPermission.ADMINISTER) DbSession(org.sonar.db.DbSession) Request(org.sonar.api.server.ws.Request) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) WebService(org.sonar.api.server.ws.WebService) Response(org.sonar.api.server.ws.Response) Optional(java.util.Optional) KEY_PROJECT_EXAMPLE_001(org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001) OrganizationDto(org.sonar.db.organization.OrganizationDto) UserSession(org.sonar.server.user.UserSession) Nullable(javax.annotation.Nullable) ComponentCleanerService(org.sonar.server.component.ComponentCleanerService) DbSession(org.sonar.db.DbSession) ComponentDto(org.sonar.db.component.ComponentDto) OrganizationDto(org.sonar.db.organization.OrganizationDto)

Example 5 with Response

use of org.sonar.api.server.ws.Response in project sonarqube by SonarSource.

the class WebServiceEngine method sendErrors.

private static void sendErrors(Response response, int status, List<String> errors) {
    Response.Stream stream = response.stream();
    if (stream instanceof ServletResponse.ServletStream) {
        ((ServletResponse.ServletStream) stream).reset();
    }
    stream.setStatus(status);
    stream.setMediaType(MediaTypes.JSON);
    try (JsonWriter json = JsonWriter.of(new OutputStreamWriter(stream.output(), StandardCharsets.UTF_8))) {
        json.beginObject();
        writeErrors(json, errors);
        json.endObject();
    } catch (Exception e) {
        // Do not hide the potential exception raised in the try block.
        throw Throwables.propagate(e);
    }
}
Also used : Response(org.sonar.api.server.ws.Response) OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(org.sonar.api.utils.text.JsonWriter) ServerException(org.sonar.server.exceptions.ServerException) BadRequestException(org.sonar.server.exceptions.BadRequestException)

Aggregations

Response (org.sonar.api.server.ws.Response)7 WebService (org.sonar.api.server.ws.WebService)5 List (java.util.List)4 Request (org.sonar.api.server.ws.Request)4 DbClient (org.sonar.db.DbClient)4 DbSession (org.sonar.db.DbSession)4 UserSession (org.sonar.server.user.UserSession)4 ComponentDto (org.sonar.db.component.ComponentDto)3 String.format (java.lang.String.format)2 Optional (java.util.Optional)2 Nullable (javax.annotation.Nullable)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Test (org.junit.Test)2 ValidatingRequest (org.sonar.api.server.ws.internal.ValidatingRequest)2 UserRole (org.sonar.api.web.UserRole)2 Collectors (org.sonar.core.util.stream.Collectors)2 OrganizationDto (org.sonar.db.organization.OrganizationDto)2 ADMINISTER (org.sonar.db.permission.OrganizationPermission.ADMINISTER)2 ComponentFinder (org.sonar.server.component.ComponentFinder)2 BadRequestException (org.sonar.server.exceptions.BadRequestException)2