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.impl.Content2BeanTransformerImpl;
41 import info.magnolia.objectfactory.Components;
42
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.Iterator;
46 import java.util.List;
47
48 import org.apache.commons.lang.StringUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52
53
54
55
56
57
58 public final class VirtualURIManager extends ObservedManager {
59
60 private static final Logger log = LoggerFactory.getLogger(VirtualURIManager.class);
61
62 public static final String FROM_URI_NODEDATANAME = "fromURI";
63
64 public static final String TO_URI_NODEDATANAME = "toURI";
65
66
67
68
69 public VirtualURIManager() {
70 }
71
72
73
74
75 private final List cachedURImapping = new ArrayList();
76
77
78
79
80
81
82
83
84 public String getURIMapping(String uri) {
85 return getURIMapping(uri, null);
86 }
87
88
89
90
91
92
93
94
95
96 public String getURIMapping(String uri, String queryString) {
97 Iterator e = cachedURImapping.iterator();
98 String mappedURI = StringUtils.EMPTY;
99 int lastMatchedLevel = 0;
100 while (e.hasNext()) {
101 try{
102 VirtualURIMapping vm = (VirtualURIMapping) e.next();
103 final VirtualURIMapping.MappingResult result;
104 if (queryString != null && vm instanceof QueryAwareVirtualURIMapping){
105 result = ((QueryAwareVirtualURIMapping)vm).mapURI(uri, queryString);
106 } else {
107 result = vm.mapURI(uri);
108 }
109 if (result != null && lastMatchedLevel < result.getLevel()) {
110 lastMatchedLevel = result.getLevel();
111 mappedURI = result.getToURI();
112 }
113 }catch(ClassCastException ex){
114 log.error("Virtual URI configuration error, mapping rule is skipped: " + ex.getMessage(), ex);
115 }
116 }
117 return mappedURI;
118 }
119
120 protected void onRegister(Content node) {
121 try {
122 log.info("Loading VirtualURIMapping from {}", node.getHandle());
123 Content2BeanUtil.setProperties(this.cachedURImapping, node, true, new Content2BeanTransformerImpl(){
124 protected TypeDescriptor onResolveType(TransformationState state, TypeDescriptor resolvedType) {
125 if(state.getLevel()==2 && resolvedType == null){
126 return this.getTypeMapping().getTypeDescriptor(DefaultVirtualURIMapping.class);
127 }
128 return resolvedType;
129 }
130 });
131 log.debug("VirtualURIMapping loaded from {}", node.getHandle());
132 }
133 catch (Exception e) {
134 log.error("Failed to load VirtualURIMapping from " + node.getHandle() + " - " + e.getMessage(), e);
135 }
136 }
137
138 protected void onClear() {
139 this.cachedURImapping.clear();
140 }
141
142
143 public Collection getURIMappings() {
144 return cachedURImapping;
145 }
146
147
148
149
150 public static VirtualURIManager getInstance() {
151 return Components.getSingleton(VirtualURIManager.class);
152 }
153 }