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 public void setHosts(List<String> hosts) {
89 String[] hostToPath = null;
90 for (String host : hosts) {
91 hostToPath = StringUtils.split(host, "=");
92 if (hostToPath != null && hostToPath.length == 2) {
93 this.hosts.put(hostToPath[0], hostToPath[1]);
94 }
95 }
96 }
97
98
99
100
101
102 public void addHost(String mapping) {
103 String[] hostToPath = StringUtils.split(mapping, "=");
104 if (hostToPath != null && hostToPath.length == 2) {
105 synchronized (hosts) {
106 hosts.put(hostToPath[0], hostToPath[1]);
107 }
108 }
109 }
110
111 @Override
112 public MappingResult mapURI(String uri) {
113
114 String destination = toURI;
115
116 if (pattern != null && pattern.match(uri)) {
117
118 String requestHost = ((WebContext) MgnlContext.getInstance()).getRequest().getServerName();
119
120 if (hosts != null) {
121
122 Iterator<Map.Entry<String, String>> hostIt = hosts.entrySet().iterator();
123 while (hostIt.hasNext()) {
124
125 Map.Entry<String, String> hk = hostIt.next();
126 if (requestHost.endsWith(hk.getKey())) {
127 destination = hk.getValue();
128 break;
129 }
130 }
131 }
132
133 MappingResult r = new MappingResult();
134 r.setLevel(pattern.getLength());
135 r.setToURI(destination);
136 return r;
137 }
138 return null;
139 }
140
141 public String getFromURI() {
142 return this.fromURI;
143 }
144
145 public void setFromURI(String fromURI) {
146 this.fromURI = fromURI;
147 this.pattern = new SimpleUrlPattern(fromURI);
148 }
149
150 public String getToURI() {
151 return this.toURI;
152 }
153
154 public void setToURI(String toURI) {
155 this.toURI = toURI;
156 }
157
158 @Override
159 public String toString() {
160 return fromURI + " --> " + toURI;
161 }
162
163 }