use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class OkHttp3BodyBuilder method setStringBody.
@Override
protected void setStringBody(Request.Builder builder, ForestRequest request, String text, Charset charset, String contentType, boolean mergeCharset) {
MediaType mediaType = MediaType.parse(contentType);
Charset cs = StandardCharsets.UTF_8;
if (charset != null) {
cs = charset;
}
if (contentType != null) {
if (mediaType == null) {
throw new ForestRuntimeException("[Forest] '" + contentType + "' is not a valid content type");
}
Charset mtcs = mediaType.charset();
if (mtcs == null) {
if (charset != null && mergeCharset) {
mediaType = MediaType.parse(contentType + "; charset=" + charset.name().toLowerCase());
}
}
}
byte[] bytes = text.getBytes(cs);
RequestBody body = RequestBody.create(mediaType, bytes);
builder.method(request.getType().getName(), body);
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class ForestProtobufConverterManager method getForestProtobufConverter.
public ForestProtobufConverter getForestProtobufConverter() {
if (forestProtobufConverter == null) {
synchronized (this) {
if (forestProtobufConverter == null) {
if (checkSupportProtobuf()) {
try {
Class clazz = Class.forName(PROTOBUF_CONVERTER_CLASS);
forestProtobufConverter = (ForestProtobufConverter) clazz.newInstance();
} catch (Throwable th) {
throw new ForestRuntimeException("forestProtobufConverter create exception", th);
}
}
}
}
}
return forestProtobufConverter;
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class ForestJaxbConverter method createMarshaller.
public Marshaller createMarshaller(JAXBContext jaxbContext, String encoding) {
try {
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
if (StringUtils.isNotEmpty(encoding)) {
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
}
return marshaller;
} catch (JAXBException e) {
throw new ForestRuntimeException(e);
}
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class ForestJaxbConverter method encodeToString.
@Override
public String encodeToString(Object obj) {
if (obj == null) {
return null;
}
if (obj instanceof CharSequence) {
return obj.toString();
}
if (obj instanceof Map || obj instanceof List) {
throw new ForestRuntimeException("[Forest] JAXB XML converter dose not support translating instance of java.util.Map or java.util.List");
}
try {
JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());
StringWriter writer = new StringWriter();
createMarshaller(jaxbContext, "UTF-8").marshal(obj, writer);
return writer.toString();
} catch (JAXBException e) {
throw new ForestConvertException(this, e);
}
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class TestSuccessWhenClient method testRetry_with_error_successWhen.
@Test
public void testRetry_with_error_successWhen() {
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));
AtomicReference<Boolean> isError = new AtomicReference<>(false);
ForestRequest<String> request = successWhenClient.testRetryRequest_with_error_successWhen(3, (ex, req, res) -> {
isError.set(true);
});
assertThat(request).isNotNull();
assertThat(request.getSuccessWhen()).isNotNull().isInstanceOf(ErrorSuccessWhen.class);
ForestRuntimeException exception = null;
try {
request.execute();
} catch (ForestRuntimeException ex) {
exception = ex;
}
assertThat(request.getMaxRetryCount()).isEqualTo(3);
assertThat(request.getCurrentRetryCount()).isEqualTo(0);
assertThat(isError.get()).isFalse();
assertThat(exception).isNotNull();
}
Aggregations