View Javadoc
1   /**
2    * This file Copyright (c) 2017 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.module.googlesitemap.config.mapping;
35  
36  import info.magnolia.context.SystemContext;
37  import info.magnolia.jcr.predicate.AbstractPredicate;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration;
40  import info.magnolia.module.googlesitemap.SiteMapNodeTypes;
41  import info.magnolia.observation.WorkspaceEventListenerRegistration;
42  import info.magnolia.virtualuri.VirtualUriMapping;
43  
44  import java.net.URI;
45  import java.net.URISyntaxException;
46  import java.util.HashMap;
47  import java.util.Iterator;
48  import java.util.Map;
49  import java.util.Optional;
50  
51  import javax.inject.Inject;
52  import javax.jcr.Node;
53  import javax.jcr.RepositoryException;
54  
55  import org.apache.commons.lang3.StringUtils;
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  /**
60   * Virtual URI mapping that compares source URI to names of sitemaps available in google-sitemap workspace
61   * and prepends the prefix.
62   */
63  public class SiteMapVirtualUriMapping implements VirtualUriMapping {
64  
65      public static final String XML_EXTENSION = ".xml";
66  
67      private static final Logger log = LoggerFactory.getLogger(SiteMapVirtualUriMapping.class);
68  
69      private final SystemContext context;
70  
71      private final Map<URI, String> siteMapUrlMappings = new HashMap();
72  
73      private String prefix;
74  
75      private AbstractPredicate<Node> siteMapNodePredicate = new AbstractPredicate<Node>() {
76          @Override
77          public boolean evaluateTyped(Node node) {
78              try {
79                  return NodeUtil.isNodeType(node, SiteMapNodeTypes.SiteMap.NAME);
80              } catch (RepositoryException e) {
81                  return false;
82              }
83          }
84      };
85  
86      @Inject
87      public SiteMapVirtualUriMapping(SystemContext context) {
88          this.context = context;
89  
90          try {
91              WorkspaceEventListenerRegistration.observe(GoogleSiteMapConfiguration.WORKSPACE, "/", events -> reloadSiteMapNames())
92                      .withSubNodes(true).register();
93          } catch (RepositoryException e) {
94              log.error("Cannot register workspace listener.", e);
95          }
96          reloadSiteMapNames();
97      }
98  
99      private void reloadSiteMapNames() {
100         try {
101             siteMapUrlMappings.clear();
102             Node root = context.getJCRSession(GoogleSiteMapConfiguration.WORKSPACE).getRootNode();
103             final Iterator<Node> it = NodeUtil.collectAllChildren(root, siteMapNodePredicate).iterator();
104             while (it.hasNext()) {
105                 Node node = it.next();
106                 if (SiteMapNodeTypes.SiteMap.getUrl(node) != null) {
107                     URI uri = new URI(SiteMapNodeTypes.SiteMap.getUrl(node));
108                     siteMapUrlMappings.put(uri, node.getPath() + XML_EXTENSION);
109                 }
110             }
111         } catch (RepositoryException | URISyntaxException e) {
112             log.warn("Failed to collect sitemap names: ", e.getMessage());
113         }
114     }
115 
116     @Override
117     public Optional<Result> mapUri(URI uri) {
118         String mapping = siteMapUrlMappings.get(uri);
119         return mapping == null ? Optional.empty() : Optional.of(new Result(prefix + mapping, uri.getPath().length(), this));
120     }
121 
122     @Override
123     public boolean isValid() {
124         return StringUtils.isNotEmpty(prefix);
125     }
126 
127     public void setPrefix(String prefix) {
128         this.prefix = prefix;
129     }
130 }