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.wsclient.connectors; 021 022 import org.apache.commons.httpclient.HttpException; 023 import org.apache.http.HttpStatus; 024 import org.apache.http.util.EntityUtils; 025 import org.sonar.wsclient.Host; 026 import org.sonar.wsclient.services.Query; 027 import org.apache.http.HttpEntity; 028 import org.apache.http.HttpResponse; 029 import org.apache.http.auth.AuthScope; 030 import org.apache.http.auth.UsernamePasswordCredentials; 031 import org.apache.http.client.methods.HttpGet; 032 import org.apache.http.impl.client.DefaultHttpClient; 033 034 import java.io.IOException; 035 036 public class HttpClient4Connector implements Connector { 037 038 private final Host server; 039 040 public HttpClient4Connector(Host server) { 041 this.server = server; 042 } 043 044 public String execute(Query query) { 045 String json = null; 046 047 DefaultHttpClient client = createClient(); 048 HttpGet get = createGetMethod(query); 049 050 try { 051 HttpResponse response = client.execute(get); 052 HttpEntity entity = response.getEntity(); 053 if (entity != null) { 054 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 055 json = EntityUtils.toString(entity); 056 057 } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NOT_FOUND) { 058 throw new ConnectionException("HTTP error: " + response.getStatusLine().getStatusCode() + ", msg: " + response.getStatusLine().getReasonPhrase() + ", query: " + get.toString()); 059 } 060 } 061 062 } catch (HttpException e) { 063 throw new ConnectionException("Query: " + server.getHost() + query.getUrl(), e); 064 065 } catch (IOException e) { 066 throw new ConnectionException("Query: " + server.getHost() + query.getUrl(), e); 067 068 } finally { 069 client.getConnectionManager().shutdown(); 070 } 071 return json; 072 } 073 074 private HttpGet createGetMethod(Query query) { 075 HttpGet get = new HttpGet(server.getHost() + query.getUrl()); 076 get.setHeader("Accept", "application/json"); 077 return get; 078 } 079 080 private DefaultHttpClient createClient() { 081 DefaultHttpClient client = new DefaultHttpClient(); 082 if (server.getUsername() != null) { 083 client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(server.getUsername(), server.getPassword())); 084 } 085 return client; 086 } 087 }