-
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
9 changed files
with
325 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
17 changes: 17 additions & 0 deletions
17
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,17 @@ | ||
package de.uni_jena.thunibib.his.api; | ||
|
||
import jakarta.ws.rs.core.Response; | ||
import org.mycore.common.config.MCRConfiguration2; | ||
import org.mycore.common.events.MCRShutdownHandler; | ||
|
||
public interface HISInOneClient extends MCRShutdownHandler.Closeable, AutoCloseable { | ||
|
||
String HIS_IN_ONE_BASE_URL = MCRConfiguration2.getStringOrThrow("ThUniBib.HISinOne.BaseURL"); | ||
String API_PATH = "api/v1/"; | ||
|
||
enum AuthType { | ||
Basic, Bearer | ||
} | ||
|
||
Response get(String path); | ||
} |
80 changes: 80 additions & 0 deletions
80
common/src/main/java/de/uni_jena/thunibib/his/api/HISInOneClientDefaultImpl.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,80 @@ | ||
package de.uni_jena.thunibib.his.api; | ||
|
||
import de.uni_jena.thunibib.his.api.v1.cs.psv.oauth.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; | ||
|
||
class HISInOneClientDefaultImpl implements HISInOneClient { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(HISInOneClientDefaultImpl.class); | ||
private static HISInOneClientDefaultImpl instance; | ||
private final String clientKey; | ||
private final String clientSecret; | ||
private final Client jerseyClient; | ||
|
||
HISInOneClientDefaultImpl() { | ||
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 Response get(String path) { | ||
WebTarget webTarget = getJerseyClient() | ||
.target(HISInOneClientDefaultImpl.HIS_IN_ONE_BASE_URL + API_PATH) | ||
.path(path); | ||
|
||
Token token = fetchToken(); | ||
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); | ||
invocationBuilder.header("Authorization", getAuthorizationHeaderValue(AuthType.Bearer, token.getAccessToken())); | ||
|
||
Response response = invocationBuilder.get(); | ||
return response; | ||
} | ||
|
||
protected Token fetchToken() { | ||
WebTarget webTarget = getJerseyClient() | ||
.target(HISInOneClientDefaultImpl.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)); | ||
}; | ||
} | ||
|
||
protected Client getJerseyClient() { | ||
return jerseyClient; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
getJerseyClient().close(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
common/src/main/java/de/uni_jena/thunibib/his/api/HISinOneClientFactory.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,8 @@ | ||
package de.uni_jena.thunibib.his.api; | ||
|
||
public class HISinOneClientFactory { | ||
|
||
public static HISInOneClient create() { | ||
return new HISInOneClientDefaultImpl(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
common/src/main/java/de/uni_jena/thunibib/his/api/v1/cs/psv/oauth/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.cs.psv.oauth; | ||
|
||
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(); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
common/src/main/java/de/uni_jena/thunibib/his/api/v1/cs/sys/values/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.cs.sys.values; | ||
|
||
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; | ||
} | ||
} |
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.