Clover icon

Google Sitemap Module 2.5

  1. Project Clover database Tue Nov 14 2017 13:12:18 CET
  2. Package info.magnolia.module.googlesitemap.config.mapping

File SiteMapVirtualUriMapping.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
20
4
1
130
76
10
0.5
5
4
2.5
11.8% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
SiteMapVirtualUriMapping 63 20 11.8% 10 2
0.9333333493.3%
 

Contributing tests

This file is covered by 2 tests. .

Source view

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  27 toggle @Override
77    public boolean evaluateTyped(Node node) {
78  27 try {
79  27 return NodeUtil.isNodeType(node, SiteMapNodeTypes.SiteMap.NAME);
80    } catch (RepositoryException e) {
81  1 return false;
82    }
83    }
84    };
85   
 
86  2 toggle @Inject
87    public SiteMapVirtualUriMapping(SystemContext context) {
88  2 this.context = context;
89   
90  2 try {
91  2 WorkspaceEventListenerRegistration.observe(GoogleSiteMapConfiguration.WORKSPACE, "/", events -> reloadSiteMapNames())
92    .withSubNodes(true).register();
93    } catch (RepositoryException e) {
94  0 log.error("Cannot register workspace listener.", e);
95    }
96  2 reloadSiteMapNames();
97    }
98   
 
99  8 toggle private void reloadSiteMapNames() {
100  8 try {
101  8 siteMapUrlMappings.clear();
102  8 Node root = context.getJCRSession(GoogleSiteMapConfiguration.WORKSPACE).getRootNode();
103  8 final Iterator<Node> it = NodeUtil.collectAllChildren(root, siteMapNodePredicate).iterator();
104  26 while (it.hasNext()) {
105  18 Node node = it.next();
106  18 if (SiteMapNodeTypes.SiteMap.getUrl(node) != null) {
107  12 URI uri = new URI(SiteMapNodeTypes.SiteMap.getUrl(node));
108  12 siteMapUrlMappings.put(uri, node.getPath() + XML_EXTENSION);
109    }
110    }
111    } catch (RepositoryException | URISyntaxException e) {
112  0 log.warn("Failed to collect sitemap names: ", e.getMessage());
113    }
114    }
115   
 
116  3 toggle @Override
117    public Optional<Result> mapUri(URI uri) {
118  3 String mapping = siteMapUrlMappings.get(uri);
119  3 return mapping == null ? Optional.empty() : Optional.of(new Result(prefix + mapping, uri.getPath().length(), this));
120    }
121   
 
122    toggle @Override
123    public boolean isValid() {
124    return StringUtils.isNotEmpty(prefix);
125    }
126   
 
127    toggle public void setPrefix(String prefix) {
128    this.prefix = prefix;
129    }
130    }