in src/main/java/org/apache/sling/servlets/resolver/internal/resource/ServletResourceProviderFactory.java [240:342]
private void addByType(Set<String> pathSet, ServiceReference<Servlet> ref) {
String[] types = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_RESOURCE_TYPES));
String[] paths = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_PATHS));
boolean hasPathRegistration = true;
if (paths == null || paths.length == 0) {
hasPathRegistration = false;
}
if (types == null || types.length == 0) {
if (log.isDebugEnabled()) {
log.debug("addByType({}): no resource types declared",
getServiceReferenceInfo(ref));
}
return;
}
// check for selectors
String[] selectors = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_SELECTORS));
if (selectors == null) {
selectors = new String[] { null };
}
// we have types and expect extensions and/or methods
String[] extensions = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_EXTENSIONS));
// handle the methods property specially (SLING-430)
String[] methods = PropertiesUtil.toStringArray(ref.getProperty(SLING_SERVLET_METHODS));
if (methods == null || methods.length == 0) {
// SLING-512 only, set default methods if no extensions are declared
if ((extensions == null || extensions.length == 0) && !hasPathRegistration) {
if (log.isDebugEnabled()) {
log.debug(
"addByType({}): No methods declared, assuming GET/HEAD",
getServiceReferenceInfo(ref));
}
methods = DEFAULT_SERVLET_METHODS;
}
} else if (methods.length == 1 && ALL_METHODS.equals(methods[0])) {
if (log.isDebugEnabled()) {
log.debug("addByType({}): Assuming all methods for '*'",
getServiceReferenceInfo(ref));
}
methods = null;
}
for (String type : types) {
// ensure namespace prefixes are converted to slashes
type = ResourceUtil.resourceTypeToPath(type);
// make absolute if relative
if (!type.startsWith("/")) {
type = this.getPrefix(ref) + type;
}
// ensure trailing slash for full path building
if (!type.endsWith("/")) {
type += "/";
}
// add entries for each selector combined with each ext and method
for (String selector : selectors) {
String selPath = type;
if (selector != null && selector.length() > 0) {
selPath += selector.replace('.', '/') + ".";
}
boolean pathAdded = false;
if (extensions != null) {
if (methods != null) {
// both methods and extensions declared
for (String ext : extensions) {
for (String method : methods) {
pathSet.add(selPath + ext + "." + method
+ SERVLET_PATH_EXTENSION);
pathAdded = true;
}
}
} else {
// only extensions declared
for (String ext : extensions) {
pathSet.add(selPath + ext + SERVLET_PATH_EXTENSION);
pathAdded = true;
}
}
} else if (methods != null) {
// only methods declared
for (String method : methods) {
pathSet.add(selPath + method + SERVLET_PATH_EXTENSION);
pathAdded = true;
}
}
// if neither methods nor extensions were added
if (!pathAdded && !hasPathRegistration) {
pathSet.add(selPath.substring(0, selPath.length() - 1)
+ SERVLET_PATH_EXTENSION);
}
}
}
}