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