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.virtualuri.mapping;
35
36 import info.magnolia.context.MgnlContext;
37 import info.magnolia.context.WebContext;
38
39 import java.net.URI;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.Comparator;
43 import java.util.List;
44 import java.util.Optional;
45
46 import org.apache.commons.lang3.StringUtils;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public class HostBasedVirtualUriMapping extends DefaultVirtualUriMapping {
69 private static final Logger log = LoggerFactory.getLogger(HostBasedVirtualUriMapping.class);
70
71 private List<HostUriMapping> hostMappings = new ArrayList<>();
72
73 public List<HostUriMapping> getMappings() {
74 return Collections.unmodifiableList(hostMappings);
75 }
76
77 public void setMappings(List<HostUriMapping> hostMappings) {
78 this.hostMappings = hostMappings;
79 }
80
81
82
83
84
85
86 @Override
87 public Optional<Result> mapUri(URI uri) {
88 Optional<Result> fallbackResult = super.mapUri(uri);
89 if (!fallbackResult.isPresent()) {
90
91 return Optional.empty();
92 }
93
94 Optional<String> hostResult = tryToMapHost();
95 if (!hostResult.isPresent()) {
96 return fallbackResult;
97 }
98
99 return Optional.of(new Result(hostResult.get(), getPattern().getLength(), this));
100 }
101
102 @Override
103 public boolean isValid() {
104 return StringUtils.isNotEmpty(getFromUri()) && hostMappings.stream()
105 .anyMatch(HostUriMapping::isValid);
106 }
107
108
109
110
111 protected Optional<String> tryToMapHost() {
112 if (hostMappings == null || hostMappings.isEmpty()) {
113 log.warn("No configured hosts to evaluate");
114 return Optional.empty();
115 }
116
117
118
119 String requestHost = ((WebContext) MgnlContext.getInstance()).getRequest().getServerName();
120 Comparator<HostUriMapping> hostLengthComparator = (hm1, hm2) -> Integer.compare(hm1.getHost().length(), hm2.getHost().length());
121
122 return hostMappings.stream()
123 .filter(hostMapping -> hostMapping.isValid() && requestHost.endsWith(hostMapping.getHost()))
124 .max(hostLengthComparator)
125 .map(HostUriMapping::getToUri);
126 }
127
128 @Override
129 public String toString() {
130 return "[" + super.toString() + "[hosts:" + hostMappings.toString() + "]]";
131 }
132
133
134
135
136 public static class HostUriMapping {
137 private String host;
138 private String toUri;
139
140 public String getHost() {
141 return host;
142 }
143
144 public void setHost(String host) {
145 this.host = host;
146 }
147
148 public String getToUri() {
149 return toUri;
150 }
151
152 public void setToUri(String toUri) {
153 this.toUri = toUri;
154 }
155
156 public boolean isValid() {
157 return StringUtils.isNotEmpty(host) && StringUtils.isNotEmpty(toUri);
158 }
159
160 @Override
161 public String toString() {
162 return String.format("[%s --> %s]", host, toUri);
163 }
164 }
165 }