Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LobidClient: flexible server URL #223

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### Added

- Property to application.yml: `application.lobidUrl`

### Fixed

- Use generics in Lobid clients properly

### Changed

- **Breaking**: Removed default constructor of `LobidClient`; URL must be provided as parameter

## [9.5.2](https://github.com/dbmdz/metadata-service/releases/tag/9.5.2) - 2025-02-10

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import de.digitalcollections.lobid.model.LobidEntity;
import de.digitalcollections.model.exception.TechnicalException;
import de.digitalcollections.model.exception.http.HttpErrorDecoder;
import java.io.IOException;
Expand All @@ -13,7 +14,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LobidBaseClient<T extends Object> {
public class LobidBaseClient<T extends LobidEntity> {

private static final Logger LOGGER = LoggerFactory.getLogger(LobidBaseClient.class);

Expand Down Expand Up @@ -51,10 +52,10 @@ private HttpRequest createGetRequest(String requestUrl) {
}

protected T doGetRequestForObject(String requestUrl) throws TechnicalException {
return (T) doGetRequestForObject(requestUrl, targetType);
return doGetRequestForObject(requestUrl, targetType);
}

protected Object doGetRequestForObject(String requestUrl, Class<?> targetType)
protected <U> U doGetRequestForObject(String requestUrl, Class<U> targetType)
throws TechnicalException {
HttpRequest req = createGetRequest(requestUrl);
try {
Expand All @@ -68,15 +69,15 @@ protected Object doGetRequestForObject(String requestUrl, Class<?> targetType)
if (body == null || body.length == 0) {
return null;
}
T result = mapper.readerFor(targetType).readValue(body);
U result = mapper.readerFor(targetType).readValue(body);
return result;
} catch (IOException | InterruptedException e) {
LOGGER.warn("Failed to retrieve response due to connection error", e);
throw HttpErrorDecoder.decode("GET " + requestUrl, 500, null);
}
}

protected List doGetRequestForObjectList(String requestUrl, Class<?> targetType)
protected <U> List<U> doGetRequestForObjectList(String requestUrl, Class<U> targetType)
throws TechnicalException {
HttpRequest req = createGetRequest(requestUrl);
// TODO add creation of a request id if needed
Expand All @@ -92,7 +93,7 @@ protected List doGetRequestForObjectList(String requestUrl, Class<?> targetType)
if (body == null || body.length == 0) {
return null;
}
List result = mapper.readerForListOf(targetType).readValue(body);
List<U> result = mapper.readerForListOf(targetType).readValue(body);
return result;
} catch (IOException | InterruptedException e) {
LOGGER.warn("Failed to retrieve response due to connection error", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import de.digitalcollections.lobid.jackson.LobidObjectMapper;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.time.Duration;

public class LobidClient {
Expand All @@ -16,10 +17,6 @@ public class LobidClient {
private final LobidSubjectsClient lobidSubjectsClient;
private final LobidWorksClient lobidWorksClient;

public LobidClient() {
this("https://lobid.org", new LobidObjectMapper());
}

public LobidClient(HttpClient http, String serverUrl, ObjectMapper mapper) {
this.http = http;
this.lobidCorporateBodiesClient = new LobidCorporateBodiesClient(http, serverUrl, mapper);
Expand All @@ -36,11 +33,16 @@ public LobidClient(String serverUrl, ObjectMapper mapper) {
HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.ALWAYS)
.connectTimeout(Duration.ofSeconds(10))
.version(Version.HTTP_1_1)
.build(),
serverUrl,
mapper);
}

public LobidClient(String serverUrl) {
this(serverUrl, new LobidObjectMapper());
}

public LobidCorporateBodiesClient forCorporateBodies() {
return lobidCorporateBodiesClient;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public LobidClientIT() {}

@Test
public void getCorporateBodyByGndId() throws TechnicalException {
CorporateBody corporateBody = new LobidClient().forCorporateBodies().getByGndId("2007744-0");
CorporateBody corporateBody =
new LobidClient("https://lobid.org").forCorporateBodies().getByGndId("2007744-0");
assertEquals("Deutsche Forschungsgemeinschaft (DFG)", corporateBody.getLabel().getText());
assertEquals("https://www.dfg.de", corporateBody.getHomepageUrl().toString());
assertEquals("2007744-0", corporateBody.getIdentifierByNamespace("gnd").getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

@SuppressFBWarnings(value = {"EI_EXPOSE_REP", "EI_EXPOSE_REP2"})
@Getter
public class CudamiConfig {

private Defaults defaults;
private int offsetForAlternativePaging = 0;
private String repositoryFolderPath;
private TypeDeclarations typeDeclarations;
private UrlAlias urlAlias;
private String lobidUrl;
morpheus-87 marked this conversation as resolved.
Show resolved Hide resolved

@SuppressFBWarnings(
value = "EI_EXPOSE_REP2",
justification = "typeDeclarations are partially filled at runtime")
@JsonCreator(mode = Mode.PROPERTIES)
public CudamiConfig(
@JsonProperty(value = "defaults") Defaults defaults,
@JsonProperty(value = "offsetForAlternativePaging") int offsetForAlternativePaging,
@JsonProperty(value = "repositoryFolderPath") String repositoryFolderPath,
@JsonProperty(value = "typeDeclarations") TypeDeclarations typeDeclarations,
@JsonProperty(value = "urlAlias") UrlAlias urlAlias) {
@JsonProperty(value = "urlAlias") UrlAlias urlAlias,
@JsonProperty(value = "lobidUrl") String lobidUrl) {
if (defaults == null) {
throw new IllegalStateException("Required `cudami.defaults` configuration missing.");
}
Expand All @@ -40,29 +43,10 @@ public CudamiConfig(
repositoryFolderPath.replace("~/", System.getProperty("user.home") + "/");
this.typeDeclarations = typeDeclarations;
this.urlAlias = urlAlias;
this.lobidUrl = lobidUrl;
}

public Defaults getDefaults() {
return defaults;
}

public int getOffsetForAlternativePaging() {
return offsetForAlternativePaging;
}

public String getRepositoryFolderPath() {
return repositoryFolderPath;
}

@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "partially filled at runtime")
public TypeDeclarations getTypeDeclarations() {
return typeDeclarations;
}

public UrlAlias getUrlAlias() {
return new UrlAlias(urlAlias);
}

@Getter
public static class Defaults {

private String language;
Expand All @@ -82,16 +66,10 @@ public Defaults(
}
this.locale = locale;
}

public String getLanguage() {
return language;
}

public Locale getLocale() {
return locale;
}
}

@Getter
@Setter
public static class UrlAlias {
morpheus-87 marked this conversation as resolved.
Show resolved Hide resolved

private static final int DB_MAX_LENGTH = 256;
Expand All @@ -109,7 +87,7 @@ public UrlAlias(
@JsonProperty(value = "generationExcludes") List<String> generationExcludes,
@JsonProperty(value = "maxLength") int maxLength) {
this.generationExcludes =
generationExcludes != null ? List.copyOf(generationExcludes) : Collections.EMPTY_LIST;
generationExcludes != null ? List.copyOf(generationExcludes) : Collections.emptyList();
if (maxLength > DB_MAX_LENGTH) {
throw new RuntimeException(
"The maxLength you configured is invalid, because it is greater than "
Expand All @@ -123,21 +101,5 @@ public UrlAlias(UrlAlias other) {
this.generationExcludes = other.generationExcludes;
this.maxLength = other.maxLength;
}

public List<String> getGenerationExcludes() {
return List.copyOf(generationExcludes);
}

public void setGenerationExcludes(List<String> generationExcludes) {
this.generationExcludes = generationExcludes != null ? List.copyOf(generationExcludes) : null;
}

public int getMaxLength() {
return maxLength;
}

public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class SpringConfigBackendTestDatabase {
5000,
"/tmp/cudami/fileResources",
null,
new UrlAlias(new ArrayList<>(), 64));
new UrlAlias(new ArrayList<>(), 64),
"");

private static PostgreSQLContainer postgreSQLContainer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.dbmdz.metadata.server.config;

import de.digitalcollections.cudami.lobid.client.LobidClient;
import de.digitalcollections.cudami.model.config.CudamiConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
Expand All @@ -14,7 +15,7 @@ public class SpringConfigBackendLobid {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringConfigBackendLobid.class);

@Bean
public LobidClient lobidClient() {
return new LobidClient();
public LobidClient lobidClient(CudamiConfig config) {
return new LobidClient(config.getLobidUrl());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ public ApplicationConfig(
int offsetForAlternativePaging,
String repositoryFolderPath,
TypeDeclarations typeDeclarations,
UrlAlias urlAlias) {
super(defaults, offsetForAlternativePaging, repositoryFolderPath, typeDeclarations, urlAlias);
UrlAlias urlAlias,
String lobidUrl) {
super(
defaults,
offsetForAlternativePaging,
repositoryFolderPath,
typeDeclarations,
urlAlias,
lobidUrl);
}
}
1 change: 1 addition & 0 deletions metasvc-server/webapp/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ application:
urlalias:
generationExcludes: []
maxlength: 64
lobidUrl: https://lobid.org

iiif:
identifier:
Expand Down
Loading