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;
021
022 import org.sonar.wsclient.qualitygate.internal.DefaultQualityGateClient;
023
024 import org.sonar.wsclient.qualitygate.QualityGateClient;
025 import org.sonar.wsclient.internal.HttpRequestFactory;
026 import org.sonar.wsclient.issue.ActionPlanClient;
027 import org.sonar.wsclient.issue.IssueClient;
028 import org.sonar.wsclient.issue.internal.DefaultActionPlanClient;
029 import org.sonar.wsclient.issue.internal.DefaultIssueClient;
030 import org.sonar.wsclient.permissions.PermissionClient;
031 import org.sonar.wsclient.permissions.internal.DefaultPermissionClient;
032 import org.sonar.wsclient.project.ProjectClient;
033 import org.sonar.wsclient.project.internal.DefaultProjectClient;
034 import org.sonar.wsclient.rule.RuleClient;
035 import org.sonar.wsclient.rule.RuleTagClient;
036 import org.sonar.wsclient.rule.internal.DefaultRuleClient;
037 import org.sonar.wsclient.rule.internal.DefaultRuleTagClient;
038 import org.sonar.wsclient.system.SystemClient;
039 import org.sonar.wsclient.system.internal.DefaultSystemClient;
040 import org.sonar.wsclient.user.UserClient;
041 import org.sonar.wsclient.user.internal.DefaultUserClient;
042
043 import javax.annotation.Nullable;
044
045 /**
046 * Entry point of the Java Client for Sonar Web Services. It does not support all web services yet.
047 * <p/>
048 * Example:
049 * <pre>
050 * SonarClient client = SonarClient.create("http://localhost:9000");
051 * IssueClient issueClient = client.issueClient();
052 * </pre>
053 *
054 * @since 3.6
055 */
056 public class SonarClient {
057
058 public static final int DEFAULT_CONNECT_TIMEOUT_MILLISECONDS = 30000;
059 public static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = 60000;
060
061 /**
062 * Visibility relaxed for unit tests
063 */
064 final HttpRequestFactory requestFactory;
065
066 private SonarClient(Builder builder) {
067 requestFactory = new HttpRequestFactory(builder.url)
068 .setLogin(builder.login)
069 .setPassword(builder.password)
070 .setProxyHost(builder.proxyHost)
071 .setProxyPort(builder.proxyPort)
072 .setProxyLogin(builder.proxyLogin)
073 .setProxyPassword(builder.proxyPassword)
074 .setConnectTimeoutInMilliseconds(builder.connectTimeoutMs)
075 .setReadTimeoutInMilliseconds(builder.readTimeoutMs);
076 }
077
078 /**
079 * New client to interact with web services related to issues
080 */
081 public IssueClient issueClient() {
082 return new DefaultIssueClient(requestFactory);
083 }
084
085 /**
086 * New client to interact with web services related to issue action plans
087 */
088 public ActionPlanClient actionPlanClient() {
089 return new DefaultActionPlanClient(requestFactory);
090 }
091
092 /**
093 * New client to interact with web services related to users
094 */
095 public UserClient userClient() {
096 return new DefaultUserClient(requestFactory);
097 }
098
099 /**
100 * New client to interact with web services related to users and groups permissions
101 */
102 public PermissionClient permissionClient() {
103 return new DefaultPermissionClient(requestFactory);
104 }
105
106 /**
107 * New client to interact with web services related to projects
108 */
109 public ProjectClient projectClient() {
110 return new DefaultProjectClient(requestFactory);
111 }
112
113 /**
114 * New client to interact with web services related to rule tags
115 */
116 public RuleTagClient ruleTagClient() {
117 return new DefaultRuleTagClient(requestFactory);
118 }
119
120 /**
121 * New client to interact with web services related to rules
122 */
123 public RuleClient ruleClient() {
124 return new DefaultRuleClient(requestFactory);
125 }
126
127 /**
128 * New client to interact with web services related to quality gates
129 */
130 public QualityGateClient qualityGateClient() {
131 return new DefaultQualityGateClient(requestFactory);
132 }
133
134 public SystemClient systemClient() {
135 return new DefaultSystemClient(requestFactory);
136 }
137
138 /**
139 * Create a builder of {@link SonarClient}s.
140 */
141 public static Builder builder() {
142 return new Builder();
143 }
144
145 /**
146 * Create a client with default configuration. Use {@link #builder()} to define
147 * a custom configuration (credentials, HTTP proxy, HTTP timeouts).
148 */
149 public static SonarClient create(String serverUrl) {
150 return builder().url(serverUrl).build();
151 }
152
153 public static class Builder {
154 private String login, password, url, proxyHost, proxyLogin, proxyPassword;
155 private int proxyPort = 0;
156 private int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, readTimeoutMs = DEFAULT_READ_TIMEOUT_MILLISECONDS;
157
158 private Builder() {
159 }
160
161 /**
162 * Mandatory HTTP server URL, eg "http://localhost:9000"
163 */
164 public Builder url(String url) {
165 this.url = url;
166 return this;
167 }
168
169 /**
170 * Optional login, for example "admin"
171 */
172 public Builder login(@Nullable String login) {
173 this.login = login;
174 return this;
175 }
176
177 /**
178 * Optional password related to {@link #login(String)}, for example "admin"
179 */
180 public Builder password(@Nullable String password) {
181 this.password = password;
182 return this;
183 }
184
185 /**
186 * Host and port of the optional HTTP proxy
187 */
188 public Builder proxy(@Nullable String proxyHost, int proxyPort) {
189 this.proxyHost = proxyHost;
190 this.proxyPort = proxyPort;
191 return this;
192 }
193
194 public Builder proxyLogin(@Nullable String proxyLogin) {
195 this.proxyLogin = proxyLogin;
196 return this;
197 }
198
199 public Builder proxyPassword(@Nullable String proxyPassword) {
200 this.proxyPassword = proxyPassword;
201 return this;
202 }
203
204 /**
205 * Sets a specified timeout value, in milliseconds, to be used when opening HTTP connection.
206 * A timeout of zero is interpreted as an infinite timeout. Default value is {@link SonarClient#DEFAULT_CONNECT_TIMEOUT_MILLISECONDS}
207 */
208 public Builder connectTimeoutMilliseconds(int i) {
209 this.connectTimeoutMs = i;
210 return this;
211 }
212
213 /**
214 * Sets the read timeout to a specified timeout, in milliseconds.
215 * A timeout of zero is interpreted as an infinite timeout. Default value is {@link SonarClient#DEFAULT_READ_TIMEOUT_MILLISECONDS}
216 */
217 public Builder readTimeoutMilliseconds(int i) {
218 this.readTimeoutMs = i;
219 return this;
220 }
221
222 /**
223 * Build a new client
224 */
225 public SonarClient build() {
226 if (url == null || "".equals(url)) {
227 throw new IllegalStateException("Server URL must be set");
228 }
229 return new SonarClient(this);
230 }
231 }
232 }