Package org.sonar.server.batch

Source Code of org.sonar.server.batch.GlobalReferentialsAction

/*
* 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.batch;

import org.apache.commons.io.IOUtils;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.RequestHandler;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.batch.protocol.input.GlobalReferentials;
import org.sonar.core.measure.db.MetricDto;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.properties.PropertiesDao;
import org.sonar.core.properties.PropertyDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.plugins.MimeTypes;
import org.sonar.server.user.UserSession;

public class GlobalReferentialsAction implements RequestHandler {

  private final DbClient dbClient;
  private final PropertiesDao propertiesDao;

  public GlobalReferentialsAction(DbClient dbClient, PropertiesDao propertiesDao) {
    this.dbClient = dbClient;
    this.propertiesDao = propertiesDao;
  }

  void define(WebService.NewController controller) {
    controller.createAction("global")
      .setDescription("Return metrics and global properties")
      .setSince("4.5")
      .setInternal(true)
      .setHandler(this);
  }

  @Override
  public void handle(Request request, Response response) throws Exception {
    UserSession userSession = UserSession.get();
    boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
    boolean hasDryRunPerm = userSession.hasGlobalPermission(GlobalPermissions.DRY_RUN_EXECUTION);

    DbSession session = dbClient.openSession(false);
    try {
      GlobalReferentials ref = new GlobalReferentials();
      addMetrics(ref, session);
      addSettings(ref, hasScanPerm, hasDryRunPerm, session);

      response.stream().setMediaType(MimeTypes.JSON);
      IOUtils.write(ref.toJson(), response.stream().output());
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  private void addMetrics(GlobalReferentials ref, DbSession session) {
    for (MetricDto metric : dbClient.metricDao().findEnabled(session)) {
      Boolean optimizedBestValue = metric.isOptimizedBestValue();
      ref.addMetric(
        new org.sonar.batch.protocol.input.Metric(metric.getId(), metric.getKey(),
          metric.getValueType(),
          metric.getDescription(),
          metric.getDirection(),
          metric.getName(),
          metric.isQualitative(),
          metric.isUserManaged(),
          metric.getWorstValue(),
          metric.getBestValue(),
          optimizedBestValue != null ? optimizedBestValue : false));
    }
  }

  private void addSettings(GlobalReferentials ref, boolean hasScanPerm, boolean hasDryRunPerm, DbSession session) {
    for (PropertyDto propertyDto : propertiesDao.selectGlobalProperties(session)) {
      String key = propertyDto.getKey();
      String value = propertyDto.getValue();

      if (isPropertyAllowed(key, hasScanPerm, hasDryRunPerm)) {
        ref.addGlobalSetting(key, value);
      }
    }
  }

  private static boolean isPropertyAllowed(String key, boolean hasScanPerm, boolean hasDryRunPerm) {
    return !key.contains(".secured") || hasScanPerm || (key.contains(".license") && hasDryRunPerm);
  }

}
TOP

Related Classes of org.sonar.server.batch.GlobalReferentialsAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.