Skip to content

Package: PhotoCollectionProviderSupport

PhotoCollectionProviderSupport

nameinstructionbranchcomplexitylinemethod
PhotoCollectionProviderSupport(String)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
clearCaches()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
clearCachesImpl()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
downloadXml(String)
M: 0 C: 25
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 5
100%
M: 0 C: 1
100%
findPhotos(MediaFolder)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
findPhotos(MediaFolder, String)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getAttribute(Node, String)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$findPhotos$0(String, MediaFolder, String)
M: 6 C: 45
88%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 9
82%
M: 0 C: 1
100%
static {...}
M: 6 C: 20
77%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 8
80%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.service.stoppingdown.impl;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Collection;
32: import java.util.Map;
33: import java.util.concurrent.ConcurrentHashMap;
34: import java.io.IOException;
35: import javax.xml.parsers.DocumentBuilderFactory;
36: import javax.xml.parsers.ParserConfigurationException;
37: import javax.xml.xpath.XPath;
38: import javax.xml.xpath.XPathConstants;
39: import javax.xml.xpath.XPathExpression;
40: import javax.xml.xpath.XPathExpressionException;
41: import javax.xml.xpath.XPathFactory;
42: import org.springframework.scheduling.annotation.Scheduled;
43: import org.w3c.dom.DOMException;
44: import org.w3c.dom.Document;
45: import org.w3c.dom.Node;
46: import org.w3c.dom.NodeList;
47: import org.xml.sax.SAXException;
48: import it.tidalwave.util.annotation.VisibleForTesting;
49: import it.tidalwave.bluemarine2.model.MediaFolder;
50: import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
51: import it.tidalwave.bluemarine2.model.spi.PathAwareFinder;
52: import lombok.RequiredArgsConstructor;
53: import lombok.extern.slf4j.Slf4j;
54:
55: /***********************************************************************************************************************
56: *
57: * @author Fabrizio Giudici
58: *
59: **********************************************************************************************************************/
60: @RequiredArgsConstructor @Slf4j
61: public class PhotoCollectionProviderSupport implements PhotoCollectionProvider
62: {
63: protected static final String URL_STOPPINGDOWN = System.getProperty("stoppingdown", "http://stoppingdown.net");
64:
65: protected static final String URL_GALLERY_TEMPLATE = "%s%s/images.xml";
66:
67: protected static final DocumentBuilderFactory PARSER_FACTORY = DocumentBuilderFactory.newInstance();
68:
69: // FIXME: XPath stuff is not thread-safe - fix!
70: protected static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance();
71:
72: private static final XPathExpression XPATH_STILLIMAGE_EXPR;
73:
74: @Nonnull
75: protected final String baseUrl;
76:
77: /**
78: * A local cache for finders. It's advisable, since clients will frequently retrieve a finder because of pagination.
79: */
80: private final Map<String, Collection<PathAwareEntity>> photoCollectionCache = new ConcurrentHashMap<>();
81:
82: /*******************************************************************************************************************
83: *
84: ******************************************************************************************************************/
85: static
86: {
87: try
88: {
89: final XPath xpath = XPATH_FACTORY.newXPath();
90: XPATH_STILLIMAGE_EXPR = xpath.compile("/gallery/stillImage");
91: }
92: catch (XPathExpressionException e)
93: {
94: throw new ExceptionInInitializerError(e);
95: }
96: }
97:
98: /*******************************************************************************************************************
99: *
100: * {@inheritDoc}
101: *
102: ******************************************************************************************************************/
103: @Override @Nonnull
104: public PathAwareFinder findPhotos (@Nonnull final MediaFolder parent)
105: {
106: throw new UnsupportedOperationException("must be implemented in subclasses");
107: }
108:
109: /*******************************************************************************************************************
110: *
111: *
112: *
113: ******************************************************************************************************************/
114: @Scheduled(fixedDelay = 14_400_000) // 12 hours TODO: yes, can use properties here
115: private void clearCaches()
116: {
117: log.info("clearCaches()");
118: clearCachesImpl();
119: }
120:
121: /*******************************************************************************************************************
122: *
123: * {@inheritDoc}
124: *
125: ******************************************************************************************************************/
126: protected void clearCachesImpl()
127: {
128: photoCollectionCache.clear();
129: }
130:
131: /*******************************************************************************************************************
132: *
133: * Creates a collection of entities for the given gallery URL.
134: *
135: * @param parent the parent node
136: * @param galleryUrl the gallery URL
137: * @return the collection of entities
138: *
139: ******************************************************************************************************************/
140: @Nonnull
141: @VisibleForTesting Collection<PathAwareEntity> findPhotos (@Nonnull final MediaFolder parent,
142: @Nonnull final String galleryUrl)
143: {
144: log.debug("findPhotos({}, {}", parent, galleryUrl);
145:
146: return photoCollectionCache.computeIfAbsent(galleryUrl, u ->
147: {
148: try
149: {
150: final Document document = downloadXml(galleryUrl);
151: final NodeList nodes = (NodeList)XPATH_STILLIMAGE_EXPR.evaluate(document, XPathConstants.NODESET);
152:
153: final Collection<PathAwareEntity> photoItems = new ArrayList<>();
154:
155:• for (int i = 0; i < nodes.getLength(); i++)
156: {
157: final Node node = nodes.item(i);
158: final String id = getAttribute(node, "id");
159: final String title = getAttribute(node, "title");
160: photoItems.add(new PhotoItem(parent, id, title));
161: }
162:
163: return photoItems;
164: }
165: catch (SAXException | IOException | XPathExpressionException | ParserConfigurationException e)
166: {
167: throw new RuntimeException(e);
168: }
169: });
170: }
171:
172: /*******************************************************************************************************************
173: *
174: ******************************************************************************************************************/
175: // FIXME: implement a local cache on disk
176: @Nonnull
177: protected Document downloadXml (@Nonnull String url)
178: throws SAXException, ParserConfigurationException, IOException
179: {
180: log.info("downloadXml({})", url);
181:
182: url = url.replaceAll("(^.*)\\/([0-9]{2})-([0-9]{2})\\/(.*)$", "$1/$2/$3/$4");
183:
184:• if (url.startsWith("file:") && url.endsWith("/")) // To support local test resources
185: {
186: url += "/index.xhtml";
187: }
188:
189: return PARSER_FACTORY.newDocumentBuilder().parse(url);
190: }
191:
192: /*******************************************************************************************************************
193: *
194: ******************************************************************************************************************/
195: @Nonnull
196: protected static String getAttribute (@Nonnull final Node node, @Nonnull final String attrName)
197: throws DOMException
198: {
199: return node.getAttributes().getNamedItem(attrName).getNodeValue();
200: }
201: }