Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * *********************************************************************************************************************
   *
   * NorthernWind - lightweight CMS
   * http://northernwind.tidalwave.it - hg clone https://bitbucket.org/tidalwave/northernwind-src
   * %%
   * Copyright (C) 2011 - 2013 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.
  *
  * *********************************************************************************************************************
  *
  * $Id$
  *
  * *********************************************************************************************************************
  * #L%
  */
 package it.tidalwave.northernwind.core.impl.model;
 
 import  javax.annotation.Nonnull;
 import java.util.List;
 import java.util.Map;
 import  it.tidalwave.util.NotFoundException;
 import lombok.Getter;
The default implementation of Site.

Author(s):
Fabrizio Giudici
Version:
$Id$ /
 
 /* package */ class DefaultSite implements InternalSite
   {
     static interface FilePredicate
       {
         public void apply (@Nonnull ResourceFile file, @Nonnull ResourcePath relativeUri);
       }
 
     static interface FileFilter
       {
         public boolean accept (@Nonnull ResourceFile file);
       }
 
     private final FileFilter FOLDER_FILTER = new FileFilter()
       {
         @Override
         public boolean accept (final @Nonnull ResourceFile file)
           {
             return file.isFolder() && !.contains(file.getName());
           }
       };
 
     private final FileFilter FILE_FILTER = new FileFilter()
       {
         @Override
         public boolean accept (final @Nonnull ResourceFile file)
           {
             return file.isData() && !.contains(file.getName());
           }
       };
    @Inject @Nonnull
    @Inject @Nonnull
    private RequestHolder requestHolder;
    @Nonnull
    private final ModelFactory modelFactory;
    @Inject @Named("fileSystemProvider") @Getter @Nonnull
    @Nonnull
    /* package */ final String documentPath;
    @Nonnull
    /* package */ final String mediaPath;
    @Nonnull
    /* package */ final String libraryPath;
    @Nonnull
    /* package */ final String nodePath;
    @Getter
    /* package */ final boolean logConfigurationEnabled;
    @Getter @Nonnull
    /* package */ final String contextPath;
    /* package */ final List<StringignoredFolders = new ArrayList<>();
    private ResourceFile documentFolder;
    private ResourceFile libraryFolder;
    private ResourceFile mediaFolder;
    @Getter
    private ResourceFile nodeFolder;
    // Note that this class can't be final neither use ImmutableMaps, since during the traversing of the filesystem
    // resources need to access the partially created internal structure.
    /* package */  final Map<StringContentdocumentMapByRelativePath = new TreeMap<>();
    /* package */  final Map<StringResourcelibraryMapByRelativePath = new TreeMap<>();
    /* package */  final Map<StringMediamediaMapByRelativePath = new TreeMap<>();
    /* package */  final Map<StringSiteNodenodeMapByRelativePath = new TreeMap<>();
    /* package */  final RegexTreeMap<SiteNodenodeMapByRelativeUri = new RegexTreeMap<>();
    private final Map<Class<?>, Map<String, ?>> relativePathMapsByType = new HashMap<>();
    private final Map<Class<?>, RegexTreeMap<?>> relativeUriMapsByType = new HashMap<>();
    private final List<LocaleconfiguredLocales = new ArrayList<>();

    
/
    protected DefaultSite (final @Nonnull Site.Builder siteBuilder)
      {
        this. = siteBuilder.getModelFactory();
        this. = siteBuilder.getContextPath();
        this. = siteBuilder.getDocumentPath();
        this. = siteBuilder.getMediaPath();
        this. = siteBuilder.getLibraryPath();
        this. = siteBuilder.getNodePath();
        this. = siteBuilder.isLogConfigurationEnabled();
        this..addAll(siteBuilder.getConfiguredLocales());
        this..addAll(siteBuilder.getIgnoredFolders());
      }

    
/
    @Override @Nonnull @SuppressWarnings("unchecked")
    public <Type> SiteFinder<Type> find (final @Nonnull Class<Type> type)
      {
        final Map<String, Type> relativePathMap = (Map<String, Type>).get(type);
        final RegexTreeMap<Type> relativeUriMap = (RegexTreeMap<Type>).get(type);
        return new DefaultSiteFinder<>(type.getSimpleName(), relativePathMaprelativeUriMap);
      }

    
/
    @Override @Nonnull
    public String createLink (final @Nonnull ResourcePath relativeUri)
      {
        final ResourcePath link = new ResourcePath().appendedWith(relativeUri);
        String linkAsString = .get().getBaseUrl() + link.asString();
        for (final LinkPostProcessor linkPostProcessor : )
          {
            linkAsString = linkPostProcessor.postProcess(linkAsString);
          }
        return linkAsString;
      }

    
/
    @Override @Nonnull
    public List<LocalegetConfiguredLocales()
      {
        return new CopyOnWriteArrayList<>();
      }

    
/
    @Override @Nonnull
    public String toString()
      {
        return String.format("DefaultSite(@%x)", System.identityHashCode(this));
      }

    
/
    /* package */ void initialize()
      throws IOException, NotFoundException, PropertyVetoException
      {
        log.info("initialize()");
        log.info(">>>> fileSystemProvider: {}");
        final ResourceFileSystem fileSystem = .getFileSystem();
         = findMandatoryFolder(fileSystem);
          = findMandatoryFolder(fileSystem);
            = findMandatoryFolder(fileSystem);
             = findMandatoryFolder(fileSystem);
        log.info(">>>> contextPath:        {}");
        log.info(">>>> ignoredFolders:     {}");
        log.info(">>>> fileSystem:         {}"fileSystem);
        log.info(">>>> documentPath:       {}".getPath().asString());
        log.info(">>>> libraryPath:        {}".getPath().asString());
        log.info(">>>> mediaPath:          {}".getPath().asString());
        log.info(">>>> nodePath:           {}".getPath().asString());
        log.info(">>>> locales:            {}");
        .clear();
        .clear();
        .clear();
          {
            @Override
            public void apply (final @Nonnull ResourceFile filefinal @Nonnull ResourcePath relativePath)
              {
                .put(relativePath.asString(), .createResource().withFile(file).build());
              }
          });
          {
            @Override
            public void apply (final @Nonnull ResourceFile filefinal @Nonnull ResourcePath relativePath)
              {
                .put(relativePath.asString(), .createMedia().withFile(file).build());
              }
          });
          {
            @Override
            public void apply (final @Nonnull ResourceFile folderfinal @Nonnull ResourcePath relativePath)
              {
                .put(relativePath.asString(), .createContent().withFolder(folder).build());
              }
          });
          {
            @Override
            public void apply (final @Nonnull ResourceFile folderfinal @Nonnull ResourcePath relativePath)
              {
                try
                  {
                    final SiteNode siteNode = .createSiteNode(DefaultSite.thisfolder);
                    .put(relativePath.asString(), siteNode);
                    if (!siteNode.isPlaceHolder())
                      {
                        final ResourcePath relativeUri = siteNode.getRelativeUri();
                        if ("true".equals(siteNode.getProperties().getProperty(."false")))
                          {
                            .putRegex("^" + RegexTreeMap.escape(relativeUri.asString()) + "(|/.*$)"siteNode);
                          }
                        else
                          {
                            .put(relativeUri.asString(), siteNode);
                          }
                      }
                  }
                catch (IOException | NotFoundException e)
                  {
                    throw new RuntimeException(e);
                  }
              }
          });
        if ()
          {
            logConfiguration("Documents by relative path:");
            logConfiguration("Library by relative path:");
            logConfiguration("Media by relative path:");
            logConfiguration("Nodes by relative path:");
            logConfiguration("Nodes by relative URI:");
          }
      }

    
Traverse the file system with a FilePredicate.

Parameters:
folder the folder to traverse
fileFilter the filter for directory contents
predicate the predicate /
    private void traverse (final @Nonnull ResourceFile folder,
                           final @Nonnull FileFilter fileFilter,
                           final @Nonnull FilePredicate predicate)
      {
        traverse(folder.getPath(), folderfileFilterpredicate);
      }

    
Traverse the file system with a FilePredicate.

Parameters:
file the file to traverse
fileFilter the filter for directory contents
predicate the predicate /
    private void traverse (final @Nonnull ResourcePath rootPath,
                           final @Nonnull ResourceFile file,
                           final @Nonnull FileFilter fileFilter,
                           final @Nonnull FilePredicate predicate)
      {
        log.trace("traverse({}, {}, {}, {})"rootPathfilefileFilterpredicate);
        final ResourcePath relativePath = file.getPath().urlDecoded().relativeTo(rootPath);
        if (fileFilter.accept(file))
          {
            predicate.apply(filerelativePath);
          }
        for (final ResourceFile child : file.findChildren().results())
          {
            traverse(rootPathchildfileFilterpredicate);
          }
      }

    
Logs the configuration contained in the given map of properties. /
    private static void logConfiguration (final @Nonnull String nameMap<String, ?> propertyMap)
      {
        log.info(name);
        for (final Entry<String, ?> entry : propertyMap.entrySet())
          {
            log.info(">>>> {}: {}"entry.getKey(), entry.getValue());
          }
      }

    
FIXME Wrapper against ResourceFileSystem: its methods should throw NFE by themselves /
    @Nonnull
    private static ResourceFile findMandatoryFolder (final @Nonnull ResourceFileSystem fileSystem,
                                                     final @Nonnull String path)
      throws NotFoundException
      {
        return NotFoundException.throwWhenNull(fileSystem.findFileByPath(path), "Cannot find folder: " + path);
        // don't log fileSystem.getRoot() since if fileSystem is broken it can trigger secondary errors
                            // FileUtil.toFile(fileSystem.getRoot()).getAbsolutePath() + "/"  + path);
      }
  }
New to GrepCode? Check out our FAQ X