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