Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ DHIS2 Java SDK is a _lightweight_ library that hides the nuts and bolts of DHIS2
<dependency>
<groupId>org.hisp.dhis.integration.sdk</groupId>
<artifactId>dhis2-java-sdk</artifactId>
<version>2.0.1-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
</dependency>
...
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion android-jackson-resource-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>dhis2-java-sdk-pom</artifactId>
<groupId>org.hisp.dhis.integration.sdk</groupId>
<version>2.0.1-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion jackson-resource-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>dhis2-java-sdk-pom</artifactId>
<groupId>org.hisp.dhis.integration.sdk</groupId>
<version>2.0.1-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>org.hisp.dhis.integration.sdk</groupId>
<artifactId>dhis2-java-sdk-pom</artifactId>
<packaging>pom</packaging>
<version>2.0.1-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>

<name>DHIS2 Java SDK</name>
<url>https://github.com/dhis2/dhis2-java-sdk</url>
Expand Down
2 changes: 1 addition & 1 deletion sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>dhis2-java-sdk-pom</artifactId>
<groupId>org.hisp.dhis.integration.sdk</groupId>
<version>2.0.1-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dhis2-java-sdk</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,10 @@ public String getApiUrl()
{
return apiUrl;
}

@Override
public ConverterFactory getConverterFactory()
{
return converterFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,4 +51,6 @@ public interface Dhis2Client
OkHttpClient getHttpClient();

String getApiUrl();

ConverterFactory getConverterFactory();
}
Original file line number Diff line number Diff line change
@@ -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> T convert( Object from, Class<T> toType );

<T> T convert( Reader source, Class<T> sourceType);

<T> T convert( List<?> from, Class<T> toCollectionType, Class<?> toElementType );
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

public interface ConverterFactory
{
Converter createConverter();

<T> RequestConverter<T> createRequestConverter( Class<T> requestType );

<T> ResponseConverter<T> createResponseConverter( Class<T> responseType );
Expand Down
Original file line number Diff line number Diff line change
@@ -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> T convert( Object from, Class<T> toType )
{
return objectMapper.convertValue( from, toType );
}

@Override
public <T> T convert( Reader source, Class<T> toType )
{
try
{
return objectMapper.readValue( source, toType );
}
catch ( IOException e )
{
throw new Dhis2ClientException( e );
}
}

@Override
public <T> T convert( List<?> from, Class<T> toCollectionType, Class<?> toElementType )
{
return objectMapper.convertValue( from,
objectMapper.getTypeFactory().constructCollectionLikeType( toCollectionType, toElementType ) );
}

public ObjectMapper getObjectMapper()
{
return objectMapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> RequestConverter<T> createRequestConverter( Class<T> requestType )
{
return new JacksonRequestConverter<>( requestType, objectMapper );
return new JacksonRequestConverter<>( requestType, createConverter() );
}

@Override
public <T> ResponseConverter<T> createResponseConverter( Class<T> responseType )
{
return new JacksonResponseConverter<>( responseType, objectMapper );
return new JacksonResponseConverter<>( responseType, createConverter() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,17 @@ public class JacksonRequestConverter<T> implements RequestConverter<T>
{
private final Class<T> requestType;

private final ObjectMapper objectMapper;
private final JacksonConverter jacksonConverter;

public JacksonRequestConverter( Class<T> requestType, ObjectMapper objectMapper )
public JacksonRequestConverter( Class<T> 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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> implements ResponseConverter<T>
{
private final Class<T> returnType;

private final ObjectMapper objectMapper;
private final JacksonConverter jacksonConverter;

public JacksonResponseConverter( Class<T> returnType, ObjectMapper objectMapper )
public JacksonResponseConverter( Class<T> returnType, JacksonConverter jacksonConverter )
{
this.returnType = returnType;
this.objectMapper = objectMapper;
this.jacksonConverter = jacksonConverter;
}

@Override
Expand All @@ -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
{
Expand All @@ -76,7 +75,6 @@ public T convert( ResponseBody responseBody )
@Override
public T convert( List<Map<String, Object>> responseBody )
{
return objectMapper.convertValue( responseBody,
objectMapper.getTypeFactory().constructCollectionLikeType( List.class, returnType ) );
return (T) jacksonConverter.convert( responseBody, List.class, returnType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -113,7 +114,7 @@ private Iterator<T> fetchPage()
}
catch ( IOException e )
{
throw new RuntimeException( e );
throw new Dhis2ClientException( e );
}

currentPage = dhis2Response.returnAs( Page.class );
Expand Down