1 package com.squeakysand.sling.request;
2
3 import com.squeakysand.commons.lang.NullParameterException;
4 import com.squeakysand.commons.lang.StringUtils;
5 import com.squeakysand.commons.lang.TrimStrategy;
6
7 import org.apache.sling.api.request.RequestPathInfo;
8 import org.apache.sling.api.resource.Resource;
9 import org.apache.sling.engine.impl.request.SlingRequestPathInfo;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13
14
15
16
17 public class RequestPathInfoFactory {
18
19 private static class SimpleRequestPathInfo implements RequestPathInfo {
20
21 private static final Logger LOG = LoggerFactory.getLogger(RequestPathInfoFactory.SimpleRequestPathInfo.class);
22
23 private String path;
24 private String selectorString;
25 private String[] selectors;
26 private String extension;
27 private String suffix;
28
29
30
31
32 public SimpleRequestPathInfo(String urlPath) {
33 if (urlPath == null) {
34 throw new NullParameterException("urlPath");
35 }
36 if (!urlPath.startsWith("/")) {
37 throw new IllegalArgumentException(String.format("urlPath must be an absolute path, was '%s'", urlPath));
38 }
39
40 if (urlPath.contains(".")) {
41
42 String extPlusSuffix = StringUtils.trimPrefix(urlPath, '.', TrimStrategy.AFTER_LAST);
43
44 urlPath = StringUtils.trimSuffix(urlPath, '.', TrimStrategy.AT_LAST);
45
46 if (extPlusSuffix.contains("/")) {
47
48 extension = StringUtils.trimSuffix(extPlusSuffix, '/', TrimStrategy.AT_FIRST);
49
50 suffix = StringUtils.trimPrefix(extPlusSuffix, '/', TrimStrategy.AT_FIRST);
51 } else {
52 extension = extPlusSuffix;
53 suffix = null;
54 }
55
56 String selectorStr;
57 if (urlPath.contains("/")) {
58
59 selectorStr = StringUtils.trimPrefix(urlPath, '/', TrimStrategy.AFTER_LAST);
60
61 urlPath = StringUtils.trimSuffix(urlPath, '/', TrimStrategy.AFTER_LAST);
62 } else {
63 selectorStr = urlPath;
64 }
65
66 if (selectorStr.contains(".")) {
67
68 urlPath = urlPath + StringUtils.trimSuffix(selectorStr, '.', TrimStrategy.AT_FIRST);
69
70 selectorStr = StringUtils.trimPrefix(selectorStr, '.', TrimStrategy.AFTER_FIRST);
71 } else {
72 urlPath = urlPath + selectorStr;
73 selectorStr = null;
74 }
75 path = urlPath;
76 if (selectorStr == null) {
77 selectorString = null;
78 selectors = new String[0];
79 } else {
80 selectorString = selectorStr;
81 selectors = selectorStr.split("\\.");
82 }
83 } else {
84 path = urlPath;
85 selectorString = null;
86 selectors = new String[0];
87 extension = null;
88 suffix = null;
89 }
90 }
91
92
93
94
95 @Override
96 public String getExtension() {
97 return extension;
98 }
99
100
101
102
103 @Override
104 public String getResourcePath() {
105 return path;
106 }
107
108
109
110
111 @Override
112 public String[] getSelectors() {
113 return selectors;
114 }
115
116
117
118
119 @Override
120 public String getSelectorString() {
121 return selectorString;
122 }
123
124
125
126
127 @Override
128 public String getSuffix() {
129 return suffix;
130 }
131
132 }
133
134 public static RequestPathInfo create(Resource resource) {
135 return new SlingRequestPathInfo(resource);
136 }
137
138 public static RequestPathInfo create(String path) {
139 return new SimpleRequestPathInfo(path);
140 }
141
142 }