diff --git a/README.md b/README.md index 4e90aa0..8c07076 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ DHIS2 Java SDK is a _lightweight_ library that hides the nuts and bolts of DHIS2 org.hisp.dhis.integration.sdk dhis2-java-sdk - 2.0.1-SNAPSHOT + 2.1.0-SNAPSHOT ... diff --git a/android-jackson-resource-model/pom.xml b/android-jackson-resource-model/pom.xml index 7c1a4d0..1119e82 100644 --- a/android-jackson-resource-model/pom.xml +++ b/android-jackson-resource-model/pom.xml @@ -5,7 +5,7 @@ dhis2-java-sdk-pom org.hisp.dhis.integration.sdk - 2.0.1-SNAPSHOT + 2.1.0-SNAPSHOT 4.0.0 diff --git a/jackson-resource-model/pom.xml b/jackson-resource-model/pom.xml index 2b1ade0..30f0b1d 100644 --- a/jackson-resource-model/pom.xml +++ b/jackson-resource-model/pom.xml @@ -5,7 +5,7 @@ dhis2-java-sdk-pom org.hisp.dhis.integration.sdk - 2.0.1-SNAPSHOT + 2.1.0-SNAPSHOT 4.0.0 diff --git a/pom.xml b/pom.xml index 8f703fb..2619ab0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.hisp.dhis.integration.sdk dhis2-java-sdk-pom pom - 2.0.1-SNAPSHOT + 2.1.0-SNAPSHOT DHIS2 Java SDK https://github.com/dhis2/dhis2-java-sdk diff --git a/sdk/pom.xml b/sdk/pom.xml index 12ca583..4de86cd 100644 --- a/sdk/pom.xml +++ b/sdk/pom.xml @@ -5,7 +5,7 @@ dhis2-java-sdk-pom org.hisp.dhis.integration.sdk - 2.0.1-SNAPSHOT + 2.1.0-SNAPSHOT 4.0.0 dhis2-java-sdk diff --git a/sdk/src/main/java/org/hisp/dhis/integration/sdk/DefaultDhis2Client.java b/sdk/src/main/java/org/hisp/dhis/integration/sdk/DefaultDhis2Client.java index c2d4351..ab5a608 100644 --- a/sdk/src/main/java/org/hisp/dhis/integration/sdk/DefaultDhis2Client.java +++ b/sdk/src/main/java/org/hisp/dhis/integration/sdk/DefaultDhis2Client.java @@ -109,4 +109,10 @@ public String getApiUrl() { return apiUrl; } + + @Override + public ConverterFactory getConverterFactory() + { + return converterFactory; + } } diff --git a/sdk/src/main/java/org/hisp/dhis/integration/sdk/api/Dhis2Client.java b/sdk/src/main/java/org/hisp/dhis/integration/sdk/api/Dhis2Client.java index 89c65cb..494b2bf 100644 --- a/sdk/src/main/java/org/hisp/dhis/integration/sdk/api/Dhis2Client.java +++ b/sdk/src/main/java/org/hisp/dhis/integration/sdk/api/Dhis2Client.java @@ -29,6 +29,7 @@ import okhttp3.OkHttpClient; +import org.hisp.dhis.integration.sdk.api.converter.ConverterFactory; import org.hisp.dhis.integration.sdk.api.operation.DeleteOperation; import org.hisp.dhis.integration.sdk.api.operation.GetOperation; import org.hisp.dhis.integration.sdk.api.operation.PatchOperation; @@ -50,4 +51,6 @@ public interface Dhis2Client OkHttpClient getHttpClient(); String getApiUrl(); + + ConverterFactory getConverterFactory(); } diff --git a/sdk/src/main/java/org/hisp/dhis/integration/sdk/api/converter/Converter.java b/sdk/src/main/java/org/hisp/dhis/integration/sdk/api/converter/Converter.java new file mode 100644 index 0000000..a7ff7fb --- /dev/null +++ b/sdk/src/main/java/org/hisp/dhis/integration/sdk/api/converter/Converter.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2004-2023, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.integration.sdk.api.converter; + +import java.io.Reader; +import java.util.List; + +public interface Converter +{ + String convert(Object from); + + T convert( Object from, Class toType ); + + T convert( Reader source, Class sourceType); + + T convert( List from, Class toCollectionType, Class toElementType ); +} diff --git a/sdk/src/main/java/org/hisp/dhis/integration/sdk/api/converter/ConverterFactory.java b/sdk/src/main/java/org/hisp/dhis/integration/sdk/api/converter/ConverterFactory.java index 786b559..23c3d2c 100644 --- a/sdk/src/main/java/org/hisp/dhis/integration/sdk/api/converter/ConverterFactory.java +++ b/sdk/src/main/java/org/hisp/dhis/integration/sdk/api/converter/ConverterFactory.java @@ -29,6 +29,8 @@ public interface ConverterFactory { + Converter createConverter(); + RequestConverter createRequestConverter( Class requestType ); ResponseConverter createResponseConverter( Class responseType ); diff --git a/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonConverter.java b/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonConverter.java new file mode 100644 index 0000000..b17fb9c --- /dev/null +++ b/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonConverter.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2004-2023, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.integration.sdk.internal.converter; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.hisp.dhis.integration.sdk.api.Dhis2ClientException; +import org.hisp.dhis.integration.sdk.api.converter.Converter; + +import java.io.IOException; +import java.io.Reader; +import java.util.List; + +public class JacksonConverter implements Converter +{ + private final ObjectMapper objectMapper; + + public JacksonConverter( ObjectMapper objectMapper ) + { + this.objectMapper = objectMapper; + } + + @Override + public String convert( Object from ) + { + try + { + return objectMapper.writeValueAsString( from ); + } + catch ( JsonProcessingException e ) + { + throw new Dhis2ClientException( e ); + } + } + + @Override + public T convert( Object from, Class toType ) + { + return objectMapper.convertValue( from, toType ); + } + + @Override + public T convert( Reader source, Class toType ) + { + try + { + return objectMapper.readValue( source, toType ); + } + catch ( IOException e ) + { + throw new Dhis2ClientException( e ); + } + } + + @Override + public T convert( List from, Class toCollectionType, Class toElementType ) + { + return objectMapper.convertValue( from, + objectMapper.getTypeFactory().constructCollectionLikeType( toCollectionType, toElementType ) ); + } + + public ObjectMapper getObjectMapper() + { + return objectMapper; + } +} diff --git a/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonConverterFactory.java b/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonConverterFactory.java index 1f2318d..bee5d56 100644 --- a/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonConverterFactory.java +++ b/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonConverterFactory.java @@ -47,15 +47,21 @@ public JacksonConverterFactory() this.objectMapper = new ObjectMapper().registerModule( new Jdk8Module().configureAbsentsAsNulls( true ) ); } + @Override + public JacksonConverter createConverter() + { + return new JacksonConverter( objectMapper ); + } + @Override public RequestConverter createRequestConverter( Class requestType ) { - return new JacksonRequestConverter<>( requestType, objectMapper ); + return new JacksonRequestConverter<>( requestType, createConverter() ); } @Override public ResponseConverter createResponseConverter( Class responseType ) { - return new JacksonResponseConverter<>( responseType, objectMapper ); + return new JacksonResponseConverter<>( responseType, createConverter() ); } } diff --git a/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonRequestConverter.java b/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonRequestConverter.java index 98f5cc0..f36c071 100644 --- a/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonRequestConverter.java +++ b/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonRequestConverter.java @@ -36,24 +36,17 @@ public class JacksonRequestConverter implements RequestConverter { private final Class requestType; - private final ObjectMapper objectMapper; + private final JacksonConverter jacksonConverter; - public JacksonRequestConverter( Class requestType, ObjectMapper objectMapper ) + public JacksonRequestConverter( Class requestType, JacksonConverter jacksonConverter ) { this.requestType = requestType; - this.objectMapper = objectMapper; + this.jacksonConverter = jacksonConverter; } @Override public String convert( T requestBody ) { - try - { - return objectMapper.writeValueAsString( requestBody ); - } - catch ( IOException e ) - { - throw new RuntimeException( e ); - } + return jacksonConverter.convert( requestBody ); } } diff --git a/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonResponseConverter.java b/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonResponseConverter.java index 34aa4fc..37dd323 100644 --- a/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonResponseConverter.java +++ b/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/converter/JacksonResponseConverter.java @@ -33,20 +33,19 @@ import okhttp3.ResponseBody; +import org.hisp.dhis.integration.sdk.api.Dhis2ClientException; import org.hisp.dhis.integration.sdk.api.converter.ResponseConverter; -import com.fasterxml.jackson.databind.ObjectMapper; - public class JacksonResponseConverter implements ResponseConverter { private final Class returnType; - private final ObjectMapper objectMapper; + private final JacksonConverter jacksonConverter; - public JacksonResponseConverter( Class returnType, ObjectMapper objectMapper ) + public JacksonResponseConverter( Class returnType, JacksonConverter jacksonConverter ) { this.returnType = returnType; - this.objectMapper = objectMapper; + this.jacksonConverter = jacksonConverter; } @Override @@ -60,12 +59,12 @@ public T convert( ResponseBody responseBody ) } else { - return objectMapper.readValue( responseBody.charStream(), returnType ); + return jacksonConverter.convert( responseBody.charStream(), returnType ); } } catch ( IOException e ) { - throw new RuntimeException( e ); + throw new Dhis2ClientException( e ); } finally { @@ -76,7 +75,6 @@ public T convert( ResponseBody responseBody ) @Override public T convert( List> responseBody ) { - return objectMapper.convertValue( responseBody, - objectMapper.getTypeFactory().constructCollectionLikeType( List.class, returnType ) ); + return (T) jacksonConverter.convert( responseBody, List.class, returnType); } } diff --git a/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/page/PageIterable.java b/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/page/PageIterable.java index c619b51..dd85778 100644 --- a/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/page/PageIterable.java +++ b/sdk/src/main/java/org/hisp/dhis/integration/sdk/internal/operation/page/PageIterable.java @@ -36,6 +36,7 @@ import okhttp3.Request; import org.hisp.dhis.api.model.Page; +import org.hisp.dhis.integration.sdk.api.Dhis2ClientException; import org.hisp.dhis.integration.sdk.api.Dhis2Response; import org.hisp.dhis.integration.sdk.api.converter.ConverterFactory; import org.hisp.dhis.integration.sdk.internal.DefaultDhis2Response; @@ -113,7 +114,7 @@ private Iterator fetchPage() } catch ( IOException e ) { - throw new RuntimeException( e ); + throw new Dhis2ClientException( e ); } currentPage = dhis2Response.returnAs( Page.class );