source: vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/HandleClientUriResolverImpl.java @ 6192

Last change on this file since 6192 was 6192, checked in by Twan Goosen, 9 years ago

New URI resolver that uses the MPI corpus structure 2 core library to resolve handles using the handle API
Fixes #565

File size: 2.6 KB
Line 
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 */
17package eu.clarin.cmdi.vlo.service.impl;
18
19import eu.clarin.cmdi.vlo.service.handle.impl.HandleRestApiClient;
20import static eu.clarin.cmdi.vlo.FacetConstants.HANDLE_PREFIX;
21import static eu.clarin.cmdi.vlo.FacetConstants.HANDLE_PROXY;
22import eu.clarin.cmdi.vlo.service.handle.HandleClient;
23import eu.clarin.cmdi.vlo.service.UriResolver;
24import org.slf4j.Logger;
25import 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 * TODO: add support for resolving URN:NBN <https://trac.clarin.eu/ticket/535>
34 *
35 * @author twagoo
36 */
37public class HandleClientUriResolverImpl implements UriResolver {
38
39    private final static Logger logger = LoggerFactory.getLogger(HandleRestApiClient.class);
40
41    private final HandleClient handleClient;
42
43    public HandleClientUriResolverImpl(HandleClient handleClient) {
44        this.handleClient = handleClient;
45    }
46
47    @Override
48    public String resolve(String uri) {
49        final String handle = getHandle(uri);
50
51        if (handle == null) {
52            return uri;
53        } else {
54            logger.debug("Calling handle client to resolve handle [{}]", uri);
55            final String resolved = handleClient.getUrl(handle);
56            if (resolved == null) {
57                return uri;
58            } else {
59                return resolved;
60            }
61        }
62
63    }
64
65    private String getHandle(String uri) {
66        final String handle;
67        if (uri.startsWith(HANDLE_PREFIX)) {
68            handle = uri.substring(HANDLE_PREFIX.length());
69        } else if (uri.startsWith(HANDLE_PROXY)) {
70            handle = uri.substring(HANDLE_PROXY.length());
71        } else {
72            handle = null;
73        }
74        return handle;
75    }
76
77}
Note: See TracBrowser for help on using the repository browser.