Content of file ContentDirectoryClingAdapter.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>ContentDirectoryClingAdapter.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-upnp-mediaserver</a> > <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.upnp.mediaserver.impl</a> > <span class="el_source">ContentDirectoryClingAdapter.java</span></div><h1>ContentDirectoryClingAdapter.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.upnp.mediaserver.impl;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
import java.nio.file.Path;
import org.fourthline.cling.support.model.BrowseFlag;
import org.fourthline.cling.support.model.BrowseResult;
import org.fourthline.cling.support.model.SortCriterion;
import org.fourthline.cling.support.contentdirectory.AbstractContentDirectoryService;
import org.fourthline.cling.support.contentdirectory.ContentDirectoryException;
import org.fourthline.cling.support.contentdirectory.DIDLParser;
import it.tidalwave.bluemarine2.model.spi.CacheManager;
import it.tidalwave.bluemarine2.model.spi.CacheManager.Cache;
import it.tidalwave.bluemarine2.model.spi.Entity;
import it.tidalwave.bluemarine2.mediaserver.ContentDirectory;
import it.tidalwave.bluemarine2.upnp.mediaserver.impl.didl.DIDLAdapter.ContentHolder;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import static org.fourthline.cling.support.contentdirectory.ContentDirectoryErrorCode.*;
import static it.tidalwave.bluemarine2.util.Formatters.*;
import static it.tidalwave.bluemarine2.upnp.mediaserver.impl.UpnpUtilities.*;
import static it.tidalwave.bluemarine2.upnp.mediaserver.impl.didl.DIDLAdapter._DIDLAdapter_;
/***********************************************************************************************************************
*
* This class implements a minimal "ContentDirectory" compliant to the UPnP specifications. It acts as an adapter with
* respect to a {@link ContentDirectory}, which provides contents.
*
* @see http://upnp.org/specs/av/UPnP-av-ContentDirectory-v1-Service.pdf
*
* @stereotype Adapter
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="fc" id="L66">@Slf4j</span>
<span class="fc" id="L67">public class ContentDirectoryClingAdapter extends AbstractContentDirectoryService</span>
{
@Inject
private ContentDirectory contentDirectory;
@Inject
private CacheManager cacheManager;
<span class="fc" id="L75"> private final AtomicInteger updateId = new AtomicInteger(1); // FIXME: increment on database update</span>
increment on database update
<span class="pc bpc" id="L77" title="33 of 36 branches missed."> @RequiredArgsConstructor @EqualsAndHashCode @ToString</span>
static class BrowseParams
{
@Nonnull
private final String objectId;
@Nonnull
private final BrowseFlag browseFlag;
private final String filter;
private final long firstResult;
private final long maxResults;
private final SortCriterion[] orderby;
}
/*******************************************************************************************************************
*
* Returns information about an object.
*
* @see http://upnp.org/specs/av/UPnP-av-ContentDirectory-v1-Service.pdf
*
* @param objectId the id of the object to browse
* @param browseFlag whether metadata or children content are requested
* @param filter a filter for returned data
* @param firstResult the first index of the items to return
* @param maxResults the maximum number of items to return
* @param orderby the sort criteria
* @return the requested object
* @throws ContentDirectoryException in case of error
*
******************************************************************************************************************/
@Override
public BrowseResult browse (@Nonnull final String objectId,
@Nonnull final BrowseFlag browseFlag,
final String filter,
final long firstResult,
final long maxResults,
final SortCriterion[] orderby)
throws ContentDirectoryException
{
<span class="fc" id="L115"> log.info("browse({}, {}, filter: {}, startingIndex: {}, requestedCount: {}, sortCriteria: {})",</span>
<span class="fc" id="L116"> objectId, browseFlag, filter, firstResult, maxResults, orderby);</span>
// this repeated log is for capturing test recordings
<span class="fc" id="L118"> log.trace("browse @@@ {} @@@ {} @@@ {} @@@ {} @@@ {} @@@ {})",</span>
<span class="fc" id="L119"> objectId, browseFlag, firstResult, maxResults, filter, orderby);</span>
<span class="fc" id="L121"> final long baseTime = System.nanoTime();</span>
<span class="fc" id="L122"> final BrowseParams params = new BrowseParams(objectId, browseFlag, filter, firstResult, maxResults, orderby);</span>
<span class="fc" id="L123"> final Cache cache = cacheManager.getCache(getClass());</span>
<span class="fc" id="L124"> final Object result = cache.getCachedObject(params, () -> findEntity(params));</span>
<span class="fc" id="L125"> log.info(">>>> result computed in {} msec", (System.nanoTime() - baseTime) / 1E6);</span>
<span class="pc bpc" id="L127" title="1 of 2 branches missed."> if (result instanceof ContentDirectoryException)</span>
{
<span class="nc" id="L129"> throw (ContentDirectoryException)result;</span>
}
<span class="fc" id="L132"> log(">>>> returning", (BrowseResult)result);</span>
<span class="fc" id="L133"> return (BrowseResult)result;</span>
}
/*******************************************************************************************************************
*
* Searches an entity.
*
* @param params the search parameters
* @return a {@link BrowseResult} if the requested entity has been found; a
* {@link ContentDirectoryException} in case of error
*
******************************************************************************************************************/
@Nonnull
private Object findEntity (@Nonnull final BrowseParams params)
{
try
{
<span class="fc" id="L150"> final Path path = didlIdToPath(params.objectId);</span>
<span class="fc" id="L151"> final Entity entity = contentDirectory.findRoot()</span>
<span class="fc" id="L152"> .findChildren()</span>
<span class="fc" id="L153"> .withPath(path)</span>
<span class="fc" id="L154"> .optionalResult()</span>
<span class="fc" id="L155"> .orElseThrow(() -></span>
<span class="nc" id="L156"> new ContentDirectoryException(NO_SUCH_OBJECT,</span>
<span class="nc" id="L157"> String.format("%s -> %s", params.objectId, path)));</span>
<span class="fc" id="L158"> log.debug(">>>> found {}", entity);</span>
<span class="fc" id="L159"> final ContentHolder holder = entity.as(_DIDLAdapter_).toContent(params.browseFlag,</span>
(int)params.firstResult,
<span class="fc" id="L161"> maxCount(params.maxResults));</span>
<span class="fc" id="L162"> final DIDLParser parser = new DIDLParser();</span>
<span class="fc" id="L163"> return new BrowseResult(parser.generate(holder.getContent()),</span>
<span class="fc" id="L164"> holder.getNumberReturned(),</span>
<span class="fc" id="L165"> holder.getTotalMatches(),</span>
<span class="fc" id="L166"> updateId.get());</span>
}
// this method returns exceptions as a result, so they can be cached
<span class="nc" id="L169"> catch (ContentDirectoryException e)</span>
{
<span class="nc" id="L171"> log.error("", e);</span>
<span class="nc" id="L172"> return e;</span>
}
<span class="nc" id="L174"> catch (Exception e)</span>
{
<span class="nc" id="L176"> log.error("", e);</span>
<span class="nc" id="L177"> return new ContentDirectoryException(CANNOT_PROCESS);</span>
}
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
private static void log (@Nonnull final String message, @Nonnull final BrowseResult browseResult)
{
<span class="fc" id="L186"> log.info("{} BrowseResult(..., {}, {}, {})",</span>
message,
<span class="fc" id="L188"> browseResult.getCountLong(),</span>
<span class="fc" id="L189"> browseResult.getTotalMatchesLong(),</span>
<span class="fc" id="L190"> browseResult.getContainerUpdateIDLong());</span>
<span class="pc bpc" id="L192" title="1 of 2 branches missed."> if (log.isDebugEnabled())</span>
{
<span class="fc" id="L194"> Stream.of(xmlPrettyPrinted(browseResult.getResult()).split("\n"))</span>
<span class="fc" id="L195"> .forEach(s -> log.debug("{} {}", message, s));</span>
}
<span class="fc" id="L197"> }</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>