001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 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.wsclient.connectors;
021
022 import org.apache.http.*;
023 import org.apache.http.auth.*;
024 import org.apache.http.client.CredentialsProvider;
025 import org.apache.http.client.methods.HttpDelete;
026 import org.apache.http.client.methods.HttpGet;
027 import org.apache.http.client.methods.HttpPost;
028 import org.apache.http.client.methods.HttpPut;
029 import org.apache.http.client.methods.HttpRequestBase;
030 import org.apache.http.client.protocol.ClientContext;
031 import org.apache.http.entity.StringEntity;
032 import org.apache.http.impl.auth.BasicScheme;
033 import org.apache.http.impl.client.DefaultHttpClient;
034 import org.apache.http.params.HttpConnectionParams;
035 import org.apache.http.params.HttpParams;
036 import org.apache.http.protocol.BasicHttpContext;
037 import org.apache.http.protocol.ExecutionContext;
038 import org.apache.http.protocol.HttpContext;
039 import org.apache.http.util.EntityUtils;
040 import org.sonar.wsclient.Host;
041 import org.sonar.wsclient.services.CreateQuery;
042 import org.sonar.wsclient.services.DeleteQuery;
043 import org.sonar.wsclient.services.Query;
044 import org.sonar.wsclient.services.UpdateQuery;
045
046 import java.io.IOException;
047 import java.io.UnsupportedEncodingException;
048
049 /**
050 * @since 2.1
051 */
052 public class HttpClient4Connector extends Connector {
053
054 private Host server;
055
056 public HttpClient4Connector(Host server) {
057 this.server = server;
058 }
059
060 public String execute(Query<?> query) {
061 return executeRequest(newGetMethod(query));
062 }
063
064 public String execute(CreateQuery<?> query) {
065 return executeRequest(newPostMethod(query));
066 }
067
068 public String execute(UpdateQuery<?> query) {
069 return executeRequest(newPutMethod(query));
070 }
071
072 public String execute(DeleteQuery<?> query) {
073 return executeRequest(newDeleteMethod(query));
074 }
075
076 private String executeRequest(HttpRequestBase request) {
077 String json = null;
078 DefaultHttpClient client = createClient();
079 try {
080 BasicHttpContext context = createLocalContext(client);
081 HttpResponse response = client.execute(request, context);
082 HttpEntity entity = response.getEntity();
083 if (entity != null) {
084 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
085 json = EntityUtils.toString(entity);
086
087 } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NOT_FOUND) {
088 throw new ConnectionException("HTTP error: " + response.getStatusLine().getStatusCode() + ", msg: " + response.getStatusLine().getReasonPhrase() + ", query: " + request.toString());
089 }
090 }
091
092 } catch (IOException e) {
093 throw new ConnectionException("Query: " + request.getURI(), e);
094
095 } finally {
096 client.getConnectionManager().shutdown();
097 }
098 return json;
099 }
100
101 private DefaultHttpClient createClient() {
102 DefaultHttpClient client = new DefaultHttpClient();
103 HttpParams params = client.getParams();
104 HttpConnectionParams.setConnectionTimeout(params, TIMEOUT_MS);
105 HttpConnectionParams.setSoTimeout(params, TIMEOUT_MS);
106 if (server.getUsername() != null) {
107 client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(server.getUsername(), server.getPassword()));
108 }
109 return client;
110 }
111
112 private BasicHttpContext createLocalContext(DefaultHttpClient client) {
113 BasicHttpContext localcontext = new BasicHttpContext();
114
115 if (server.getUsername() != null) {
116 // Generate BASIC scheme object and stick it to the local
117 // execution context
118 BasicScheme basicAuth = new BasicScheme();
119 localcontext.setAttribute("preemptive-auth", basicAuth);
120
121 // Add as the first request interceptor
122 client.addRequestInterceptor(new PreemptiveAuth(), 0);
123 }
124 return localcontext;
125 }
126
127 private HttpGet newGetMethod(Query<?> query) {
128 HttpGet get = new HttpGet(server.getHost() + query.getUrl());
129 setJsonHeader(get);
130 return get;
131 }
132
133 private HttpDelete newDeleteMethod(DeleteQuery<?> query) {
134 HttpDelete delete = new HttpDelete(server.getHost() + query.getUrl());
135 setJsonHeader(delete);
136 return delete;
137 }
138
139 private HttpPost newPostMethod(CreateQuery<?> query) {
140 HttpPost post = new HttpPost(server.getHost() + query.getUrl());
141 if (query.getBody() != null) {
142 try {
143 post.setEntity(new StringEntity(query.getBody(), "UTF-8"));
144 } catch (UnsupportedEncodingException e) {
145 throw new ConnectionException("Encoding is not supported", e);
146 }
147 }
148 setJsonHeader(post);
149 return post;
150 }
151
152 private HttpPut newPutMethod(UpdateQuery<?> query) {
153 HttpPut put = new HttpPut(server.getHost() + query.getUrl());
154 if (query.getBody() != null) {
155 try {
156 put.setEntity(new StringEntity(query.getBody(), "UTF-8"));
157 } catch (UnsupportedEncodingException e) {
158 throw new ConnectionException("Encoding is not supported", e);
159 }
160 }
161 setJsonHeader(put);
162 return put;
163 }
164
165 private void setJsonHeader(HttpRequestBase request) {
166 request.setHeader("Accept", "application/json");
167 }
168
169 static final class PreemptiveAuth implements HttpRequestInterceptor {
170 public void process(
171 final HttpRequest request,
172 final HttpContext context) throws HttpException {
173
174 AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
175
176 // If no auth scheme available yet, try to initialize it preemptively
177 if (authState.getAuthScheme() == null) {
178 AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
179 CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
180 HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
181 if (authScheme != null) {
182 Credentials creds = credsProvider.getCredentials(
183 new AuthScope(
184 targetHost.getHostName(),
185 targetHost.getPort()));
186 if (creds == null) {
187 throw new HttpException("No credentials for preemptive authentication");
188 }
189 authState.setAuthScheme(authScheme);
190 authState.setCredentials(creds);
191 }
192 }
193 }
194 }
195 }