package org.sonar.server.settings.ws;
import java.util.List;
-import javax.annotation.CheckForNull;
+import java.util.Optional;
import org.sonar.api.PropertyType;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
+import org.sonar.api.web.UserRole;
+import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
+import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Settings;
import org.sonarqube.ws.Settings.ListDefinitionsWsResponse;
+import org.sonarqube.ws.client.setting.ListDefinitionsRequest;
import static org.elasticsearch.common.Strings.isNullOrEmpty;
-import static org.sonar.server.settings.ws.SettingsWsComponentParameters.PARAM_COMPONENT_ID;
-import static org.sonar.server.settings.ws.SettingsWsComponentParameters.PARAM_COMPONENT_KEY;
+import static org.sonar.server.component.ComponentFinder.ParamNames.ID_AND_KEY;
import static org.sonar.server.settings.ws.SettingsWsComponentParameters.addComponentParameters;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_LIST_DEFINITIONS;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_ID;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY;
public class ListDefinitionsAction implements SettingsWsAction {
private final DbClient dbClient;
- private final SettingsWsComponentParameters settingsWsComponentParameters;
+ private final ComponentFinder componentFinder;
+ private final UserSession userSession;
private final PropertyDefinitions propertyDefinitions;
- public ListDefinitionsAction(DbClient dbClient, SettingsWsComponentParameters settingsWsComponentParameters, PropertyDefinitions propertyDefinitions) {
+ public ListDefinitionsAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession, PropertyDefinitions propertyDefinitions) {
this.dbClient = dbClient;
- this.settingsWsComponentParameters = settingsWsComponentParameters;
+ this.componentFinder = componentFinder;
+ this.userSession = userSession;
this.propertyDefinitions = propertyDefinitions;
}
@Override
public void define(WebService.NewController context) {
- WebService.NewAction action = context.createAction("list_definitions")
+ WebService.NewAction action = context.createAction(ACTION_LIST_DEFINITIONS)
.setDescription(String.format("List settings definitions.<br>" +
"Either '%s' or '%s' can be provided, not both.<br> " +
"Requires one of the following permissions: " +
.setResponseExample(getClass().getResource("list_definitions-example.json"))
.setSince("6.1")
.setHandler(this);
-
addComponentParameters(action);
}
}
private ListDefinitionsWsResponse doHandle(Request request) {
- String qualifier = getQualifier(request);
+ ListDefinitionsRequest wsRequest = toWsRequest(request);
+ Optional<String> qualifier = getQualifier(wsRequest);
ListDefinitionsWsResponse.Builder wsResponse = ListDefinitionsWsResponse.newBuilder();
propertyDefinitions.getAll().stream()
- .filter(definition -> qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier))
+ .filter(definition -> qualifier.isPresent() ? definition.qualifiers().contains(qualifier.get()) : definition.global())
.filter(definition -> !definition.type().equals(PropertyType.LICENSE))
.forEach(definition -> addDefinition(definition, wsResponse));
return wsResponse.build();
}
+ private static ListDefinitionsRequest toWsRequest(Request request) {
+ return ListDefinitionsRequest.builder()
+ .setComponentId(request.param(PARAM_COMPONENT_ID))
+ .setComponentKey(request.param(PARAM_COMPONENT_KEY))
+ .build();
+ }
+
+ private Optional<String> getQualifier(ListDefinitionsRequest wsRequest) {
+ DbSession dbSession = dbClient.openSession(false);
+ try {
+ Optional<ComponentDto> component = getComponent(dbSession, wsRequest);
+ checkAdminPermission(component);
+ return component.isPresent() ? Optional.of(component.get().qualifier()) : Optional.empty();
+ } finally {
+ dbClient.closeSession(dbSession);
+ }
+ }
+
+ private Optional<ComponentDto> getComponent(DbSession dbSession, ListDefinitionsRequest wsRequest) {
+ String componentId = wsRequest.getComponentId();
+ String componentKey = wsRequest.getComponentKey();
+ if (componentId != null || componentKey != null) {
+ return Optional.of(componentFinder.getByUuidOrKey(dbSession, componentId, componentKey, ID_AND_KEY));
+ }
+ return Optional.empty();
+ }
+
+ private void checkAdminPermission(Optional<ComponentDto> component) {
+ if (component.isPresent()) {
+ userSession.checkComponentUuidPermission(UserRole.ADMIN, component.get().uuid());
+ } else {
+ userSession.checkPermission(GlobalPermissions.SYSTEM_ADMIN);
+ }
+ }
+
private void addDefinition(PropertyDefinition definition, ListDefinitionsWsResponse.Builder wsResponse) {
String key = definition.key();
Settings.Definition.Builder builder = wsResponse.addDefinitionsBuilder()
.build();
}
- @CheckForNull
- private String getQualifier(Request request) {
- DbSession dbSession = dbClient.openSession(false);
- try {
- ComponentDto component = settingsWsComponentParameters.getComponent(dbSession, request);
- settingsWsComponentParameters.checkAdminPermission(component);
- return component == null ? null : component.qualifier();
- } finally {
- dbClient.closeSession(dbSession);
- }
- }
-
}
import org.sonar.api.server.ws.WebService;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.CONTROLLER_SETTINGS;
+
public class SettingsWs implements WebService {
private final SettingsWsAction[] actions;
@Override
public void define(Context context) {
- NewController controller = context.createController("api/settings")
+ NewController controller = context.createController(CONTROLLER_SETTINGS)
.setDescription("Manage settings.")
.setSince("6.1");
for (SettingsWsAction action : actions) {
*/
package org.sonar.server.settings.ws;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService;
-import org.sonar.api.web.UserRole;
-import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.user.UserSession;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
-import static org.sonar.server.component.ComponentFinder.ParamNames.ID_AND_KEY;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_ID;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY;
public class SettingsWsComponentParameters {
- static final String PARAM_COMPONENT_ID = "componentId";
- static final String PARAM_COMPONENT_KEY = "componentKey";
-
- private final ComponentFinder componentFinder;
- private final UserSession userSession;
-
- public SettingsWsComponentParameters(ComponentFinder componentFinder, UserSession userSession) {
- this.componentFinder = componentFinder;
- this.userSession = userSession;
+ private SettingsWsComponentParameters() {
+ // Only static methods
}
static void addComponentParameters(WebService.NewAction action) {
.setExampleValue(KEY_PROJECT_EXAMPLE_001);
}
- @CheckForNull
- ComponentDto getComponent(DbSession dbSession, Request request) {
- if (request.hasParam(PARAM_COMPONENT_ID) || request.hasParam(PARAM_COMPONENT_KEY)) {
- return componentFinder.getByUuidOrKey(dbSession, request.param(PARAM_COMPONENT_ID), request.param(PARAM_COMPONENT_KEY), ID_AND_KEY);
- }
- return null;
- }
-
- void checkAdminPermission(@Nullable ComponentDto component) {
- if (component == null) {
- userSession.checkPermission(GlobalPermissions.SYSTEM_ADMIN);
- } else {
- userSession.checkComponentUuidPermission(UserRole.ADMIN, component.uuid());
- }
- }
-
}
import static org.elasticsearch.common.Strings.isNullOrEmpty;
import static org.sonar.api.PropertyType.PROPERTY_SET;
-import static org.sonar.server.settings.ws.SettingsWsComponentParameters.PARAM_COMPONENT_ID;
-import static org.sonar.server.settings.ws.SettingsWsComponentParameters.PARAM_COMPONENT_KEY;
import static org.sonar.server.settings.ws.SettingsWsComponentParameters.addComponentParameters;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_ID;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY;
public class ValuesAction implements SettingsWsAction {
ComponentDto project;
PropertyDefinitions propertyDefinitions = new PropertyDefinitions();
- SettingsWsComponentParameters settingsWsComponentParameters = new SettingsWsComponentParameters(new ComponentFinder(dbClient), userSession);
- WsActionTester ws = new WsActionTester(new ListDefinitionsAction(dbClient, settingsWsComponentParameters, propertyDefinitions));
+ WsActionTester ws = new WsActionTester(new ListDefinitionsAction(dbClient, new ComponentFinder(dbClient), userSession, propertyDefinitions));
@Before
public void setUp() throws Exception {
import org.sonarqube.ws.client.qualitygate.QualityGatesService;
import org.sonarqube.ws.client.qualityprofile.QualityProfilesService;
import org.sonarqube.ws.client.rule.RulesService;
+import org.sonarqube.ws.client.setting.SettingsService;
import org.sonarqube.ws.client.system.SystemService;
import org.sonarqube.ws.client.usertoken.UserTokensService;
private final RulesService rulesService;
private final ProjectsService projectsService;
private final ProjectLinksService projectLinksService;
+ private final SettingsService settingsService;
DefaultWsClient(WsConnector wsConnector) {
this.wsConnector = wsConnector;
this.rulesService = new RulesService(wsConnector);
this.projectsService = new ProjectsService(wsConnector);
this.projectLinksService = new ProjectLinksService(wsConnector);
+ this.settingsService = new SettingsService(wsConnector);
}
@Override
public ProjectLinksService projectLinks() {
return projectLinksService;
}
+
+ @Override
+ public SettingsService settingsService() {
+ return settingsService;
+ }
}
import org.sonarqube.ws.client.qualitygate.QualityGatesService;
import org.sonarqube.ws.client.qualityprofile.QualityProfilesService;
import org.sonarqube.ws.client.rule.RulesService;
+import org.sonarqube.ws.client.setting.SettingsService;
import org.sonarqube.ws.client.system.SystemService;
import org.sonarqube.ws.client.usertoken.UserTokensService;
* @since 6.1
*/
ProjectLinksService projectLinks();
+
+ /**
+ * @since 6.1
+ */
+ SettingsService settingsService();
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonarqube.ws.client.setting;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+public class ListDefinitionsRequest {
+
+ private final String componentId;
+ private final String componentKey;
+
+ private ListDefinitionsRequest(Builder builder) {
+ this.componentId = builder.componentId;
+ this.componentKey = builder.componentKey;
+ }
+
+ @CheckForNull
+ public String getComponentId() {
+ return componentId;
+ }
+
+ @CheckForNull
+ public String getComponentKey() {
+ return componentKey;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static class Builder {
+ private String componentId;
+ private String componentKey;
+
+ private Builder() {
+ // enforce factory method use
+ }
+
+ public Builder setComponentId(@Nullable String componentId) {
+ this.componentId = componentId;
+ return this;
+ }
+
+ public Builder setComponentKey(@Nullable String componentKey) {
+ this.componentKey = componentKey;
+ return this;
+ }
+
+ public ListDefinitionsRequest build() {
+ return new ListDefinitionsRequest(this);
+ }
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonarqube.ws.client.setting;
+
+import org.sonarqube.ws.Settings.ListDefinitionsWsResponse;
+import org.sonarqube.ws.client.BaseService;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.WsConnector;
+
+import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_COMPONENT_ID;
+import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_COMPONENT_KEY;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_LIST_DEFINITIONS;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.CONTROLLER_SETTINGS;
+
+public class SettingsService extends BaseService {
+ public SettingsService(WsConnector wsConnector) {
+ super(wsConnector, CONTROLLER_SETTINGS);
+ }
+
+ public ListDefinitionsWsResponse listDefinitions(ListDefinitionsRequest request) {
+ GetRequest getRequest = new GetRequest(path(ACTION_LIST_DEFINITIONS))
+ .setParam(PARAM_COMPONENT_ID, request.getComponentId())
+ .setParam(PARAM_COMPONENT_KEY, request.getComponentKey());
+ return call(getRequest, ListDefinitionsWsResponse.parser());
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonarqube.ws.client.setting;
+
+public class SettingsWsParameters {
+ public static final String CONTROLLER_SETTINGS = "api/settings";
+
+ public static final String ACTION_LIST_DEFINITIONS = "list_definitions";
+
+ public static final String PARAM_COMPONENT_ID = "componentId";
+ public static final String PARAM_COMPONENT_KEY = "componentKey";
+
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+
+
+@ParametersAreNonnullByDefault
+package org.sonarqube.ws.client.setting;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonarqube.ws.client.setting;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ListDefinitionsRequestTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ ListDefinitionsRequest.Builder underTest = ListDefinitionsRequest.builder();
+
+ @Test
+ public void create_request_with_no_component() {
+ ListDefinitionsRequest result = underTest.build();
+
+ assertThat(result.getComponentId()).isNull();
+ assertThat(result.getComponentKey()).isNull();
+ }
+
+ @Test
+ public void create_request_with_component_id() {
+ ListDefinitionsRequest result = underTest.setComponentId("projectId").build();
+
+ assertThat(result.getComponentId()).isEqualTo("projectId");
+ assertThat(result.getComponentKey()).isNull();
+ }
+
+ @Test
+ public void create_request_with_component_key() {
+ ListDefinitionsRequest result = underTest.setComponentKey("projectKey").build();
+
+ assertThat(result.getComponentId()).isNull();
+ assertThat(result.getComponentKey()).isEqualTo("projectKey");
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonarqube.ws.client.setting;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonarqube.ws.Settings;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.ServiceTester;
+import org.sonarqube.ws.client.WsConnector;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY;
+
+public class SettingsServiceTest {
+
+ @Rule
+ public ServiceTester<SettingsService> serviceTester = new ServiceTester<>(new SettingsService(mock(WsConnector.class)));
+
+ private SettingsService underTest = serviceTester.getInstanceUnderTest();
+
+ @Test
+ public void list_definitions() {
+ ListDefinitionsRequest request = ListDefinitionsRequest.builder()
+ .setComponentKey("KEY")
+ .build();
+
+ underTest.listDefinitions(request);
+ GetRequest getRequest = serviceTester.getGetRequest();
+
+ assertThat(serviceTester.getGetParser()).isSameAs(Settings.ListDefinitionsWsResponse.parser());
+ serviceTester.assertThat(getRequest)
+ .hasParam(PARAM_COMPONENT_KEY, "KEY")
+ .andNoOtherParam();
+ }
+
+}