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.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  import io.wcm.handler.url.UrlHandler;
43  
44  /**
45   * Default implementation of {@link io.wcm.handler.link.spi.LinkType} for internal links.
46   * Internal links are links to content pages inside the CMS.
47   * <p>
48   * This link type ensures all links target only pages inside the same inner-most configuration scope, which is usually
49   * the same site/language. All link paths referencing pages outside this content subtree are rewritten via
50   * {@link UrlHandler#rewritePathToContext(Resource)} with the root path of the inner-most configuration scope/site and
51   * then resolved.
52   * </p>
53   */
54  @Model(adaptables = {
55      SlingHttpServletRequest.class, Resource.class
56  })
57  @ProviderType
58  public final class InternalLinkType extends LinkType {
59  
60    /**
61     * Link type ID
62     */
63    public static final @NotNull String ID = "internal";
64  
65    private final @NotNull InternalLinkResolverOptions resolverOptions = new InternalLinkResolverOptions()
66        .primaryLinkRefProperty(getPrimaryLinkRefProperty())
67        .rewritePathToContext(true)
68        .useTargetContext(false);
69  
70    @Self
71    private InternalLinkResolver internalLinkResolver;
72  
73    /**
74     * @return Link type ID (is stored as identifier in repository)
75     */
76    @Override
77    public @NotNull String getId() {
78      return ID;
79    }
80  
81    @Override
82    public @NotNull String getLabel() {
83      return "Internal (same site)";
84    }
85  
86    @Override
87    public String getPrimaryLinkRefProperty() {
88      return LinkNameConstants.PN_LINK_CONTENT_REF;
89    }
90  
91    @Override
92    public @Nullable String getEditComponentResourceType() {
93      return "wcm-io/handler/link/components/granite/form/linktype/internal";
94    }
95  
96    @Override
97    public boolean hasRichTextPlugin() {
98      return true;
99    }
100 
101   @Override
102   public boolean accepts(@NotNull String linkRef) {
103     // accept as internal link if the ref starts with "/content/"
104     return StringUtils.startsWith(linkRef, "/content/")
105         && !MediaLinkType.isDefaultMediaContentPath(linkRef);
106   }
107 
108   @Override
109   public boolean accepts(@NotNull LinkRequest linkRequest) {
110     if (internalLinkResolver.acceptPage(linkRequest.getPage(), resolverOptions)) {
111       // support direct links to pages
112       return true;
113     }
114     // check for matching link type ID in link resource
115     return super.accepts(linkRequest);
116   }
117 
118   @Override
119   public @NotNull Link resolveLink(@NotNull Link link) {
120     return internalLinkResolver.resolveLink(link, resolverOptions);
121   }
122 
123   /**
124    * Get synthetic link resource for this link type.
125    * @param resourceResolver Resource resolver
126    * @param path Resource path. Can be a non-existing path, but the path should be located somewhere within the
127    *          applications content paths to make sure the handler configuration looked up via context-aware services
128    *          is the expected one.
129    * @param pageRef Path to target page
130    * @return Synthetic link resource
131    */
132   public static @NotNull Resource getSyntheticLinkResource(@NotNull ResourceResolver resourceResolver,
133       @NotNull String path, @NotNull String pageRef) {
134     Map<String, Object> map = new HashMap<>();
135     map.put(LinkNameConstants.PN_LINK_TYPE, ID);
136     map.put(LinkNameConstants.PN_LINK_CONTENT_REF, pageRef);
137     return new SyntheticLinkResource(resourceResolver, path, map);
138   }
139 
140   @Override
141   public String toString() {
142     return ID;
143   }
144 
145 }