33 */
44package com .gooddata ;
55
6+ import static com .gooddata .Validate .notNull ;
7+ import static java .lang .String .format ;
8+ import static org .springframework .http .HttpMethod .GET ;
9+
610import org .codehaus .jackson .map .ObjectMapper ;
711import org .springframework .http .HttpHeaders ;
812import org .springframework .http .HttpStatus ;
1721import java .io .ByteArrayInputStream ;
1822import java .io .IOException ;
1923import java .io .InputStream ;
24+ import java .io .OutputStream ;
2025import java .util .concurrent .TimeUnit ;
2126
22- import static com .gooddata .Validate .notNull ;
23- import static java .lang .String .format ;
24- import static org .springframework .http .HttpMethod .GET ;
2527
2628/**
2729 * Parent for GoodData services providing helpers for REST API calls and polling.
@@ -58,7 +60,7 @@ public AbstractService(RestTemplate restTemplate) {
5860 this .restTemplate = notNull (restTemplate , "restTemplate" );
5961 }
6062
61- final <T > T poll (final PollHandler <T > handler , long timeout , final TimeUnit unit ) {
63+ final <R > R poll (final PollHandler <?, R > handler , long timeout , final TimeUnit unit ) {
6264 notNull (handler , "handler" );
6365 final long start = System .currentTimeMillis ();
6466 while (true ) {
@@ -77,15 +79,15 @@ final <T> T poll(final PollHandler<T> handler, long timeout, final TimeUnit unit
7779 }
7880 }
7981
80- final <T > boolean pollOnce (final PollHandler <T > handler ) {
82+ final <P > boolean pollOnce (final PollHandler <P ,? > handler ) {
8183 notNull (handler , "handler" );
8284 final ClientHttpResponse response = restTemplate .execute (handler .getPollingUri (), GET , noopRequestCallback ,
8385 reusableResponseExtractor );
8486
8587 try {
8688 if (handler .isFinished (response )) {
87- final T data = extractData (response , handler .getResultClass ());
88- handler .setResult (data );
89+ final P data = extractData (response , handler .getPollClass ());
90+ handler .handlePollResult (data );
8991 } else if (HttpStatus .Series .CLIENT_ERROR .equals (response .getStatusCode ().series ())) {
9092 throw new GoodDataException (
9193 format ("Polling returned client error HTTP status %s" , response .getStatusCode ().value ())
@@ -108,15 +110,18 @@ protected final <T> T extractData(ClientHttpResponse response, Class<T> cls) thr
108110
109111 private class ReusableClientHttpResponse implements ClientHttpResponse {
110112
111- private final byte [] body ;
113+ private byte [] body ;
112114 private final HttpStatus statusCode ;
113115 private final int rawStatusCode ;
114116 private final String statusText ;
115117 private final HttpHeaders headers ;
116118
117119 public ReusableClientHttpResponse (ClientHttpResponse response ) {
118120 try {
119- body = FileCopyUtils .copyToByteArray (response .getBody ());
121+ final InputStream bodyStream = response .getBody ();
122+ if (bodyStream != null ) {
123+ body = FileCopyUtils .copyToByteArray (bodyStream );
124+ }
120125 statusCode = response .getStatusCode ();
121126 rawStatusCode = response .getRawStatusCode ();
122127 statusText = response .getStatusText ();
@@ -152,7 +157,7 @@ public HttpHeaders getHeaders() {
152157
153158 @ Override
154159 public InputStream getBody () throws IOException {
155- return new ByteArrayInputStream (body );
160+ return body != null ? new ByteArrayInputStream (body ) : null ;
156161 }
157162
158163 @ Override
@@ -161,4 +166,17 @@ public void close() {
161166 }
162167 }
163168
169+ protected static class OutputStreamResponseExtractor implements ResponseExtractor <Integer > {
170+ private final OutputStream output ;
171+
172+ public OutputStreamResponseExtractor (OutputStream output ) {
173+ this .output = output ;
174+ }
175+
176+ @ Override
177+ public Integer extractData (ClientHttpResponse response ) throws IOException {
178+ return FileCopyUtils .copy (response .getBody (), output );
179+ }
180+ }
181+
164182}
0 commit comments