*/
package org.sonar.server.platform;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-import java.util.Properties;
-
-import javax.annotation.Nullable;
-
+import com.google.common.collect.Lists;
import org.sonar.api.config.EmailSettings;
import org.sonar.api.issue.action.Actions;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.server.ui.Views;
import org.sonar.server.ui.ws.GlobalNavigationAction;
import org.sonar.server.ui.ws.NavigationWs;
+import org.sonar.server.ui.ws.SettingsNavigationAction;
import org.sonar.server.updatecenter.ws.UpdateCenterWs;
import org.sonar.server.user.DefaultUserService;
import org.sonar.server.user.DoPrivileged;
import org.sonar.server.ws.ListingWs;
import org.sonar.server.ws.WebServiceEngine;
-import com.google.common.collect.Lists;
+import javax.annotation.Nullable;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Properties;
class ServerComponents {
// UI
pico.addSingleton(GlobalNavigationAction.class);
+ pico.addSingleton(SettingsNavigationAction.class);
pico.addSingleton(NavigationWs.class);
for (Object components : level4AddedComponents) {
import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils;
-import org.sonar.api.utils.log.Loggers;
import org.sonar.api.Properties;
import org.sonar.api.Property;
import org.sonar.api.PropertyType;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.UriReader;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.updatecenter.common.UpdateCenter;
import org.sonar.updatecenter.common.UpdateCenterDeserializer;
import org.sonar.updatecenter.common.UpdateCenterDeserializer.Mode;
*/
@Properties({
@Property(
- key = "sonar.updatecenter.activate",
+ key = UpdateCenterClient.ACTIVATION_PROPERTY,
defaultValue = "true",
name = "Enable Update Center",
project = false,
public class UpdateCenterClient {
public static final String URL_PROPERTY = "sonar.updatecenter.url";
+ public static final String ACTIVATION_PROPERTY = "sonar.updatecenter.activate";
public static final int PERIOD_IN_MILLISECONDS = 60 * 60 * 1000;
private final URI uri;
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.server.ui.ws;
+
+import org.sonar.api.config.Settings;
+import org.sonar.api.i18n.I18n;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService.NewController;
+import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.api.web.NavigationSection;
+import org.sonar.api.web.Page;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.plugins.UpdateCenterClient;
+import org.sonar.server.ui.ViewProxy;
+import org.sonar.server.ui.Views;
+import org.sonar.server.user.UserSession;
+
+public class SettingsNavigationAction implements NavigationAction {
+
+ private final Settings settings;
+ private final Views views;
+ private final I18n i18n;
+
+ public SettingsNavigationAction(Settings settings, Views views, I18n i18n) {
+ this.views = views;
+ this.settings = settings;
+ this.i18n = i18n;
+ }
+
+ @Override
+ public void define(NewController context) {
+ context.createAction("settings")
+ .setDescription("Get configuration information for the settings page:" +
+ "<ul>" +
+ " <li>List plugin-contributed pages</li>" +
+ " <li>Show update center (or not)</li>" +
+ "</ul>")
+ .setHandler(this)
+ .setInternal(true)
+ .setResponseExample(getClass().getResource("example-settings.json"))
+ .setSince("5.2");
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ boolean isAdmin = UserSession.get().hasGlobalPermission(GlobalPermissions.SYSTEM_ADMIN);
+
+ JsonWriter json = response.newJsonWriter().beginObject();
+ json.prop("showUpdateCenter", isAdmin && settings.getBoolean(UpdateCenterClient.ACTIVATION_PROPERTY));
+
+ json.name("plugins").beginArray();
+ if (isAdmin) {
+ for (ViewProxy<Page> page : views.getPages(NavigationSection.CONFIGURATION, null, null, null, null)) {
+ json.beginObject()
+ .prop("name", i18n.message(UserSession.get().locale(), String.format("%s.page", page.getTitle()), page.getTitle()))
+ .prop("url", getPageUrl(page))
+ .endObject();
+ }
+ }
+ json.endArray();
+
+ json.endObject().close();
+ }
+
+ private String getPageUrl(ViewProxy<Page> page) {
+ return page.isController() ? page.getId() : String.format("/plugins/configuration/%s", page.getId());
+ }
+}
--- /dev/null
+{
+ "showUpdateCenter": true,
+ "plugins": [
+ {
+ "name": "Views",
+ "url": "/plugins/configuration/views"
+ }
+ ]
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.server.ui.ws;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.sonar.api.config.Settings;
+import org.sonar.api.i18n.I18n;
+import org.sonar.api.web.NavigationSection;
+import org.sonar.api.web.Page;
+import org.sonar.api.web.View;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.plugins.UpdateCenterClient;
+import org.sonar.server.ui.Views;
+import org.sonar.server.user.MockUserSession;
+import org.sonar.server.ws.WsTester;
+
+import java.util.Locale;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class SettingsNavigationActionTest {
+
+ private WsTester wsTester;
+
+ private Settings settings;
+
+ private I18n i18n;
+
+ @Before
+ public void before() throws Exception {
+ settings = new Settings();
+ i18n = mock(I18n.class);
+ when(i18n.message(any(Locale.class), anyString(), anyString())).thenAnswer(new Answer<String>() {
+ @Override
+ public String answer(InvocationOnMock invocation) throws Throwable {
+ return invocation.getArgumentAt(2, String.class);
+ }
+
+ });
+ }
+
+ @Test
+ public void empty() throws Exception {
+ MockUserSession.set().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+ wsTester = new WsTester(new NavigationWs(new SettingsNavigationAction(settings, new Views(), i18n)));
+
+ wsTester.newGetRequest("api/navigation", "settings").execute().assertJson(getClass(), "empty.json");
+ }
+
+ @Test
+ public void with_views() throws Exception {
+ MockUserSession.set().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+ wsTester = new WsTester(new NavigationWs(new SettingsNavigationAction(settings, createViews(), i18n)));
+
+ wsTester.newGetRequest("api/navigation", "settings").execute().assertJson(getClass(), "with_views.json");
+ }
+
+ @Test
+ public void with_update_center() throws Exception {
+ settings.setProperty(UpdateCenterClient.ACTIVATION_PROPERTY, true);
+ MockUserSession.set().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+ wsTester = new WsTester(new NavigationWs(new SettingsNavigationAction(settings, new Views(), i18n)));
+
+ wsTester.newGetRequest("api/navigation", "settings").execute().assertJson(getClass(), "with_update_center.json");
+ }
+
+ @Test
+ public void with_views_and_update_center_but_not_admin() throws Exception {
+ MockUserSession.set();
+ settings.setProperty(UpdateCenterClient.ACTIVATION_PROPERTY, true);
+ wsTester = new WsTester(new NavigationWs(new SettingsNavigationAction(settings, createViews(), i18n)));
+
+ wsTester.newGetRequest("api/navigation", "settings").execute().assertJson(getClass(), "empty.json");
+ }
+
+ private Views createViews() {
+
+
+ @NavigationSection(NavigationSection.CONFIGURATION)
+ class FirstPage implements Page {
+ @Override
+ public String getTitle() {
+ return "First Page";
+ }
+
+ @Override
+ public String getId() {
+ return "first_page";
+ }
+ }
+
+ @NavigationSection(NavigationSection.CONFIGURATION)
+ class SecondPage implements Page {
+ @Override
+ public String getTitle() {
+ return "Second Page";
+ }
+
+ @Override
+ public String getId() {
+ return "/second/page";
+ }
+ }
+
+ Page page = new FirstPage();
+ Page controller = new SecondPage();
+ return new Views(new View[] {page, controller});
+ }
+}
--- /dev/null
+{
+ "showUpdateCenter": false,
+ "plugins": []
+}
--- /dev/null
+{
+ "showUpdateCenter": true,
+ "plugins": []
+}
--- /dev/null
+{
+ "showUpdateCenter": false,
+ "plugins": [
+ {
+ "name": "First Page",
+ "url": "/plugins/configuration/first_page"
+ },
+ {
+ "name": "Second Page",
+ "url": "/second/page"
+ }
+ ]
+}