001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
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.batch;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.codehaus.plexus.util.IOUtil;
024    import org.sonar.api.BatchComponent;
025    import org.sonar.api.platform.Server;
026    
027    import java.io.IOException;
028    import java.io.InputStream;
029    import java.net.HttpURLConnection;
030    import java.net.URL;
031    
032    public class RemoteServerMetadata implements BatchComponent {
033    
034      public static final int CONNECT_TIMEOUT_MILLISECONDS = 30000;
035      public static final int READ_TIMEOUT_MILLISECONDS = 60000;
036    
037      private String serverUrl;
038    
039      public RemoteServerMetadata(Server server) {
040        serverUrl = server.getURL();
041        if (serverUrl.endsWith("/")) {
042          serverUrl = StringUtils.chop(serverUrl);
043        }
044      }
045    
046      public String getServerId() throws IOException {
047        String remoteServerInfo = remoteContent("/api/server");
048        // don't use JSON utilities to extract ID from such a small string
049        return extractId(remoteServerInfo);
050      }
051    
052      protected String extractId(String remoteServerInfo) {
053        String partialId = StringUtils.substringAfter(remoteServerInfo, "\"id\":\"");
054        return StringUtils.substringBefore(partialId, "\"");
055      }
056    
057      protected String getUrlFor(String path) {
058        return serverUrl + path;
059      }
060    
061      protected String remoteContent(String path) throws IOException {
062        String fullUrl = getUrlFor(path);
063        HttpURLConnection conn = getConnection(fullUrl, "GET");
064        InputStream input = (InputStream) conn.getContent();
065        try {
066          int statusCode = conn.getResponseCode();
067          if (statusCode != HttpURLConnection.HTTP_OK) {
068            throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + statusCode);
069          }
070          return IOUtil.toString(input);
071    
072        } finally {
073          IOUtil.close(input);
074          conn.disconnect();
075        }
076      }
077    
078      static HttpURLConnection getConnection(String url, String method) throws IOException {
079        URL page = new URL(url);
080        HttpURLConnection conn = (HttpURLConnection) page.openConnection();
081        conn.setConnectTimeout(CONNECT_TIMEOUT_MILLISECONDS);
082        conn.setReadTimeout(READ_TIMEOUT_MILLISECONDS);
083        conn.setRequestMethod(method);
084        conn.connect();
085        return conn;
086      }
087    
088    }