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.sling.commons.adapter.AdaptTo;
34  import io.wcm.wcm.commons.util.RunMode;
35  
36  /**
37   * Enforce the generation of a full URL with protocol and hostname and non-secure mode.
38   */
39  @SuppressWarnings("java:S2160") // equals is implemented via AbstractUrlMode
40  public final class FullUrlForceNonSecureUrlMode extends AbstractUrlMode {
41  
42    private final boolean forcePublish;
43  
44    /**
45     * @param forcePublish Force to select publish URLs even on author instance
46     */
47    public FullUrlForceNonSecureUrlMode(boolean forcePublish) {
48      this.forcePublish = forcePublish;
49    }
50  
51    @Override
52    public @NotNull String getId() {
53      return "FULL_URL_FORCENONSECURE";
54    }
55  
56    @SuppressWarnings("deprecation")
57    @Override
58    public String getLinkUrlPrefix(@NotNull Adaptable adaptable, @NotNull Set<String> runModes,
59        @Nullable Page currentPage, @Nullable Page targetPage) {
60  
61      // if integrator template mode with placeholders is active return link url placeholder
62      IntegratorHandler integratorHandler = AdaptTo.notNull(adaptable, IntegratorHandler.class);
63      if (integratorHandler.isIntegratorTemplateMode()
64          && integratorHandler.getIntegratorMode().isUseUrlPlaceholders()) {
65        return IntegratorPlaceholder.URL_CONTENT;
66      }
67  
68      UrlConfig config = getUrlConfigForTarget(adaptable, targetPage);
69  
70      // in author mode return author site url
71      if (!forcePublish && RunMode.isAuthor(runModes) && config.hasSiteUrlAuthor()) {
72        return config.getSiteUrlAuthor();
73      }
74  
75      // return non-secure site url
76      return config.getSiteUrl();
77    }
78  
79    @SuppressWarnings("deprecation")
80    @Override
81    public String getResourceUrlPrefix(@NotNull Adaptable adaptable, @NotNull Set<String> runModes,
82        @Nullable Page currentPage, @Nullable Resource targetResource) {
83  
84      // if integrator template mode with placeholders is active return resource url placeholder
85      IntegratorHandler integratorHandler = AdaptTo.notNull(adaptable, IntegratorHandler.class);
86      if (integratorHandler.isIntegratorTemplateMode()
87          && integratorHandler.getIntegratorMode().isUseUrlPlaceholders()) {
88        return IntegratorPlaceholder.URL_CONTENT_PROXY;
89      }
90  
91      UrlConfig config = getUrlConfigForTarget(adaptable, targetResource);
92  
93      // in author mode return author site url
94      if (!forcePublish && RunMode.isAuthor(runModes) && config.hasSiteUrlAuthor()) {
95        return config.getSiteUrlAuthor();
96      }
97  
98      // return non-secure site url
99      return config.getSiteUrl();
100   }
101 
102 }