66import com .github .sardine .Sardine ;
77import com .github .sardine .impl .SardineImpl ;
88import 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 ;
925import org .apache .http .impl .client .HttpClientBuilder ;
26+ import org .apache .http .params .HttpParams ;
27+ import org .apache .http .protocol .HttpContext ;
1028
1129import java .io .IOException ;
1230import java .io .InputStream ;
1331import java .net .URI ;
32+ import java .util .Locale ;
1433
1534import static com .gooddata .util .Validate .notEmpty ;
1635import 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