/*
 * The MIT License
 *
 * Copyright 2014 Matija Mazi.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package si.mazi.rescu.serialization;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import si.mazi.rescu.InvocationResult;
import si.mazi.rescu.ResponseReader;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * Returns the response body as a string.
 */
public class PlainTextResponseReader extends ResponseReader {
    private static final Logger log = LoggerFactory.getLogger(PlainTextResponseReader.class);

    public PlainTextResponseReader(boolean ignoreHttpErrorCodes) {
        super(ignoreHttpErrorCodes);
    }

    @SuppressWarnings("unchecked")
    @Override
    public String read(InvocationResult invocationResult, Type returnType) throws IOException {
        return invocationResult.getHttpBody();
    }

    @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
    @Override
    protected RuntimeException readException(InvocationResult invocationResult, Class<? extends RuntimeException> exceptionType) throws IOException {
        final String message = read(invocationResult, exceptionType);
        RuntimeException constructedException;
        try {
            // try constructng the exception with message
            constructedException = exceptionType.getConstructor(String.class).newInstance(message);
        } catch (ReflectiveOperationException e) {
            try {
                // fallback to no-parameter constructor
                constructedException = exceptionType.newInstance();
                log.warn("Cannot construct a {} with message parameter. Ommiting the message, which was: {}", exceptionType, message);
            } catch (ReflectiveOperationException e1) {
                log.warn("Cannot construct a {}. Throwing a RuntimeException instead. Main cause: {}", exceptionType, e.toString());
                throw new RuntimeException(message);
            }
        }
        return constructedException;
    }
}

@@ -1224,16 +1224,17 @@
 actory;%0A
+%0A
 import s
@@ -1330,16 +1330,68 @@
 eption;%0A
+import java.lang.reflect.InvocationTargetException;%0A
 import j
@@ -2266,24 +2266,87 @@
 tedException
+ = null;%0A%0A        Exception reflectiveOperationException = null
 ;%0A        tr
@@ -2528,41 +2528,469 @@
 ch (
-ReflectiveOperationException e) %7B
+IllegalAccessException e) %7B%0A            reflectiveOperationException = e;%0A        %7D catch (InstantiationException e) %7B%0A            reflectiveOperationException = e;%0A        %7D catch (InvocationTargetException e) %7B%0A            reflectiveOperationException = e;%0A        %7D catch (NoSuchMethodException e) %7B%0A            reflectiveOperationException = e;%0A        %7D%0A%0A        if (reflectiveOperationException != null) %7B%0A            reflectiveOperationException = null;%0A
 %0A   
@@ -3288,39 +3288,252 @@
 ch (
-ReflectiveOperationException e1
+IllegalAccessException e) %7B%0A                reflectiveOperationException = e;%0A            %7D catch (InstantiationException e) %7B%0A                reflectiveOperationException = e;%0A            %7D%0A%0A            if (reflectiveOperationException != null
 ) %7B%0A
@@ -3650,17 +3650,44 @@
 onType, 
-e
+reflectiveOperationException
 .toStrin

Fix readException for Java 1.5, the ReflectiveOperationException is since Java 1.7.