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
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": "^.secrets.baseline$",
"lines": null
},
"generated_at": "2025-10-06T10:13:43Z",
"generated_at": "2025-10-15T15:53:30Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down Expand Up @@ -100,7 +100,7 @@
"hashed_secret": "52671c8abfb5a16b27e80d10952d43f0beff63d3",
"is_secret": false,
"is_verified": false,
"line_number": 139,
"line_number": 152,
"type": "Secret Keyword",
"verified_result": null
}
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.2.5 (UNRELEASED)
- [NEW] Added User-Agent to requests.
- [UPGRADED] Spring Boot compilation version to `3.5.6`.
- [UPGRADED] `com.ibm.cloud:cloudant` to `0.10.9`.

# 0.2.4 (2025-09-16)
- [UPGRADED] Spring Boot compilation version to `3.5.5`.
- [UPGRADED] `com.ibm.cloud:cloudant` to `0.10.8`.
Expand Down
15 changes: 14 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ plugins {
}

group = 'com.ibm.cloud'
version = new File(rootDir, 'VERSION').text.trim()
version = file('VERSION').text.trim()

// If the version says "snapshot" anywhere assume it is not a release
ext.isReleaseVersion = !version.toUpperCase(Locale.ENGLISH).contains("SNAPSHOT")
Expand Down Expand Up @@ -77,6 +77,19 @@ tasks.named("jar") {
archiveClassifier = ''
}

jar {
manifest {
attributes(['Implementation-Title': rootProject.name,
'Implementation-Version': version,
'Implementation-Vendor': 'IBM'],
'com/ibm/cloud/cloudant/spring/boot/'
)
}
into "META-INF", {
from 'LICENSE'
}
}

tasks.named('test', Test) {
useJUnitPlatform()
}
Expand Down
9 changes: 8 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ group = 'com.example'
version = '0.0.1-SNAPSHOT'

repositories {
mavenCentral()
mavenLocal()
mavenCentral()
}

dependencies {
Expand All @@ -18,6 +18,13 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.withType(JavaCompile).configureEach {
// Build with Java 17 compatibility
options.release = 17
// Always UTF-8
options.encoding = 'UTF-8'
}

tasks.named('test') {
useJUnitPlatform()
}
38 changes: 31 additions & 7 deletions src/main/java/com/ibm/cloud/cloudant/internal/CloudantFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,62 @@
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.stream.StreamSupport;

/**
* <p>Factory for obtaining {@link Cloudant} client instance.</p>
* <p>
* Factory for obtaining {@link Cloudant} client instance.
* </p>
*/
public class CloudantFactory {

static final String UA;
private static final String UNKNOWN = "unknown";
static {
String version = Optional.ofNullable(CloudantFactory.class.getClassLoader()
.getDefinedPackage("com.ibm.cloud.cloudant.spring.boot")
.getImplementationVersion()).orElse(UNKNOWN);
String lang = "java";
String langVersion = System.getProperty("java.version", UNKNOWN);
String langVendor = System.getProperty("java.vendor", UNKNOWN);
String osName = System.getProperty("os.name", UNKNOWN);
String osVersion = System.getProperty("os.version", UNKNOWN);
String osArch = System.getProperty("os.arch", UNKNOWN);
UA = String.format(
"%s/%s (%s.version=%s; %s.vendor=%s; os.name=%s; os.version=%s; os.arch=%s; lang=%s;)",
"cloudant-spring", version, lang, langVersion, lang, langVendor, osName, osVersion,
osArch, lang);
}

private final Cloudant cloudant;

public CloudantFactory(Environment env) {
Map<String, String> springProperties = getSpringProperties(env);
Authenticator authenticator = DelegatingAuthenticatorFactory.getAuthenticator(springProperties);
Authenticator authenticator =
DelegatingAuthenticatorFactory.getAuthenticator(springProperties);
cloudant = new Cloudant(Cloudant.DEFAULT_SERVICE_NAME, authenticator);
cloudant.setDefaultHeaders(Collections.singletonMap("User-Agent", UA));
}

public Cloudant cloudant() {
return cloudant;
}

// Get spring properties from all sources
// NB the usual spring precedence order is respected since we use `env.getProperty()` at the end of the pipeline
// NB the usual spring precedence order is respected since we use `env.getProperty()` at the end
// of the pipeline
private Map<String, String> getSpringProperties(Environment env) {
return StreamSupport.stream(((AbstractEnvironment)env).getPropertySources().spliterator(), false)
return StreamSupport
.stream(((AbstractEnvironment) env).getPropertySources().spliterator(), false)
.filter(EnumerablePropertySource.class::isInstance)
.map(ps -> ((EnumerablePropertySource<?>) ps).getPropertyNames())
.flatMap(Arrays::stream)
.distinct()
.flatMap(Arrays::stream).distinct()
.filter(ps -> ps.toLowerCase(Locale.ROOT).startsWith("cloudant."))
.collect(HashMap::new, (h, ps) -> h.put(ps, env.getProperty(ps)), HashMap::putAll);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.ibm.cloud.cloudant.spring.boot;
49 changes: 49 additions & 0 deletions src/test/java/unit/CloudantFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright © 2025 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

package unit;

import com.ibm.cloud.cloudant.internal.CloudantFactory;
import org.junit.jupiter.api.Test;
import org.springframework.mock.env.MockEnvironment;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class CloudantFactoryTest {

@Test
void testUserAgent() {
// Mock the Spring environment
MockEnvironment env = new MockEnvironment();
env.setProperty("cloudant.auth.type", "NOAUTH");

// Create an instance of CloudantFactory
CloudantFactory factory = new CloudantFactory(env);

// Verify that the Cloudant instance is not null
assertNotNull(factory.cloudant());

// Verify that the UA string is correctly formatted
// Note this uses "unknown" for version because in test environment the jar manifest is not
// available
String expectedUserAgent = "cloudant-spring/unknown (java.version="
+ System.getProperty("java.version") + "; java.vendor="
+ System.getProperty("java.vendor") + "; os.name=" + System.getProperty("os.name")
+ "; os.version=" + System.getProperty("os.version") + "; os.arch="
+ System.getProperty("os.arch") + "; lang=java;)";
assertEquals(expectedUserAgent, factory.cloudant().getDefaultHeaders().get("User-Agent"));

}

}