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);
}
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);
}
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);
}
}
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();
}
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);
}
Aggregations