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
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Java checks

on:
push:
branches: [master]
pull_request:

permissions:
contents: read

jobs:
verify:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
java: ['8', '21']
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
with:
persist-credentials: false
- uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad
with:
distribution: temurin
java-version: ${{ matrix.java }}
- run: mvn --batch-mode --no-transfer-progress clean verify
env:
IPGEO_RUN_LIVE_TESTS: 'false'
IPGEO_RUN_LIVE_HARDENING: 'false'
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ Language values supported by `.lang(...)`:
- `Language.KO` for Korean
- `Language.FA` for Persian
- `Language.PT` for Portuguese
- `Language.AR` for Arabic

Dot-separated field paths can be passed to `.fields(...)` and `.excludes(...)` to control which parts of the response are returned.

Expand Down Expand Up @@ -690,7 +691,7 @@ String full = JsonOutput.toPrettyJson(response.data(), JsonOutputMode.FULL);

## Error Handling

All SDK exceptions extend `IpGeolocationException`.
All SDK exceptions extend `IpGeolocationException`. If an error includes your API key, the SDK replaces it with `[REDACTED]`. The error type and HTTP status stay the same. Some technical error details may be shortened to keep the key hidden.

| HTTP Status | Exception |
|-------------|-----------|
Expand Down
141 changes: 133 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
<properties>
<maven.compiler.release>8</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jackson.version>2.21.7</jackson.version>
<jackson.version>2.21.8</jackson.version>
<junit.version>5.11.4</junit.version>
<assertj.version>3.27.7</assertj.version>
<jacoco.version>0.8.12</jacoco.version>
<jacoco.version>0.8.16</jacoco.version>
<jacoco.min.line.coverage>0.90</jacoco.min.line.coverage>
<jacoco.min.branch.coverage>0.90</jacoco.min.branch.coverage>
<jacoco.min.method.coverage>0.90</jacoco.min.method.coverage>
<maven.source.plugin.version>3.3.1</maven.source.plugin.version>
<maven.javadoc.plugin.version>3.8.0</maven.javadoc.plugin.version>
<maven.source.plugin.version>3.4.0</maven.source.plugin.version>
<maven.javadoc.plugin.version>3.12.0</maven.javadoc.plugin.version>
<maven.gpg.plugin.version>3.2.7</maven.gpg.plugin.version>
<central.publishing.plugin.version>0.10.0</central.publishing.plugin.version>
</properties>
Expand Down Expand Up @@ -99,10 +99,36 @@
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.5.0</version>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>4.0.3</version>
</dependency>
</dependencies>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.5.0</version>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.6.1</version>
</dependency>
</dependencies>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
<version>3.5.1</version>
<configuration>
<archive>
<manifest>
Expand All @@ -115,22 +141,48 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<version>3.16.0</version>
<configuration>
<release>${maven.compiler.release}</release>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>3.2.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.2.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
<version>3.5.6</version>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.6.1</version>
</dependency>
</dependencies>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.6.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>prepare-agent</id>
Expand Down Expand Up @@ -184,7 +236,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.5.0</version>
<version>3.7.0</version>
<executions>
<execution>
<id>enforce-java</id>
Expand Down Expand Up @@ -218,6 +270,18 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven.source.plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>io.airlift</groupId>
<artifactId>aircompressor</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -232,6 +296,23 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven.javadoc.plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>io.airlift</groupId>
<artifactId>aircompressor</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.11.0</version>
</dependency>
</dependencies>
<configuration>
<source>${maven.compiler.release}</source>
<doclint>all,-missing</doclint>
Expand All @@ -252,6 +333,28 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>${maven.gpg.plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpg-jdk18on</artifactId>
<version>1.85</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.85</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcutil-jdk18on</artifactId>
<version>1.85</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>sign-artifacts</id>
Expand All @@ -268,6 +371,28 @@
<artifactId>central-publishing-maven-plugin</artifactId>
<version>${central.publishing.plugin.version}</version>
<extensions>true</extensions>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.7.2-jre</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.6.3</version>
</dependency>
</dependencies>
<configuration>
<publishingServerId>central</publishingServerId>
<autoPublish>true</autoPublish>
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/io/ipgeolocation/sdk/IpGeolocationClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.ipgeolocation.sdk.exceptions.TransportException;
import io.ipgeolocation.sdk.exceptions.ValidationException;
import io.ipgeolocation.sdk.internal.HttpExecutor;
import io.ipgeolocation.sdk.internal.ErrorSanitizer;
import io.ipgeolocation.sdk.internal.HttpRequestData;
import io.ipgeolocation.sdk.internal.HttpResponseData;
import io.ipgeolocation.sdk.internal.JavaNetHttpExecutor;
Expand Down Expand Up @@ -46,6 +47,7 @@ public final class IpGeolocationClient implements AutoCloseable {
private final ObjectMapper objectMapper;
private final HttpExecutor executor;
private final ResponseMapper responseMapper;
private final ErrorSanitizer errors;

/**
* Creates a client that uses the provided SDK configuration.
Expand All @@ -63,7 +65,8 @@ public IpGeolocationClient(IpGeolocationClientConfig config) {
this.config = Objects.requireNonNull(config, "config must not be null");
this.executor = Objects.requireNonNull(executor, "executor must not be null");
this.objectMapper = Objects.requireNonNull(objectMapper, "objectMapper must not be null");
this.responseMapper = new ResponseMapper(this.objectMapper);
this.responseMapper = new ResponseMapper(this.objectMapper, config.apiKey());
this.errors = new ErrorSanitizer(config.apiKey());
}
/**
* Executes a single-IP lookup request and returns typed data plus response metadata.
Expand Down Expand Up @@ -249,7 +252,7 @@ private HttpRequestData buildBulkHttpRequest(
try {
payload = objectMapper.writeValueAsString(new BulkGeolocationRequestBody(request.ips()));
} catch (JsonProcessingException e) {
throw new SerializationException("Failed to serialize bulk lookup request body", e);
throw new SerializationException("Failed to serialize bulk lookup request body", errors.cause(e));
}

Map<String, List<String>> headers = new LinkedHashMap<String, List<String>>();
Expand Down Expand Up @@ -318,12 +321,12 @@ private HttpResponseData executeOnce(HttpRequestData request, Duration timeout)
try {
return executor.send(request);
} catch (SocketTimeoutException e) {
throw new RequestTimeoutException("HTTP request timed out", e);
throw new RequestTimeoutException("HTTP request timed out", errors.cause(e));
} catch (IOException e) {
throw new TransportException("HTTP transport error", e);
throw new TransportException("HTTP transport error", errors.cause(e));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new TransportException("Request execution was interrupted", e);
throw new TransportException("Request execution was interrupted", errors.cause(e));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/ipgeolocation/sdk/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public enum Language {
IT("it"),
KO("ko"),
FA("fa"),
PT("pt");
PT("pt"),
AR("ar");

private final String code;

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/io/ipgeolocation/sdk/internal/ErrorSanitizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.ipgeolocation.sdk.internal;

import java.io.IOException;

public final class ErrorSanitizer {
private final String apiKey;
private final String encodedApiKey;

public ErrorSanitizer(String apiKey) {
this.apiKey = apiKey;
this.encodedApiKey = Compat.isBlank(apiKey) ? null : UriBuilder.encode(apiKey, "UTF-8");
}

public String message(String value) {
if (value == null || encodedApiKey == null) {
return value;
}
return value.replace(encodedApiKey, "[REDACTED]").replace(apiKey, "[REDACTED]");
}

public Throwable cause(Throwable error) {
if (encodedApiKey == null) {
return error;
}
IOException sanitized = new IOException(error.getClass().getName());
sanitized.setStackTrace(error.getStackTrace());
return sanitized;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ private String readBody(HttpURLConnection connection, int statusCode) throws IOE
}

private static int toMillis(Duration duration) {
long millis = duration.toMillis();
if (millis > Integer.MAX_VALUE) {
if (duration.isNegative() || duration.isZero()) {
throw new IllegalArgumentException("timeout must be greater than zero");
}
if (duration.compareTo(Duration.ofMillis(Integer.MAX_VALUE)) >= 0) {
return Integer.MAX_VALUE;
}
return (int) millis;
return (int) Math.max(1L, duration.toMillis());
}

interface ConnectionFactory {
Expand Down
Loading
Loading