View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
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   * Driver configuration parameters.
32   * 
33   * @author Francois-Xavier Bonnet
34   * @author Nicolas Richeton
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       * Read the "Mappings" parameter and create the corresponding UriMapping rules.
60       * 
61       * @param props
62       * @return The mapping rules for this driver instance.
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      * Get the URI mappings for this driver instance.
124      * 
125      * @return The URI mappings for this driver instance.
126      */
127     public List<UriMapping> getUriMappings() {
128         return this.uriMappings;
129     }
130 
131     /**
132      * 
133      * @return stripMappingPath
134      */
135     public boolean isStripMappingPath() {
136         return stripMappingPath;
137     }
138 }