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.sling.api.adapter.Adaptable;
25  import org.apache.sling.api.resource.Resource;
26  import org.jetbrains.annotations.NotNull;
27  import org.jetbrains.annotations.Nullable;
28  
29  import com.day.cq.wcm.api.Page;
30  
31  import io.wcm.handler.url.integrator.IntegratorHandler;
32  import io.wcm.handler.url.integrator.IntegratorPlaceholder;
33  import io.wcm.handler.url.spi.UrlHandlerConfig;
34  import io.wcm.sling.commons.adapter.AdaptTo;
35  import io.wcm.wcm.commons.util.RunMode;
36  
37  /**
38   * Enforce the generation of a full URL with protocol and hostname.
39   * If the target is an internal content page, siteUrl or siteUrlSecure is chosen automatically depending on the secure
40   * state of page.
41   */
42  @SuppressWarnings("java:S2160") // equals is implemented via AbstractUrlMode
43  public final class FullUrlUrlMode extends AbstractUrlMode {
44  
45    private final boolean forcePublish;
46  
47    /**
48     * @param forcePublish Force to select publish URLs even on author instance
49     */
50    public FullUrlUrlMode(boolean forcePublish) {
51      this.forcePublish = forcePublish;
52    }
53  
54    @Override
55    public @NotNull String getId() {
56      return "FULL_URL";
57    }
58  
59    @SuppressWarnings("deprecation")
60    @Override
61    public String getLinkUrlPrefix(@NotNull Adaptable adaptable, @NotNull Set<String> runModes,
62        @Nullable Page currentPage, @Nullable Page targetPage) {
63      UrlHandlerConfig urlHandlerConfig = AdaptTo.notNull(adaptable, UrlHandlerConfig.class);
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        if (targetPage != null && urlHandlerConfig.isSecure(targetPage)) {
70          return IntegratorPlaceholder.URL_CONTENT_SECURE;
71        }
72        else {
73          return IntegratorPlaceholder.URL_CONTENT;
74        }
75      }
76  
77      UrlConfig config = getUrlConfigForTarget(adaptable, targetPage);
78  
79      // in author mode return author site url
80      if (!forcePublish && RunMode.isAuthor(runModes) && config.hasSiteUrlAuthor()) {
81        return config.getSiteUrlAuthor();
82      }
83  
84      // return secure or non-secure site url
85      if (targetPage != null && urlHandlerConfig.isSecure(targetPage)) {
86        return config.getSiteUrlSecure();
87      }
88      else {
89        return config.getSiteUrl();
90      }
91    }
92  
93    @SuppressWarnings("deprecation")
94    @Override
95    public String getResourceUrlPrefix(@NotNull Adaptable adaptable, @NotNull Set<String> runModes,
96        @Nullable Page currentPage, @Nullable Resource targetResource) {
97  
98      // if integrator template mode with placeholders is active return resource url placeholder
99      IntegratorHandler integratorHandler = AdaptTo.notNull(adaptable, IntegratorHandler.class);
100     if (integratorHandler.isIntegratorTemplateMode()
101         && integratorHandler.getIntegratorMode().isUseUrlPlaceholders()) {
102       return IntegratorPlaceholder.URL_CONTENT_PROXY;
103     }
104 
105     UrlConfig config = getUrlConfigForTarget(adaptable, targetResource);
106 
107     // in author mode return author site url
108     if (!forcePublish && RunMode.isAuthor(runModes) && config.hasSiteUrlAuthor()) {
109       return config.getSiteUrlAuthor();
110     }
111 
112     // return secure or non-secure site url
113     UrlHandlerConfig urlHandlerConfig = AdaptTo.notNull(adaptable, UrlHandlerConfig.class);
114     if ((currentPage != null && urlHandlerConfig.isSecure(currentPage))
115         || integratorHandler.isIntegratorTemplateSecureMode()) {
116       return config.getSiteUrlSecure();
117     }
118     else {
119       return config.getSiteUrl();
120     }
121   }
122 
123 }