1 | /* |
---|
2 | * Copyright (C) 2014 CLARIN |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | package eu.clarin.cmdi.vlo.service.impl; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.service.handle.impl.HandleRestApiClient; |
---|
20 | import static eu.clarin.cmdi.vlo.FacetConstants.HANDLE_PREFIX; |
---|
21 | import static eu.clarin.cmdi.vlo.FacetConstants.HANDLE_PROXY; |
---|
22 | import eu.clarin.cmdi.vlo.service.handle.HandleClient; |
---|
23 | import eu.clarin.cmdi.vlo.service.UriResolver; |
---|
24 | import org.slf4j.Logger; |
---|
25 | import org.slf4j.LoggerFactory; |
---|
26 | |
---|
27 | /** |
---|
28 | * Resolves a URI as follows: if the URI starts with the handle scheme or the |
---|
29 | * handle proxy, the handle is extracted and passed on to this resolver's |
---|
30 | * {@link HandleClient} and the result of {@link HandleClient#getUrl(java.lang.String) |
---|
31 | * } is returned; otherwise the original URI is returned. |
---|
32 | * |
---|
33 | * @author twagoo |
---|
34 | */ |
---|
35 | public class UriResolverImpl implements UriResolver { |
---|
36 | |
---|
37 | private final static Logger logger = LoggerFactory.getLogger(HandleRestApiClient.class); |
---|
38 | |
---|
39 | private final HandleClient handleClient; |
---|
40 | |
---|
41 | public UriResolverImpl(HandleClient handleClient) { |
---|
42 | this.handleClient = handleClient; |
---|
43 | } |
---|
44 | |
---|
45 | @Override |
---|
46 | public String resolve(String uri) { |
---|
47 | final String handle = getHandle(uri); |
---|
48 | |
---|
49 | if (handle == null) { |
---|
50 | return uri; |
---|
51 | } else { |
---|
52 | logger.debug("Calling handle client to resolve handle [{}]", uri); |
---|
53 | final String resolved = handleClient.getUrl(handle); |
---|
54 | if (resolved == null) { |
---|
55 | return uri; |
---|
56 | } else { |
---|
57 | return resolved; |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | } |
---|
62 | |
---|
63 | private String getHandle(String uri) { |
---|
64 | final String handle; |
---|
65 | if (uri.startsWith(HANDLE_PREFIX)) { |
---|
66 | handle = uri.substring(HANDLE_PREFIX.length()); |
---|
67 | } else if (uri.startsWith(HANDLE_PROXY)) { |
---|
68 | handle = uri.substring(HANDLE_PROXY.length()); |
---|
69 | } else { |
---|
70 | handle = null; |
---|
71 | } |
---|
72 | return handle; |
---|
73 | } |
---|
74 | |
---|
75 | } |
---|