Search in sources :

Example 1 with ServletFilterDescriptor

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

the class WeldDeployer method registerWeldConversationFilter.

private void registerWeldConversationFilter(WebBundleDescriptor webBundleDescriptor) {
    for (ServletFilterMapping filterMapping : webBundleDescriptor.getServletFilterMappings()) {
        if (WELD_CONVERSATION_FILTER_NAME.equals(((ServletFilterMappingDescriptor) filterMapping).getDisplayName())) {
            ServletFilterDescriptor servletFilter = new ServletFilterDescriptor();
            servletFilter.setClassName(WELD_CONVERSATION_FILTER_CLASS);
            servletFilter.setName(WELD_CONVERSATION_FILTER_NAME);
            webBundleDescriptor.addServletFilter(servletFilter);
            break;
        }
    }
}
Also used : ServletFilterDescriptor(org.glassfish.web.deployment.descriptor.ServletFilterDescriptor) ServletFilterMapping(com.sun.enterprise.deployment.web.ServletFilterMapping)

Example 2 with ServletFilterDescriptor

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

the class WebBundleValidator method accept.

public void accept(BundleDescriptor descriptor) {
    if (descriptor instanceof WebBundleDescriptor) {
        WebBundleDescriptor webBundle = (WebBundleDescriptor) descriptor;
        accept(webBundle);
        // inject field.
        for (InjectionCapable injectable : webBundle.getInjectableResources(webBundle)) {
            accept(injectable);
        }
        for (Iterator<WebComponentDescriptor> i = webBundle.getWebComponentDescriptors().iterator(); i.hasNext(); ) {
            WebComponentDescriptor aWebComp = i.next();
            accept(aWebComp);
        }
        for (Iterator<WebService> itr = webBundle.getWebServices().getWebServices().iterator(); itr.hasNext(); ) {
            WebService aWebService = itr.next();
            accept(aWebService);
        }
        super.accept(descriptor);
        for (Iterator<ServletFilter> itr = webBundle.getServletFilterDescriptors().iterator(); itr.hasNext(); ) {
            ServletFilterDescriptor servletFilterDescriptor = (ServletFilterDescriptor) itr.next();
            accept(servletFilterDescriptor);
        }
    }
}
Also used : ServletFilter(com.sun.enterprise.deployment.web.ServletFilter) ServletFilterDescriptor(org.glassfish.web.deployment.descriptor.ServletFilterDescriptor)

Example 3 with ServletFilterDescriptor

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

the class FilterClass method check.

/**
 * iterates over all declared filter in the archive file and
 * delegates actual test on individual filter class to
 * runIndividualFilterTest
 *
 * @param descriptor the Web deployment descriptor
 *
 * @return <code>Result</code> the results for this assertion
 */
public Result check(WebBundleDescriptor descriptor) {
    Result result;
    ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
    Enumeration filterEnum = descriptor.getServletFilterDescriptors().elements();
    if (filterEnum.hasMoreElements()) {
        boolean oneFailed = false;
        // test the filters in this .war
        result = loadWarFile(descriptor);
        while (filterEnum.hasMoreElements()) {
            ServletFilterDescriptor filter = (ServletFilterDescriptor) filterEnum.nextElement();
            Class filterClass = loadClass(result, filter.getClassName());
            if (!runIndividualFilterTest(result, filterClass))
                oneFailed = true;
        }
        if (oneFailed) {
            result.setStatus(Result.FAILED);
        } else {
            result.setStatus(Result.PASSED);
        }
    } else {
        result = getInitializedResult();
        result.setStatus(Result.NOT_APPLICABLE);
        result.addNaDetails(smh.getLocalString("tests.componentNameConstructor", "For [ {0} ]", new Object[] { compName.toString() }));
        result.notApplicable(smh.getLocalString("com.sun.enterprise.tools.verifier.tests.web.FilterClass" + ".notApplicable", "There are no filter components within the web archive [ {0} ]", new Object[] { descriptor.getName() }));
    }
    return result;
}
Also used : Enumeration(java.util.Enumeration) ServletFilterDescriptor(org.glassfish.web.deployment.descriptor.ServletFilterDescriptor) Result(com.sun.enterprise.tools.verifier.Result)

Example 4 with ServletFilterDescriptor

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

the class FilterInitParamName method check.

/**
 *  Filter Param Name exists test.
 *
 * @param descriptor the Web deployment descriptor
 *
 * @return <code>Result</code> the results for this assertion
 */
public Result check(WebBundleDescriptor descriptor) {
    Result result = getInitializedResult();
    ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
    boolean oneFailed = false;
    Enumeration filterEnum = descriptor.getServletFilterDescriptors().elements();
    if (filterEnum.hasMoreElements()) {
        // get the filters in this .war
        while (filterEnum.hasMoreElements()) {
            ServletFilterDescriptor filter = (ServletFilterDescriptor) filterEnum.nextElement();
            HashSet<String> envSet = new HashSet<String>();
            Vector epVector = filter.getInitializationParameters();
            for (int i = 0; i < epVector.size(); i++) {
                EnvironmentProperty ep = (EnvironmentProperty) epVector.elementAt(i);
                String epName = ep.getName();
                if (epName.length() == 0) {
                    oneFailed = true;
                    result.addErrorDetails(smh.getLocalString("tests.componentNameConstructor", "For [ {0} ]", new Object[] { compName.toString() }));
                    result.addErrorDetails(smh.getLocalString(getClass().getName() + ".failed1", "Error: Param name/value entry should of finite length."));
                } else {
                    // Do duplicate name test.
                    if (!envSet.contains(epName)) {
                        envSet.add(epName);
                    } else {
                        oneFailed = true;
                        result.addErrorDetails(smh.getLocalString("tests.componentNameConstructor", "For [ {0} ]", new Object[] { compName.toString() }));
                        result.addErrorDetails(smh.getLocalString(getClass().getName() + ".failed2", "Error: Duplicate param names are not allowed."));
                    }
                }
            }
        }
        if (oneFailed) {
            result.setStatus(Result.FAILED);
        } else {
            result.addGoodDetails(smh.getLocalString("tests.componentNameConstructor", "For [ {0} ]", new Object[] { compName.toString() }));
            result.passed(smh.getLocalString(getClass().getName() + ".passed", "All init parameter names are unique"));
        }
    } else {
        result.addNaDetails(smh.getLocalString("tests.componentNameConstructor", "For [ {0} ]", new Object[] { compName.toString() }));
        result.notApplicable(smh.getLocalString(getClass().getName() + ".notApplicable", "There are no initialization parameters for the filter within the web archive [ {0} ]", new Object[] { descriptor.getName() }));
    }
    return result;
}
Also used : ServletFilterDescriptor(org.glassfish.web.deployment.descriptor.ServletFilterDescriptor) EnvironmentProperty(com.sun.enterprise.deployment.EnvironmentProperty) Result(com.sun.enterprise.tools.verifier.Result)

Example 5 with ServletFilterDescriptor

use of org.glassfish.web.deployment.descriptor.ServletFilterDescriptor 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)

Aggregations

ServletFilterDescriptor (org.glassfish.web.deployment.descriptor.ServletFilterDescriptor)8 EnvironmentProperty (com.sun.enterprise.deployment.EnvironmentProperty)3 Result (com.sun.enterprise.tools.verifier.Result)3 Enumeration (java.util.Enumeration)3 ServletFilter (com.sun.enterprise.deployment.web.ServletFilter)2 ServletFilterMapping (com.sun.enterprise.deployment.web.ServletFilterMapping)2 ServletFilterMappingDescriptor (org.glassfish.web.deployment.descriptor.ServletFilterMappingDescriptor)2 ComponentNameConstructor (com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor)1 Vector (java.util.Vector)1 DispatcherType (javax.servlet.DispatcherType)1 WebFilter (javax.servlet.annotation.WebFilter)1 WebInitParam (javax.servlet.annotation.WebInitParam)1