forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDSpaceApi.java
More file actions
132 lines (111 loc) · 4.69 KB
/
DSpaceApi.java
File metadata and controls
132 lines (111 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
/* Created for LINDAT/CLARIAH-CZ (UFAL) */
package org.dspace.api;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.content.DSpaceObject;
import org.dspace.handle.HandlePlugin;
import org.dspace.handle.PIDService;
import org.dspace.services.ConfigurationService;
import org.dspace.utils.DSpace;
public class DSpaceApi {
private static final org.apache.logging.log4j.Logger log = LogManager.getLogger();
private static ConfigurationService configurationService = new DSpace().getConfigurationService();
private DSpaceApi() {
}
/**
* Create a new handle PID. This is modified implementation for UFAL, using
* the PID service pidconsortium.eu as wrapped in the PIDService class.
*
* Note: this function creates a handle to a provisional existing URL and
* the handle must be updated to point to the final URL once DSpace is able
* to report the URL exists (otherwise the pidservice will refuse to set the
* URL)
*
* @return A new handle PID
* @exception Exception If error occurrs
*/
public static String handle_HandleManager_createId(Logger log, Long id,
String prefix, String suffix) throws IOException {
/* Modified by PP for use pidconsortium.eu at UFAL/CLARIN */
String base_url = configurationService.getProperty("dspace.server.url") + "?dummy=" + id;
/* OK check whether this url has not received pid earlier */
//This should usually return null (404)
String handle = null;
try {
handle = PIDService.findHandle(base_url, prefix);
} catch (Exception e) {
log.error("Error finding handle: " + e);
}
//if not then log and reuse - this is a dummy url, those should not be seen anywhere
if (handle != null) {
log.warn("Url [" + base_url + "] already has PID(s) (" + handle + ").");
return handle;
}
/* /OK/ */
log.debug("Asking for a new PID using a dummy URL " + base_url);
/* request a new PID, initially pointing to dspace base_uri+id */
String pid = null;
try {
if (suffix != null && !suffix.isEmpty() && PIDService.supportsCustomPIDs()) {
pid = PIDService.createCustomPID(base_url, prefix, suffix);
} else {
pid = PIDService.createPID(base_url, prefix);
}
} catch (Exception e) {
throw new IOException(e);
}
log.debug("got PID " + pid);
return pid;
}
/**
* Modify an existing PID to point to the corresponding DSpace handle
*
* @exception SQLException If a database error occurs
*/
public static void handle_HandleManager_registerFinalHandleURL(Logger log,
String pid, DSpaceObject dso) throws IOException {
if (pid == null) {
log.info("Modification failed invalid/null PID.");
return;
}
String url = configurationService.getProperty("dspace.ui.url");
url = generateItemURLWithHandle(url, dso);
/*
* request modification of the PID to point to the correct URL, which
* itself should contain the PID as a substring
*/
log.debug("Asking for changing the PID '" + pid + "' to " + url);
try {
Map<String, String> fields = HandlePlugin.extractMetadata(dso);
PIDService.modifyPID(pid, url, fields);
} catch (Exception e) {
throw new IOException("Failed to map PID " + pid + " to " + url
+ " (" + e.toString() + ")");
}
}
/**
* Generate a URL with the handle for the given DSpaceObject.
* The URL consist of the base URL and the handle of the DSpace object.
* E.g. `http://localhost:4000/handle/<ITEMS_HANDLE>`
* @param url base URL of the DSpace instance
* @param dSpaceObject the DSpace object for which the URL is generated
* @return the generated URL
*/
public static String generateItemURLWithHandle(String url, DSpaceObject dSpaceObject) {
if (dSpaceObject == null) {
log.error("DSpaceObject is null, cannot generate URL");
return url;
}
return url + (url.endsWith("/") ? "" : "/") + "handle/" + dSpaceObject.getHandle();
}
}