Search in sources :

Example 1 with ForestConvertException

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

the class DefaultBinaryConverter method convertToJavaObject.

private <T> T convertToJavaObject(Object source, Class<T> targetType, Charset charset) {
    if (source instanceof byte[]) {
        source = new ByteArrayInputStream((byte[]) source);
    }
    if (source instanceof InputStream) {
        InputStream in = (InputStream) source;
        if (InputStream.class.isAssignableFrom(targetType)) {
            return (T) source;
        }
        if (byte[].class.isAssignableFrom(targetType)) {
            return (T) inputStreamToByteArray(in);
        }
        byte[] tmp = inputStreamToByteArray(in);
        String str = null;
        try {
            String encode;
            if (charset == null) {
                encode = ByteEncodeUtils.getCharsetName(tmp);
                if (encode.toUpperCase().startsWith("GB")) {
                    encode = "GBK";
                }
            } else {
                encode = charset.name();
            }
            str = IOUtils.toString(tmp, encode);
        } catch (IOException e) {
            throw new ForestRuntimeException(e);
        }
        if (String.class.isAssignableFrom(targetType)) {
            return (T) str;
        }
        return autoConverter.convertToJavaObject(str, targetType);
    } else if (source instanceof File) {
        File file = (File) source;
        if (File.class.isAssignableFrom(targetType)) {
            return (T) file;
        }
        try {
            if (InputStream.class.isAssignableFrom(targetType)) {
                return (T) FileUtils.openInputStream(file);
            }
            if (byte[].class.isAssignableFrom(targetType)) {
                return (T) FileUtils.readFileToByteArray(file);
            }
            String str = FileUtils.readFileToString(file);
            if (String.class.isAssignableFrom(targetType)) {
                return (T) str;
            }
            return autoConverter.convertToJavaObject(str, targetType);
        } catch (IOException e) {
            throw new ForestConvertException(this, e);
        }
    }
    return convertToJavaObjectEx(source, targetType);
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException)

Example 2 with ForestConvertException

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

the class ForestGoogleProtobufConverter method convertToJavaObject.

@Override
public <T> T convertToJavaObject(byte[] source, Type targetType) {
    Class<?> c = (Class<?>) targetType;
    Parser<T> parser;
    try {
        // 转换器 都会有parser方法
        Method method = c.getDeclaredMethod("parser");
        // noinspection unchecked
        parser = (Parser<T>) method.invoke(null);
        return parser.parseFrom(source);
    } catch (ReflectiveOperationException | InvalidProtocolBufferException e) {
        throw new ForestConvertException(this, e);
    }
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Method(java.lang.reflect.Method) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException)

Example 3 with ForestConvertException

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

the class ForestJaxbConverter method convertToJavaObject.

@Override
public <T> T convertToJavaObject(String source, Class<T> targetType) {
    JAXBContext jaxbContext = null;
    try {
        jaxbContext = JAXBContext.newInstance(targetType);
        StringReader reader = new StringReader(source);
        return (T) createUnmarshaller(jaxbContext).unmarshal(reader);
    } catch (JAXBException e) {
        throw new ForestConvertException(this, e);
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException)

Example 4 with ForestConvertException

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

the class TestExceptions method testConvertException.

@Test
public void testConvertException() {
    Throwable th = new Exception("xxx");
    ForestConverter<?> converter = new ForestFastjsonConverter();
    ForestConvertException exception = new ForestConvertException(converter, th);
    assertThat(exception.getMessage()).isEqualTo("[Forest] json converter: 'ForestFastjsonConverter' error: xxx");
    assertThat(exception.getConverterClass()).isEqualTo(ForestFastjsonConverter.class);
    converter = new ForestJacksonConverter();
    exception = new ForestConvertException(converter, th);
    assertThat(exception.getMessage()).isEqualTo("[Forest] json converter: 'ForestJacksonConverter' error: xxx");
    assertThat(exception.getConverterClass()).isEqualTo(ForestJacksonConverter.class);
    converter = new DefaultAutoConverter(ForestConfiguration.configuration());
    exception = new ForestConvertException(converter, th);
    assertThat(exception.getMessage()).isEqualTo("[Forest] auto converter: 'DefaultAutoConverter' error: xxx");
    assertThat(exception.getConverterClass()).isEqualTo(DefaultAutoConverter.class);
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) DefaultAutoConverter(com.dtflys.forest.converter.auto.DefaultAutoConverter) ForestJacksonConverter(com.dtflys.forest.converter.json.ForestJacksonConverter) 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) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException) Test(org.junit.Test)

Example 5 with ForestConvertException

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

the class ForestGsonConverter method convertToJavaObject.

@Override
public <T> T convertToJavaObject(String source, Type targetType) {
    if (StringUtils.isBlank(source)) {
        return null;
    }
    try {
        if (targetType instanceof ParameterizedType || targetType.getClass().getName().startsWith("com.google.gson")) {
            Gson gson = createGson();
            return gson.fromJson(source, targetType);
        }
        Class clazz = ReflectUtils.toClass(targetType);
        try {
            if (Map.class.isAssignableFrom(clazz)) {
                JsonParser jsonParser = new JsonParser();
                JsonObject jsonObject = jsonParser.parse(source).getAsJsonObject();
                return (T) toMap(jsonObject, false);
            } else if (List.class.isAssignableFrom(clazz)) {
                JsonParser jsonParser = new JsonParser();
                JsonArray jsonArray = jsonParser.parse(source).getAsJsonArray();
                return (T) toList(jsonArray);
            }
            Gson gson = createGson();
            return (T) gson.fromJson(source, targetType);
        } catch (Throwable th) {
            throw new ForestConvertException(this, th);
        }
    } catch (Exception ex) {
        throw new ForestConvertException(this, ex);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException) IOException(java.io.IOException)

Aggregations

ForestConvertException (com.dtflys.forest.exceptions.ForestConvertException)7 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)3 JAXBContext (javax.xml.bind.JAXBContext)2 JAXBException (javax.xml.bind.JAXBException)2 DefaultAutoConverter (com.dtflys.forest.converter.auto.DefaultAutoConverter)1 ForestFastjsonConverter (com.dtflys.forest.converter.json.ForestFastjsonConverter)1 ForestJacksonConverter (com.dtflys.forest.converter.json.ForestJacksonConverter)1 ForestFileNotFoundException (com.dtflys.forest.exceptions.ForestFileNotFoundException)1 ForestHandlerException (com.dtflys.forest.exceptions.ForestHandlerException)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 ForestUnsupportException (com.dtflys.forest.exceptions.ForestUnsupportException)1 ForestVariableUndefinedException (com.dtflys.forest.exceptions.ForestVariableUndefinedException)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1