From 1b23182408205903bc19541b6e3de25994464b3b Mon Sep 17 00:00:00 2001 From: Silvio Hermann Date: Wed, 12 Jun 2024 12:17:21 +0200 Subject: [PATCH] #550 Added HISInOneClient --- common/pom.xml | 20 ++++ .../thunibib/his/api/HISInOneClient.java | 100 ++++++++++++++++++ .../thunibib/his/api/v1/LanguageValue.java | 91 ++++++++++++++++ .../uni_jena/thunibib/his/api/v1/Token.java | 55 ++++++++++ .../PublicationHisResTransformer.java | 6 +- .../thunibib/his/xml/HISinOneResolver.java | 28 ++++- pom.xml | 28 +++++ 7 files changed, 319 insertions(+), 9 deletions(-) create mode 100644 common/src/main/java/de/uni_jena/thunibib/his/api/HISInOneClient.java create mode 100644 common/src/main/java/de/uni_jena/thunibib/his/api/v1/LanguageValue.java create mode 100644 common/src/main/java/de/uni_jena/thunibib/his/api/v1/Token.java diff --git a/common/pom.xml b/common/pom.xml index e6d394382..df979fe97 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -12,6 +12,18 @@ ThUniBib Common Files + + com.fasterxml.jackson.core + jackson-annotations + + + com.fasterxml.jackson.core + jackson-core + + + com.fasterxml.jackson.core + jackson-databind + jakarta.servlet jakarta.servlet-api @@ -20,6 +32,14 @@ org.apache.solr solr-solrj + + org.glassfish.jersey.core + jersey-client + + + org.glassfish.jersey.media + jersey-media-sse + org.jdom jdom2 diff --git a/common/src/main/java/de/uni_jena/thunibib/his/api/HISInOneClient.java b/common/src/main/java/de/uni_jena/thunibib/his/api/HISInOneClient.java new file mode 100644 index 000000000..719f7de8d --- /dev/null +++ b/common/src/main/java/de/uni_jena/thunibib/his/api/HISInOneClient.java @@ -0,0 +1,100 @@ +package de.uni_jena.thunibib.his.api; + +import de.uni_jena.thunibib.his.api.v1.Token; +import de.uni_jena.thunibib.his.xml.HISinOneResolver; +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.Entity; +import jakarta.ws.rs.client.Invocation; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.glassfish.jersey.client.ClientConfig; +import org.mycore.common.config.MCRConfiguration2; +import org.mycore.common.events.MCRShutdownHandler; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +public class HISInOneClient implements MCRShutdownHandler.Closeable { + + public static final String HIS_IN_ONE_BASE_URL = MCRConfiguration2.getStringOrThrow("ThUniBib.HISinOne.BaseURL"); + public static final String API_PATH = "api/v1/"; + private static Logger LOGGER = LogManager.getLogger(HISInOneClient.class); + + private enum AuthType { + Basic, Bearer + } + + private String clientKey; + private String clientSecret; + private Client jerseyClient; + + private static HISInOneClient instance; + + private HISInOneClient() { + clientKey = MCRConfiguration2.getStringOrThrow("ThUniBib.HISinOne.ClientKey"); + clientSecret = MCRConfiguration2.getStringOrThrow("ThUniBib.HISinOne.ClientSecret"); + jerseyClient = ClientBuilder.newClient(new ClientConfig().register(HISinOneResolver.class)); + MCRShutdownHandler.getInstance().addCloseable(this); + } + + public static HISInOneClient getInstance() { + if (instance == null) { + instance = new HISInOneClient(); + } + return instance; + } + + public static Response get(String path) { + WebTarget webTarget = HISInOneClient + .getInstance() + .getJerseyClient() + .target(HISInOneClient.HIS_IN_ONE_BASE_URL + API_PATH) + .path(path); + + Token token = getInstance().fetchToken(); + Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); + invocationBuilder.header("Authorization", + getInstance().getAuthorizationHeaderValue(AuthType.Bearer, token.getAccessToken())); + + Response response = invocationBuilder.get(); + return response; + } + + protected Token fetchToken() { + WebTarget webTarget = HISInOneClient + .getInstance() + .getJerseyClient() + .target(HISInOneClient.HIS_IN_ONE_BASE_URL + API_PATH) + .path("cs/psv/oauth/token"); + + Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); + invocationBuilder + .header("Authorization", getAuthorizationHeaderValue(AuthType.Basic, clientKey + ":" + clientSecret)); + + Entity body = Entity.entity("grant_type=client_credentials", MediaType.APPLICATION_FORM_URLENCODED); + try (Response response = invocationBuilder.post(body)) { + return response.readEntity(Token.class); + } + } + + private String getAuthorizationHeaderValue(AuthType authType, String in) { + return switch (authType) { + case Bearer -> authType.name() + " " + in; + case Basic -> + authType.name() + " " + Base64.getEncoder().encodeToString(in.getBytes(StandardCharsets.UTF_8)); + }; + } + + private Client getJerseyClient() { + return getInstance().jerseyClient; + } + + @Override + public void close() { + getJerseyClient().close(); + } +} diff --git a/common/src/main/java/de/uni_jena/thunibib/his/api/v1/LanguageValue.java b/common/src/main/java/de/uni_jena/thunibib/his/api/v1/LanguageValue.java new file mode 100644 index 000000000..50af7b02e --- /dev/null +++ b/common/src/main/java/de/uni_jena/thunibib/his/api/v1/LanguageValue.java @@ -0,0 +1,91 @@ +package de.uni_jena.thunibib.his.api.v1; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class LanguageValue { + @JsonProperty("id") + int id; + @JsonProperty("lockVersion") + String lockVersion; + @JsonProperty("shorttext") + String shortText; + @JsonProperty("text") + String text; + @JsonProperty("sortorder") + int sortOrder; + @JsonProperty("uniquename") + String uniqueName; + @JsonProperty("iso_639_2") + String iso6392; + @JsonProperty("iso_639_1") + String iso6391; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLockVersion() { + return lockVersion; + } + + public void setLockVersion(String lockVersion) { + this.lockVersion = lockVersion; + } + + public String getShortText() { + return shortText; + } + + public void setShortText(String shortText) { + this.shortText = shortText; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public int getSortOrder() { + return sortOrder; + } + + public void setSortOrder(int sortOrder) { + this.sortOrder = sortOrder; + } + + public String getUniqueName() { + return uniqueName; + } + + public void setUniqueName(String uniqueName) { + this.uniqueName = uniqueName; + } + + public String getIso6392() { + return iso6392; + } + + public void setIso6392(String iso6392) { + this.iso6392 = iso6392; + } + + public String getIso6391() { + return iso6391; + } + + public void setIso6391(String iso6391) { + this.iso6391 = iso6391; + } + + @Override + public String toString() { + return id + ":" + uniqueName; + } +} diff --git a/common/src/main/java/de/uni_jena/thunibib/his/api/v1/Token.java b/common/src/main/java/de/uni_jena/thunibib/his/api/v1/Token.java new file mode 100644 index 000000000..66cf33d29 --- /dev/null +++ b/common/src/main/java/de/uni_jena/thunibib/his/api/v1/Token.java @@ -0,0 +1,55 @@ +package de.uni_jena.thunibib.his.api.v1; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Token { + + @JsonProperty("access_token") + String accessToken; + @JsonProperty("scope") + String scope; + @JsonProperty("token_type") + String tokenType; + @JsonProperty("expires_in") + String expiresIn; + + public Token() { + } + + public String getAccessToken() { + return accessToken; + } + + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + } + + public String getScope() { + return scope; + } + + public void setScope(String scope) { + this.scope = scope; + } + + public String getExpiresIn() { + return expiresIn; + } + + public void setExpiresIn(String expiresIn) { + this.expiresIn = expiresIn; + } + + public String getTokenType() { + return tokenType; + } + + public void setTokenType(String tokenType) { + this.tokenType = tokenType; + } + + @Override + public String toString() { + return getAccessToken(); + } +} diff --git a/common/src/main/java/de/uni_jena/thunibib/his/content/transformer/PublicationHisResTransformer.java b/common/src/main/java/de/uni_jena/thunibib/his/content/transformer/PublicationHisResTransformer.java index b6055540f..6408d3ff0 100644 --- a/common/src/main/java/de/uni_jena/thunibib/his/content/transformer/PublicationHisResTransformer.java +++ b/common/src/main/java/de/uni_jena/thunibib/his/content/transformer/PublicationHisResTransformer.java @@ -20,12 +20,12 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import de.uni_jena.thunibib.his.api.HISInOneClient; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jdom2.Document; import org.jdom2.JDOMException; import org.jdom2.filter.Filters; -import org.mycore.common.config.MCRConfiguration2; import org.mycore.common.content.MCRContent; import org.mycore.common.content.transformer.MCRToJSONTransformer; import org.xml.sax.SAXException; @@ -38,8 +38,6 @@ public class PublicationHisResTransformer extends MCRToJSONTransformer { private static final Logger LOGGER = LogManager.getLogger(PublicationHisResTransformer.class); - static final String HIS_IN_ONE_BASE_URL = MCRConfiguration2.getStringOrThrow("ThUniBib.HISinOne.BaseURL"); - protected JsonObject toJSON(MCRContent source) throws IOException { try { Document xml = source.asXML(); @@ -67,7 +65,7 @@ private void addLanguages(JsonObject jsonObject, Document xml) { final JsonArray languages = new JsonArray(); XPATH_FACTORY.compile( - "//mods:language/mods:languageTerm[@authorityURI='" + HIS_IN_ONE_BASE_URL + "']/text()", + "//mods:language/mods:languageTerm[@authorityURI='" + HISInOneClient.HIS_IN_ONE_BASE_URL + "']/text()", Filters.text(), null, MODS_NAMESPACE).evaluate(xml) .forEach(text -> { try { diff --git a/common/src/main/java/de/uni_jena/thunibib/his/xml/HISinOneResolver.java b/common/src/main/java/de/uni_jena/thunibib/his/xml/HISinOneResolver.java index a26ffb0f4..992763796 100644 --- a/common/src/main/java/de/uni_jena/thunibib/his/xml/HISinOneResolver.java +++ b/common/src/main/java/de/uni_jena/thunibib/his/xml/HISinOneResolver.java @@ -1,21 +1,26 @@ package de.uni_jena.thunibib.his.xml; +import de.uni_jena.thunibib.his.api.HISInOneClient; +import de.uni_jena.thunibib.his.api.v1.LanguageValue; +import jakarta.ws.rs.core.GenericType; +import jakarta.ws.rs.core.Response; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jdom2.Element; import org.jdom2.transform.JDOMSource; -import org.mycore.common.xml.MCRFunctionResolver; import javax.xml.transform.Source; import javax.xml.transform.TransformerException; import javax.xml.transform.URIResolver; +import java.util.ArrayList; +import java.util.List; public class HISinOneResolver implements URIResolver { - public enum URI_PART { + public enum SUPPORTED_URI_PARTS { language } - private static Logger LOGGER = LogManager.getLogger(MCRFunctionResolver.class); + private static Logger LOGGER = LogManager.getLogger(HISinOneResolver.class); @Override public Source resolve(String href, String base) throws TransformerException { @@ -25,7 +30,7 @@ public Source resolve(String href, String base) throws TransformerException { String entity = parts[1]; String value = parts[2]; - switch (URI_PART.valueOf(entity)) { + switch (SUPPORTED_URI_PARTS.valueOf(entity)) { case language: return new JDOMSource(new Element("int").setText(String.valueOf(resolveLanguage(value)))); } @@ -34,6 +39,19 @@ public Source resolve(String href, String base) throws TransformerException { } private int resolveLanguage(String rfc5646) { - return 42; + HISInOneClient hisClient = HISInOneClient.getInstance(); + + try (Response response = hisClient.get("cs/sys/values/languageValue")) { + List languageValues = response.readEntity(new GenericType>() { + }); + + int id = languageValues + .stream() + .filter(lv -> lv.getIso6391().equals(rfc5646)) + .findFirst() + .get() + .getId(); + return id; + } } } diff --git a/pom.xml b/pom.xml index 5fd4a31cb..19f42deef 100644 --- a/pom.xml +++ b/pom.xml @@ -17,8 +17,10 @@ 2019 + 2.17.1 6.0.0 2.0.6.1 + 3.1.3 2022.06.3-SNAPSHOT 2.1 8.11.3 @@ -95,6 +97,21 @@ pom import + + com.fasterxml.jackson.core + jackson-annotations + ${jackson.version} + + + com.fasterxml.jackson.core + jackson-core + ${jackson.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + de.uni-jena.thulb.ubo thunibib-common @@ -110,6 +127,16 @@ solr-solrj ${solr.version} + + org.glassfish.jersey.core + jersey-client + ${jersey.client.version} + + + org.glassfish.jersey.media + jersey-media-sse + ${jersey.client.version} + org.jdom jdom2 @@ -151,6 +178,7 @@ ${ubo.version} + common