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