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