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.Set;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.sling.api.adapter.Adaptable;
26  import org.apache.sling.api.resource.Resource;
27  import org.jetbrains.annotations.NotNull;
28  import org.jetbrains.annotations.Nullable;
29  
30  import com.day.cq.wcm.api.Page;
31  
32  import io.wcm.handler.url.integrator.IntegratorHandler;
33  import io.wcm.handler.url.integrator.IntegratorPlaceholder;
34  import io.wcm.handler.url.spi.UrlHandlerConfig;
35  import io.wcm.sling.commons.adapter.AdaptTo;
36  import io.wcm.wcm.commons.util.RunMode;
37  
38  /**
39   * Enforce the generation of a full URL with hostname and "//" as protocol (protocol-relative mode).
40   * Using "//" instead of "http://" or "https://" results in using the same protocol as the current request
41   * in the browser.
42   */
43  @SuppressWarnings("java:S2160") // equals is implemented via AbstractUrlMode
44  public final class FullUrlProtocolRelativeUrlMode extends AbstractUrlMode {
45  
46    private final boolean forcePublish;
47  
48    /**
49     * @param forcePublish Force to select publish URLs even on author instance
50     */
51    public FullUrlProtocolRelativeUrlMode(boolean forcePublish) {
52      this.forcePublish = forcePublish;
53    }
54  
55    @Override
56    public @NotNull String getId() {
57      return "FULL_URL_PROTOCOLRELATIVE";
58    }
59  
60    @SuppressWarnings("deprecation")
61    @Override
62    public String getLinkUrlPrefix(@NotNull Adaptable adaptable, @NotNull Set<String> runModes,
63        @Nullable Page currentPage, @Nullable Page targetPage) {
64  
65      // if integrator template mode with placeholders is active return link url placeholder
66      IntegratorHandler integratorHandler = AdaptTo.notNull(adaptable, IntegratorHandler.class);
67      if (integratorHandler.isIntegratorTemplateMode()
68          && integratorHandler.getIntegratorMode().isUseUrlPlaceholders()) {
69        UrlHandlerConfig urlHandlerConfig = AdaptTo.notNull(adaptable, UrlHandlerConfig.class);
70        if (targetPage != null && urlHandlerConfig.isSecure(targetPage)) {
71          return IntegratorPlaceholder.URL_CONTENT_SECURE;
72        }
73        else {
74          return IntegratorPlaceholder.URL_CONTENT;
75        }
76      }
77  
78      UrlConfig config = getUrlConfigForTarget(adaptable, targetPage);
79  
80      // in author mode return author site url
81      if (!forcePublish && RunMode.isAuthor(runModes) && config.hasSiteUrlAuthor()) {
82        return config.getSiteUrlAuthor();
83      }
84  
85      // return siteUrl in protocol-relative mode
86      return convertToProtocolRelative(config.getSiteUrl());
87    }
88  
89    @SuppressWarnings("deprecation")
90    @Override
91    public String getResourceUrlPrefix(@NotNull Adaptable adaptable, @NotNull Set<String> runModes,
92        @Nullable Page currentPage, @Nullable Resource targetResource) {
93  
94      // if integrator template mode with placeholders is active return resource url placeholder
95      IntegratorHandler integratorHandler = AdaptTo.notNull(adaptable, IntegratorHandler.class);
96      if (integratorHandler.isIntegratorTemplateMode()
97          && integratorHandler.getIntegratorMode().isUseUrlPlaceholders()) {
98        return IntegratorPlaceholder.URL_CONTENT_PROXY;
99      }
100 
101     UrlConfig config = getUrlConfigForTarget(adaptable, targetResource);
102 
103     // in author mode return author site url
104     if (!forcePublish && RunMode.isAuthor(runModes) && config.hasSiteUrlAuthor()) {
105       return config.getSiteUrlAuthor();
106     }
107 
108     // return secure or non-secure site url
109     return convertToProtocolRelative(config.getSiteUrl());
110   }
111 
112   /**
113    * Strips of protocol from given URL (if any protocol is included)
114    * @param pPrefix Prefix with protocol
115    * @return Prefix without protocol (protocol-relative mode)
116    */
117   private String convertToProtocolRelative(String pPrefix) {
118     if (StringUtils.isEmpty(pPrefix)) {
119       return null;
120     }
121     int index = StringUtils.indexOf(pPrefix, "://");
122     if (index >= 0) {
123       return pPrefix.substring(index + 1);
124     }
125     else {
126       return pPrefix;
127     }
128   }
129 
130 }