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.CommonUtils; |
---|
20 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
21 | import static eu.clarin.cmdi.vlo.FacetConstants.HANDLE_PREFIX; |
---|
22 | import eu.clarin.cmdi.vlo.pojo.ResourceInfo; |
---|
23 | import eu.clarin.cmdi.vlo.pojo.ResourceType; |
---|
24 | import eu.clarin.cmdi.vlo.service.ResourceStringConverter; |
---|
25 | import eu.clarin.cmdi.vlo.service.UriResolver; |
---|
26 | import java.net.URI; |
---|
27 | import java.net.URISyntaxException; |
---|
28 | import java.util.regex.Pattern; |
---|
29 | import org.apache.commons.io.FilenameUtils; |
---|
30 | import org.slf4j.Logger; |
---|
31 | import org.slf4j.LoggerFactory; |
---|
32 | |
---|
33 | /** |
---|
34 | * |
---|
35 | * @author twagoo |
---|
36 | */ |
---|
37 | public class ResourceStringConverterImpl implements ResourceStringConverter { |
---|
38 | |
---|
39 | private final static Logger logger = LoggerFactory.getLogger(ResourceStringConverterImpl.class); |
---|
40 | |
---|
41 | private final static String SPLIT_PATTERN = Pattern.quote(FacetConstants.FIELD_RESOURCE_SPLIT_CHAR); |
---|
42 | private final UriResolver resolver; |
---|
43 | |
---|
44 | /** |
---|
45 | * creates a converter that does not attempt to resolve |
---|
46 | */ |
---|
47 | public ResourceStringConverterImpl() { |
---|
48 | this(null); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * |
---|
53 | * @param resolver resolver to apply to resource URI to get final href |
---|
54 | */ |
---|
55 | public ResourceStringConverterImpl(UriResolver resolver) { |
---|
56 | this.resolver = resolver; |
---|
57 | } |
---|
58 | |
---|
59 | @Override |
---|
60 | public ResourceInfo getResourceInfo(String resourceString) { |
---|
61 | // split resource string to find href and mime type |
---|
62 | final String[] tokens = resourceString.split(SPLIT_PATTERN, 2); |
---|
63 | final String mimeType = tokens[0]; |
---|
64 | final String href = tokens[1]; |
---|
65 | |
---|
66 | // if there is a resolver, get file name from resolved URL |
---|
67 | final String fileName; |
---|
68 | if (resolver == null) { |
---|
69 | fileName = getFileName(href); |
---|
70 | } else { |
---|
71 | fileName = getFileName(resolver.resolve(href)); |
---|
72 | } |
---|
73 | |
---|
74 | // determine resource type based on mime type |
---|
75 | final ResourceType resourceType = determineResourceType(mimeType); |
---|
76 | return new ResourceInfo(href, fileName, mimeType, resourceType); |
---|
77 | } |
---|
78 | |
---|
79 | private String getFileName(final String href) { |
---|
80 | try { |
---|
81 | //analyse URI |
---|
82 | final URI uri = new URI(href); |
---|
83 | final String scheme = uri.getScheme(); |
---|
84 | final String path = uri.getPath(); |
---|
85 | // in case of path information or handle, return original href |
---|
86 | if (path == null || path.isEmpty() || (scheme != null && scheme.equals(HANDLE_PREFIX))) { |
---|
87 | return href; |
---|
88 | } else { |
---|
89 | //strip trailing slash, then get name |
---|
90 | return FilenameUtils.getName(path.replaceAll("\\/$", "")); |
---|
91 | } |
---|
92 | } catch (URISyntaxException ex) { |
---|
93 | logger.debug("Invalid URI, coult not extract file name: {}", href, ex); |
---|
94 | return href; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | private ResourceType determineResourceType(final String mimeType) { |
---|
99 | final String normalizeMimeType = CommonUtils.normalizeMimeType(mimeType); |
---|
100 | // map to ResourceType and add to bag |
---|
101 | if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_ANNOTATION)) { |
---|
102 | return ResourceType.ANNOTATION; |
---|
103 | } else if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_AUDIO)) { |
---|
104 | return ResourceType.AUDIO; |
---|
105 | } else if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_IMAGE)) { |
---|
106 | return ResourceType.IMAGE; |
---|
107 | } else if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_TEXT)) { |
---|
108 | return ResourceType.TEXT; |
---|
109 | } else if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_VIDEO)) { |
---|
110 | return ResourceType.VIDEO; |
---|
111 | } else { |
---|
112 | return ResourceType.OTHER; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | } |
---|