1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.beans.config;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.content2bean.Content2BeanUtil;
38 import info.magnolia.content2bean.TransformationState;
39 import info.magnolia.content2bean.TypeDescriptor;
40 import info.magnolia.content2bean.TypeMapping;
41 import info.magnolia.content2bean.impl.Content2BeanTransformerImpl;
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.Singleton;
51
52 import org.apache.commons.lang.StringUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56
57
58
59
60
61
62 @Singleton
63 public final class VirtualURIManager extends ObservedManager {
64
65 private static final Logger log = LoggerFactory.getLogger(VirtualURIManager.class);
66
67 public static final String FROM_URI_NODEDATANAME = "fromURI";
68
69 public static final String TO_URI_NODEDATANAME = "toURI";
70
71
72
73
74 public VirtualURIManager() {
75 }
76
77
78
79
80 private final List<VirtualURIMapping> cachedURImapping = new ArrayList<VirtualURIMapping>();
81
82
83
84
85
86
87
88
89 public String getURIMapping(String uri) {
90 return getURIMapping(uri, null);
91 }
92
93
94
95
96
97
98
99
100
101 public String getURIMapping(String uri, String queryString) {
102 Iterator<VirtualURIMapping> e = cachedURImapping.iterator();
103 String mappedURI = StringUtils.EMPTY;
104 int lastMatchedLevel = 0;
105 while (e.hasNext()) {
106 try{
107 VirtualURIMapping vm = e.next();
108 final VirtualURIMapping.MappingResult result;
109 if (queryString != null && vm instanceof QueryAwareVirtualURIMapping){
110 result = ((QueryAwareVirtualURIMapping)vm).mapURI(uri, queryString);
111 } else {
112 result = vm.mapURI(uri);
113 }
114 if (result != null && lastMatchedLevel < result.getLevel()) {
115 lastMatchedLevel = result.getLevel();
116 mappedURI = result.getToURI();
117 }
118 }catch(ClassCastException ex){
119 log.error("Virtual URI configuration error, mapping rule is skipped: " + ex.getMessage(), ex);
120 }
121 }
122 return mappedURI;
123 }
124
125 @Override
126 protected void onRegister(Content node) {
127 try {
128 log.info("Loading VirtualURIMapping from {}", node.getHandle());
129 Content2BeanUtil.setProperties(this.cachedURImapping, node, true, new Content2BeanTransformerImpl(){
130 @Override
131 protected TypeDescriptor onResolveType(TypeMapping typeMapping, TransformationState state, TypeDescriptor resolvedType, ComponentProvider componentProvider) {
132 if(state.getLevel()==2 && resolvedType == null){
133 return typeMapping.getTypeDescriptor(DefaultVirtualURIMapping.class);
134 }
135 return resolvedType;
136 }
137 });
138 log.debug("VirtualURIMapping loaded from {}", node.getHandle());
139 }
140 catch (Exception e) {
141 log.error("Failed to load VirtualURIMapping from " + node.getHandle() + " - " + e.getMessage(), e);
142 }
143 }
144
145 @Override
146 protected void onClear() {
147 this.cachedURImapping.clear();
148 }
149
150
151 public Collection<VirtualURIMapping> getURIMappings() {
152 return cachedURImapping;
153 }
154
155
156
157
158
159 public static VirtualURIManager getInstance() {
160 return Components.getSingleton(VirtualURIManager.class);
161 }
162 }