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 org.apache.sling.api.adapter.Adaptable;
23 import org.apache.sling.api.resource.Resource;
24
25 import com.day.cq.wcm.api.Page;
26
27 import io.wcm.handler.url.UrlMode;
28
29 abstract class AbstractUrlMode implements UrlMode {
30
31 @Override
32 public int hashCode() {
33 return getId().hashCode();
34 }
35
36 @Override
37 public boolean equals(Object obj) {
38 if (obj == null || obj.getClass() != getClass()) {
39 return false;
40 }
41 return getId().equals(((UrlMode)obj).getId());
42 }
43
44 @Override
45 public String toString() {
46 return getId();
47 }
48
49 /**
50 * Get URL configuration for target page. If this is invalid or not available, get it from adaptable.
51 * @param adaptable Adaptable (request or resource)
52 * @param targetPage Target page (may be null)
53 * @return Url config (never null)
54 */
55 protected UrlConfig getUrlConfigForTarget(Adaptable adaptable, Page targetPage) {
56 Resource targetResource = null;
57 if (targetPage != null) {
58 targetResource = targetPage.adaptTo(Resource.class);
59 }
60 return getUrlConfigForTarget(adaptable, targetResource);
61 }
62
63 /**
64 * Get URL configuration for target resource. If this is invalid or not available, get it from adaptable.
65 * @param adaptable Adaptable (request or resource)
66 * @param targetResource Target resource (may be null)
67 * @return Url config (never null)
68 */
69 protected UrlConfig getUrlConfigForTarget(Adaptable adaptable, Resource targetResource) {
70 UrlConfig config = null;
71 if (targetResource != null) {
72 config = new UrlConfig(targetResource);
73 }
74 if (config == null || !config.isValid()) {
75 config = new UrlConfig(adaptable);
76 }
77 return config;
78 }
79
80 }