View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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.core.Content;
37  import info.magnolia.jcr.node2bean.Node2BeanProcessor;
38  import info.magnolia.jcr.node2bean.TransformationState;
39  import info.magnolia.jcr.node2bean.TypeDescriptor;
40  import info.magnolia.jcr.node2bean.TypeMapping;
41  import info.magnolia.jcr.node2bean.impl.Node2BeanTransformerImpl;
42  import info.magnolia.objectfactory.ComponentProvider;
43  import info.magnolia.objectfactory.Components;
44  
45  import java.util.ArrayList;
46  import java.util.Collection;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  import javax.inject.Inject;
51  import javax.inject.Singleton;
52  
53  import org.apache.commons.lang.StringUtils;
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  /**
58   * Store for all virtual URI to template/page mapping.
59   *
60   * @author Sameer Charles
61   * @version 2.0
62   */
63  @Singleton
64  public final class VirtualURIManager extends ObservedManager {
65  
66      private static final Logger log = LoggerFactory.getLogger(VirtualURIManager.class);
67  
68      public static final String FROM_URI_NODEDATANAME = "fromURI";
69  
70      public static final String TO_URI_NODEDATANAME = "toURI";
71  
72      private final Node2BeanProcessor nodeToBean;
73  
74      /**
75       * Instantiated by the system.
76       */
77      @Inject
78      public VirtualURIManager(Node2BeanProcessor nodeToBean) {
79          this.nodeToBean = nodeToBean;
80      }
81  
82      /**
83       * all cached data. <UrlPattern, String[target, pattern]>
84       */
85      private final List<VirtualURIMapping> cachedURImapping = new ArrayList<VirtualURIMapping>();
86  
87      /**
88       * checks for the requested URI mapping in Server config : Servlet Specification 2.3 Section 10 "Mapping Requests to
89       * Servlets".
90       *
91       * @param uri the URI of the current request, decoded and without the context path
92       * @return URI string mapping
93       */
94      public String getURIMapping(String uri) {
95          return getURIMapping(uri, null);
96      }
97  
98      /**
99       * checks for the requested URI mapping in Server config : Servlet Specification 2.3 Section 10 "Mapping Requests to
100      * Servlets".
101      *
102      * @param uri the URI of the current request, decoded and without the context path
103      * @param queryString the Query String of the current request
104      * @return URI string mapping
105      */
106     public String getURIMapping(String uri, String queryString) {
107         Iterator<VirtualURIMapping> e = cachedURImapping.iterator();
108         String mappedURI = StringUtils.EMPTY;
109         int lastMatchedLevel = 0;
110         while (e.hasNext()) {
111             try{
112                 VirtualURIMapping vm = e.next();
113                 final VirtualURIMapping.MappingResult result;
114                 if (queryString != null && vm instanceof QueryAwareVirtualURIMapping){
115                     result = ((QueryAwareVirtualURIMapping)vm).mapURI(uri, queryString);
116                 } else {
117                     result = vm.mapURI(uri);
118                 }
119                 if (result != null && lastMatchedLevel < result.getLevel()) {
120                     lastMatchedLevel = result.getLevel();
121                     mappedURI = result.getToURI();
122                 }
123             }catch(ClassCastException ex){
124                 log.error("Virtual URI configuration error, mapping rule is skipped: " + ex.getMessage(), ex);
125             }
126         }
127         return mappedURI;
128     }
129 
130     @Override
131     protected void onRegister(Content node) {
132         try {
133             log.info("Loading VirtualURIMapping from {}", node.getHandle()); //$NON-NLS-1$
134             nodeToBean.setProperties(this.cachedURImapping, node.getJCRNode(), true, new Node2BeanTransformerImpl() {
135                 @Override
136                 protected TypeDescriptor onResolveType(TypeMapping typeMapping, TransformationState state, TypeDescriptor resolvedType, ComponentProvider componentProvider) {
137                     if (state.getLevel() == 2 && resolvedType == null) {
138                         return typeMapping.getTypeDescriptor(DefaultVirtualURIMapping.class);
139                     }
140                     return resolvedType;
141                 }
142             }, Components.getComponentProvider());
143             log.debug("VirtualURIMapping loaded from {}", node.getHandle()); //$NON-NLS-1$
144         }
145         catch (Exception e) {
146             log.error("Failed to load VirtualURIMapping from " + node.getHandle() + " - " + e.getMessage(), e); //$NON-NLS-1$ //$NON-NLS-2$
147         }
148     }
149 
150     @Override
151     protected void onClear() {
152         this.cachedURImapping.clear();
153     }
154 
155     // TODO : should this really be public ?
156     public Collection<VirtualURIMapping> getURIMappings() {
157         return cachedURImapping;
158     }
159 
160     /**
161      * @return Returns the instance.
162      * @deprecated since 4.5, use IoC !
163      */
164     @Deprecated
165     public static VirtualURIManager getInstance() {
166         return Components.getSingleton(VirtualURIManager.class);
167     }
168 }