001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2013 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.internal;
021    
022    import com.github.kevinsawicki.http.HttpRequest;
023    import org.sonar.wsclient.base.HttpException;
024    
025    import javax.annotation.Nullable;
026    import java.util.Map;
027    
028    /**
029     * Not an API. Please do not use this class, except maybe for unit tests.
030     */
031    public class HttpRequestFactory {
032    
033      private final String baseUrl;
034      private String login, password, proxyHost, proxyLogin, proxyPassword;
035      private int proxyPort;
036      private int connectTimeoutInMilliseconds;
037      private int readTimeoutInMilliseconds;
038    
039      public HttpRequestFactory(String baseUrl) {
040        this.baseUrl = baseUrl;
041      }
042    
043      public HttpRequestFactory setLogin(@Nullable String login) {
044        this.login = login;
045        return this;
046      }
047    
048      public HttpRequestFactory setPassword(@Nullable String password) {
049        this.password = password;
050        return this;
051      }
052    
053      public HttpRequestFactory setProxyHost(@Nullable String proxyHost) {
054        this.proxyHost = proxyHost;
055        return this;
056      }
057    
058      public HttpRequestFactory setProxyLogin(@Nullable String proxyLogin) {
059        this.proxyLogin = proxyLogin;
060        return this;
061      }
062    
063      public HttpRequestFactory setProxyPassword(@Nullable String proxyPassword) {
064        this.proxyPassword = proxyPassword;
065        return this;
066      }
067    
068      public HttpRequestFactory setProxyPort(int proxyPort) {
069        this.proxyPort = proxyPort;
070        return this;
071      }
072    
073      public HttpRequestFactory setConnectTimeoutInMilliseconds(int connectTimeoutInMilliseconds) {
074        this.connectTimeoutInMilliseconds = connectTimeoutInMilliseconds;
075        return this;
076      }
077    
078      public HttpRequestFactory setReadTimeoutInMilliseconds(int readTimeoutInMilliseconds) {
079        this.readTimeoutInMilliseconds = readTimeoutInMilliseconds;
080        return this;
081      }
082    
083      public String getBaseUrl() {
084        return baseUrl;
085      }
086    
087      public String getLogin() {
088        return login;
089      }
090    
091      public String getPassword() {
092        return password;
093      }
094    
095      public String getProxyHost() {
096        return proxyHost;
097      }
098    
099      public String getProxyLogin() {
100        return proxyLogin;
101      }
102    
103      public String getProxyPassword() {
104        return proxyPassword;
105      }
106    
107      public int getProxyPort() {
108        return proxyPort;
109      }
110    
111      public int getConnectTimeoutInMilliseconds() {
112        return connectTimeoutInMilliseconds;
113      }
114    
115      public int getReadTimeoutInMilliseconds() {
116        return readTimeoutInMilliseconds;
117      }
118    
119      public String get(String wsUrl, Map<String, Object> queryParams) {
120        HttpRequest request = HttpRequest.get(baseUrl + wsUrl, queryParams, true);
121        return execute(request);
122      }
123    
124      public String post(String wsUrl, Map<String, Object> queryParams) {
125        HttpRequest request = HttpRequest.post(baseUrl + wsUrl, queryParams, true);
126        return execute(request);
127      }
128    
129      private String execute(HttpRequest request) {
130        try {
131          prepare(request);
132          if (request.ok()) {
133            return request.body("UTF-8");
134          }
135          // TODO handle error messages
136          throw new HttpException(request.url().toString(), request.code());
137    
138        } catch (HttpRequest.HttpRequestException e) {
139          throw new IllegalStateException("Fail to request " + request.url(), e.getCause());
140        }
141      }
142    
143      private void prepare(HttpRequest request) {
144        if (proxyHost != null) {
145          request.useProxy(proxyHost, proxyPort);
146          if (proxyLogin != null) {
147            request.proxyBasic(proxyLogin, proxyPassword);
148          }
149        }
150        request
151          .acceptGzipEncoding()
152          .uncompress(true)
153          .acceptJson()
154          .acceptCharset(HttpRequest.CHARSET_UTF8)
155          .connectTimeout(connectTimeoutInMilliseconds)
156          .readTimeout(readTimeoutInMilliseconds)
157          .trustAllCerts()
158          .trustAllCerts();
159        if (login != null) {
160          request.basic(login, password);
161        }
162      }
163    }