View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2019 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.link.type;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.sling.api.SlingHttpServletRequest;
27  import org.apache.sling.api.resource.Resource;
28  import org.apache.sling.api.resource.ResourceResolver;
29  import org.apache.sling.models.annotations.Model;
30  import org.apache.sling.models.annotations.injectorspecific.Self;
31  import org.jetbrains.annotations.NotNull;
32  import org.jetbrains.annotations.Nullable;
33  import org.osgi.annotation.versioning.ProviderType;
34  
35  import io.wcm.handler.link.Link;
36  import io.wcm.handler.link.LinkNameConstants;
37  import io.wcm.handler.link.LinkRequest;
38  import io.wcm.handler.link.SyntheticLinkResource;
39  import io.wcm.handler.link.spi.LinkType;
40  import io.wcm.handler.link.type.helpers.InternalLinkResolver;
41  import io.wcm.handler.link.type.helpers.InternalLinkResolverOptions;
42  
43  /**
44   * Implementation of {@link io.wcm.handler.link.spi.LinkType} for internal links with supports
45   * links between different sites or configuration context paths.
46   * Internal links are links to content pages inside the CMS.
47   * <p>
48   * This link type ensures that links that are referenced from other sites/configuration contexts are resolved
49   * using the URL handler configuration of the target context, e.g. with the Site URL from the other site.
50   * </p>
51   */
52  @Model(adaptables = {
53      SlingHttpServletRequest.class, Resource.class
54  })
55  @ProviderType
56  public final class InternalCrossContextLinkType extends LinkType {
57  
58    /**
59     * Link type ID
60     */
61    public static final @NotNull String ID = "internalCrossContext";
62  
63    private final @NotNull InternalLinkResolverOptions resolverOptions = new InternalLinkResolverOptions()
64        .primaryLinkRefProperty(getPrimaryLinkRefProperty())
65        .rewritePathToContext(false)
66        .useTargetContext(true);
67  
68    @Self
69    private InternalLinkResolver internalLinkResolver;
70  
71    /**
72     * @return Link type ID (is stored as identifier in repository)
73     */
74    @Override
75    public @NotNull String getId() {
76      return ID;
77    }
78  
79    @Override
80    public @NotNull String getLabel() {
81      return "Internal (other site)";
82    }
83  
84    @Override
85    public String getPrimaryLinkRefProperty() {
86      return LinkNameConstants.PN_LINK_CROSSCONTEXT_CONTENT_REF;
87    }
88  
89    @Override
90    public @Nullable String getEditComponentResourceType() {
91      return "wcm-io/handler/link/components/granite/form/linktype/internalCrossContext";
92    }
93  
94    @Override
95    public boolean hasRichTextPlugin() {
96      return true;
97    }
98  
99    @Override
100   public boolean accepts(@NotNull String linkRef) {
101     // accept as internal link if the ref starts with "/content/"
102     return StringUtils.startsWith(linkRef, "/content/")
103         && !MediaLinkType.isDefaultMediaContentPath(linkRef);
104   }
105 
106   @Override
107   public boolean accepts(@NotNull LinkRequest linkRequest) {
108     if (internalLinkResolver.acceptPage(linkRequest.getPage(), resolverOptions)) {
109       // support direct links to pages
110       return true;
111     }
112     // check for matching link type ID in link resource
113     return super.accepts(linkRequest);
114   }
115 
116   @Override
117   public @NotNull Link resolveLink(@NotNull Link link) {
118     return internalLinkResolver.resolveLink(link, resolverOptions);
119   }
120 
121   /**
122    * Get synthetic link resource for this link type.
123    * @param resourceResolver Resource resolver
124    * @param path Resource path. Can be a non-existing path, but the path should be located somewhere within the
125    *          applications content paths to make sure the handler configuration looked up via context-aware services
126    *          is the expected one.
127    * @param pageRef Path to target page
128    * @return Synthetic link resource
129    */
130   public static @NotNull Resource getSyntheticLinkResource(@NotNull ResourceResolver resourceResolver,
131       @NotNull String path, @NotNull String pageRef) {
132     Map<String, Object> map = new HashMap<>();
133     map.put(LinkNameConstants.PN_LINK_TYPE, ID);
134     map.put(LinkNameConstants.PN_LINK_CROSSCONTEXT_CONTENT_REF, pageRef);
135     return new SyntheticLinkResource(resourceResolver, path, map);
136   }
137 
138   @Override
139   public String toString() {
140     return ID;
141   }
142 
143 }