001    /*
002     * Copyright (C) 2010 Evgeny Mandrikov
003     *
004     * Sonar-IDE is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Lesser General Public
006     * License as published by the Free Software Foundation; either
007     * version 3 of the License, or (at your option) any later version.
008     *
009     * Sonar-IDE is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with Sonar-IDE; if not, write to the Free Software
016     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
017     */
018    
019    package org.sonar.ide.shared;
020    
021    import org.apache.commons.lang.StringUtils;
022    
023    /**
024     * @author Evgeny Mandrikov
025     */
026    public final class SonarUrlUtils {
027      public static final String HOST_DEFAULT = "http://localhost:9000";
028    
029      private static final String PROJECT = "/project/index/";
030      private static final String COMPONENTS = "/components/index/";
031      private static final String RESOURCE = "/resource/index/";
032      private static final String TIMEMACHINE = "/timemachine/index/";
033      private static final String MEASURES_DRILLDOWN = "/drilldown/measures/";
034      private static final String VIOLATIONS_DRILLDOWN = "/drilldown/violations/";
035    
036      public static String getDashboard(String host, String resourceKey) {
037        return getUrlBuilder(host, PROJECT, resourceKey).toString();
038      }
039    
040      public static String getComponents(String host, String resourceKey) {
041        return getUrlBuilder(host, COMPONENTS, resourceKey).toString();
042      }
043    
044      public static String getResource(String host, String resourceKey) {
045        return getUrlBuilder(host, RESOURCE, resourceKey).toString();
046      }
047    
048      public static String getTimemachine(String host, String resourceKey) {
049        return getUrlBuilder(host, TIMEMACHINE, resourceKey).toString();
050      }
051    
052      public static String getMeasuresDrilldown(String host, String resourceKey, String metric) {
053        StringBuilder url = getUrlBuilder(host, MEASURES_DRILLDOWN, resourceKey);
054        append(url, "metric", metric);
055        return url.toString();
056      }
057    
058      public static String getMeasuresDrilldown(String host, String resourceKey) {
059        return getMeasuresDrilldown(host, resourceKey, null);
060      }
061    
062      public static String getViolationsDrilldown(String host, String resourceKey) {
063        return getUrlBuilder(host, VIOLATIONS_DRILLDOWN, resourceKey).toString();
064      }
065    
066      private static StringBuilder getUrlBuilder(String host, String prefix, String resourceKey) {
067        StringBuilder sb = new StringBuilder()
068            .append(host)
069            .append(prefix);
070        if (StringUtils.isNotBlank(resourceKey)) {
071          sb.append(resourceKey);
072        }
073        sb.append("?");
074        return sb;
075      }
076    
077      private static void append(StringBuilder url, String paramKey, String paramValue) {
078        if (StringUtils.isNotBlank(paramValue)) {
079          url.append(paramKey)
080              .append('=')
081              .append(paramValue)
082              .append('&');
083        }
084      }
085    
086      /**
087       * Hide utility-class constructor.
088       */
089      private SonarUrlUtils() {
090      }
091    }