001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.wsclient.rule.internal;
021    
022    import org.sonar.wsclient.internal.HttpRequestFactory;
023    import org.sonar.wsclient.rule.RuleClient;
024    
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    /**
029     * Do not instantiate this class, but use {@link org.sonar.wsclient.SonarClient#ruleClient()}.
030     */
031    public class DefaultRuleClient implements RuleClient {
032    
033      private static final String ROOT_URL = "/api/rules";
034      private static final String ADD_TAGS_URL = ROOT_URL + "/add_tags";
035      private static final String REMOVE_TAGS_URL = ROOT_URL + "/remove_tags";
036    
037      private final HttpRequestFactory requestFactory;
038    
039      public DefaultRuleClient(HttpRequestFactory requestFactory) {
040        this.requestFactory = requestFactory;
041      }
042    
043      @Override
044      public void addTags(String key, String... tags) {
045        requestFactory.post(ADD_TAGS_URL, buildQueryParams(key, tags));
046      }
047    
048      @Override
049      public void removeTags(String key, String... tags) {
050        requestFactory.post(REMOVE_TAGS_URL, buildQueryParams(key, tags));
051      }
052    
053      private Map<String, Object> buildQueryParams(String key, String... tags) {
054        Map<String, Object> params = new HashMap<String, Object>();
055        params.put("key", key);
056        StringBuilder tagsBuilder = new StringBuilder();
057        for (int i=0; i < tags.length - 1; i++) {
058          tagsBuilder.append(tags[i]).append(",");
059        }
060        tagsBuilder.append(tags[tags.length - 1]);
061        params.put("tags", tagsBuilder.toString());
062        return params;
063      }
064    }