package de.tse.simplerestfacade;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;

import de.tse.simplerestfacade.marshalling.Unmarshaller;

public class ResultConverterResponseHandler<T> implements ResponseHandler<Object> {
    
    private final Unmarshaller unmarshaller;
    private final Class<T> returnType;
    
    public ResultConverterResponseHandler(final Unmarshaller unmarshaller, final Class<T> returnType) {
        this.unmarshaller = unmarshaller;
        this.returnType = returnType;
    }
    
    @Override
    public Object handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {

        validateStatusCode(response);
        
        final String content = EntityUtils.toString(response.getEntity());

        return unmarshaller.unmarshall(content, returnType);
    }

    private void validateStatusCode(final HttpResponse response) {
        final StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
            throw new RestClientException(statusLine.toString());
        }
    }
}
@@ -904,24 +904,112 @@
 );%0A        %0A
+        Object result = null;%0A        %0A        if (response.getEntity() != null) %7B%0A%0A    
         fina
@@ -1071,25 +1071,25 @@
 ity());%0A
-%0A
+ 
         
 return u
@@ -1080,22 +1080,27 @@
         
-return
+   result =
  unmarsh
@@ -1134,24 +1134,66 @@
 eturnType);%0A
+        %7D%0A        %0A        return result;%0A
     %7D%0A%0A    p
@@ -1329,16 +1329,38 @@
      if 
+(!requestWasSuccessful
 (statusL
@@ -1382,28 +1382,9 @@
 de()
- != HttpStatus.SC_OK
+)
 ) %7B%0A
@@ -1465,9 +1465,179 @@
 %7D%0A    %7D%0A
+    %0A    private boolean requestWasSuccessful(final int statusCode) %7B%0A        return statusCode %3E= HttpStatus.SC_OK %7C%7C statusCode %3C HttpStatus.SC_MULTIPLE_CHOICES;%0A    %7D%0A
 %7D

Throw Exception only if StatusCode is out of 2xx Range; Unmarshal Response only, if ResponseEntity exist.