2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.qualityprofile.ws;
22 import com.google.common.base.Preconditions;
23 import org.sonar.api.resources.Languages;
24 import org.sonar.api.server.ws.Request;
25 import org.sonar.api.server.ws.WebService.NewAction;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.qualityprofile.QualityProfileDto;
28 import org.sonar.server.exceptions.NotFoundException;
29 import org.sonar.server.qualityprofile.QProfileFactory;
31 import static org.apache.commons.lang.StringUtils.isEmpty;
33 public class QProfileIdentificationParamUtils {
35 public static final String PARAM_LANGUAGE = "language";
36 public static final String PARAM_PROFILE_NAME = "profileName";
37 public static final String PARAM_PROFILE_KEY = "profileKey";
39 private QProfileIdentificationParamUtils() {
43 public static void defineProfileParams(NewAction action, Languages languages) {
44 action.createParam(PARAM_PROFILE_KEY)
45 .setDescription("A quality profile key. Either this parameter, or a combination of profileName + language must be set.")
46 .setExampleValue("sonar-way-java-12345");
47 action.createParam(PARAM_PROFILE_NAME)
48 .setDescription("A quality profile name. If this parameter is set, profileKey must not be set and language must be set to disambiguate.")
49 .setExampleValue("Sonar way");
50 action.createParam(PARAM_LANGUAGE)
51 .setDescription("A quality profile language. If this parameter is set, profileKey must not be set and profileName must be set to disambiguate.")
52 .setPossibleValues(LanguageParamUtils.getLanguageKeys(languages))
53 .setExampleValue("js");
56 public static String getProfileKeyFromParameters(Request request, QProfileFactory profileFactory, DbSession session) {
57 String language = request.param(PARAM_LANGUAGE);
58 String profileName = request.param(PARAM_PROFILE_NAME);
59 String profileKey = request.param(PARAM_PROFILE_KEY);
61 Preconditions.checkArgument(
62 (!isEmpty(language) && !isEmpty(profileName)) ^ !isEmpty(profileKey), "Either profileKey or profileName + language must be set");
64 if (profileKey == null) {
65 profileKey = getProfileKeyFromLanguageAndName(language, profileName, profileFactory, session);
70 public static String getProfileKeyFromLanguageAndName(String language, String profileName, QProfileFactory profileFactory, DbSession session) {
71 QualityProfileDto profile = profileFactory.getByNameAndLanguage(session, profileName, language);
72 if (profile == null) {
73 throw new NotFoundException(String.format("Unable to find a profile for language '%s' with name '%s'", language, profileName));
75 return profile.getKey();