3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program 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 * This program 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;
30 import org.sonar.server.util.LanguageParamUtils;
32 import static org.apache.commons.lang.StringUtils.isEmpty;
34 public class QProfileIdentificationParamUtils {
36 public static final String PARAM_LANGUAGE = "language";
37 public static final String PARAM_PROFILE_NAME = "profileName";
38 public static final String PARAM_PROFILE_KEY = "profileKey";
40 private QProfileIdentificationParamUtils() {
44 public static void defineProfileParams(NewAction action, Languages languages) {
45 action.createParam(PARAM_PROFILE_KEY)
46 .setDescription("A quality profile key. Either this parameter, or a combination of profileName + language must be set.")
47 .setExampleValue("sonar-way-java-12345");
48 action.createParam(PARAM_PROFILE_NAME)
49 .setDescription("A quality profile name. If this parameter is set, profileKey must not be set and language must be set to disambiguate.")
50 .setExampleValue("Sonar way");
51 action.createParam(PARAM_LANGUAGE)
52 .setDescription("A quality profile language. If this parameter is set, profileKey must not be set and profileName must be set to disambiguate.")
53 .setPossibleValues(LanguageParamUtils.getLanguageKeys(languages))
54 .setExampleValue("js");
57 public static String getProfileKeyFromParameters(Request request, QProfileFactory profileFactory, DbSession session) {
58 String language = request.param(PARAM_LANGUAGE);
59 String profileName = request.param(PARAM_PROFILE_NAME);
60 String profileKey = request.param(PARAM_PROFILE_KEY);
62 Preconditions.checkArgument(
63 (!isEmpty(language) && !isEmpty(profileName)) ^ !isEmpty(profileKey), "Either profileKey or profileName + language must be set");
65 if (profileKey == null) {
66 profileKey = getProfileKeyFromLanguageAndName(language, profileName, profileFactory, session);
71 public static String getProfileKeyFromLanguageAndName(String language, String profileName, QProfileFactory profileFactory, DbSession session) {
72 QualityProfileDto profile = profileFactory.getByNameAndLanguage(session, profileName, language);
73 if (profile == null) {
74 throw new NotFoundException(String.format("Unable to find a profile for language '%s' with name '%s'", language, profileName));
76 return profile.getKey();