View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.caravan.io.http.impl;
21  
22  import static io.wcm.caravan.io.http.impl.CaravanHttpServiceConfig.RIBBON_HOSTS_PROPERTY;
23  import static io.wcm.caravan.io.http.impl.CaravanHttpServiceConfig.RIBBON_PARAM_LISTOFSERVERS;
24  
25  import java.util.Map;
26  
27  import org.apache.commons.configuration.Configuration;
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.sling.commons.osgi.PropertiesUtil;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * Validates http service configuration for service IDs.
35   */
36  public final class CaravanHttpServiceConfigValidator {
37  
38    private static final Logger log = LoggerFactory.getLogger(CaravanHttpServiceConfigValidator.class);
39  
40    private CaravanHttpServiceConfigValidator() {
41      // static methods only
42    }
43  
44    /**
45     * Checks if a valid configuration exists for the given service ID. This does not mean that the host
46     * name is correct or returns correct responses, it only checks that the minimum required configuration
47     * properties are set to a value.
48     * @param serviceId Service ID
49     * @return true if configuration is valid
50     */
51    public static boolean hasValidConfiguration(String serviceId) {
52      Configuration archaiusConfig = ArchaiusConfig.getConfiguration();
53      return StringUtils.isNotEmpty(archaiusConfig.getString(serviceId + RIBBON_PARAM_LISTOFSERVERS));
54    }
55  
56    /**
57     * Validates service configuration when reading the configuration.
58     * @param serviceId Service ID
59     * @param config OSGi config
60     */
61    public static boolean isValidServiceConfig(String serviceId, Map<String, Object> config) {
62      if (StringUtils.isBlank(serviceId)) {
63        log.warn("Invalid transport layer service configuration without service ID, ignoring.", serviceId);
64        return false;
65      }
66      String[] hosts = PropertiesUtil.toStringArray(config.get(RIBBON_HOSTS_PROPERTY));
67      if (hosts == null || hosts.length == 0) {
68        log.warn("Invalid transport layer service configuration for '{}' without hosts, ignoring.", serviceId);
69        return false;
70      }
71      return true;
72    }
73  
74    /**
75     * get configuration for "THROW_EXCEPTION_FOR_STATUS_500"
76     * @param serviceId
77     * @return Configured value
78     */
79    public static boolean throwExceptionForStatus500(String serviceId) {
80      return ArchaiusConfig.getConfiguration().getBoolean(serviceId + CaravanHttpServiceConfig.THROW_EXCEPTION_FOR_STATUS_500,
81          CaravanHttpServiceConfig.THROW_EXCEPTION_FOR_STATUS_500_DEFAULT);
82    }
83  
84  }