001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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.api.utils;
021    
022    import org.apache.commons.configuration.Configuration;
023    import org.apache.commons.io.IOUtils;
024    import org.apache.commons.lang.StringUtils;
025    import org.sonar.api.BatchExtension;
026    
027    import java.io.IOException;
028    import java.io.InputStream;
029    import java.io.InputStreamReader;
030    import java.io.Reader;
031    import java.net.HttpURLConnection;
032    import java.net.URL;
033    
034    /**
035     * @since 1.10
036     */
037    public class ServerHttpClient implements BatchExtension {
038    
039      private String url;
040    
041      protected static final String SERVER_API_PATH = "/api/server";
042      private static final String KEY_PATH = SERVER_API_PATH + "/key";
043      private static final String VERSION_PATH = SERVER_API_PATH + "/version";
044      protected static final String BATCH_PATH = SERVER_API_PATH + "/consolidate_snapshot";
045    
046      protected static final String MAVEN_PATH = "/deploy/maven";
047    
048      private static final int CONNECT_TIMEOUT_MILLISECONDS = 30000;
049      private static final int READ_TIMEOUT_MILLISECONDS = 60000;
050    
051    
052      public ServerHttpClient(String remoteServerUrl) {
053        this.url = StringUtils.chomp(remoteServerUrl, "/");
054      }
055    
056      public ServerHttpClient(Configuration configuration) {
057        this(configuration.getString("sonar.host.url", "http://localhost:9000"));
058      }
059    
060      /**
061       * Throws a runtime ServerConnectionException if it fails to connect Sonar server
062       */
063      public void checkUp() {
064        String exceptionLabel = "Sonar server at " + url +
065            " is unreacheable. Either start it or setup the sonar.host.url maven setting if the URL is incorrect";
066        if (getId() == null) {
067          throw new ServerConnectionException(exceptionLabel);
068        }
069      }
070    
071      public String getId() {
072        return executeAction(KEY_PATH);
073      }
074    
075      public String getVersion() {
076        return executeAction(VERSION_PATH);
077      }
078    
079      public String getMavenRepositoryUrl() {
080        return this.url + MAVEN_PATH;
081      }
082    
083      protected String executeAction(String action) {
084        String result = getRemoteContent(url + action);
085        if (result.trim().length() == 0) {
086          throw new ServerApiEmptyContentException("Empty " + action + " returned from server");
087        }
088        return result;
089      }
090    
091      protected String getRemoteContent(String url) {
092        HttpURLConnection conn = null;
093        Reader reader = null;
094        try {
095          conn = getConnection(url, "GET");
096          reader = new InputStreamReader((InputStream) conn.getContent());
097    
098          int statusCode = conn.getResponseCode();
099          if (statusCode != HttpURLConnection.HTTP_OK) {
100            throw new ServerConnectionException("Status returned by url : '" + url + "' is invalid : " + statusCode);
101          }
102    
103          return IOUtils.toString(reader);
104        } catch (IOException e) {
105          throw new ServerConnectionException("url=" + url, e);
106    
107        } finally {
108          IOUtils.closeQuietly(reader);
109          if (conn != null) {
110            conn.disconnect();
111          }
112        }
113      }
114    
115      public String getUrl() {
116        return url;
117      }
118    
119      private HttpURLConnection getConnection(String url, String method) throws IOException {
120        URL page = new URL(url);
121        HttpURLConnection conn = (HttpURLConnection) page.openConnection();
122        conn.setConnectTimeout(CONNECT_TIMEOUT_MILLISECONDS);
123        conn.setReadTimeout(READ_TIMEOUT_MILLISECONDS);
124        conn.setRequestMethod(method);
125        conn.connect();
126        return conn;
127      }
128    
129      public static class ServerApiEmptyContentException extends SonarException {
130    
131        public ServerApiEmptyContentException(String s) {
132          super(s);
133        }
134      }
135    
136      public static class ServerConnectionException extends SonarException {
137    
138        public ServerConnectionException(String msg) {
139          super(msg);
140        }
141    
142        public ServerConnectionException(String msg, Throwable throwable) {
143          super(msg, throwable);
144        }
145    
146      }
147    }