View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 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.cms.beans.config;
35  
36  import info.magnolia.cms.util.ModuleConfigurationObservingManager;
37  import info.magnolia.event.EventBus;
38  import info.magnolia.event.SystemEventBus;
39  import info.magnolia.jcr.node2bean.Node2BeanProcessor;
40  import info.magnolia.jcr.util.NodeUtil;
41  import info.magnolia.module.ModuleRegistry;
42  import info.magnolia.module.ModulesStartedEvent;
43  
44  import java.util.ArrayList;
45  import java.util.Collection;
46  import java.util.Collections;
47  import java.util.Iterator;
48  import java.util.List;
49  import java.util.concurrent.atomic.AtomicReference;
50  
51  import javax.inject.Inject;
52  import javax.inject.Named;
53  import javax.inject.Singleton;
54  import javax.jcr.Node;
55  import javax.jcr.RepositoryException;
56  
57  import org.apache.commons.lang3.StringUtils;
58  import org.slf4j.Logger;
59  import org.slf4j.LoggerFactory;
60  
61  /**
62   * Manages the configured virtual URI mappings and invokes them to perform the mapping operation. Monitors configuration
63   * of virtual URI mappings in JCR within each module under a node named <code>virtualURIMapping</code>.
64   *
65   * @see VirtualURIMapping
66   * @see info.magnolia.cms.filters.VirtualUriFilter
67   *
68   * @deprecated since 5.5.6, VirtualURIManager has been replaced by a registry in the virtual-uri module (see MAGNOLIA-3349).
69   */
70  @Singleton
71  @Deprecated
72  public class VirtualURIManager extends ModuleConfigurationObservingManager {
73  
74      private static final Logger log = LoggerFactory.getLogger(VirtualURIManager.class);
75  
76      public static final String FROM_URI_NODEDATANAME = "fromURI";
77  
78      public static final String TO_URI_NODEDATANAME = "toURI";
79  
80      private final Node2BeanProcessor nodeToBean;
81  
82      /**
83       * Atomic reference to a list containing the virtual URI mappings in use. For thread safety the list must never be
84       * changed and is replaced entirely when something is changed in the JCR.
85       */
86      private final AtomicReference<List<VirtualURIMapping>> virtualUriMappings = new AtomicReference<List<VirtualURIMapping>>(new ArrayList<VirtualURIMapping>());
87  
88      @Inject
89      public VirtualURIManager(ModuleRegistry moduleRegistry, Node2BeanProcessor nodeToBean, @Named(SystemEventBus.NAME) EventBus systemEventBus) {
90          super("virtualURIMapping", moduleRegistry);
91          this.nodeToBean = nodeToBean;
92          if (systemEventBus != null) {
93              systemEventBus.addHandler(ModulesStartedEvent.class, new ModulesStartedEvent.Handler() {
94                  @Override
95                  public void onModuleStartupCompleted(ModulesStartedEvent event) {
96                      VirtualURIManager.this.start();
97                  }
98              });
99          }
100     }
101 
102     /**
103      * Checks for the requested URI mapping in Server config : Servlet Specification 2.3 Section 10 "Mapping Requests to
104      * Servlets".
105      *
106      * @param uri the URI of the current request, decoded and without the context path
107      * @return URI string mapping
108      */
109     public String getURIMapping(String uri) {
110         return getURIMapping(uri, null);
111     }
112 
113     /**
114      * Checks for the requested URI mapping in Server config : Servlet Specification 2.3 Section 10 "Mapping Requests to
115      * Servlets".
116      *
117      * @param uri the URI of the current request, decoded and without the context path
118      * @param queryString the Query String of the current request
119      * @return URI string mapping
120      */
121     public String getURIMapping(String uri, String queryString) {
122         Iterator<VirtualURIMapping> e = virtualUriMappings.get().iterator();
123         String mappedURI = StringUtils.EMPTY;
124         int lastMatchedLevel = 0;
125         while (e.hasNext()) {
126             try {
127                 VirtualURIMapping vm = e.next();
128                 final VirtualURIMapping.MappingResult result;
129                 if (queryString != null && vm instanceof QueryAwareVirtualURIMapping) {
130                     result = ((QueryAwareVirtualURIMapping) vm).mapURI(uri, queryString);
131                 } else {
132                     result = vm.mapURI(uri);
133                 }
134                 if (result != null && lastMatchedLevel < result.getLevel()) {
135                     lastMatchedLevel = result.getLevel();
136                     mappedURI = result.getToURI();
137                 }
138             } catch (ClassCastException ex) {
139                 log.error("Virtual URI configuration error, mapping rule is skipped: {}", ex.getMessage(), ex);
140             }
141         }
142         return mappedURI;
143     }
144 
145     @Override
146     protected void reload(List<Node> nodes) throws RepositoryException {
147         try {
148 
149             final List<VirtualURIMapping> foundMappings = new ArrayList<VirtualURIMapping>();
150 
151             for (Node node : nodes) {
152                 for (Node child : NodeUtil.getNodes(node)) {
153                     VirtualURIMapping virtualURIMapping = readVirtualURIMapping(child);
154                     if (virtualURIMapping != null) {
155                         foundMappings.add(virtualURIMapping);
156                     }
157                 }
158             }
159 
160             this.virtualUriMappings.set(foundMappings);
161 
162         } catch (Exception e) {
163             log.error("Failed to load VirtualURIMappings {}", e.getMessage(), e);
164         }
165     }
166 
167     protected VirtualURIMapping readVirtualURIMapping(Node node) {
168         try {
169             log.info("Loading VirtualURIMapping from {}", node.getPath());
170             VirtualURIMappinggnolia/cms/beans/config/VirtualURIMapping.html#VirtualURIMapping">VirtualURIMapping virtualURIMapping = (VirtualURIMapping) nodeToBean.toBean(node, DefaultVirtualURIMapping.class);
171             log.debug("VirtualURIMapping loaded from {}", node.getPath());
172             return virtualURIMapping;
173         } catch (Exception e) {
174             log.error("Unable to read VirtualURIMapping from node [{}]", NodeUtil.getNodePathIfPossible(node), e);
175             return null;
176         }
177     }
178 
179     public Collection<VirtualURIMapping> getURIMappings() {
180         return Collections.unmodifiableList(virtualUriMappings.get());
181     }
182 }