1 | package eu.clarin.cmdi.virtualcollectionregistry.pid; |
---|
2 | |
---|
3 | import de.uni_leipzig.asv.clarin.webservices.pidservices2.Configuration; |
---|
4 | import de.uni_leipzig.asv.clarin.webservices.pidservices2.HandleField; |
---|
5 | import de.uni_leipzig.asv.clarin.webservices.pidservices2.interfaces.PidWriter; |
---|
6 | import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryException; |
---|
7 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection; |
---|
8 | import java.net.URI; |
---|
9 | import java.util.EnumMap; |
---|
10 | import java.util.Map; |
---|
11 | import java.util.UUID; |
---|
12 | import java.util.zip.CRC32; |
---|
13 | import org.apache.commons.codec.binary.Base64; |
---|
14 | import org.apache.commons.httpclient.HttpException; |
---|
15 | import org.slf4j.Logger; |
---|
16 | import org.slf4j.LoggerFactory; |
---|
17 | import org.springframework.beans.factory.annotation.Autowired; |
---|
18 | import org.springframework.beans.factory.annotation.Value; |
---|
19 | import org.springframework.context.annotation.Profile; |
---|
20 | import org.springframework.stereotype.Service; |
---|
21 | |
---|
22 | /** |
---|
23 | * PID provider that uses the EPIC API v2 to communicate with a handle provider. |
---|
24 | * Wraps around the PID Resolver / PID Writer library by Thomas Eckart (Leipzig |
---|
25 | * University) |
---|
26 | * |
---|
27 | * @author twagoo |
---|
28 | * @see âhttp://www.pidconsortium.eu/ |
---|
29 | * @see http://epic.gwdg.de/wiki/index.php/EPIC:API |
---|
30 | * @see âhttps://github.com/CatchPlus/EPIC-API-v2/ |
---|
31 | */ |
---|
32 | @Service |
---|
33 | @Profile("vcr.pid.epic") |
---|
34 | public class EPICPersistentIdentifierProvider implements PersistentIdentifierProvider { |
---|
35 | |
---|
36 | private static final Logger logger = LoggerFactory.getLogger(EPICPersistentIdentifierProvider.class); |
---|
37 | private final PidWriter pidWriter; |
---|
38 | private final Configuration configuration; |
---|
39 | |
---|
40 | @Value("${pid_provider.base_uri}") |
---|
41 | private String baseUri; |
---|
42 | |
---|
43 | /** |
---|
44 | * |
---|
45 | * @param pidWriter PID writer implementation to use |
---|
46 | * @param configuration configuration to be passed to PID writer methods |
---|
47 | */ |
---|
48 | @Autowired |
---|
49 | public EPICPersistentIdentifierProvider(PidWriter pidWriter, Configuration configuration) { |
---|
50 | this.pidWriter = pidWriter; |
---|
51 | this.configuration = configuration; |
---|
52 | } |
---|
53 | |
---|
54 | @Override |
---|
55 | public PersistentIdentifier createIdentifier(VirtualCollection vc) throws VirtualCollectionRegistryException { |
---|
56 | logger.debug("creating handle for virtual collection \"{}\"", vc.getId()); |
---|
57 | final Map<HandleField, String> fieldMap = createPIDFieldMap(vc); |
---|
58 | try { |
---|
59 | final String requestedPid = String.format("VCR-%d", vc.getId()); |
---|
60 | final String pid = pidWriter.registerNewPID(configuration, fieldMap, requestedPid); |
---|
61 | return new PersistentIdentifier(vc, PersistentIdentifier.Type.HANDLE, pid); |
---|
62 | } catch (HttpException ex) { |
---|
63 | throw new VirtualCollectionRegistryException("Could not create EPIC identifier", ex); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | private Map<HandleField, String> createPIDFieldMap(VirtualCollection vc) { |
---|
68 | final Map<HandleField, String> pidMap = new EnumMap<>(HandleField.class); |
---|
69 | pidMap.put(HandleField.URL, makeCollectionURI(vc)); |
---|
70 | pidMap.put(HandleField.TITLE, vc.getName()); |
---|
71 | if (!vc.getCreators().isEmpty()) { |
---|
72 | pidMap.put(HandleField.CREATOR, vc.getCreators().get(0).getPerson()); |
---|
73 | } |
---|
74 | return pidMap; |
---|
75 | } |
---|
76 | |
---|
77 | @Override |
---|
78 | public void updateIdentifier(String pid, URI target) throws VirtualCollectionRegistryException { |
---|
79 | throw new UnsupportedOperationException("Not supported yet."); |
---|
80 | } |
---|
81 | |
---|
82 | @Override |
---|
83 | public void deleteIdentifier(String pid) throws VirtualCollectionRegistryException { |
---|
84 | throw new UnsupportedOperationException("Not supported yet."); |
---|
85 | } |
---|
86 | |
---|
87 | private String makeCollectionURI(VirtualCollection vc) { |
---|
88 | return String.format("%sservice/virtualcollections/%d", baseUri, vc.getId()); |
---|
89 | } |
---|
90 | |
---|
91 | protected void setBaseUri(String baseUri) { |
---|
92 | this.baseUri = baseUri; |
---|
93 | } |
---|
94 | |
---|
95 | } |
---|