-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
319 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
common/src/main/java/de/uni_jena/thunibib/his/api/HISInOneClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
common/src/main/java/de/uni_jena/thunibib/his/api/v1/LanguageValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
common/src/main/java/de/uni_jena/thunibib/his/api/v1/Token.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.