Skip to content

Commit

Permalink
write mets file once after all urns where added
Browse files Browse the repository at this point in the history
  • Loading branch information
rsehr committed Dec 3, 2024
1 parent 0534b36 commit 91e494e
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 244 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class UrnDatabaseException extends Exception {

private static final long serialVersionUID = 3254785323237361463L;

public UrnDatabaseException(String string) {
super(string);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
Expand All @@ -16,10 +17,10 @@
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import de.intranda.goobi.plugins.Messages.UrlListMessage;
import de.intranda.goobi.plugins.Messages.UrnCreationMessage;
import de.intranda.goobi.plugins.ResponseHandler.CreateResponseHandler;
import de.intranda.goobi.plugins.ResponseHandler.PatchResponseHandler;
import de.intranda.goobi.plugins.messages.UrlListMessage;
import de.intranda.goobi.plugins.messages.UrnCreationMessage;
import de.intranda.goobi.plugins.responsehandler.CreateResponseHandler;
import de.intranda.goobi.plugins.responsehandler.PatchResponseHandler;
import de.sub.goobi.config.ConfigurationHelper;
import lombok.extern.log4j.Log4j2;

Expand All @@ -28,37 +29,35 @@ public class UrnRestClient {

private String uri;
private String auth;
private String namespaceName;
private Gson gson;
private boolean useProxy = false;
private HttpHost proxy;

/**
* @param Uri URL of the URN service
* @param namespace namespace in which URNs are created
* @param User Username of the API User
* @param Password Password of the User
* @param user Username of the API User
* @param password Password of the User
*/
public UrnRestClient(String Uri, String Namespace, String User, String Password) {
this.auth = Base64.getEncoder().encodeToString((User.trim() + ":" + Password.trim()).getBytes());
if (!Uri.startsWith("https")) {
public UrnRestClient(String uri, String namespace, String user, String password) {
this.auth = Base64.getEncoder().encodeToString((user.trim() + ":" + password.trim()).getBytes());
if (!uri.startsWith("https")) {
throw new IllegalArgumentException("Bad URL - only https is permitted");
}
uri = (!Uri.endsWith("/")) ? Uri + "/" : Uri;
this.namespaceName = Namespace.trim();
this.uri = (!uri.endsWith("/")) ? uri + "/" : uri;
gson = new Gson();
ConfigurationHelper cHelper = ConfigurationHelper.getInstance();
if (cHelper.isUseProxy()) {
try {
URL url = new URL(uri);
URL url = new URL(this.uri);
if (!cHelper.isProxyWhitelisted(url)) {
this.useProxy = cHelper.isUseProxy();
this.proxy = new HttpHost(cHelper.getProxyUrl(), cHelper.getProxyPort());
} else {
log.debug("URN PLUGIN: url was on proxy whitelist, no proxy used: " + uri);
log.debug("URN PLUGIN: url was on proxy whitelist, no proxy used: " + this.uri);
}
} catch (MalformedURLException e) {
log.debug("URN PLUGIN: could not convert into URL: {} ", uri);
log.debug("URN PLUGIN: could not convert into URL: {} ", this.uri);
}
}
}
Expand All @@ -72,17 +71,16 @@ public UrnRestClient(String Uri, String Namespace, String User, String Password)
* @throws IOException
* @throws InterruptedException
*/
public String registerUrn(String urn, ArrayList<String> urls) throws ClientProtocolException, IOException, JsonSyntaxException {
public String registerUrn(String urn, List<String> urls) throws IOException, JsonSyntaxException {

Request request = Request.Post(uri + "urns");
request = addHeadersAndProxy(request);

String response = request.addHeader("Content-Type", "application/json")
return request.addHeader("Content-Type", "application/json")
.bodyString(createUrnBodyString(urn, urls), ContentType.APPLICATION_JSON)
.execute()
.handleResponse(new CreateResponseHandler());

return response;
}

/**
Expand All @@ -95,8 +93,8 @@ public String registerUrn(String urn, ArrayList<String> urls) throws ClientProto
* @throws IOException
* @throws IllegalArgumentException
*/
public boolean replaceUrls(String urn, ArrayList<String> urls)
throws ClientProtocolException, IOException, IllegalArgumentException, JsonSyntaxException {
public boolean replaceUrls(String urn, List<String> urls)
throws IOException, IllegalArgumentException, JsonSyntaxException {

Request request = Request.Patch(uri + "urns/urn/" + urn + "/" + "my-urls");
request = addHeadersAndProxy(request);
Expand All @@ -113,8 +111,8 @@ public boolean replaceUrls(String urn, ArrayList<String> urls)
* @param urls urls that shall be added to the JSONArray
* @return String with JSONarray
*/
private String replaceUrlsBodyString(String urn, ArrayList<String> urls) {
ArrayList<UrlListMessage> ulm = createUrlList(urn, urls);
private String replaceUrlsBodyString(String urn, List<String> urls) {
List<UrlListMessage> ulm = createUrlList(urn, urls);
return gson.toJson(ulm);
}

Expand All @@ -124,8 +122,8 @@ private String replaceUrlsBodyString(String urn, ArrayList<String> urls) {
* @param urls urls to put in the array
* @return arrayBuilder object
*/
private ArrayList<UrlListMessage> createUrlList(String urn, ArrayList<String> urls) {
ArrayList<UrlListMessage> ulm = new ArrayList<>();
private List<UrlListMessage> createUrlList(String urn, List<String> urls) {
List<UrlListMessage> ulm = new ArrayList<>();
for (String url : urls) {
ulm.add(new UrlListMessage().setUrl(url.replace("{pi.urn}", urn)));
}
Expand All @@ -135,16 +133,14 @@ private ArrayList<UrlListMessage> createUrlList(String urn, ArrayList<String> ur
/**
* Helper method of createUrn. Creates JSON-String needed to create an URN
*
* @param Urn
* @param urn
* @param urls
* @return
* @throws IOException
*/
private String createUrnBodyString(String Urn, ArrayList<String> urls) throws IOException {
private String createUrnBodyString(String urn, List<String> urls) {
UrnCreationMessage createArk = new UrnCreationMessage();
ArrayList<UrlListMessage> ulm = new ArrayList<>();
createArk.setUrn(Urn);
createArk.setUrls(createUrlList(Urn, urls));
createArk.setUrn(urn);
createArk.setUrls(createUrlList(urn, urls));
return gson.toJson(createArk);
}

Expand Down
Loading

0 comments on commit 91e494e

Please sign in to comment.