1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.esigate;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Properties;
22
23 import org.esigate.api.BaseUrlRetrieveStrategy;
24 import org.esigate.impl.UriMapping;
25 import org.esigate.url.IpHashBaseUrlRetrieveStrategy;
26 import org.esigate.url.RoundRobinBaseUrlRetrieveStrategy;
27 import org.esigate.url.SingleBaseUrlRetrieveStrategy;
28 import org.esigate.url.StickySessionBaseUrlRetrieveStrategy;
29
30
31
32
33
34
35
36 public class DriverConfiguration {
37
38 private final String instanceName;
39 private final String uriEncoding;
40 private final String visibleBaseURL;
41 private final Properties properties;
42 private final boolean preserveHost;
43 private final BaseUrlRetrieveStrategy baseUrlRetrieveStrategy;
44 private final List<UriMapping> uriMappings;
45 private boolean stripMappingPath;
46
47 public DriverConfiguration(String instanceName, Properties props) {
48 this.instanceName = instanceName;
49 this.baseUrlRetrieveStrategy = getBaseUrlRetrieveSession(props);
50 this.uriEncoding = Parameters.URI_ENCODING.getValue(props);
51 this.preserveHost = Parameters.PRESERVE_HOST.getValue(props);
52 this.visibleBaseURL = Parameters.VISIBLE_URL_BASE.getValue(props);
53 this.stripMappingPath = Parameters.STRIP_MAPPING_PATH.getValue(props);
54 this.uriMappings = parseMappings(props);
55 properties = props;
56 }
57
58
59
60
61
62
63
64 private static List<UriMapping> parseMappings(Properties props) {
65 List<UriMapping> mappings = new ArrayList<>();
66
67 Collection<String> mappingsParam = Parameters.MAPPINGS.getValue(props);
68 for (String mappingParam : mappingsParam) {
69 mappings.add(UriMapping.create(mappingParam));
70 }
71
72 return mappings;
73 }
74
75 private BaseUrlRetrieveStrategy getBaseUrlRetrieveSession(Properties props) {
76 BaseUrlRetrieveStrategy urlStrategy;
77 String[] baseURLs = Parameters.REMOTE_URL_BASE.getValue(props);
78 if (baseURLs.length == 0) {
79 throw new ConfigurationException(Parameters.REMOTE_URL_BASE.getName()
80 + " property cannot be empty for instance '" + instanceName + "'");
81 } else if (baseURLs.length == 1) {
82 urlStrategy = new SingleBaseUrlRetrieveStrategy(baseURLs[0]);
83 } else {
84 String strategy = Parameters.REMOTE_URL_BASE_STRATEGY.getValue(props);
85 if (Parameters.ROUNDROBIN.equalsIgnoreCase(strategy)) {
86 urlStrategy = new RoundRobinBaseUrlRetrieveStrategy(baseURLs);
87 } else if (Parameters.IPHASH.equalsIgnoreCase(strategy)) {
88 urlStrategy = new IpHashBaseUrlRetrieveStrategy(baseURLs);
89 } else if (Parameters.STICKYSESSION.equalsIgnoreCase(strategy)) {
90 urlStrategy = new StickySessionBaseUrlRetrieveStrategy(baseURLs);
91 } else {
92 throw new ConfigurationException("No such BaseUrlRetrieveStrategy '" + strategy + "'");
93 }
94 }
95 return urlStrategy;
96 }
97
98 public String getVisibleBaseURL() {
99 return visibleBaseURL;
100 }
101
102 public boolean isPreserveHost() {
103 return preserveHost;
104 }
105
106 public String getInstanceName() {
107 return instanceName;
108 }
109
110 public String getUriEncoding() {
111 return uriEncoding;
112 }
113
114 public Properties getProperties() {
115 return properties;
116 }
117
118 public BaseUrlRetrieveStrategy getBaseUrlRetrieveStrategy() {
119 return baseUrlRetrieveStrategy;
120 }
121
122
123
124
125
126
127 public List<UriMapping> getUriMappings() {
128 return this.uriMappings;
129 }
130
131
132
133
134
135 public boolean isStripMappingPath() {
136 return stripMappingPath;
137 }
138 }