Content of file DefaultContentDirectory.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>DefaultContentDirectory.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 :: Media Server :: UPnP</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-mediaserver</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.mediaserver.impl</a> &gt; <span class="el_source">DefaultContentDirectory.java</span></div><h1>DefaultContentDirectory.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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.mediaserver.impl;

import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.beans.factory.annotation.Autowired;
import it.tidalwave.role.ui.Displayable;
import it.tidalwave.bluemarine2.model.MediaFolder;
import it.tidalwave.bluemarine2.model.VirtualMediaFolder;
import it.tidalwave.bluemarine2.model.VirtualMediaFolder.EntityCollectionFactory;
import it.tidalwave.bluemarine2.model.impl.PathAwareMediaFolderDecorator;
import it.tidalwave.bluemarine2.model.role.EntityBrowser;
import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
import it.tidalwave.bluemarine2.mediaserver.ContentDirectory;
import it.tidalwave.bluemarine2.mediaserver.spi.MediaServerService;
import lombok.extern.slf4j.Slf4j;
import static java.util.Comparator.*;
import static java.util.stream.Collectors.*;
import static it.tidalwave.util.Parameters.r;
import static it.tidalwave.role.Identifiable._Identifiable_;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L58">@Slf4j</span>
<span class="fc" id="L59">public class DefaultContentDirectory implements ContentDirectory</span>
  {
<span class="fc" id="L61">    private static final Path PATH_ROOT = Paths.get(&quot;/&quot;);</span>

<span class="fc" id="L63">    private static final Path PATH_VIDEOS = Paths.get(&quot;videos&quot;);</span>

<span class="fc" id="L65">    private static final Path PATH_PHOTOS = Paths.get(&quot;photos&quot;);</span>

<span class="fc" id="L67">    private static final Path PATH_MUSIC = Paths.get(&quot;music&quot;);</span>

<span class="fc" id="L69">    private static final Path PATH_SERVICES = Paths.get(&quot;services&quot;);</span>

<span class="fc" id="L71">    private static final EntityCollectionFactory EMPTY = x -&gt; Collections.emptyList(); // FIXME: move to ECF</span>

//    @Inject FIXME
<span class="fc" id="L74">    @Autowired(required = false)</span>
<span class="fc" id="L75">    private final List&lt;EntityBrowser&gt; entityBrowsers = Collections.emptyList();</span>

//    @Inject FIXME
<span class="fc" id="L78"> @Autowired(required = false)</span> <span class="fc" id="L79"> private final List&lt;MediaServerService&gt; services = Collections.emptyList();</span> private MediaFolder root; @Override @Nonnull public MediaFolder findRoot() { <span class="fc" id="L86"> return root;</span> } @PostConstruct private void initialize() { // FIXME: why is this called multiple times? <span class="fc" id="L93"> log.info(&quot;&gt;&gt;&gt;&gt; discovered entity browsers: {}&quot;, entityBrowsers);</span> <span class="fc" id="L94"> log.info(&quot;&gt;&gt;&gt;&gt; discovered services: {}&quot;, services);</span> <span class="fc" id="L95"> root = new VirtualMediaFolder(Optional.empty(), PATH_ROOT, &quot;&quot;, this::childrenFactory);</span> <span class="fc" id="L96"> }</span> @Nonnull private Collection&lt;PathAwareEntity&gt; childrenFactory (@Nonnull final MediaFolder parent) { <span class="fc" id="L101"> return List.of(new VirtualMediaFolder(parent, PATH_MUSIC, &quot;Music&quot;, this::musicFactory),</span> new VirtualMediaFolder(parent, PATH_PHOTOS, &quot;Photos&quot;, EMPTY), new VirtualMediaFolder(parent, PATH_VIDEOS, &quot;Videos&quot;, EMPTY), new VirtualMediaFolder(parent, PATH_SERVICES, &quot;Services&quot;, this::servicesFactory)); } @Nonnull private Collection&lt;MediaFolder&gt; musicFactory (@Nonnull final MediaFolder parent) { // TODO: filter by MIME type <span class="fc" id="L111"> return entityBrowsers.stream()</span> <span class="fc" id="L112"> .sorted(comparing(browser -&gt; browser.as(Displayable.class).getDisplayName()))</span> <span class="fc" id="L113"> .map(browser -&gt; createMediaFolder(parent, browser))</span> <span class="fc" id="L114"> .collect(toList());</span> } @Nonnull private Collection&lt;MediaFolder&gt; servicesFactory (@Nonnull final MediaFolder parent) { <span class="pc" id="L120"> return services.stream().map(service -&gt; service.createRootFolder(parent)).collect(toList());</span> } @Nonnull private static MediaFolder createMediaFolder (@Nonnull final MediaFolder parent, @Nonnull final EntityBrowser browser) { <span class="fc" id="L127"> final String fallBack = browser.getClass().getSimpleName();</span> <span class="pc" id="L128"> final String pathSegment = browser.maybeAs(_Identifiable_).map(i -&gt; i.getId().stringValue()).orElse(fallBack);</span> <span class="fc" id="L129"> final Displayable displayable = browser.maybeAs(Displayable.class).orElse(Displayable.of(fallBack));</span> <span class="fc" id="L130"> log.trace(&quot;createMediaFolder({}, {}) - path: {} displayable: {}&quot;, parent, browser, pathSegment, displayable);</span> <span class="fc" id="L131"> return new PathAwareMediaFolderDecorator(browser.getRoot(), parent, Paths.get(pathSegment), r(displayable));</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>