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