diff --git a/src/main/java/org/hisp/dhis/BaseDhis2.java b/src/main/java/org/hisp/dhis/BaseDhis2.java index db4aa495..1287cd8d 100644 --- a/src/main/java/org/hisp/dhis/BaseDhis2.java +++ b/src/main/java/org/hisp/dhis/BaseDhis2.java @@ -167,6 +167,11 @@ public class BaseDhis2 { protected final CloseableHttpClient httpClient; + /** + * Constructor. + * + * @param config the {@link Dhis2Config}. + */ public BaseDhis2(Dhis2Config config) { Objects.requireNonNull(config, "Config must be specified"); this.config = config; diff --git a/src/main/java/org/hisp/dhis/Dhis2.java b/src/main/java/org/hisp/dhis/Dhis2.java index 3ff8aa99..f025dc0f 100644 --- a/src/main/java/org/hisp/dhis/Dhis2.java +++ b/src/main/java/org/hisp/dhis/Dhis2.java @@ -73,6 +73,7 @@ import org.hisp.dhis.auth.AccessTokenAuthentication; import org.hisp.dhis.auth.BasicAuthentication; import org.hisp.dhis.auth.CookieAuthentication; +import org.hisp.dhis.auth.NoAuthentication; import org.hisp.dhis.model.AnalyticsTableHook; import org.hisp.dhis.model.Attribute; import org.hisp.dhis.model.Category; @@ -240,6 +241,17 @@ public static Dhis2 withCookieAuth(String url, String sessionId) { return new Dhis2(new Dhis2Config(url, new CookieAuthentication(sessionId))); } + /** + * Creates a {@link Dhis2} instance with no authentication. + * + * @param url the URL to the DHIS2 instance, do not include the {@code /api} part or a trailing + * {@code /}. + * @return a {@link Dhis2} instance. + */ + public static Dhis2 withoutAuth(String url) { + return new Dhis2(new Dhis2Config(url, new NoAuthentication())); + } + // ------------------------------------------------------------------------- // Log level // ------------------------------------------------------------------------- diff --git a/src/main/java/org/hisp/dhis/auth/Authentication.java b/src/main/java/org/hisp/dhis/auth/Authentication.java index 8e57394c..764fb250 100644 --- a/src/main/java/org/hisp/dhis/auth/Authentication.java +++ b/src/main/java/org/hisp/dhis/auth/Authentication.java @@ -28,6 +28,7 @@ package org.hisp.dhis.auth; import java.io.Serializable; +import org.apache.commons.lang3.StringUtils; /** Authentication interface. */ public interface Authentication extends Serializable { @@ -44,4 +45,13 @@ public interface Authentication extends Serializable { * @return the value of the HTTP header to use for authentication. */ String getHttpHeaderAuthValue(); + + /** + * Indicates whether authentication details are present. + * + * @return true if authentication details are present, false otherwise. + */ + default boolean hasAuth() { + return StringUtils.isNotEmpty(getHttpHeaderAuthName()); + } } diff --git a/src/main/java/org/hisp/dhis/auth/NoAuthentication.java b/src/main/java/org/hisp/dhis/auth/NoAuthentication.java new file mode 100644 index 00000000..9d494b4e --- /dev/null +++ b/src/main/java/org/hisp/dhis/auth/NoAuthentication.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2004-2025, 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.auth; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.apache.commons.lang3.StringUtils; + +/** Class representing no authentication. */ +@Getter +@RequiredArgsConstructor +public class NoAuthentication implements Authentication { + @Override + public String getHttpHeaderAuthName() { + return StringUtils.EMPTY; + } + + @Override + public String getHttpHeaderAuthValue() { + return StringUtils.EMPTY; + } +} diff --git a/src/main/java/org/hisp/dhis/util/DateTimeUtils.java b/src/main/java/org/hisp/dhis/util/DateTimeUtils.java index dc2c4e15..e3f6b03c 100644 --- a/src/main/java/org/hisp/dhis/util/DateTimeUtils.java +++ b/src/main/java/org/hisp/dhis/util/DateTimeUtils.java @@ -99,6 +99,16 @@ public static Date toDate(LocalDateTime dateTime) { return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant()); } + /** + * Returns a {@link java.util.Date} from a {@link java.time.Instant}. + * + * @param instant the {@link java.time.Instant}. + * @return the {@link java.util.Date}. + */ + public static Date toDate(Instant instant) { + return Date.from(instant); + } + /** * Returns a {@link java.util.Date} based on the given string. The string must be on the format * yyyy-MM-dd'T'HH:mm:ss.SSS. diff --git a/src/main/java/org/hisp/dhis/util/HttpUtils.java b/src/main/java/org/hisp/dhis/util/HttpUtils.java index be8a4dbc..afc42583 100644 --- a/src/main/java/org/hisp/dhis/util/HttpUtils.java +++ b/src/main/java/org/hisp/dhis/util/HttpUtils.java @@ -65,7 +65,11 @@ public class HttpUtils { */ public static T withAuth(T request, Dhis2Config config) { Authentication auth = config.getAuthentication(); - request.setHeader(auth.getHttpHeaderAuthName(), auth.getHttpHeaderAuthValue()); + + if (auth.hasAuth()) { + request.setHeader(auth.getHttpHeaderAuthName(), auth.getHttpHeaderAuthValue()); + } + return request; } diff --git a/src/test/java/org/hisp/dhis/AuthTest.java b/src/test/java/org/hisp/dhis/AuthTest.java index 3cf4c9c4..5f41df3b 100644 --- a/src/test/java/org/hisp/dhis/AuthTest.java +++ b/src/test/java/org/hisp/dhis/AuthTest.java @@ -48,10 +48,17 @@ void testWithCookieAuth() { assertNotNull(dhis2); } + @Test void testWithAccessTokenAuth() { Dhis2 dhis2 = Dhis2.withAccessTokenAuth( "https://play.dhis2.org/demo", "d2pat_2bBQecgNcxrS4EPhBJuRlQkwiLr2ATnC2557514242"); assertNotNull(dhis2); } + + @Test + void testWithoutAuth() { + Dhis2 dhis2 = Dhis2.withoutAuth("https://play.dhis2.org/demo"); + assertNotNull(dhis2); + } } diff --git a/src/test/java/org/hisp/dhis/util/HttpUtilsTest.java b/src/test/java/org/hisp/dhis/util/HttpUtilsTest.java index 81a36c6c..57099679 100644 --- a/src/test/java/org/hisp/dhis/util/HttpUtilsTest.java +++ b/src/test/java/org/hisp/dhis/util/HttpUtilsTest.java @@ -28,6 +28,7 @@ package org.hisp.dhis.util; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import java.net.URI; import java.util.ArrayList; @@ -39,6 +40,7 @@ import org.hisp.dhis.auth.AccessTokenAuthentication; import org.hisp.dhis.auth.BasicAuthentication; import org.hisp.dhis.auth.CookieAuthentication; +import org.hisp.dhis.auth.NoAuthentication; import org.hisp.dhis.support.TestTags; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -102,6 +104,16 @@ void testWithCookieAuth() throws Exception { assertEquals("JSESSIONID=KJH8KJ24fRD3FK491", post.getHeader(HttpHeaders.COOKIE).getValue()); } + @Test + void testWithoutAuth() throws Exception { + HttpPost post = new HttpPost(DEV_URL); + + HttpUtils.withAuth(post, new Dhis2Config(DEV_URL, new NoAuthentication())); + + assertNull(post.getHeader(HttpHeaders.AUTHORIZATION)); + assertNull(post.getHeader(HttpHeaders.COOKIE)); + } + @Test void testGetBasicAuthString() { assertEquals("Basic YWRtaW46ZGlzdHJpY3Q=", HttpUtils.getBasicAuthString("admin", "district"));