Search in sources :

Example 6 with ForestRuntimeException

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

the class OkHttp3LogBodyMessage method getBodyString.

@Override
public String getBodyString() {
    if (requestBody == null) {
        return null;
    }
    MediaType mediaType = requestBody.contentType();
    if (mediaType == null) {
        return getLogContentForStringBody(this.requestBody);
    }
    ContentType contentType = new ContentType(mediaType.toString());
    if (contentType.isMultipart()) {
        MultipartBody multipartBody = (MultipartBody) requestBody;
        String boundary = multipartBody.boundary();
        Long contentLength = null;
        try {
            contentLength = multipartBody.contentLength();
        } catch (IOException e) {
        }
        StringBuilder builder = new StringBuilder();
        builder.append("[").append("boundary=").append(boundary);
        if (contentLength != null) {
            builder.append("; length=").append(contentLength);
        }
        builder.append("] parts:");
        List<MultipartBody.Part> parts = multipartBody.parts();
        for (MultipartBody.Part part : parts) {
            RequestBody partBody = part.body();
            List<String> disposition = part.headers().values("Content-Disposition");
            builder.append("\n             -- [").append(disposition.get(0));
            MediaType partMediaType = partBody.contentType();
            if (partMediaType == null) {
                builder.append("; content-type=\"").append(partBody.contentType()).append("\"");
                builder.append("; value=\"").append(getLogContentForStringBody(partBody)).append("\"]");
            } else {
                Long length = null;
                try {
                    length = partBody.contentLength();
                } catch (IOException e) {
                }
                if (length != null) {
                    builder.append("; length=").append(length);
                }
                builder.append("; content-type=\"").append(partBody.contentType()).append("\"");
                builder.append("]");
            }
        }
        return builder.toString();
    } else if (contentType.isBinary()) {
        try {
            return "[Binary length=" + requestBody.contentLength() + "]";
        } catch (IOException e) {
            throw new ForestRuntimeException(e);
        }
    }
    return getLogContentForStringBody(this.requestBody);
}
Also used : ContentType(com.dtflys.forest.backend.ContentType) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) IOException(java.io.IOException) MultipartBody(okhttp3.MultipartBody) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 7 with ForestRuntimeException

use of com.dtflys.forest.exceptions.ForestRuntimeException 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 8 with ForestRuntimeException

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

the class HttpclientForestResponse method readContentAsString.

private String readContentAsString() {
    try {
        InputStream inputStream = entity.getContent();
        if (inputStream == null) {
            return null;
        }
        bytes = IOUtils.toByteArray(inputStream);
        return byteToString(bytes);
    } catch (IOException e) {
        throw new ForestRuntimeException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) IOException(java.io.IOException)

Example 9 with ForestRuntimeException

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

the class TestRetryClient method testRetryWhen_with_error.

@Test
public void testRetryWhen_with_error() {
    server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(203));
    server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(203));
    server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(203));
    server.enqueue(new MockResponse().setBody(EXPECTED).setResponseCode(203));
    AtomicInteger count = new AtomicInteger(0);
    String ret = null;
    ForestRuntimeException exception = null;
    try {
        ret = retryClient.testRetryWhenWithError(3, 10, ((data, req, res) -> {
            count.incrementAndGet();
        }));
    } catch (ForestRuntimeException ex) {
        exception = ex;
    }
    assertThat(ret).isNull();
    assertThat(count.get()).isEqualTo(0);
    assertThat(exception).isNotNull();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 10 with ForestRuntimeException

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

the class TestForestConfiguration method testBackend.

@Test
public void testBackend() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    configuration.setBackendName("okhttp3");
    assertEquals("okhttp3", configuration.getBackend().getName());
    configuration.setBackend(null);
    configuration.setBackendName("httpclient");
    assertEquals("httpclient", configuration.getBackend().getName());
    HttpBackendSelector originSelector = new HttpBackendSelector();
    HttpBackendSelector selector = Mockito.spy(originSelector);
    configuration.setHttpBackendSelector(selector);
    Mockito.when(selector.findOkHttp3BackendInstance()).thenReturn(null);
    configuration.setBackendName(null);
    configuration.setBackend(null);
    Assert.assertEquals("httpclient", configuration.getBackend().getName());
    Mockito.when(selector.findHttpclientBackendInstance()).thenReturn(null);
    configuration.setBackendName(null);
    configuration.setBackend(null);
    boolean thrown = false;
    try {
        HttpBackend backend = configuration.getBackend();
        System.out.print(backend);
    } catch (ForestRuntimeException e) {
        thrown = true;
    }
    assertTrue(thrown);
}
Also used : HttpBackendSelector(com.dtflys.forest.backend.HttpBackendSelector) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) HttpBackend(com.dtflys.forest.backend.HttpBackend) Test(org.junit.Test)

Aggregations

ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)64 Test (org.junit.Test)14 Map (java.util.Map)9 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)7 MetaRequest (com.dtflys.forest.reflection.MetaRequest)6 ForestLogHandler (com.dtflys.forest.logging.ForestLogHandler)5 MappingParameter (com.dtflys.forest.mapping.MappingParameter)5 Method (java.lang.reflect.Method)5 Parameter (java.lang.reflect.Parameter)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 SSLKeyStore (com.dtflys.forest.ssl.SSLKeyStore)4 IOException (java.io.IOException)4 Annotation (java.lang.annotation.Annotation)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 MalformedURLException (java.net.MalformedURLException)4 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)4 ForestConverter (com.dtflys.forest.converter.ForestConverter)3 ForestRequest (com.dtflys.forest.http.ForestRequest)3 Interceptor (com.dtflys.forest.interceptor.Interceptor)3