FileDocCategorySizeDatePackage
FilterMapping.javaAPI DocGlassfish v2 API6406Fri May 04 22:34:10 BST 2007com.sun.enterprise.tools.verifier.tests.web

FilterMapping

public class FilterMapping extends WebTest
Check that all the mappings for the declated filters are correct.
author
Jerome Dochez
version
1.0

Fields Summary
com.sun.enterprise.tools.verifier.Result
result
com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor
compName
Constructors Summary
Methods Summary
public com.sun.enterprise.tools.verifier.Resultcheck(com.sun.enterprise.deployment.WebBundleDescriptor descriptor)
Check that the mappings for all filters are correct

param
descriptor the Web deployment descriptor
return
Result the results for this assertion

        result = getInitializedResult();
        compName = getVerifierContext().getComponentNameConstructor();
        Enumeration filterEnum = descriptor.getServletFilterDescriptors().elements();
        if (filterEnum.hasMoreElements()) {
            while (filterEnum.hasMoreElements()) {
                ServletFilterDescriptor filter = (ServletFilterDescriptor) filterEnum.nextElement();
                hasValidMapping(descriptor, filter.getName());
            }
        }
        if (result.getStatus() != Result.FAILED) {
            addGoodDetails(result, compName);
            result.passed(smh.getLocalString
                    (getClass().getName() + ".passed",
                            "All filter mappings are correct"));
        }
        return result;
    
private voidhasValidMapping(com.sun.enterprise.deployment.WebBundleDescriptor descriptor, java.lang.String filterName)

        Enumeration filtermapperEnum = descriptor.getServletFilterMappingDescriptors().elements();
        if (filtermapperEnum.hasMoreElements()) {
            ServletFilterMappingDescriptor filterMapper = null;
            boolean mappingFound = false;
            do {
                filterMapper = (ServletFilterMappingDescriptor)filtermapperEnum.nextElement();
                String filterMapping = filterMapper.getName();
                mappingFound = filterName.equals(filterMapping);
            } while (!mappingFound && filtermapperEnum.hasMoreElements());

            if (mappingFound) {
                List<String> urlPatterns = filterMapper.getURLPatterns();
                for(String url : urlPatterns) {
                    if (!((url.startsWith("/")) ||
                            ((url.startsWith("/")) && (url.endsWith("/*"))) ||
                            (url.startsWith("*.")))) {
                        addErrorDetails(result, compName);
                        result.failed(smh.getLocalString
                                (getClass().getName() + ".failed",
                                "Filter Mapping for [ {0} ] has invalid " +
                                "url-mapping [ {1} ] ",
                                new Object[] {filterName, url} ));
                    }
                }
                List<String> servletsInFilter = filterMapper.getServletNames();
                List<String> servletsInWAR = new ArrayList<String>();

                if(servletsInFilter.size() > 0) {
                    Set servletDescriptor = descriptor.getServletDescriptors();
                    Iterator itr = servletDescriptor.iterator();
                    // test the servlets in this .war
                    while (itr.hasNext()) {
                        WebComponentDescriptor servlet = (WebComponentDescriptor) itr.next();
                        servletsInWAR.add(servlet.getCanonicalName());
                    }
                    if (!(servletsInWAR != null && servletsInWAR.containsAll(servletsInFilter))) {
                        addErrorDetails(result, compName);
                        result.failed(smh.getLocalString
                                (getClass().getName() + ".failed1",
                                "Filter Mapping for [ {0} ] has invalid servlet-name",
                                new Object[] {filterName}));
                    }
                }
            }
        }