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.handler.url.impl.modes;
21  
22  import java.util.Objects;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.sling.api.adapter.Adaptable;
26  
27  import io.wcm.handler.url.SiteConfig;
28  
29  /**
30   * Helper class for accessing site URL configuration in URL mode implementation.
31   */
32  class UrlConfig {
33  
34    private final String siteUrl;
35    private final String siteUrlSecure;
36    private final String siteUrlAuthor;
37  
38    UrlConfig(Adaptable adaptable) {
39      SiteConfig config = adaptable.adaptTo(SiteConfig.class);
40      if (config != null) {
41        this.siteUrl = config.siteUrl();
42        this.siteUrlSecure = Objects.toString(config.siteUrlSecure(), this.siteUrl);
43        this.siteUrlAuthor = config.siteUrlAuthor();
44      }
45      else {
46        this.siteUrl = null;
47        this.siteUrlSecure = null;
48        this.siteUrlAuthor = null;
49      }
50    }
51  
52    /**
53     * @return Site URL
54     */
55    public String getSiteUrl() {
56      return this.siteUrl;
57    }
58  
59    /**
60     * @return Site URL secure (fallback to Site URL)
61     */
62    public String getSiteUrlSecure() {
63      return this.siteUrlSecure;
64    }
65  
66    /**
67     * @return Site URL author
68     */
69    public String getSiteUrlAuthor() {
70      return this.siteUrlAuthor;
71    }
72  
73    /**
74     * @return true if at least Site URL is set
75     */
76    public boolean isValid() {
77      return StringUtils.isNotEmpty(this.siteUrl);
78    }
79  
80    /**
81     * @return true if site URL for author is set
82     */
83    public boolean hasSiteUrlAuthor() {
84      return StringUtils.isNotEmpty(this.siteUrlAuthor);
85    }
86  
87  }