Search in sources :

Example 31 with ForestRuntimeException

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);
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) Charset(java.nio.charset.Charset) ForestRequestBody(com.dtflys.forest.http.ForestRequestBody) ObjectRequestBody(com.dtflys.forest.http.body.ObjectRequestBody) NameValueRequestBody(com.dtflys.forest.http.body.NameValueRequestBody)

Example 32 with ForestRuntimeException

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;
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Example 33 with ForestRuntimeException

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);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Example 34 with ForestRuntimeException

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);
    }
}
Also used : StringWriter(java.io.StringWriter) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) JAXBException(javax.xml.bind.JAXBException) List(java.util.List) JAXBContext(javax.xml.bind.JAXBContext) Map(java.util.Map) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException)

Example 35 with ForestRuntimeException

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();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

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