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 12, 2024
1 parent fd9740a commit dc27fbd
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 6 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.uni_jena.thunibib.his;

import org.mycore.common.config.MCRConfiguration2;

public class HISInOneClient {
public static final String HIS_IN_ONE_BASE_URL = MCRConfiguration2.getStringOrThrow("ThUniBib.HISinOne.BaseURL");
}
55 changes: 55 additions & 0 deletions common/src/main/java/de/uni_jena/thunibib/his/Token.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.uni_jena.thunibib.his;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Token {

String access_token;
String scope;
String token_type;
String expires_in;

public Token() {
}

@JsonProperty("access_token")
public String getAccessToken() {
return access_token;
}

public void setAccessToken(String access_token) {
this.access_token = access_token;
}

@JsonProperty("scope")
public String getScope() {
return scope;
}

public void setScope(String scope) {
this.scope = scope;
}

@JsonProperty("expires_in")
public String getExpiresIn() {
return expires_in;
}

public void setExpiresIn(String expires_in) {
this.expires_in = expires_in;
}

@JsonProperty("token_type")
public String getTokenType() {
return token_type;
}

public void setTokenType(String token_type) {
this.token_type = token_type;
}

@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.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,33 @@
package de.uni_jena.thunibib.his.xml;

import de.uni_jena.thunibib.his.HISInOneClient;
import de.uni_jena.thunibib.his.Token;
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.jdom2.Element;
import org.jdom2.transform.JDOMSource;
import org.mycore.common.xml.MCRFunctionResolver;
import org.mycore.common.config.MCRConfiguration2;

import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class HISinOneResolver implements URIResolver {
public enum URI_PART {
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 @@ -34,6 +46,25 @@ public Source resolve(String href, String base) throws TransformerException {
}

private int resolveLanguage(String rfc5646) {
try (Client client = ClientBuilder.newClient(new ClientConfig().register(HISinOneResolver.class))) {
String clientKeySecret = MCRConfiguration2.getStringOrThrow("ThUniBib.HISinOne.ClientKey") + ":"
+ MCRConfiguration2.getStringOrThrow("ThUniBib.HISinOne.ClientSecret");

WebTarget webTarget = client
.target(HISInOneClient.HIS_IN_ONE_BASE_URL + "api/v1/")
.path("cs/psv/oauth/token");

Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
invocationBuilder.header("Authorization",
"Basic " + Base64.getEncoder().encodeToString(clientKeySecret.getBytes(StandardCharsets.UTF_8)));
Entity<String> body = Entity.entity("grant_type=client_credentials", MediaType.APPLICATION_FORM_URLENCODED);

try (Response response = invocationBuilder.post(body)) {
Token token = response.readEntity(Token.class);
LOGGER.info(token);
}
}

return 42;
}
}
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
<inceptionYear>2019</inceptionYear>

<properties>
<jackson.version>2.17.1</jackson.version>
<jakarta.servlet-api.version>6.0.0</jakarta.servlet-api.version>
<jdom.version>2.0.6.1</jdom.version>
<jersey.client.version>3.1.3</jersey.client.version>
<mycore.version>2022.06.3-SNAPSHOT</mycore.version>
<oaipmh.version>2.1</oaipmh.version>
<solr.version>8.11.3</solr.version>
Expand Down Expand Up @@ -95,6 +97,21 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>de.uni-jena.thulb.ubo</groupId>
<artifactId>thunibib-common</artifactId>
Expand All @@ -110,6 +127,16 @@
<artifactId>solr-solrj</artifactId>
<version>${solr.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.client.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-sse</artifactId>
<version>${jersey.client.version}</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
Expand Down Expand Up @@ -151,6 +178,7 @@
<version>${ubo.version}</version>
</dependency>
</dependencies>

</dependencyManagement>
<modules>
<module>common</module>
Expand Down

0 comments on commit dc27fbd

Please sign in to comment.