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.util.SimpleUrlPattern;
37 import info.magnolia.cms.util.UrlPattern;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.context.WebContext;
40
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.Map;
45
46 import org.apache.commons.lang.StringUtils;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class HostBasedVirtualURIMapping implements VirtualURIMapping {
67
68 private String fromURI;
69
70 private UrlPattern pattern;
71
72 private String toURI;
73
74 private Map<String, String> hosts = new HashMap<String, String>();
75
76
77
78
79 public HostBasedVirtualURIMapping() {
80 hosts = new HashMap<String, String>();
81 }
82
83
84 public List<String> getHosts() {
85 return null;
86 }
87
88
89
90
91
92 public void addHost(String mapping) {
93 String[] hostToPath = StringUtils.split(mapping, "=");
94 if (hostToPath != null && hostToPath.length == 2) {
95 synchronized (hosts) {
96 hosts.put(hostToPath[0], hostToPath[1]);
97 }
98 }
99 }
100
101 public MappingResult mapURI(String uri) {
102
103 String destination = toURI;
104
105 if (pattern != null && pattern.match(uri)) {
106
107 String requestHost = ((WebContext) MgnlContext.getInstance()).getRequest().getServerName();
108
109 if (hosts != null) {
110
111 Iterator<Map.Entry<String, String>> hostIt = hosts.entrySet().iterator();
112 while (hostIt.hasNext()) {
113
114 Map.Entry<String, String> hk = hostIt.next();
115 if (requestHost.endsWith(hk.getKey())) {
116 destination = hk.getValue();
117 break;
118 }
119 }
120 }
121
122 MappingResult r = new MappingResult();
123 r.setLevel(pattern.getLength());
124 r.setToURI(destination);
125 return r;
126 }
127 return null;
128 }
129
130 public String getFromURI() {
131 return this.fromURI;
132 }
133
134 public void setFromURI(String fromURI) {
135 this.fromURI = fromURI;
136 this.pattern = new SimpleUrlPattern(fromURI);
137 }
138
139 public String getToURI() {
140 return this.toURI;
141 }
142
143 public void setToURI(String toURI) {
144 this.toURI = toURI;
145 }
146
147 @Override
148 public String toString() {
149 return fromURI + " --> " + toURI;
150 }
151
152 }