1 | package eu.clarin.sru.server.fcs.utils; |
---|
2 | |
---|
3 | import java.net.URI; |
---|
4 | import java.util.Collections; |
---|
5 | import java.util.List; |
---|
6 | |
---|
7 | import eu.clarin.sru.server.SRUException; |
---|
8 | import eu.clarin.sru.server.fcs.DataView; |
---|
9 | import eu.clarin.sru.server.fcs.EndpointDescription; |
---|
10 | import eu.clarin.sru.server.fcs.ResourceInfo; |
---|
11 | |
---|
12 | |
---|
13 | /** |
---|
14 | * A very simple implementation of an endpoint description that is initialized |
---|
15 | * from static information supplied at construction time. Mostly used together |
---|
16 | * with {@link SimpleEndpointDescriptionParser}, but it is agnostic how the |
---|
17 | * static list of resource info records is generated. |
---|
18 | * |
---|
19 | * @see EndpointDescription |
---|
20 | * @see SimpleEndpointDescriptionParser |
---|
21 | */ |
---|
22 | public class SimpleEndpointDescription extends AbstractEndpointDescriptionBase { |
---|
23 | private final boolean pidCaseSensitive; |
---|
24 | private final List<ResourceInfo> entries; |
---|
25 | |
---|
26 | |
---|
27 | /** |
---|
28 | * Constructor. |
---|
29 | * |
---|
30 | * @param capabilities |
---|
31 | * a list of capabilities supported by this endpoint |
---|
32 | * @param supportedDataViews |
---|
33 | * a list of data views that are supported by this endpoint |
---|
34 | * @param resources |
---|
35 | * a static list of resource info records |
---|
36 | * @param pidCaseSensitive |
---|
37 | * <code>true</code> if comparison of persistent identifiers |
---|
38 | * should be performed case-sensitive, <code>false</code> |
---|
39 | * otherwise |
---|
40 | */ |
---|
41 | public SimpleEndpointDescription(List<URI> capabilities, |
---|
42 | List<DataView> supportedDataViews, List<ResourceInfo> resources, |
---|
43 | boolean pidCaseSensitive) { |
---|
44 | super(capabilities, supportedDataViews); |
---|
45 | |
---|
46 | if (resources == null) { |
---|
47 | throw new NullPointerException("entries == null"); |
---|
48 | } |
---|
49 | this.entries = Collections.unmodifiableList(resources); |
---|
50 | this.pidCaseSensitive = pidCaseSensitive; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | @Override |
---|
55 | public void destroy() { |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | @Override |
---|
60 | public List<ResourceInfo> getResourceList(String pid) throws SRUException { |
---|
61 | if (pid == null) { |
---|
62 | throw new NullPointerException("pid == null"); |
---|
63 | } |
---|
64 | if (pid.isEmpty()) { |
---|
65 | throw new IllegalArgumentException("pid is empty"); |
---|
66 | } |
---|
67 | if (!pidCaseSensitive) { |
---|
68 | pid = pid.toLowerCase(); |
---|
69 | } |
---|
70 | if (pid.equals(PID_ROOT)) { |
---|
71 | return entries; |
---|
72 | } else { |
---|
73 | ResourceInfo ri = findRecursive(entries, pid); |
---|
74 | if (ri != null) { |
---|
75 | return ri.getSubResources(); |
---|
76 | } |
---|
77 | } |
---|
78 | return null; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | private ResourceInfo findRecursive(List<ResourceInfo> items, String pid) { |
---|
83 | if ((items != null) && !items.isEmpty()) { |
---|
84 | for (ResourceInfo item : items) { |
---|
85 | if (pidCaseSensitive) { |
---|
86 | if (pid.equals(item.getPid())) { |
---|
87 | return item; |
---|
88 | } |
---|
89 | } else { |
---|
90 | if (pid.equalsIgnoreCase(item.getPid())) { |
---|
91 | return item; |
---|
92 | } |
---|
93 | } |
---|
94 | if (item.hasSubResources()) { |
---|
95 | ResourceInfo ri = |
---|
96 | findRecursive(item.getSubResources(), pid); |
---|
97 | if (ri != null) { |
---|
98 | return ri; |
---|
99 | } |
---|
100 | } |
---|
101 | } // for |
---|
102 | } |
---|
103 | return null; |
---|
104 | } |
---|
105 | |
---|
106 | } // class SimpleEndpointDescription |
---|