1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.wcm.ui.granite.util;
21
22 import javax.servlet.http.HttpServletRequest;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.apache.commons.lang3.Strings;
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.api.resource.ResourceUtil;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 import org.osgi.annotation.versioning.ProviderType;
33
34 import com.adobe.granite.ui.components.Value;
35 import com.day.cq.wcm.api.Page;
36 import com.day.cq.wcm.api.PageManager;
37
38
39
40
41
42
43
44
45
46
47
48
49
50 @ProviderType
51 public final class GraniteUi {
52
53 static final String CREATEPAGEWITZARD_PROPERTIES_URI = "/mnt/overlay/wcm/core/content/sites/createpagewizard/properties.html";
54 static final String CREATEPAGEWITZARD_URI = "/mnt/overlay/wcm/core/content/sites/createpagewizard.html";
55 static final String HEADER_REFERER = "Referer";
56
57 private GraniteUi() {
58
59 }
60
61
62
63
64
65
66 public static @Nullable Resource getContentResource(@NotNull HttpServletRequest request) {
67 String contentPath = getContentPath(request);
68 if (contentPath != null) {
69 SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)request;
70 return slingRequest.getResourceResolver().getResource(contentPath);
71 }
72 return null;
73 }
74
75
76
77
78
79
80
81 public static @Nullable Resource getContentResourceOrParent(@NotNull HttpServletRequest request) {
82 String contentPath = getContentPath(request);
83 return getContentResourceOrParentFromPath((SlingHttpServletRequest)request, contentPath);
84 }
85
86
87
88
89
90
91
92 public static @Nullable Page getContentPage(@NotNull HttpServletRequest request) {
93 SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)request;
94 Resource contentResource = getContentResourceOrParent(request);
95 if (contentResource != null) {
96 PageManager pageManager = slingRequest.getResourceResolver().adaptTo(PageManager.class);
97 if (pageManager != null) {
98 return pageManager.getContainingPage(contentResource);
99 }
100 }
101 return null;
102 }
103
104
105
106
107
108
109
110 public static @Nullable String getExistingResourceType(@NotNull ResourceResolver resourceResolver, @NotNull String @NotNull... resourceTypes) {
111 for (String path : resourceTypes) {
112 if (resourceResolver.getResource(path) != null) {
113 return path;
114 }
115 }
116 return null;
117 }
118
119
120
121
122
123
124 @SuppressWarnings("deprecation")
125 private static @Nullable String getContentPath(@NotNull HttpServletRequest request) {
126
127 String contentPath = (String)request.getAttribute(Value.CONTENTPATH_ATTRIBUTE);
128
129
130
131 if (!isValidContentPath(contentPath) && StringUtils.contains(request.getRequestURI(), CREATEPAGEWITZARD_PROPERTIES_URI)) {
132 String referer = request.getHeader(HEADER_REFERER);
133 if (referer != null && StringUtils.contains(referer, CREATEPAGEWITZARD_URI)) {
134 contentPath = StringUtils.substringAfter(referer, CREATEPAGEWITZARD_URI);
135 }
136 }
137
138 if (!isValidContentPath(contentPath)) {
139
140
141 contentPath = ((SlingHttpServletRequest)request).getRequestPathInfo().getSuffix();
142 }
143
144 if (!isValidContentPath(contentPath)) {
145
146
147 contentPath = request.getParameter("item");
148 }
149
150 if (!isValidContentPath(contentPath)) {
151 contentPath = null;
152 }
153
154 return contentPath;
155 }
156
157 private static boolean isValidContentPath(@Nullable String path) {
158 if (path == null) {
159 return false;
160 }
161 return Strings.CS.startsWith(path, "/");
162 }
163
164 private static Resource getContentResourceOrParentFromPath(SlingHttpServletRequest slingRequest, String contentPath) {
165 if (StringUtils.isNotEmpty(contentPath)) {
166 Resource contentResource = slingRequest.getResourceResolver().getResource(contentPath);
167 if (contentResource != null) {
168 return contentResource;
169 }
170 else {
171 return getContentResourceOrParentFromPath(slingRequest, ResourceUtil.getParent(contentPath));
172 }
173 }
174 return null;
175 }
176
177 }