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.qa.glnm.aem;
21  
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  
25  import io.wcm.qa.glnm.exceptions.GaleniumException;
26  
27  /**
28   * Encapsulates logic to directly talk to components.
29   *
30   * @since 3.0.0
31   */
32  public class AemComponentUrlBuilder {
33  
34  
35    private static final String NO_CONTENT_PATH_CONFIGURED = "CONTENT_PATH_FROM_AEM__COMPONENT_URL_BUILDER";
36    private static final String NO_COMPONENT_NAME_CONFIGURED = "COMPONENT_NAME_FROM_AEM__COMPONENT_URL_BUILDER";
37  
38    private String protocol = "http";
39    private String host = "localhost";
40    private int port = 4502;
41    private String componentName = NO_COMPONENT_NAME_CONFIGURED;
42    private String contentPath = NO_CONTENT_PATH_CONFIGURED;
43    private boolean authorInstance = true;
44    private String extension = "html";
45  
46  
47    AemComponentUrlBuilder() {
48    }
49  
50    /**
51     * <p>build.</p>
52     *
53     * @return configured URL
54     */
55    public URL build() {
56      try {
57        return new URL(getProtocol(), getHost(), getPort(), getFile());
58      }
59      catch (MalformedURLException ex) {
60        StringBuilder stringBuilder = new StringBuilder()
61            .append("could not construct URL: [protocol: '")
62            .append(getProtocol())
63            .append("host: '")
64            .append(getHost())
65            .append("', port: ")
66            .append(getPort())
67            .append("file: '")
68            .append(getFile())
69            .append("']");
70        throw new GaleniumException(stringBuilder.toString(), ex);
71      }
72    }
73  
74    /**
75     * <p>Setter for the field <code>authorInstance</code>.</p>
76     *
77     * @param isAuthor whether SUT is an author instance
78     * @return this
79     */
80    public AemComponentUrlBuilder setAuthorInstance(boolean isAuthor) {
81      this.authorInstance = isAuthor;
82      return this;
83    }
84  
85    /**
86     * <p>Setter for the field <code>componentName</code>.</p>
87     *
88     * @param name of component to address
89     * @return this
90     */
91    public AemComponentUrlBuilder setComponentName(String name) {
92      this.componentName = name;
93      return this;
94    }
95  
96    /**
97     * <p>Setter for the field <code>contentPath</code>.</p>
98     *
99     * @param path to page
100    * @return this
101    */
102   public AemComponentUrlBuilder setContentPath(String path) {
103     this.contentPath = path;
104     return this;
105   }
106 
107   /**
108    * Default 'html'.
109    *
110    * @param renderFormat defines rendering format for component
111    * @return this
112    */
113   public AemComponentUrlBuilder setExtension(String renderFormat) {
114     this.extension = renderFormat;
115     return this;
116   }
117 
118   /**
119    * Default 'localhost'.
120    *
121    * @param hostName of AEM instance
122    * @return this
123    */
124   public AemComponentUrlBuilder setHost(String hostName) {
125     this.host = hostName;
126     return this;
127   }
128 
129   /**
130    * Default 4502.
131    *
132    * @param aemPort of AEM instance, set to -1 to omit port
133    * @return this
134    */
135   public AemComponentUrlBuilder setPort(int aemPort) {
136     this.port = aemPort;
137     return this;
138   }
139 
140   /**
141    * Default 'http'.
142    *
143    * @param networkProtocol to use for fetching
144    * @return this
145    */
146   public AemComponentUrlBuilder setProtocol(String networkProtocol) {
147     this.protocol = networkProtocol;
148     return this;
149   }
150 
151   private String getFile() {
152     StringBuilder builder = new StringBuilder()
153         .append(getContentPath())
154         .append("/jcr:content/")
155         .append(getComponentName())
156         .append(".")
157         .append(getExtension());
158     if (isAuthorInstance()) {
159       builder.append("?wcmmode=disabled");
160     }
161     return builder.toString();
162   }
163 
164   protected String getComponentName() {
165     return componentName;
166   }
167 
168   protected String getContentPath() {
169     return contentPath;
170   }
171 
172   protected String getExtension() {
173     return extension;
174   }
175 
176   protected String getHost() {
177     return host;
178   }
179 
180   protected int getPort() {
181     return port;
182   }
183 
184   protected String getProtocol() {
185     return protocol;
186   }
187 
188   protected boolean isAuthorInstance() {
189     return authorInstance;
190   }
191 
192 }