001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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     * Sonar 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
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.wsclient.services;
021    
022    import java.util.Date;
023    import java.util.Locale;
024    
025    /**
026     * @since 2.2
027     */
028    public abstract class AbstractQuery<MODEL extends Model> {
029    
030      /**
031       * Default timeout for waiting data, in milliseconds.
032       *
033       * @since 2.10
034       */
035      public static final int DEFAULT_TIMEOUT_MILLISECONDS = 30 * 1000;
036    
037      private int timeoutMilliseconds = DEFAULT_TIMEOUT_MILLISECONDS;
038    
039      // accepted-language as defined in http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
040      private String locale;
041    
042      /**
043       * Must start with a slash, for example: /api/metrics
044       * <p>
045       * IMPORTANT: In implementations of this method we must use helper methods to construct URL.
046       * </p>
047       *
048       * @see #encode(String)
049       * @see #appendUrlParameter(StringBuilder, String, Object)
050       * @see #appendUrlParameter(StringBuilder, String, Object[])
051       * @see #appendUrlParameter(StringBuilder, String, Date, boolean)
052       */
053      public abstract String getUrl();
054    
055      /**
056       * Request body. By default it is empty but it can be overridden.
057       */
058      public String getBody() {
059        return null;
060      }
061    
062      /**
063       * Get the timeout for waiting data, in milliseconds. A value of zero is interpreted as an infinite timeout.
064       *
065       * @since 2.10
066       */
067      public final int getTimeoutMilliseconds() {
068        return timeoutMilliseconds;
069      }
070    
071      /**
072       * Set the timeout for waiting data, in milliseconds. Avalue of zero is interpreted as an infinite timeout.
073       *
074       * @since 2.10
075       */
076      public final AbstractQuery<MODEL> setTimeoutMilliseconds(int i) {
077        this.timeoutMilliseconds = (i < 0 ? 0 : i);
078        return this;
079      }
080    
081      /**
082       * Accepted-language, as defined in http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
083       *
084       * @since 2.10
085       */
086      public final String getLocale() {
087        return locale;
088      }
089    
090      /**
091       * Set the Accepted-language HTTP parameter
092       *
093       * @since 2.10
094       */
095      public final AbstractQuery<MODEL> setLocale(String locale) {
096        this.locale = locale;
097        return this;
098      }
099    
100      /**
101       * Encodes single parameter value.
102       */
103      protected static String encode(String value) {
104        return WSUtils.getINSTANCE().encodeUrl(value);
105      }
106    
107      protected static void appendUrlParameter(StringBuilder url, String paramKey, Object paramValue) {
108        if (paramValue != null) {
109          url.append(paramKey)
110              .append('=')
111              .append(encode(paramValue.toString()))
112              .append('&');
113        }
114      }
115    
116      protected static void appendUrlParameter(StringBuilder url, String paramKey, Object[] paramValues) {
117        if (paramValues != null) {
118          url.append(paramKey).append('=');
119          for (int index = 0; index < paramValues.length; index++) {
120            if (index > 0) {
121              url.append(',');
122            }
123            if (paramValues[index] != null) {
124              url.append(encode(paramValues[index].toString()));
125            }
126          }
127          url.append('&');
128        }
129      }
130    
131      protected static void appendUrlParameter(StringBuilder url, String paramKey, Date paramValue, boolean includeTime) {
132        if (paramValue != null) {
133          String format = (includeTime ? "yyyy-MM-dd'T'HH:mm:ssZ" : "yyyy-MM-dd");
134          url.append(paramKey)
135              .append('=')
136              .append(encode(WSUtils.getINSTANCE().format(paramValue, format)))
137              .append('&');
138        }
139      }
140    }