Search in sources :

Example 1 with ServletFilterMappingDescriptor

use of org.glassfish.web.deployment.descriptor.ServletFilterMappingDescriptor in project Payara by payara.

the class FilterMapping method hasValidMapping.

private void hasValidMapping(WebBundleDescriptor descriptor, 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();
            // Section SRV.18.0.2 Filter All Dispatches of Servlet 2.5 spec allows "*"
            // to be specified as Servlet name to allow filtering of requests for all servlets.
            final String ALL_SERVLETS_NAME = "*";
            servletsInFilter.remove(ALL_SERVLETS_NAME);
            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 }));
                }
            }
        }
    }
}
Also used : WebComponentDescriptor(com.sun.enterprise.deployment.WebComponentDescriptor) Enumeration(java.util.Enumeration) Set(java.util.Set) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ServletFilterMappingDescriptor(org.glassfish.web.deployment.descriptor.ServletFilterMappingDescriptor)

Example 2 with ServletFilterMappingDescriptor

use of org.glassfish.web.deployment.descriptor.ServletFilterMappingDescriptor in project Payara by payara.

the class WebFilterHandler method processAnnotation.

private HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, WebBundleDescriptor webBundleDesc) throws AnnotationProcessorException {
    Class filterClass = (Class) ainfo.getAnnotatedElement();
    if (!javax.servlet.Filter.class.isAssignableFrom(filterClass)) {
        log(Level.SEVERE, ainfo, localStrings.getLocalString("web.deployment.annotation.handlers.needtoimpl", "The Class {0} having annotation {1} need to implement the interface {2}.", new Object[] { filterClass.getName(), WebFilter.class.getName(), javax.servlet.Filter.class.getName() }));
        return getDefaultFailedResult();
    }
    WebFilter webFilterAn = (WebFilter) ainfo.getAnnotation();
    String filterName = webFilterAn.filterName();
    if (filterName == null || filterName.length() == 0) {
        filterName = filterClass.getName();
    }
    ServletFilterDescriptor servletFilterDesc = null;
    for (ServletFilter sfDesc : webBundleDesc.getServletFilters()) {
        if (filterName.equals(sfDesc.getName())) {
            servletFilterDesc = (ServletFilterDescriptor) sfDesc;
            break;
        }
    }
    if (servletFilterDesc == null) {
        servletFilterDesc = new ServletFilterDescriptor();
        servletFilterDesc.setName(filterName);
        webBundleDesc.addServletFilter(servletFilterDesc);
    } else {
        String filterImpl = servletFilterDesc.getClassName();
        if (filterImpl != null && filterImpl.length() > 0 && !filterImpl.equals(filterClass.getName())) {
            log(Level.SEVERE, ainfo, localStrings.getLocalString("web.deployment.annotation.handlers.filternamedontmatch", "The filter ''{0}'' has implementation ''{1}'' in xml. It does not match with ''{2}'' from annotation @{3}.", new Object[] { filterName, filterImpl, filterClass.getName(), WebFilter.class.getName() }));
            return getDefaultFailedResult();
        }
    }
    servletFilterDesc.setClassName(filterClass.getName());
    if (servletFilterDesc.getDescription() == null || servletFilterDesc.getDescription().length() == 0) {
        servletFilterDesc.setDescription(webFilterAn.description());
    }
    if (servletFilterDesc.hasSetDisplayName()) {
        servletFilterDesc.setDisplayName(webFilterAn.displayName());
    }
    if (servletFilterDesc.getInitializationParameters().size() == 0) {
        WebInitParam[] initParams = webFilterAn.initParams();
        if (initParams != null && initParams.length > 0) {
            for (WebInitParam initParam : initParams) {
                servletFilterDesc.addInitializationParameter(new EnvironmentProperty(initParam.name(), initParam.value(), initParam.description()));
            }
        }
    }
    if (servletFilterDesc.getSmallIconUri() == null) {
        servletFilterDesc.setSmallIconUri(webFilterAn.smallIcon());
    }
    if (servletFilterDesc.getLargeIconUri() == null) {
        servletFilterDesc.setLargeIconUri(webFilterAn.largeIcon());
    }
    if (servletFilterDesc.isAsyncSupported() == null) {
        servletFilterDesc.setAsyncSupported(webFilterAn.asyncSupported());
    }
    ServletFilterMapping servletFilterMappingDesc = null;
    boolean hasUrlPattern = false;
    boolean hasServletName = false;
    for (ServletFilterMapping sfm : webBundleDesc.getServletFilterMappings()) {
        if (filterName.equals(sfm.getName())) {
            servletFilterMappingDesc = sfm;
            hasUrlPattern = hasUrlPattern || (sfm.getUrlPatterns().size() > 0);
            hasServletName = hasServletName || (sfm.getServletNames().size() > 0);
        }
    }
    if (servletFilterMappingDesc == null) {
        servletFilterMappingDesc = new ServletFilterMappingDescriptor();
        servletFilterMappingDesc.setName(filterName);
        webBundleDesc.addServletFilterMapping(servletFilterMappingDesc);
    }
    if (!hasUrlPattern) {
        String[] urlPatterns = webFilterAn.urlPatterns();
        if (urlPatterns == null || urlPatterns.length == 0) {
            urlPatterns = webFilterAn.value();
        }
        // accept here as url patterns may be defined in top level xml
        boolean validUrlPatterns = true;
        if (urlPatterns != null && urlPatterns.length > 0) {
            for (String up : urlPatterns) {
                if (!URLPattern.isValid(up)) {
                    validUrlPatterns = false;
                    break;
                }
                servletFilterMappingDesc.addURLPattern(up);
            }
        }
        if (!validUrlPatterns) {
            String urlPatternString = (urlPatterns != null) ? Arrays.toString(urlPatterns) : "";
            throw new IllegalArgumentException(localStrings.getLocalString("web.deployment.annotation.handlers.invalidUrlPatterns", "Invalid url patterns: {0}.", urlPatternString));
        }
    }
    if (!hasServletName) {
        String[] servletNames = webFilterAn.servletNames();
        if (servletNames != null && servletNames.length > 0) {
            for (String sn : servletNames) {
                servletFilterMappingDesc.addServletName(sn);
            }
        }
    }
    if (servletFilterMappingDesc.getDispatchers().size() == 0) {
        DispatcherType[] dispatcherTypes = webFilterAn.dispatcherTypes();
        if (dispatcherTypes != null && dispatcherTypes.length > 0) {
            for (DispatcherType dType : dispatcherTypes) {
                servletFilterMappingDesc.addDispatcher(dType.name());
            }
        }
    }
    return getDefaultProcessedResult();
}
Also used : WebFilter(javax.servlet.annotation.WebFilter) ServletFilter(com.sun.enterprise.deployment.web.ServletFilter) ServletFilterDescriptor(org.glassfish.web.deployment.descriptor.ServletFilterDescriptor) ServletFilterMappingDescriptor(org.glassfish.web.deployment.descriptor.ServletFilterMappingDescriptor) ServletFilter(com.sun.enterprise.deployment.web.ServletFilter) WebFilter(javax.servlet.annotation.WebFilter) EnvironmentProperty(com.sun.enterprise.deployment.EnvironmentProperty) WebInitParam(javax.servlet.annotation.WebInitParam) ServletFilterMapping(com.sun.enterprise.deployment.web.ServletFilterMapping) DispatcherType(javax.servlet.DispatcherType)

Example 3 with ServletFilterMappingDescriptor

use of org.glassfish.web.deployment.descriptor.ServletFilterMappingDescriptor in project Payara by payara.

the class WeldDeployer method registerProbeFilter.

private void registerProbeFilter(WebBundleDescriptor webBundleDescriptor) {
    ServletFilterDescriptor servletFilter = new ServletFilterDescriptor();
    servletFilter.setClassName(PROBE_FILTER_CLASS_NAME);
    servletFilter.setName(PROBE_FILTER_NAME);
    servletFilter.setAsyncSupported(PROBE_FILTER_ASYNC_SUPPORT);
    webBundleDescriptor.addServletFilter(servletFilter);
    ServletFilterMappingDescriptor servletFilterMapping = new ServletFilterMappingDescriptor();
    servletFilterMapping.setName(PROBE_FILTER_NAME);
    servletFilterMapping.addURLPattern(PROBE_FILTER_URL_PATTERN);
    servletFilterMapping.addDispatcher(PROBE_FILTER_DISPATCHER_TYPE);
    webBundleDescriptor.addServletFilterMapping(servletFilterMapping);
}
Also used : ServletFilterDescriptor(org.glassfish.web.deployment.descriptor.ServletFilterDescriptor) ServletFilterMappingDescriptor(org.glassfish.web.deployment.descriptor.ServletFilterMappingDescriptor)

Aggregations

ServletFilterMappingDescriptor (org.glassfish.web.deployment.descriptor.ServletFilterMappingDescriptor)3 ServletFilterDescriptor (org.glassfish.web.deployment.descriptor.ServletFilterDescriptor)2 EnvironmentProperty (com.sun.enterprise.deployment.EnvironmentProperty)1 WebComponentDescriptor (com.sun.enterprise.deployment.WebComponentDescriptor)1 ServletFilter (com.sun.enterprise.deployment.web.ServletFilter)1 ServletFilterMapping (com.sun.enterprise.deployment.web.ServletFilterMapping)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 DispatcherType (javax.servlet.DispatcherType)1 WebFilter (javax.servlet.annotation.WebFilter)1 WebInitParam (javax.servlet.annotation.WebInitParam)1