-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
1,563 additions
and
419 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
371 changes: 204 additions & 167 deletions
371
bundle/src/main/java/com/adobe/acs/commons/redirects/filter/RedirectFilter.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
bundle/src/main/java/com/adobe/acs/commons/redirects/filter/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* #%L | ||
* ACS AEM Commons Bundle | ||
* %% | ||
* Copyright (C) 2016 Adobe | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
@org.osgi.annotation.versioning.Version("6.0.0") | ||
package com.adobe.acs.commons.redirects.filter; |
70 changes: 70 additions & 0 deletions
70
bundle/src/main/java/com/adobe/acs/commons/redirects/models/Configurations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* #%L | ||
* ACS AEM Commons Bundle | ||
* %% | ||
* Copyright (C) 2016 Adobe | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package com.adobe.acs.commons.redirects.models; | ||
|
||
import com.adobe.acs.commons.redirects.filter.RedirectFilterMBean; | ||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.models.annotations.Model; | ||
import org.apache.sling.models.annotations.injectorspecific.OSGiService; | ||
import org.apache.sling.models.annotations.injectorspecific.SlingObject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.jcr.query.Query; | ||
import java.lang.invoke.MethodHandles; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Comparator; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
/** | ||
* A Sling model to list available redirect configurations on | ||
* /apps/acs-commons/content/redirect-manager/redirects.html | ||
*/ | ||
@Model(adaptables = SlingHttpServletRequest.class) | ||
public class Configurations { | ||
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
|
||
@SlingObject | ||
private SlingHttpServletRequest request; | ||
|
||
@OSGiService | ||
private RedirectFilterMBean redirectFilter; | ||
|
||
private static final String REDIRECTS_RESOURCE_TYPE = "acs-commons/components/utilities/manage-redirects/redirects"; | ||
|
||
public Collection<RedirectConfiguration> getConfigurations() { | ||
String sql = "SELECT * FROM [nt:unstructured] AS s WHERE ISDESCENDANTNODE([/conf]) " | ||
+ "AND s.[sling:resourceType]='" + REDIRECTS_RESOURCE_TYPE + "'"; | ||
log.debug(sql); | ||
Iterator<Resource> it = request.getResourceResolver().findResources(sql, Query.JCR_SQL2); | ||
List<RedirectConfiguration> lst = new ArrayList<>(); | ||
while (it.hasNext()) { | ||
Resource resource = it.next(); | ||
String storageSuffix = redirectFilter.getBucket() + "/" + redirectFilter.getConfigName(); | ||
RedirectConfiguration cfg = new RedirectConfiguration(resource, storageSuffix); | ||
lst.add(cfg); | ||
} | ||
lst.sort(Comparator.comparing(RedirectConfiguration::getName)); | ||
return lst; | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
bundle/src/main/java/com/adobe/acs/commons/redirects/models/RedirectConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* #%L | ||
* ACS AEM Commons Bundle | ||
* %% | ||
* Copyright (C) 2016 Adobe | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package com.adobe.acs.commons.redirects.models; | ||
|
||
import com.adobe.acs.commons.redirects.filter.RedirectFilter; | ||
import org.apache.sling.api.resource.Resource; | ||
|
||
import java.util.Collection; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* A collection of redirect rules | ||
*/ | ||
public class RedirectConfiguration { | ||
/** | ||
* path rules keyed by source, e.g. path1 -> path2. | ||
* This makes lookup by path a O(1) operation | ||
*/ | ||
private Map<String, RedirectRule> pathRules; | ||
/** | ||
* regex rules keyed by their regex pattern. | ||
*/ | ||
private Map<Pattern, RedirectRule> patternRules; | ||
private String path; | ||
private String name; | ||
|
||
public static final RedirectConfiguration EMPTY = new RedirectConfiguration(); | ||
|
||
private RedirectConfiguration(){ | ||
pathRules = new LinkedHashMap<>(); | ||
patternRules = new LinkedHashMap<>(); | ||
} | ||
|
||
public RedirectConfiguration(Resource resource, String storageSuffix) { | ||
pathRules = new LinkedHashMap<>(); | ||
patternRules = new LinkedHashMap<>(); | ||
path = resource.getPath(); | ||
name = path.replace("/" + storageSuffix, ""); | ||
Collection<RedirectRule> rules = RedirectFilter.getRules(resource); | ||
for (RedirectRule rule : rules) { | ||
if (rule.getRegex() != null) { | ||
patternRules.put(rule.getRegex(), rule); | ||
} else { | ||
pathRules.put(normalizePath(rule.getSource()), rule); | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
* @return resource path without .html extension | ||
*/ | ||
|
||
public static String normalizePath(String resourcePath) { | ||
int sep = resourcePath.lastIndexOf('.'); | ||
if (sep != -1 && !resourcePath.startsWith("/content/dam/")) { | ||
// strip off extension if present | ||
resourcePath = resourcePath.substring(0, sep); | ||
} | ||
return resourcePath; | ||
} | ||
|
||
public Map<String, RedirectRule> getPathRules() { | ||
return pathRules; | ||
} | ||
|
||
public Map<Pattern, RedirectRule> getPatternRules() { | ||
return patternRules; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Match a request path to a redirect configuration | ||
* Performs two tries: | ||
* <ol> | ||
* <li>Match by exact path. This is O(1) lookup in a hashtable keyed by path</li> | ||
* <li>Match by a regular expression. This is O(N) linear lookup in a list of rules keyed by their regex patterns</li> | ||
* </ol> | ||
* | ||
* @param requestPath the request to match | ||
* @return match or null | ||
*/ | ||
public RedirectMatch match(String requestPath) { | ||
String normalizedPath = normalizePath(requestPath); | ||
RedirectMatch match = null; | ||
RedirectRule rule = getPathRules().get(normalizedPath); | ||
if (rule != null) { | ||
match = new RedirectMatch(rule, null); | ||
} else { | ||
for (Map.Entry<Pattern, RedirectRule> entry : getPatternRules().entrySet()) { | ||
Matcher m = entry.getKey().matcher(normalizedPath); | ||
if (m.matches()) { | ||
match = new RedirectMatch(entry.getValue(), m); | ||
break; | ||
} | ||
} | ||
} | ||
return match; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.