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.context.MgnlContext;
37 import info.magnolia.context.WebContext;
38
39 import java.util.ArrayList;
40 import java.util.Iterator;
41 import java.util.List;
42
43 import org.apache.commons.lang3.StringUtils;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 @Deprecated
84 public class HostBasedVirtualURIMapping extends DefaultVirtualURIMapping {
85
86 protected ArrayList<HostURIMapping> hosts;
87
88
89
90
91 public HostBasedVirtualURIMapping() {
92 hosts = new ArrayList<HostURIMapping>();
93 }
94
95
96 public List<HostURIMapping> getMappings() {
97 return null;
98 }
99
100
101
102
103
104
105 public void addMapping(HostURIMapping mapping) {
106 if (mapping.getHost() != null && mapping.getToURI() != null) {
107 synchronized (hosts) {
108 hosts.add(mapping);
109 }
110 }
111 }
112
113
114
115 @Deprecated
116 public List<String> getHosts() {
117 return null;
118 }
119
120
121
122
123
124
125
126 @Deprecated
127 public void addHost(String mapping) {
128 String[] hostToPath = StringUtils.split(mapping, "=");
129 if (hostToPath != null && hostToPath.length == 2) {
130 synchronized (hosts) {
131 hosts.add(new HostURIMapping(hostToPath[0], hostToPath[1]));
132 }
133 }
134 }
135
136 @Override
137 public MappingResult mapURI(String uri) {
138
139 MappingResult result = super.mapURI(uri);
140
141 if (result != null) {
142 String destination = toURI;
143 String hostMapping = this.tryToMapHost();
144 if (hostMapping != null) {
145 destination = hostMapping;
146 }
147 result.setToURI(destination);
148 return result;
149 }
150
151 return null;
152 }
153
154
155
156
157
158
159 protected String tryToMapHost() {
160 final int NO_MATCHED_MAPPINGS = -1;
161 int bestMatchValue = NO_MATCHED_MAPPINGS;
162 String toURI = null;
163
164 if (hosts != null && !hosts.isEmpty()) {
165 String requestHost = ((WebContext) MgnlContext.getInstance()).getRequest().getServerName();
166
167 Iterator<HostURIMapping> hostIt = hosts.iterator();
168
169 while (hostIt.hasNext()) {
170 HostURIMapping hk = hostIt.next();
171 if (requestHost.endsWith(hk.getHost())) {
172 int matchValue = (requestHost.length() - hk.getHost().length());
173 if (bestMatchValue == NO_MATCHED_MAPPINGS || matchValue < bestMatchValue) {
174 bestMatchValue = matchValue;
175 toURI = hk.getToURI();
176 }
177 }
178 }
179 }
180 return toURI;
181 }
182
183 @Override
184 public String toString() {
185 return "[" + super.toString() + "[hosts:" + hosts.toString() + "]]";
186 }
187
188 @Override
189 public boolean isValid() {
190 if (this.getClass().equals(HostBasedVirtualURIMapping.class)) {
191 return StringUtils.isNotEmpty(fromURI) && StringUtils.isNotEmpty(toURI);
192 }
193
194 return true;
195 }
196
197
198
199
200 public static class HostURIMapping {
201
202
203 public HostURIMapping() {
204 }
205
206 public HostURIMapping(String host, String toURI) {
207 this.host = host;
208 this.toURI = toURI;
209 }
210
211 private String host;
212
213 private String toURI;
214
215 public String getHost() {
216 return this.host;
217 }
218
219 public void setHost(String host) {
220 this.host = host;
221 }
222
223 public String getToURI() {
224 return this.toURI;
225 }
226
227 public void setToURI(String toURI) {
228 this.toURI = toURI;
229 }
230
231 @Override
232 public String toString() {
233 return getHost() + "=" + getToURI();
234
235 }
236 }
237
238 }