public boolean match()

in src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java [381:502]


    public boolean match(final ProcessingContext processContext) {
        if ( !this.processErrorResponse && processContext.getRequest().getAttribute("javax.servlet.error.status_code") != null ) {
            return false;
        }

        final Object pipelineName = processContext.getRequest().getAttribute(ATTR_PIPELINE);
        if (pipelineName != null && !pipelineName.equals(this.name)) {
            return false;
        }

        String contentType = processContext.getContentType();
        // if no content type is supplied, we assume html
        if ( contentType == null ) {
            contentType = ProcessorManagerImpl.MIME_TYPE_HTML;
        } else {
            final int idx = contentType.indexOf(';');
            if (idx != -1) {
                contentType = contentType.substring(0, idx);
            }
        }

        // check content type first
        // if no content type is configured we apply to all
        if ( this.contentTypes != null && this.contentTypes.length > 0 ) {
            int index = 0;
            boolean found = false;
            while ( !found && index < this.contentTypes.length ) {
                if ( this.contentTypes[index].equals("*") ) {
                    found = true;
                } else if ( this.contentTypes[index].equals(contentType) ) {
                    found = true;
                }
                index++;
            }
            if ( !found ) {
                return false;
            }
        }
        // now check extenstions
        // if no extenstion is configured, we apply to all extenstions
        if ( this.extensions != null && this.extensions.length > 0 ) {
             boolean found = false;
             int index = 0;
             while ( !found && index < this.extensions.length ) {
                 if ( this.extensions[index].equals(processContext.getRequest().getRequestPathInfo().getExtension()) ) {
                     found = true;
                 }
                 index++;
             }
             if ( !found ) {
                 return false;
             }
        }
        // check resource types
        if ( this.resourceTypes != null && this.resourceTypes.length > 0 ) {
            final ResourceResolver resourceResolver = processContext.getRequest().getResourceResolver();
            final Resource resource = processContext.getRequest().getResource();
            boolean found = false;
            int index = 0;
            while ( !found && index < this.resourceTypes.length ) {
                if ( resourceResolver.isResourceType(resource, resourceTypes[index]) ) {
                    found = true;
                }
                else if ( unwrapResources && resource instanceof ResourceWrapper ) {
                    // accept resource as well if type was overridden and unwrapped resource has a matching type
                    final Resource unwrappedResource = unwrap(resource);
                    if ( resourceResolver.isResourceType(unwrappedResource, resourceTypes[index]) ) {
                        found = true;
                    }
                }
                index++;
            }
            if ( !found ) {
                return false;
            }
        }

        // now check for path
        // if no path is configured, we apply to all paths
        if ( this.paths != null && this.paths.length > 0 ) {
            final String path = processContext.getRequest().getRequestPathInfo().getResourcePath();
            int index = 0;
            boolean found = false;
            while ( !found && index < this.paths.length ) {
                if ( this.paths[index].equals("*") ) {
                    found = true;
                } else if ( path.startsWith(this.paths[index]) ) {
                    found = true;
                }
                index++;
            }
            if ( !found ) {
                return false;
            }
        }

        // now check for selectors
        if( this.selectors != null && this.selectors.length > 0 ) {
            final String selectorString = processContext.getRequest().getRequestPathInfo().getSelectorString();
            if ( selectorString == null || "".equals(selectorString )) {
                // selectors required but not set
                return false;
            }

            final Set<String> selectors = new HashSet<String>(Arrays.asList(selectorString.split("\\.")));
            int index = 0;
            boolean found = false;
            while ( !found && index < this.selectors.length ) {
                final String selector = this.selectors[index];
                if( selectors.contains(selector) ) {
                    found = true;
                }
                index++;
            }

            if( !found ) {
                return false;
            }
        }

        return true;
    }