Skip to content

Commit

Permalink
#550 Added HISInOneClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Possommi committed Jun 13, 2024
1 parent fd9740a commit 1b23182
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 9 deletions.
20 changes: 20 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
<name>ThUniBib Common Files</name>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
Expand All @@ -20,6 +32,14 @@
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-sse</artifactId>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
Expand Down
100 changes: 100 additions & 0 deletions common/src/main/java/de/uni_jena/thunibib/his/api/HISInOneClient.java
Original file line number Diff line number Diff line change
@@ -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<String> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
55 changes: 55 additions & 0 deletions common/src/main/java/de/uni_jena/thunibib/his/api/v1/Token.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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))));
}
Expand All @@ -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<LanguageValue> languageValues = response.readEntity(new GenericType<List<LanguageValue>>() {
});

int id = languageValues
.stream()
.filter(lv -> lv.getIso6391().equals(rfc5646))
.findFirst()
.get()
.getId();
return id;
}
}
}
Loading

0 comments on commit 1b23182

Please sign in to comment.