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 public class HostBasedVirtualURIMapping extends DefaultVirtualURIMapping {
84
85 protected ArrayList<HostURIMapping> hosts;
86
87
88
89
90 public HostBasedVirtualURIMapping() {
91 hosts = new ArrayList<HostURIMapping>();
92 }
93
94
95 public List<HostURIMapping> getMappings() {
96 return null;
97 }
98
99
100
101
102
103
104 public void addMapping(HostURIMapping mapping) {
105 if (mapping.getHost() != null && mapping.getToURI() != null) {
106 synchronized (hosts) {
107 hosts.add(mapping);
108 }
109 }
110 }
111
112
113
114 @Deprecated
115 public List<String> getHosts() {
116 return null;
117 }
118
119
120
121
122
123
124
125 @Deprecated
126 public void addHost(String mapping) {
127 String[] hostToPath = StringUtils.split(mapping, "=");
128 if (hostToPath != null && hostToPath.length == 2) {
129 synchronized (hosts) {
130 hosts.add(new HostURIMapping(hostToPath[0], hostToPath[1]));
131 }
132 }
133 }
134
135 @Override
136 public MappingResult mapURI(String uri) {
137
138 String destination = toURI;
139
140 if (pattern != null && pattern.match(uri)) {
141 String hostMapping = this.tryToMapHost();
142 if (hostMapping != null) {
143 destination = hostMapping;
144 }
145
146 MappingResult r = new MappingResult();
147 r.setLevel(pattern.getLength());
148 r.setToURI(destination);
149 return r;
150 }
151 return null;
152 }
153
154
155
156
157
158 protected String tryToMapHost() {
159 final int NO_MATCHED_MAPPINGS = -1;
160 int bestMatchValue = NO_MATCHED_MAPPINGS;
161 String toURI = null;
162
163 if (hosts != null && !hosts.isEmpty()) {
164 String requestHost = ((WebContext) MgnlContext.getInstance()).getRequest().getServerName();
165
166 Iterator<HostURIMapping> hostIt = hosts.iterator();
167
168 while (hostIt.hasNext()) {
169 HostURIMapping hk = hostIt.next();
170 if (requestHost.endsWith(hk.getHost())) {
171 int matchValue = (requestHost.length() - hk.getHost().length());
172 if (bestMatchValue==NO_MATCHED_MAPPINGS || matchValue < bestMatchValue){
173 bestMatchValue = matchValue;
174 toURI = hk.getToURI();
175 }
176 }
177 }
178 }
179 return toURI;
180 }
181
182 @Override
183 public String toString() {
184 return "[" + super.toString() + "[hosts:" + hosts.toString() + "]]";
185 }
186
187
188
189
190 public static class HostURIMapping{
191
192
193 public HostURIMapping(){
194 }
195
196 public HostURIMapping(String host, String toURI){
197 this.host = host;
198 this.toURI = toURI;
199 }
200
201 private String host;
202
203 private String toURI;
204
205 public String getHost() {
206 return this.host;
207 }
208
209 public void setHost(String host) {
210 this.host = host;
211 }
212
213 public String getToURI() {
214 return this.toURI;
215 }
216
217 public void setToURI(String toURI) {
218 this.toURI = toURI;
219 }
220
221 @Override
222 public String toString(){
223 return getHost() + "=" + getToURI();
224
225 }
226 }
227
228 }