View Javadoc
1   /**
2    * This file Copyright (c) 2016-2020 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.content.observer;
35  
36  import info.magnolia.content.filter.BootstrapPathFilter;
37  import info.magnolia.context.Context;
38  import info.magnolia.dirwatch.DirectoryWatcherService;
39  import info.magnolia.init.MagnoliaConfigurationProperties;
40  import info.magnolia.module.ModuleLifecycle;
41  import info.magnolia.module.ModuleLifecycleContext;
42  
43  import java.io.IOException;
44  import java.nio.file.FileVisitResult;
45  import java.nio.file.Files;
46  import java.nio.file.Path;
47  import java.nio.file.Paths;
48  import java.nio.file.SimpleFileVisitor;
49  import java.nio.file.attribute.BasicFileAttributes;
50  import java.security.MessageDigest;
51  import java.security.NoSuchAlgorithmException;
52  import java.util.EnumSet;
53  
54  import javax.inject.Inject;
55  import javax.jcr.RepositoryException;
56  import javax.jcr.Session;
57  import javax.xml.bind.DatatypeConverter;
58  
59  import info.magnolia.repository.RepositoryConstants;
60  import org.apache.commons.lang3.StringUtils;
61  import org.slf4j.Logger;
62  import org.slf4j.LoggerFactory;
63  
64  import com.google.common.base.Predicate;
65  
66  import static java.nio.file.FileVisitOption.FOLLOW_LINKS;
67  
68  /**
69   * Responsible for observation of bootstrap content folder which should be specified under magnolia.properties file.
70   * <p>
71   * Only starts to observe if {@link #CONTENT_BOOTSTRAP_DIR_PROPERTY_KEY dedicated property key} is specified.
72   * Filters out all the files except the ones with 'XML' extension.
73   * </p>
74   */
75  public class ContentImporterModule implements ModuleLifecycle {
76  
77      private static final Logger log = LoggerFactory.getLogger(ContentImporterModule.class);
78  
79      public static final String CONTENT_BOOTSTRAP_DIR_PROPERTY_KEY = "magnolia.content.bootstrap.dir";
80  
81      public static final String CONTENT_BOOTSTRAP_ONLY_AT_INSTALL_PROPERTY_KEY = "magnolia.content.bootstrap.onlyImportAtInstall";
82  
83      public static final String CONTENT_BOOTSTRAP_CREATE_TASKS_PROPERTY_KEY = "magnolia.content.bootstrap.createTasks";
84  
85      public static final String MGNL_CHECKSUM_PROPERTY = "mgnl:checksum";
86  
87      public static final String CONTENT_BOOTSTRAP_INSTALL_PROPERTY_KEY = "install";
88  
89      private final MagnoliaConfigurationProperties properties;
90      private final DirectoryWatcherService directoryWatcherService;
91      private final TaskCreatorWatcherCallback taskCreatorCallback;
92      private final BootstrapPathFilter pathFilter;
93      private final Context context;
94  
95      // this config property is true after the module was freshly installed
96      private boolean install;
97  
98      @Inject
99      public ContentImporterModule(MagnoliaConfigurationProperties properties,
100                                  DirectoryWatcherService directoryWatcherService,
101                                  TaskCreatorWatcherCallback taskCreatorCallback,
102                                  Context context) {
103         this.properties = properties;
104         this.directoryWatcherService = directoryWatcherService;
105         this.taskCreatorCallback = taskCreatorCallback;
106         this.pathFilter = new BootstrapPathFilter(properties);
107         this.context = context;
108     }
109 
110     @Override
111     public void start(ModuleLifecycleContext moduleLifecycleContext) {
112         String rootPath = properties.getProperty(CONTENT_BOOTSTRAP_DIR_PROPERTY_KEY);
113         if (StringUtils.isNotBlank(rootPath)) {
114             try {
115                 Path path = Paths.get(rootPath);
116                 log.info("Starting Content Importer module. Watching directory '{}' for files to import.", path);
117                 directoryWatcherService.register(path, new AlwaysTruePredicate(), taskCreatorCallback);
118                 if (!properties.getBooleanProperty(CONTENT_BOOTSTRAP_ONLY_AT_INSTALL_PROPERTY_KEY) ||
119                         (properties.getBooleanProperty(CONTENT_BOOTSTRAP_ONLY_AT_INSTALL_PROPERTY_KEY) && isInstall())) {
120                     Files.walkFileTree(path, EnumSet.of(FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
121                         @Override
122                         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
123                             if (pathFilter.apply(file)) {
124                                 taskCreatorCallback.added(file);
125                             }
126                             return FileVisitResult.CONTINUE;
127                         }
128                     });
129                     if (isInstall()) {
130                         Session session = context.getJCRSession(RepositoryConstants.CONFIG);
131                         session.getNode("/modules/content-importer/config").setProperty(ContentImporterModule.CONTENT_BOOTSTRAP_INSTALL_PROPERTY_KEY, false);
132                         session.save();
133                     }
134                 }
135             } catch (RepositoryException | IOException e) {
136                 throw new RuntimeException("Could not start observation of content bootstrap files", e);
137             }
138         } else {
139             log.info("Content bootstrap directory is not defined with property of '{}' in magnolia.properties file, " +
140                     "therefore observation is not started.", CONTENT_BOOTSTRAP_DIR_PROPERTY_KEY);
141         }
142     }
143 
144     public void setInstall(boolean install) {
145         this.install = install;
146     }
147 
148     public boolean isInstall() {
149         return this.install;
150     }
151 
152     @Override
153     public void stop(ModuleLifecycleContext moduleLifecycleContext) {
154     }
155 
156     /**
157      * Converts file path to node path with stripping leading repository name.
158      * <ul>
159      *     <li>website.foo -> /foo</li>
160      *     <li>website.foo.bar -> /foo/bar</li>
161      * </ul>
162      */
163     public static String getJCRPath(Path path) {
164         String fileName = path.getFileName().toString();
165         String nodeNameWithoutExtension = StringUtils.substringBeforeLast(fileName, ".");
166         String nodeNameWithSlashes = StringUtils.replace(nodeNameWithoutExtension, ".", "/");
167         return "/" + StringUtils.substringAfter(nodeNameWithSlashes, "/");
168     }
169 
170     /**
171      * Computes md5 checksum of file on given path.
172      * @param path
173      * @return hexadecimal representation of MD5 hash.
174      */
175     public static String generateChecksum(Path path) throws IOException, NoSuchAlgorithmException {
176         byte[] data = Files.readAllBytes(path);
177         byte[] digest = MessageDigest.getInstance("MD5").digest(data);
178         return DatatypeConverter.printHexBinary(digest).toUpperCase();
179     }
180 
181     /**
182      * Predicate accepts all folders to be registered via {@link DirectoryWatcherService}.
183      */
184     private static class AlwaysTruePredicate implements Predicate<Path> {
185         @Override
186         public boolean apply(Path ignored) {
187             return true;
188         }
189     }
190 }