1
2
3
4
5 package org.fedoracommons.funapi.pmh.dspace;
6
7 import java.io.IOException;
8 import java.io.InputStream;
9
10 import java.net.MalformedURLException;
11 import java.net.URL;
12
13 import java.util.Properties;
14
15 import org.fedoracommons.funapi.UnapiException;
16 import org.fedoracommons.funapi.pmh.AbstractPmhResolver;
17
18
19
20
21
22
23
24
25 public class DSpacePmhResolver
26 extends AbstractPmhResolver {
27
28 private Properties props;
29 private URL baseUrl;
30 private String pmhIdPrefix;
31 private String username;
32 private String password;
33
34 public DSpacePmhResolver()
35 throws UnapiException {
36 InputStream in = getClass().getResourceAsStream("/DSpacePmhResolver.properties");
37 props = new Properties();
38 try {
39 props.load(in);
40 } catch (IOException e) {
41 throw new UnapiException(e.getMessage(), e);
42 }
43
44
45 String temp = props.getProperty("pmhBaseUrl");
46 if (temp.endsWith("/")) {
47 temp.substring(0, temp.length() -1);
48 }
49 try {
50 baseUrl = new URL(temp);
51 } catch (MalformedURLException e) {
52 throw new UnapiException(e.getMessage(), e);
53 }
54
55 pmhIdPrefix = props.getProperty("pmhIdPrefix");
56 if (pmhIdPrefix == null) {
57 pmhIdPrefix = String.format("oai:%s:", baseUrl.getHost());
58 }
59 username = props.getProperty("username");
60 password = props.getProperty("password");
61 }
62
63
64
65
66 @Override
67 protected String getPmhId(String id) {
68 String oaiId;
69 if (id.startsWith("hdl:")) {
70 oaiId = pmhIdPrefix + id.substring("hdl:".length());
71 } else if (id.startsWith("http://")) {
72 oaiId = pmhIdPrefix + (id.substring(id.indexOf('/', "http://".length()) + 1));
73 } else {
74 oaiId = id;
75 }
76 return oaiId;
77 }
78
79
80
81
82 @Override
83 protected URL getPmhBaseUrl() {
84 return baseUrl;
85 }
86
87
88
89
90 @Override
91 protected String getPassword() {
92 return password;
93 }
94
95
96
97
98 @Override
99 protected String getUsername() {
100 return username;
101 }
102 }