Content of file PhotoCollectionProviderSupport.java.html
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>PhotoCollectionProviderSupport.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">blueMarine II :: Headless Service</a> > <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-services-stoppingdown</a> > <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.service.stoppingdown.impl</a> > <span class="el_source">PhotoCollectionProviderSupport.java</span></div><h1>PhotoCollectionProviderSupport.java</h1><pre class="source lang-java linenums">/*
* *********************************************************************************************************************
*
* blueMarine II: Semantic Media Centre
* http://tidalwave.it/projects/bluemarine2
*
* Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *********************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/bluemarine2-src
* git clone https://github.com/tidalwave-it/bluemarine2-src
*
* *********************************************************************************************************************
*/
package it.tidalwave.bluemarine2.service.stoppingdown.impl;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import it.tidalwave.util.annotation.VisibleForTesting;
import it.tidalwave.bluemarine2.model.MediaFolder;
import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
import it.tidalwave.bluemarine2.model.spi.PathAwareFinder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="fc" id="L60">@RequiredArgsConstructor @Slf4j</span>
public class PhotoCollectionProviderSupport implements PhotoCollectionProvider
{
<span class="fc" id="L63"> protected static final String URL_STOPPINGDOWN = System.getProperty("stoppingdown", "http://stoppingdown.net");</span>
protected static final String URL_GALLERY_TEMPLATE = "%s%s/images.xml";
<span class="fc" id="L67"> protected static final DocumentBuilderFactory PARSER_FACTORY = DocumentBuilderFactory.newInstance();</span>
// FIXME: XPath stuff is not thread-safe - fix!
| XPath stuff is not thread-safe - fix! | |
<span class="fc" id="L70"> protected static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance();</span>
private static final XPathExpression XPATH_STILLIMAGE_EXPR;
@Nonnull
protected final String baseUrl;
/**
* A local cache for finders. It's advisable, since clients will frequently retrieve a finder because of pagination.
*/
<span class="fc" id="L80"> private final Map<String, Collection<PathAwareEntity>> photoCollectionCache = new ConcurrentHashMap<>();</span>
/*******************************************************************************************************************
*
******************************************************************************************************************/
static
{
try
{
<span class="fc" id="L89"> final XPath xpath = XPATH_FACTORY.newXPath();</span>
<span class="fc" id="L90"> XPATH_STILLIMAGE_EXPR = xpath.compile("/gallery/stillImage");</span>
}
<span class="nc" id="L92"> catch (XPathExpressionException e)</span>
{
<span class="nc" id="L94"> throw new ExceptionInInitializerError(e);</span>
<span class="fc" id="L95"> }</span>
<span class="fc" id="L96"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public PathAwareFinder findPhotos (@Nonnull final MediaFolder parent)
{
<span class="nc" id="L106"> throw new UnsupportedOperationException("must be implemented in subclasses");</span>
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Scheduled(fixedDelay = 14_400_000) // 12 hours TODO: yes, can use properties here
private void clearCaches()
{
<span class="fc" id="L117"> log.info("clearCaches()");</span>
<span class="fc" id="L118"> clearCachesImpl();</span>
<span class="fc" id="L119"> }</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
protected void clearCachesImpl()
{
<span class="fc" id="L128"> photoCollectionCache.clear();</span>
<span class="fc" id="L129"> }</span>
/*******************************************************************************************************************
*
* Creates a collection of entities for the given gallery URL.
*
* @param parent the parent node
* @param galleryUrl the gallery URL
* @return the collection of entities
*
******************************************************************************************************************/
@Nonnull
@VisibleForTesting Collection<PathAwareEntity> findPhotos (@Nonnull final MediaFolder parent,
@Nonnull final String galleryUrl)
{
<span class="fc" id="L144"> log.debug("findPhotos({}, {}", parent, galleryUrl);</span>
<span class="fc" id="L146"> return photoCollectionCache.computeIfAbsent(galleryUrl, u -></span>
{
try
{
<span class="fc" id="L150"> final Document document = downloadXml(galleryUrl);</span>
<span class="fc" id="L151"> final NodeList nodes = (NodeList)XPATH_STILLIMAGE_EXPR.evaluate(document, XPathConstants.NODESET);</span>
<span class="fc" id="L153"> final Collection<PathAwareEntity> photoItems = new ArrayList<>();</span>
<span class="fc bfc" id="L155" title="All 2 branches covered."> for (int i = 0; i < nodes.getLength(); i++)</span>
{
<span class="fc" id="L157"> final Node node = nodes.item(i);</span>
<span class="fc" id="L158"> final String id = getAttribute(node, "id");</span>
<span class="fc" id="L159"> final String title = getAttribute(node, "title");</span>
<span class="fc" id="L160"> photoItems.add(new PhotoItem(parent, id, title));</span>
}
<span class="fc" id="L163"> return photoItems;</span>
}
<span class="nc" id="L165"> catch (SAXException | IOException | XPathExpressionException | ParserConfigurationException e)</span>
{
<span class="nc" id="L167"> throw new RuntimeException(e);</span>
}
});
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
// FIXME: implement a local cache on disk
@Nonnull
protected Document downloadXml (@Nonnull String url)
throws SAXException, ParserConfigurationException, IOException
{
<span class="fc" id="L180"> log.info("downloadXml({})", url);</span>
<span class="fc" id="L182"> url = url.replaceAll("(^.*)\\/([0-9]{2})-([0-9]{2})\\/(.*)$", "$1/$2/$3/$4");</span>
<span class="pc bpc" id="L184" title="1 of 4 branches missed."> if (url.startsWith("file:") && url.endsWith("/")) // To support local test resources</span>
{
<span class="fc" id="L186"> url += "/index.xhtml";</span>
}
<span class="fc" id="L189"> return PARSER_FACTORY.newDocumentBuilder().parse(url);</span>
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
@Nonnull
protected static String getAttribute (@Nonnull final Node node, @Nonnull final String attrName)
throws DOMException
{
<span class="fc" id="L199"> return node.getAttributes().getNamedItem(attrName).getNodeValue();</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>