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;
21  
22  import java.util.HashMap;
23  
24  import org.apache.commons.lang3.builder.ToStringBuilder;
25  import org.apache.sling.api.resource.Resource;
26  import org.apache.sling.api.resource.ValueMap;
27  import org.apache.sling.api.wrappers.ValueMapDecorator;
28  import org.jetbrains.annotations.NotNull;
29  import org.jetbrains.annotations.Nullable;
30  import org.osgi.annotation.versioning.ProviderType;
31  
32  import com.day.cq.wcm.api.Page;
33  
34  import io.wcm.wcm.commons.util.ToStringStyle;
35  
36  /**
37   * Holds all properties that are part of a link handling request.
38   */
39  @ProviderType
40  public final class LinkRequest {
41  
42    private final Resource resource;
43    private final Page page;
44    private final String reference;
45    private final LinkArgs linkArgs;
46  
47    private ValueMap resourceProperties;
48  
49    /**
50     * @param resource Resource containing properties that define the link target
51     * @param page Target content page
52     * @param linkArgs Link arguments
53     */
54    public LinkRequest(@Nullable Resource resource, @Nullable Page page, @Nullable LinkArgs linkArgs) {
55      this(resource, page, null, linkArgs);
56    }
57  
58    /**
59     * @param resource Resource containing properties that define the link target
60     * @param page Target content page
61     * @param reference Link reference (internal or external).
62     * @param linkArgs Link arguments
63     */
64    public LinkRequest(@Nullable Resource resource, @Nullable Page page, @Nullable String reference, @Nullable LinkArgs linkArgs) {
65      this.resource = resource;
66      this.page = page;
67      this.reference = reference;
68      this.linkArgs = linkArgs != null ? linkArgs : new LinkArgs();
69  
70      // validate parameters
71      int linkParamCount = (resource != null ? 1 : 0)
72          + (page != null ? 1 : 0)
73          + (reference != null ? 1 : 0);
74      if (linkParamCount > 1) {
75        throw new IllegalArgumentException("Set only one of resource, page, or reference.");
76      }
77    }
78  
79    /**
80     * @return Resource containing properties that define the link target
81     */
82    public @Nullable Resource getResource() {
83      return this.resource;
84    }
85  
86    /**
87     * @return Target content page
88     */
89    public @Nullable Page getPage() {
90      return this.page;
91    }
92  
93    /**
94     * @return Link reference (internal or external).
95     */
96    public @Nullable String getReference() {
97      return this.reference;
98    }
99  
100   /**
101    * @return Link arguments
102    */
103   public @NotNull LinkArgs getLinkArgs() {
104     return this.linkArgs;
105   }
106 
107   /**
108    * @return Properties from resource containing target link. The value map is a copy
109    *         of the original map so it is safe to change the property values contained in the map.
110    */
111   public @NotNull ValueMap getResourceProperties() {
112     if (this.resourceProperties == null) {
113       // create a copy of the original map
114       this.resourceProperties = new ValueMapDecorator(new HashMap<>());
115       if (this.resource != null) {
116         this.resourceProperties.putAll(resource.getValueMap());
117       }
118     }
119     return this.resourceProperties;
120   }
121 
122   @Override
123   public String toString() {
124     return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_OMIT_NULL_STYLE);
125   }
126 
127 }