Skip to content

Commit 515fc2a

Browse files
committed
Use GoodData HTTP client in DataStoreService
Replace user+login authentication with GoodData authentication which is a prerequisite for authentitacion with SST
1 parent d0c1cb1 commit 515fc2a

File tree

2 files changed

+272
-12
lines changed

2 files changed

+272
-12
lines changed

src/main/java/com/gooddata/GoodData.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,29 +164,28 @@ protected GoodData(String hostname, String login, String password, int port, Str
164164
notEmpty(login, "login");
165165
notEmpty(password, "password");
166166
notEmpty(protocol, "protocol");
167-
final HttpClientBuilder httpClientBuilder = createHttpClientBuilder(settings);
167+
final HttpClient httpClient = createHttpClient(login, password, hostname, port, protocol,
168+
createHttpClientBuilder(settings));
168169

169-
restTemplate = createRestTemplate(login, password, hostname, httpClientBuilder, port, protocol);
170+
restTemplate = createRestTemplate(hostname, httpClient, port, protocol);
170171

171172
accountService = new AccountService(getRestTemplate());
172173
projectService = new ProjectService(getRestTemplate(), accountService);
173174
metadataService = new MetadataService(getRestTemplate());
174175
modelService = new ModelService(getRestTemplate());
175176
gdcService = new GdcService(getRestTemplate());
176-
dataStoreService = new DataStoreService(httpClientBuilder, gdcService, new HttpHost(hostname, port, protocol).toURI(), login, password);
177+
dataStoreService = new DataStoreService(httpClient, gdcService, new HttpHost(hostname, port, protocol).toURI());
177178
datasetService = new DatasetService(getRestTemplate(), dataStoreService);
178179
reportService = new ReportService(getRestTemplate());
179180
processService = new ProcessService(getRestTemplate(), accountService, dataStoreService);
180181
warehouseService = new WarehouseService(getRestTemplate(), hostname, port);
181182
connectorService = new ConnectorService(getRestTemplate(), projectService);
182183
}
183184

184-
private RestTemplate createRestTemplate(String login, String password, String hostname, HttpClientBuilder builder,
185-
int port, String protocol) {
186-
final HttpClient client = createHttpClient(login, password, hostname, port, protocol, builder);
185+
private RestTemplate createRestTemplate(String hostname, HttpClient httpClient, int port, String protocol) {
187186

188187
final UriPrefixingClientHttpRequestFactory factory = new UriPrefixingClientHttpRequestFactory(
189-
new HttpComponentsClientHttpRequestFactory(client), hostname, port, protocol);
188+
new HttpComponentsClientHttpRequestFactory(httpClient), hostname, port, protocol);
190189
final RestTemplate restTemplate = new RestTemplate(factory);
191190
restTemplate.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList(
192191
new HeaderSettingRequestInterceptor(singletonMap("Accept", getAcceptHeaderValue()))));

src/main/java/com/gooddata/gdc/DataStoreService.java

Lines changed: 266 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,30 @@
66
import com.github.sardine.Sardine;
77
import com.github.sardine.impl.SardineImpl;
88
import com.gooddata.UriPrefixer;
9+
import org.apache.commons.lang.Validate;
10+
import org.apache.http.Header;
11+
import org.apache.http.HeaderIterator;
12+
import org.apache.http.HttpEntity;
13+
import org.apache.http.HttpHost;
14+
import org.apache.http.HttpRequest;
15+
import org.apache.http.HttpResponse;
16+
import org.apache.http.ProtocolVersion;
17+
import org.apache.http.StatusLine;
18+
import org.apache.http.client.ClientProtocolException;
19+
import org.apache.http.client.HttpClient;
20+
import org.apache.http.client.ResponseHandler;
21+
import org.apache.http.client.methods.CloseableHttpResponse;
22+
import org.apache.http.client.methods.HttpUriRequest;
23+
import org.apache.http.conn.ClientConnectionManager;
24+
import org.apache.http.impl.client.CloseableHttpClient;
925
import org.apache.http.impl.client.HttpClientBuilder;
26+
import org.apache.http.params.HttpParams;
27+
import org.apache.http.protocol.HttpContext;
1028

1129
import java.io.IOException;
1230
import java.io.InputStream;
1331
import java.net.URI;
32+
import java.util.Locale;
1433

1534
import static com.gooddata.util.Validate.notEmpty;
1635
import static com.gooddata.util.Validate.notNull;
@@ -28,16 +47,14 @@ public class DataStoreService {
2847

2948
/**
3049
* Creates new DataStoreService
31-
* @param httClientBuilder httpClientBuilder to build datastore connection
50+
* @param httpClient httpClient to make datastore connection
3251
* @param gdcService used to obtain datastore URI
3352
* @param gdcUri complete GDC URI used to prefix possibly relative datastore path
34-
* @param user datastore user
35-
* @param pass datastore password
3653
*/
37-
public DataStoreService(HttpClientBuilder httClientBuilder, GdcService gdcService, String gdcUri, String user, String pass) {
54+
public DataStoreService(HttpClient httpClient, GdcService gdcService, String gdcUri) {
3855
this.gdcService = notNull(gdcService, "gdcService");
3956
this.gdcUri = URI.create(notEmpty(gdcUri, "gdcUri"));
40-
sardine = new SardineImpl(httClientBuilder, user, pass);
57+
sardine = new SardineImpl(new CustomHttpClientBuilder(httpClient));
4158
}
4259

4360
private UriPrefixer getPrefixer() {
@@ -109,4 +126,248 @@ public void delete(String path) {
109126
throw new DataStoreException("Unable to delete " + uri, e);
110127
}
111128
}
129+
130+
/**
131+
* This class is needed to provide Sardine with instance of {@link CloseableHttpClient}, because
132+
* used {@link com.gooddata.http.client.GoodDataHttpClient} is not Closeable at all (on purpose).
133+
* Thanks to that we can use proper GoodData authentication mechanism instead of basic auth.
134+
*
135+
* It creates simple closeable wrapper around plain {@link HttpClient} where {@code close()}
136+
* is implemented as noop (respectively for the response used).
137+
*/
138+
private static class CustomHttpClientBuilder extends HttpClientBuilder {
139+
140+
private final HttpClient client;
141+
142+
private CustomHttpClientBuilder(HttpClient client) {
143+
this.client = client;
144+
}
145+
146+
@Override
147+
public CloseableHttpClient build() {
148+
return new FakeCloseableHttpClient(client);
149+
}
150+
}
151+
152+
private static class FakeCloseableHttpClient extends CloseableHttpClient {
153+
private final HttpClient client;
154+
155+
private FakeCloseableHttpClient(HttpClient client) {
156+
notNull(client, "client can't be null");
157+
this.client = client;
158+
}
159+
160+
@Override
161+
protected CloseableHttpResponse doExecute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
162+
// nothing to do - this method is never called, because we override all methods from CloseableHttpClient
163+
return null;
164+
}
165+
166+
@Override
167+
public void close() throws IOException {
168+
// nothing to close - wrappedClient doesn't have to implement CloseableHttpClient
169+
}
170+
171+
@Override
172+
@Deprecated
173+
public HttpParams getParams() {
174+
return client.getParams();
175+
}
176+
177+
@Override
178+
@Deprecated
179+
public ClientConnectionManager getConnectionManager() {
180+
return client.getConnectionManager();
181+
}
182+
183+
@Override
184+
public CloseableHttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException {
185+
return new FakeCloseableHttpResponse(client.execute(request));
186+
}
187+
188+
@Override
189+
public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException {
190+
return new FakeCloseableHttpResponse(client.execute(request, context));
191+
}
192+
193+
@Override
194+
public CloseableHttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException {
195+
return new FakeCloseableHttpResponse(client.execute(target, request));
196+
}
197+
198+
@Override
199+
public CloseableHttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
200+
return new FakeCloseableHttpResponse(client.execute(target, request, context));
201+
}
202+
203+
@Override
204+
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
205+
return client.execute(request, responseHandler);
206+
}
207+
208+
@Override
209+
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
210+
return client.execute(request, responseHandler, context);
211+
}
212+
213+
@Override
214+
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
215+
return client.execute(target, request, responseHandler);
216+
}
217+
218+
@Override
219+
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
220+
return client.execute(target, request, responseHandler, context);
221+
}
222+
}
223+
224+
private static class FakeCloseableHttpResponse implements CloseableHttpResponse {
225+
226+
private final HttpResponse wrappedResponse;
227+
228+
public FakeCloseableHttpResponse(HttpResponse wrappedResponse) {
229+
Validate.notNull(wrappedResponse, "wrappedResponse cannot be null!");
230+
this.wrappedResponse = wrappedResponse;
231+
}
232+
233+
@Override
234+
public void close() throws IOException {
235+
// nothing to close - wrappedClient doesn't have to implement CloseableHttpResponse
236+
}
237+
238+
@Override
239+
public StatusLine getStatusLine() {
240+
return wrappedResponse.getStatusLine();
241+
}
242+
243+
@Override
244+
public void setStatusLine(StatusLine statusline) {
245+
wrappedResponse.setStatusLine(statusline);
246+
}
247+
248+
@Override
249+
public void setStatusLine(ProtocolVersion ver, int code) {
250+
wrappedResponse.setStatusLine(ver, code);
251+
}
252+
253+
@Override
254+
public void setStatusLine(ProtocolVersion ver, int code, String reason) {
255+
wrappedResponse.setStatusLine(ver, code, reason);
256+
}
257+
258+
@Override
259+
public void setStatusCode(int code) throws IllegalStateException {
260+
wrappedResponse.setStatusCode(code);
261+
}
262+
263+
@Override
264+
public void setReasonPhrase(String reason) throws IllegalStateException {
265+
wrappedResponse.setReasonPhrase(reason);
266+
}
267+
268+
@Override
269+
public HttpEntity getEntity() {
270+
return wrappedResponse.getEntity();
271+
}
272+
273+
@Override
274+
public void setEntity(HttpEntity entity) {
275+
wrappedResponse.setEntity(entity);
276+
}
277+
278+
@Override
279+
public Locale getLocale() {
280+
return wrappedResponse.getLocale();
281+
}
282+
283+
@Override
284+
public void setLocale(Locale loc) {
285+
wrappedResponse.setLocale(loc);
286+
}
287+
288+
@Override
289+
public ProtocolVersion getProtocolVersion() {
290+
return wrappedResponse.getProtocolVersion();
291+
}
292+
293+
@Override
294+
public boolean containsHeader(String name) {
295+
return wrappedResponse.containsHeader(name);
296+
}
297+
298+
@Override
299+
public Header[] getHeaders(String name) {
300+
return wrappedResponse.getHeaders(name);
301+
}
302+
303+
@Override
304+
public Header getFirstHeader(String name) {
305+
return wrappedResponse.getFirstHeader(name);
306+
}
307+
308+
@Override
309+
public Header getLastHeader(String name) {
310+
return wrappedResponse.getLastHeader(name);
311+
}
312+
313+
@Override
314+
public Header[] getAllHeaders() {
315+
return wrappedResponse.getAllHeaders();
316+
}
317+
318+
@Override
319+
public void addHeader(Header header) {
320+
wrappedResponse.addHeader(header);
321+
}
322+
323+
@Override
324+
public void addHeader(String name, String value) {
325+
wrappedResponse.addHeader(name, value);
326+
}
327+
328+
@Override
329+
public void setHeader(Header header) {
330+
wrappedResponse.setHeader(header);
331+
}
332+
333+
@Override
334+
public void setHeader(String name, String value) {
335+
wrappedResponse.setHeader(name, value);
336+
}
337+
338+
@Override
339+
public void setHeaders(Header[] headers) {
340+
wrappedResponse.setHeaders(headers);
341+
}
342+
343+
@Override
344+
public void removeHeader(Header header) {
345+
wrappedResponse.removeHeader(header);
346+
}
347+
348+
@Override
349+
public void removeHeaders(String name) {
350+
wrappedResponse.removeHeaders(name);
351+
}
352+
353+
@Override
354+
public HeaderIterator headerIterator() {
355+
return wrappedResponse.headerIterator();
356+
}
357+
358+
@Override
359+
public HeaderIterator headerIterator(String name) {
360+
return wrappedResponse.headerIterator(name);
361+
}
362+
363+
@Override
364+
public HttpParams getParams() {
365+
return wrappedResponse.getParams();
366+
}
367+
368+
@Override
369+
public void setParams(HttpParams params) {
370+
wrappedResponse.setParams(params);
371+
}
372+
}
112373
}

0 commit comments

Comments
 (0)