Search in sources :

Example 1 with ForestHandlerException

use of com.dtflys.forest.exceptions.ForestHandlerException in project forest by dromara.

the class ResultHandler method getResult.

public Object getResult(ForestRequest request, ForestResponse response, Type resultType, Class resultClass) {
    if (request.isDownloadFile()) {
        return null;
    }
    Object result = response.getResult();
    if (result != null && resultClass.isAssignableFrom(result.getClass())) {
        return result;
    }
    if (isReceivedResponseData(response)) {
        try {
            if (void.class.isAssignableFrom(resultClass)) {
                return null;
            }
            if (ForestResponse.class.isAssignableFrom(resultClass) || ForestRequest.class.isAssignableFrom(resultClass)) {
                if (resultType instanceof ParameterizedType) {
                    ParameterizedType parameterizedType = (ParameterizedType) resultType;
                    Class rowClass = (Class) parameterizedType.getRawType();
                    if (ForestResponse.class.isAssignableFrom(rowClass) || ForestRequest.class.isAssignableFrom(resultClass)) {
                        Type realType = parameterizedType.getActualTypeArguments()[0];
                        Class realClass = ReflectUtils.toClass(parameterizedType.getActualTypeArguments()[0]);
                        if (realClass == null) {
                            realClass = String.class;
                        }
                        Object realResult = getResult(request, response, realType, realClass);
                        response.setResult(realResult);
                    }
                } else {
                    Object realResult = getResult(request, response, Object.class, Object.class);
                    response.setResult(realResult);
                }
                return response;
            }
            if (Future.class.isAssignableFrom(resultClass)) {
                if (resultType instanceof ParameterizedType) {
                    ParameterizedType parameterizedType = (ParameterizedType) resultType;
                    Class rowClass = (Class) parameterizedType.getRawType();
                    if (Future.class.isAssignableFrom(rowClass)) {
                        Type realType = parameterizedType.getActualTypeArguments()[0];
                        Class realClass = ReflectUtils.toClass(parameterizedType.getActualTypeArguments()[0]);
                        return getResult(request, response, realType, realClass);
                    }
                }
            }
            if (resultClass.isArray()) {
                if (byte[].class.isAssignableFrom(resultClass)) {
                    return response.getByteArray();
                }
            }
            Object attFile = request.getAttachment(DownloadLifeCycle.ATTACHMENT_NAME_FILE);
            if (attFile != null && attFile instanceof File) {
                ForestConverter converter = request.getConfiguration().getConverter(ForestDataType.JSON);
                return converter.convertToJavaObject(attFile, resultClass);
            }
            String responseText = null;
            if (result != null && CharSequence.class.isAssignableFrom(result.getClass())) {
                responseText = result.toString();
            } else if (CharSequence.class.isAssignableFrom(resultClass)) {
                try {
                    responseText = response.readAsString();
                } catch (Throwable th) {
                    request.getLifeCycleHandler().handleError(request, response, th);
                }
            } else {
                try {
                    responseText = response.getContent();
                } catch (Throwable th) {
                    request.getLifeCycleHandler().handleError(request, response, th);
                }
            }
            response.setContent(responseText);
            if (CharSequence.class.isAssignableFrom(resultClass)) {
                return responseText;
            }
            if (InputStream.class.isAssignableFrom(resultClass)) {
                return response.getInputStream();
            }
            ContentType contentType = response.getContentType();
            if (request.getDecoder() != null) {
                if (contentType != null && contentType.canReadAsString()) {
                    return request.getDecoder().convertToJavaObject(responseText, resultType);
                } else {
                    return request.getDecoder().convertToJavaObject(response.getByteArray(), resultType);
                }
            }
            ForestDataType dataType = request.getDataType();
            ForestConverter converter = request.getConfiguration().getConverter(dataType);
            if (contentType != null && contentType.canReadAsString()) {
                return converter.convertToJavaObject(responseText, resultType);
            }
            Charset charset = null;
            String resCharset = response.getCharset();
            if (resCharset != null) {
                charset = Charset.forName(resCharset);
            }
            return converter.convertToJavaObject(response.getByteArray(), resultType, charset);
        } catch (Exception e) {
            throw new ForestHandlerException(e, request, response);
        }
    } else if (ForestResponse.class.isAssignableFrom(resultClass)) {
        return response;
    }
    return null;
}
Also used : ForestResponse(com.dtflys.forest.http.ForestResponse) ContentType(com.dtflys.forest.backend.ContentType) ForestDataType(com.dtflys.forest.utils.ForestDataType) Charset(java.nio.charset.Charset) ForestRequest(com.dtflys.forest.http.ForestRequest) ForestHandlerException(com.dtflys.forest.exceptions.ForestHandlerException) ParameterizedType(java.lang.reflect.ParameterizedType) ContentType(com.dtflys.forest.backend.ContentType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ForestDataType(com.dtflys.forest.utils.ForestDataType) ForestHandlerException(com.dtflys.forest.exceptions.ForestHandlerException) ForestConverter(com.dtflys.forest.converter.ForestConverter) File(java.io.File)

Example 2 with ForestHandlerException

use of com.dtflys.forest.exceptions.ForestHandlerException in project forest by dromara.

the class TestExceptions method testForestHandlerException.

@Test
public void testForestHandlerException() {
    ForestRequest<?> request = mock(ForestRequest.class);
    ForestResponse<?> response = mock(ForestResponse.class);
    try {
        throw new ForestHandlerException("misc", request, response);
    } catch (ForestHandlerException e) {
        assertEquals("misc", e.getMessage());
        assertEquals(request, e.getRequest());
        assertEquals(response, e.getResponse());
        e.setRequest(null);
        assertNull(e.getRequest());
        e.setResponse(null);
        assertNull(e.getResponse());
    }
    try {
        throw new Exception("first Exception");
    } catch (Exception e) {
        try {
            throw new ForestHandlerException("second Exception", e, request, response);
        } catch (ForestHandlerException fe) {
            assertEquals("second Exception", fe.getMessage());
            assertEquals(e, fe.getCause());
            assertEquals(request, fe.getRequest());
            assertEquals(response, fe.getResponse());
        }
    }
    try {
        throw new Exception("first Exception");
    } catch (Exception e) {
        try {
            throw new ForestHandlerException(e, request, response);
        } catch (ForestHandlerException fe) {
            assertEquals(e, fe.getCause());
            assertEquals(request, fe.getRequest());
            assertEquals(response, fe.getResponse());
        }
    }
}
Also used : ForestHandlerException(com.dtflys.forest.exceptions.ForestHandlerException) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestUnsupportException(com.dtflys.forest.exceptions.ForestUnsupportException) ForestInterceptorDefineException(com.dtflys.forest.exceptions.ForestInterceptorDefineException) ForestHandlerException(com.dtflys.forest.exceptions.ForestHandlerException) ForestVariableUndefinedException(com.dtflys.forest.exceptions.ForestVariableUndefinedException) ForestRetryException(com.dtflys.forest.exceptions.ForestRetryException) ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) ForestNoFileNameException(com.dtflys.forest.exceptions.ForestNoFileNameException) ForestFileNotFoundException(com.dtflys.forest.exceptions.ForestFileNotFoundException) Test(org.junit.Test)

Aggregations

ForestHandlerException (com.dtflys.forest.exceptions.ForestHandlerException)2 ContentType (com.dtflys.forest.backend.ContentType)1 ForestConverter (com.dtflys.forest.converter.ForestConverter)1 ForestConvertException (com.dtflys.forest.exceptions.ForestConvertException)1 ForestFileNotFoundException (com.dtflys.forest.exceptions.ForestFileNotFoundException)1 ForestInterceptorDefineException (com.dtflys.forest.exceptions.ForestInterceptorDefineException)1 ForestNetworkException (com.dtflys.forest.exceptions.ForestNetworkException)1 ForestNoFileNameException (com.dtflys.forest.exceptions.ForestNoFileNameException)1 ForestRetryException (com.dtflys.forest.exceptions.ForestRetryException)1 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)1 ForestUnsupportException (com.dtflys.forest.exceptions.ForestUnsupportException)1 ForestVariableUndefinedException (com.dtflys.forest.exceptions.ForestVariableUndefinedException)1 ForestRequest (com.dtflys.forest.http.ForestRequest)1 ForestResponse (com.dtflys.forest.http.ForestResponse)1 ForestDataType (com.dtflys.forest.utils.ForestDataType)1 File (java.io.File)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Charset (java.nio.charset.Charset)1 Test (org.junit.Test)1