LinkCheckerSettings.java
6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.RepositoryException
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.rewriter.linkchecker;
import com.day.cq.rewriter.linkchecker.LinkRewriteConfig;
import java.net.URI;
import java.net.URISyntaxException;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LinkCheckerSettings {
private static final Logger log = LoggerFactory.getLogger(LinkCheckerSettings.class);
public static final String REQUEST_ATTRIBUTE_NAME = LinkCheckerSettings.class.getName();
private SlingHttpServletRequest request;
private LinkRewriteConfig invalidConfig;
private LinkRewriteConfig expiredConfig;
private LinkRewriteConfig predatedConfig;
private String baseRef;
private boolean ignoreInternals;
private boolean ignoreExternals;
private URI contextUri;
private URI baseUri;
public static LinkCheckerSettings fromRequest(SlingHttpServletRequest request) {
LinkCheckerSettings settings = (LinkCheckerSettings)request.getAttribute(REQUEST_ATTRIBUTE_NAME);
if (settings == null) {
settings = new LinkCheckerSettings();
request.setAttribute(REQUEST_ATTRIBUTE_NAME, (Object)settings);
}
return settings;
}
public void init(SlingHttpServletRequest request) {
this.request = request;
}
public SlingHttpServletRequest getRequest() {
return this.request;
}
public ResourceResolver getResourceResolver() {
return this.request.getResourceResolver();
}
public String getContextPath() {
return this.request.getContextPath();
}
public URI getContextURI() {
if (this.contextUri == null) {
try {
String context = this.request.getContextPath() + "/";
URI uri = new URI(this.request.getRequestURL().toString());
this.contextUri = new URI(uri.getScheme(), uri.getRawUserInfo(), uri.getHost(), uri.getPort(), context, null, null);
}
catch (URISyntaxException e) {
log.warn("Ignoring malformed context URI: {}", (Object)e.toString());
}
}
return this.contextUri;
}
public LinkRewriteConfig getInvalidConfig() {
return this.invalidConfig;
}
public void setInvalidConfig(LinkRewriteConfig config) {
this.invalidConfig = config;
}
public LinkRewriteConfig getExpiredConfig() {
return this.expiredConfig;
}
public void setExpiredConfig(LinkRewriteConfig config) {
this.expiredConfig = config;
}
public LinkRewriteConfig getPredatedConfig() {
return this.predatedConfig;
}
public void setPredatedConfig(LinkRewriteConfig config) {
this.predatedConfig = config;
}
public String getRequestURI() {
return this.request.getRequestURI();
}
public String getParentRequestURI() {
String prefix;
Node n;
Resource parent;
int pos;
String requestURI = this.getRequestURI();
if (requestURI == null) {
return "";
}
String parentRequestURI = requestURI;
boolean doFallback = true;
if (this.request.getResource().adaptTo(Item.class) != null && (parent = ResourceUtil.getParent((Resource)this.request.getResource())) != null && (n = (Node)parent.adaptTo(Node.class)) != null) {
int index = 1;
try {
while (!n.isNodeType("nt:hierarchyNode")) {
n = n.getParent();
++index;
}
int pos2 = requestURI.length();
for (int i = 0; i < ++index; ++i) {
pos2 = requestURI.lastIndexOf(47, pos2 - 1);
}
if (pos2 > 0) {
parentRequestURI = requestURI.substring(0, pos2 + 1);
doFallback = false;
}
}
catch (RepositoryException e) {
// empty catch block
}
}
if (doFallback && (pos = requestURI.lastIndexOf(47)) > 0) {
parentRequestURI = requestURI.substring(0, pos + 1);
}
if ((prefix = this.getContextPath()) != null && prefix.length() > 0 && parentRequestURI.startsWith(prefix) && (parentRequestURI.length() == prefix.length() || parentRequestURI.charAt(prefix.length()) == '/')) {
return parentRequestURI.substring(prefix.length());
}
return parentRequestURI;
}
public String getBaseRef() {
return this.baseRef;
}
public URI getBaseURI() {
if (this.baseUri == null) {
String base = this.baseRef;
if (base == null) {
StringBuffer url = this.request.getRequestURL();
if (url.indexOf("/jcr:content") > 0) {
base = url.substring(0, url.indexOf("/jcr:content"));
} else if (this.request.getQueryString() != null) {
url.append('?');
url.append(this.request.getQueryString());
base = url.toString();
} else {
base = url.toString();
}
}
try {
this.baseUri = new URI(base);
}
catch (URISyntaxException e) {
log.warn("Ignoring malformed base URI: {}", (Object)e.toString());
return null;
}
}
return this.baseUri;
}
public void setBaseRef(String baseRef) {
this.baseRef = baseRef;
this.baseUri = null;
}
public boolean isIgnoreInternals() {
return this.ignoreInternals;
}
public boolean setIgnoreInternals(boolean ignoreInternals) {
boolean ret = this.ignoreInternals;
this.ignoreInternals = ignoreInternals;
return ret;
}
public boolean isIgnoreExternals() {
return this.ignoreExternals;
}
public boolean setIgnoreExternals(boolean ignoreExternals) {
boolean ret = this.ignoreExternals;
this.ignoreExternals = ignoreExternals;
return ret;
}
}