Search in sources :

Example 1 with ElementValuePair

use of org.apache.tomcat.util.bcel.classfile.ElementValuePair in project tomcat70 by apache.

the class ContextConfig method processAnnotationWebFilter.

/**
 * process filter annotation and merge with existing one!
 * FIXME: refactoring method too long and has redundant subroutines with
 *        processAnnotationWebServlet!
 * @param className
 * @param ae
 * @param fragment
 */
protected void processAnnotationWebFilter(String className, AnnotationEntry ae, WebXml fragment) {
    String filterName = null;
    // must search for name s. Spec Servlet API 3.0 - 8.2.3.3.n.ii page 81
    List<ElementValuePair> evps = ae.getElementValuePairs();
    for (ElementValuePair evp : evps) {
        String name = evp.getNameString();
        if ("filterName".equals(name)) {
            filterName = evp.getValue().stringifyValue();
            break;
        }
    }
    if (filterName == null) {
        // classname is default filterName as annotation has no name!
        filterName = className;
    }
    FilterDef filterDef = fragment.getFilters().get(filterName);
    FilterMap filterMap = new FilterMap();
    boolean isWebXMLfilterDef;
    if (filterDef == null) {
        filterDef = new FilterDef();
        filterDef.setFilterName(filterName);
        filterDef.setFilterClass(className);
        isWebXMLfilterDef = false;
    } else {
        isWebXMLfilterDef = true;
    }
    boolean urlPatternsSet = false;
    boolean servletNamesSet = false;
    boolean dispatchTypesSet = false;
    String[] urlPatterns = null;
    for (ElementValuePair evp : evps) {
        String name = evp.getNameString();
        if ("value".equals(name) || "urlPatterns".equals(name)) {
            if (urlPatternsSet) {
                throw new IllegalArgumentException(sm.getString("contextConfig.urlPatternValue", "WebFilter", className));
            }
            urlPatterns = processAnnotationsStringArray(evp.getValue());
            urlPatternsSet = urlPatterns.length > 0;
            for (String urlPattern : urlPatterns) {
                filterMap.addURLPattern(urlPattern);
            }
        } else if ("servletNames".equals(name)) {
            String[] servletNames = processAnnotationsStringArray(evp.getValue());
            servletNamesSet = servletNames.length > 0;
            for (String servletName : servletNames) {
                filterMap.addServletName(servletName);
            }
        } else if ("dispatcherTypes".equals(name)) {
            String[] dispatcherTypes = processAnnotationsStringArray(evp.getValue());
            dispatchTypesSet = dispatcherTypes.length > 0;
            for (String dispatcherType : dispatcherTypes) {
                filterMap.setDispatcher(dispatcherType);
            }
        } else if ("description".equals(name)) {
            if (filterDef.getDescription() == null) {
                filterDef.setDescription(evp.getValue().stringifyValue());
            }
        } else if ("displayName".equals(name)) {
            if (filterDef.getDisplayName() == null) {
                filterDef.setDisplayName(evp.getValue().stringifyValue());
            }
        } else if ("largeIcon".equals(name)) {
            if (filterDef.getLargeIcon() == null) {
                filterDef.setLargeIcon(evp.getValue().stringifyValue());
            }
        } else if ("smallIcon".equals(name)) {
            if (filterDef.getSmallIcon() == null) {
                filterDef.setSmallIcon(evp.getValue().stringifyValue());
            }
        } else if ("asyncSupported".equals(name)) {
            if (filterDef.getAsyncSupported() == null) {
                filterDef.setAsyncSupported(evp.getValue().stringifyValue());
            }
        } else if ("initParams".equals(name)) {
            Map<String, String> initParams = processAnnotationWebInitParams(evp.getValue());
            if (isWebXMLfilterDef) {
                Map<String, String> webXMLInitParams = filterDef.getParameterMap();
                for (Map.Entry<String, String> entry : initParams.entrySet()) {
                    if (webXMLInitParams.get(entry.getKey()) == null) {
                        filterDef.addInitParameter(entry.getKey(), entry.getValue());
                    }
                }
            } else {
                for (Map.Entry<String, String> entry : initParams.entrySet()) {
                    filterDef.addInitParameter(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    if (!isWebXMLfilterDef) {
        fragment.addFilter(filterDef);
        if (urlPatternsSet || servletNamesSet) {
            filterMap.setFilterName(filterName);
            fragment.addFilterMapping(filterMap);
        }
    }
    if (urlPatternsSet || dispatchTypesSet) {
        Set<FilterMap> fmap = fragment.getFilterMappings();
        FilterMap descMap = null;
        for (FilterMap map : fmap) {
            if (filterName.equals(map.getFilterName())) {
                descMap = map;
                break;
            }
        }
        if (descMap != null) {
            String[] urlsPatterns = descMap.getURLPatterns();
            if (urlPatternsSet && (urlsPatterns == null || urlsPatterns.length == 0)) {
                for (String urlPattern : filterMap.getURLPatterns()) {
                    descMap.addURLPattern(urlPattern);
                }
            }
            String[] dispatcherNames = descMap.getDispatcherNames();
            if (dispatchTypesSet && (dispatcherNames == null || dispatcherNames.length == 0)) {
                for (String dis : filterMap.getDispatcherNames()) {
                    descMap.setDispatcher(dis);
                }
            }
        }
    }
}
Also used : FilterDef(org.apache.catalina.deploy.FilterDef) ElementValuePair(org.apache.tomcat.util.bcel.classfile.ElementValuePair) FilterMap(org.apache.catalina.deploy.FilterMap) FilterMap(org.apache.catalina.deploy.FilterMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ElementValuePair

use of org.apache.tomcat.util.bcel.classfile.ElementValuePair in project tomcat by apache.

the class ContextConfig method processAnnotationWebFilter.

/**
 * process filter annotation and merge with existing one!
 * FIXME: refactoring method too long and has redundant subroutines with
 *        processAnnotationWebServlet!
 * @param className The filter class name
 * @param ae The filter annotation
 * @param fragment The corresponding fragment
 */
protected void processAnnotationWebFilter(String className, AnnotationEntry ae, WebXml fragment) {
    String filterName = null;
    // must search for name s. Spec Servlet API 3.0 - 8.2.3.3.n.ii page 81
    List<ElementValuePair> evps = ae.getElementValuePairs();
    for (ElementValuePair evp : evps) {
        String name = evp.getNameString();
        if ("filterName".equals(name)) {
            filterName = evp.getValue().stringifyValue();
            break;
        }
    }
    if (filterName == null) {
        // classname is default filterName as annotation has no name!
        filterName = className;
    }
    FilterDef filterDef = fragment.getFilters().get(filterName);
    FilterMap filterMap = new FilterMap();
    boolean isWebXMLfilterDef;
    if (filterDef == null) {
        filterDef = new FilterDef();
        filterDef.setFilterName(filterName);
        filterDef.setFilterClass(className);
        isWebXMLfilterDef = false;
    } else {
        isWebXMLfilterDef = true;
    }
    boolean urlPatternsSet = false;
    boolean servletNamesSet = false;
    boolean dispatchTypesSet = false;
    String[] urlPatterns = null;
    for (ElementValuePair evp : evps) {
        String name = evp.getNameString();
        if ("value".equals(name) || "urlPatterns".equals(name)) {
            if (urlPatternsSet) {
                throw new IllegalArgumentException(sm.getString("contextConfig.urlPatternValue", "WebFilter", className));
            }
            urlPatterns = processAnnotationsStringArray(evp.getValue());
            urlPatternsSet = urlPatterns.length > 0;
            for (String urlPattern : urlPatterns) {
                // % decoded (if required) using UTF-8
                filterMap.addURLPattern(urlPattern);
            }
        } else if ("servletNames".equals(name)) {
            String[] servletNames = processAnnotationsStringArray(evp.getValue());
            servletNamesSet = servletNames.length > 0;
            for (String servletName : servletNames) {
                filterMap.addServletName(servletName);
            }
        } else if ("dispatcherTypes".equals(name)) {
            String[] dispatcherTypes = processAnnotationsStringArray(evp.getValue());
            dispatchTypesSet = dispatcherTypes.length > 0;
            for (String dispatcherType : dispatcherTypes) {
                filterMap.setDispatcher(dispatcherType);
            }
        } else if ("description".equals(name)) {
            if (filterDef.getDescription() == null) {
                filterDef.setDescription(evp.getValue().stringifyValue());
            }
        } else if ("displayName".equals(name)) {
            if (filterDef.getDisplayName() == null) {
                filterDef.setDisplayName(evp.getValue().stringifyValue());
            }
        } else if ("largeIcon".equals(name)) {
            if (filterDef.getLargeIcon() == null) {
                filterDef.setLargeIcon(evp.getValue().stringifyValue());
            }
        } else if ("smallIcon".equals(name)) {
            if (filterDef.getSmallIcon() == null) {
                filterDef.setSmallIcon(evp.getValue().stringifyValue());
            }
        } else if ("asyncSupported".equals(name)) {
            if (filterDef.getAsyncSupported() == null) {
                filterDef.setAsyncSupported(evp.getValue().stringifyValue());
            }
        } else if ("initParams".equals(name)) {
            Map<String, String> initParams = processAnnotationWebInitParams(evp.getValue());
            if (isWebXMLfilterDef) {
                Map<String, String> webXMLInitParams = filterDef.getParameterMap();
                for (Map.Entry<String, String> entry : initParams.entrySet()) {
                    if (webXMLInitParams.get(entry.getKey()) == null) {
                        filterDef.addInitParameter(entry.getKey(), entry.getValue());
                    }
                }
            } else {
                for (Map.Entry<String, String> entry : initParams.entrySet()) {
                    filterDef.addInitParameter(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    if (!isWebXMLfilterDef) {
        fragment.addFilter(filterDef);
        if (urlPatternsSet || servletNamesSet) {
            filterMap.setFilterName(filterName);
            fragment.addFilterMapping(filterMap);
        }
    }
    if (urlPatternsSet || dispatchTypesSet) {
        Set<FilterMap> fmap = fragment.getFilterMappings();
        FilterMap descMap = null;
        for (FilterMap map : fmap) {
            if (filterName.equals(map.getFilterName())) {
                descMap = map;
                break;
            }
        }
        if (descMap != null) {
            String[] urlsPatterns = descMap.getURLPatterns();
            if (urlPatternsSet && (urlsPatterns == null || urlsPatterns.length == 0)) {
                for (String urlPattern : filterMap.getURLPatterns()) {
                    // % decoded (if required) using UTF-8
                    descMap.addURLPattern(urlPattern);
                }
            }
            String[] dispatcherNames = descMap.getDispatcherNames();
            if (dispatchTypesSet && (dispatcherNames == null || dispatcherNames.length == 0)) {
                for (String dis : filterMap.getDispatcherNames()) {
                    descMap.setDispatcher(dis);
                }
            }
        }
    }
}
Also used : FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) ElementValuePair(org.apache.tomcat.util.bcel.classfile.ElementValuePair) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ElementValuePair

use of org.apache.tomcat.util.bcel.classfile.ElementValuePair in project tomcat70 by apache.

the class ContextConfig method processAnnotationWebServlet.

protected void processAnnotationWebServlet(String className, AnnotationEntry ae, WebXml fragment) {
    String servletName = null;
    // must search for name s. Spec Servlet API 3.0 - 8.2.3.3.n.ii page 81
    List<ElementValuePair> evps = ae.getElementValuePairs();
    for (ElementValuePair evp : evps) {
        String name = evp.getNameString();
        if ("name".equals(name)) {
            servletName = evp.getValue().stringifyValue();
            break;
        }
    }
    if (servletName == null) {
        // classname is default servletName as annotation has no name!
        servletName = className;
    }
    ServletDef servletDef = fragment.getServlets().get(servletName);
    boolean isWebXMLservletDef;
    if (servletDef == null) {
        servletDef = new ServletDef();
        servletDef.setServletName(servletName);
        servletDef.setServletClass(className);
        isWebXMLservletDef = false;
    } else {
        isWebXMLservletDef = true;
    }
    boolean urlPatternsSet = false;
    String[] urlPatterns = null;
    // List<ElementValuePair> evps = ae.getElementValuePairs();
    for (ElementValuePair evp : evps) {
        String name = evp.getNameString();
        if ("value".equals(name) || "urlPatterns".equals(name)) {
            if (urlPatternsSet) {
                throw new IllegalArgumentException(sm.getString("contextConfig.urlPatternValue", "WebServlet", className));
            }
            urlPatternsSet = true;
            urlPatterns = processAnnotationsStringArray(evp.getValue());
        } else if ("description".equals(name)) {
            if (servletDef.getDescription() == null) {
                servletDef.setDescription(evp.getValue().stringifyValue());
            }
        } else if ("displayName".equals(name)) {
            if (servletDef.getDisplayName() == null) {
                servletDef.setDisplayName(evp.getValue().stringifyValue());
            }
        } else if ("largeIcon".equals(name)) {
            if (servletDef.getLargeIcon() == null) {
                servletDef.setLargeIcon(evp.getValue().stringifyValue());
            }
        } else if ("smallIcon".equals(name)) {
            if (servletDef.getSmallIcon() == null) {
                servletDef.setSmallIcon(evp.getValue().stringifyValue());
            }
        } else if ("asyncSupported".equals(name)) {
            if (servletDef.getAsyncSupported() == null) {
                servletDef.setAsyncSupported(evp.getValue().stringifyValue());
            }
        } else if ("loadOnStartup".equals(name)) {
            if (servletDef.getLoadOnStartup() == null) {
                servletDef.setLoadOnStartup(evp.getValue().stringifyValue());
            }
        } else if ("initParams".equals(name)) {
            Map<String, String> initParams = processAnnotationWebInitParams(evp.getValue());
            if (isWebXMLservletDef) {
                Map<String, String> webXMLInitParams = servletDef.getParameterMap();
                for (Map.Entry<String, String> entry : initParams.entrySet()) {
                    if (webXMLInitParams.get(entry.getKey()) == null) {
                        servletDef.addInitParameter(entry.getKey(), entry.getValue());
                    }
                }
            } else {
                for (Map.Entry<String, String> entry : initParams.entrySet()) {
                    servletDef.addInitParameter(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    if (!isWebXMLservletDef && urlPatterns != null) {
        fragment.addServlet(servletDef);
    }
    if (urlPatterns != null) {
        if (!fragment.getServletMappings().containsValue(servletName)) {
            for (String urlPattern : urlPatterns) {
                fragment.addServletMapping(urlPattern, servletName);
            }
        }
    }
}
Also used : AnnotationEntry(org.apache.tomcat.util.bcel.classfile.AnnotationEntry) ElementValuePair(org.apache.tomcat.util.bcel.classfile.ElementValuePair) ServletDef(org.apache.catalina.deploy.ServletDef) FilterMap(org.apache.catalina.deploy.FilterMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with ElementValuePair

use of org.apache.tomcat.util.bcel.classfile.ElementValuePair in project tomcat70 by apache.

the class ContextConfig method processAnnotationWebInitParams.

protected Map<String, String> processAnnotationWebInitParams(ElementValue ev) {
    Map<String, String> result = new HashMap<String, String>();
    if (ev instanceof ArrayElementValue) {
        ElementValue[] arrayValues = ((ArrayElementValue) ev).getElementValuesArray();
        for (ElementValue value : arrayValues) {
            if (value instanceof AnnotationElementValue) {
                List<ElementValuePair> evps = ((AnnotationElementValue) value).getAnnotationEntry().getElementValuePairs();
                String initParamName = null;
                String initParamValue = null;
                for (ElementValuePair evp : evps) {
                    if ("name".equals(evp.getNameString())) {
                        initParamName = evp.getValue().stringifyValue();
                    } else if ("value".equals(evp.getNameString())) {
                        initParamValue = evp.getValue().stringifyValue();
                    } else {
                    // Ignore
                    }
                }
                result.put(initParamName, initParamValue);
            }
        }
    }
    return result;
}
Also used : ArrayElementValue(org.apache.tomcat.util.bcel.classfile.ArrayElementValue) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ElementValuePair(org.apache.tomcat.util.bcel.classfile.ElementValuePair) AnnotationElementValue(org.apache.tomcat.util.bcel.classfile.AnnotationElementValue) ArrayElementValue(org.apache.tomcat.util.bcel.classfile.ArrayElementValue) ElementValue(org.apache.tomcat.util.bcel.classfile.ElementValue) AnnotationElementValue(org.apache.tomcat.util.bcel.classfile.AnnotationElementValue)

Example 5 with ElementValuePair

use of org.apache.tomcat.util.bcel.classfile.ElementValuePair in project tomcat by apache.

the class ContextConfig method processAnnotationWebInitParams.

protected Map<String, String> processAnnotationWebInitParams(ElementValue ev) {
    Map<String, String> result = new HashMap<>();
    if (ev instanceof ArrayElementValue) {
        ElementValue[] arrayValues = ((ArrayElementValue) ev).getElementValuesArray();
        for (ElementValue value : arrayValues) {
            if (value instanceof AnnotationElementValue) {
                List<ElementValuePair> evps = ((AnnotationElementValue) value).getAnnotationEntry().getElementValuePairs();
                String initParamName = null;
                String initParamValue = null;
                for (ElementValuePair evp : evps) {
                    if ("name".equals(evp.getNameString())) {
                        initParamName = evp.getValue().stringifyValue();
                    } else if ("value".equals(evp.getNameString())) {
                        initParamValue = evp.getValue().stringifyValue();
                    } else {
                    // Ignore
                    }
                }
                result.put(initParamName, initParamValue);
            }
        }
    }
    return result;
}
Also used : ArrayElementValue(org.apache.tomcat.util.bcel.classfile.ArrayElementValue) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ElementValuePair(org.apache.tomcat.util.bcel.classfile.ElementValuePair) AnnotationElementValue(org.apache.tomcat.util.bcel.classfile.AnnotationElementValue) ArrayElementValue(org.apache.tomcat.util.bcel.classfile.ArrayElementValue) ElementValue(org.apache.tomcat.util.bcel.classfile.ElementValue) AnnotationElementValue(org.apache.tomcat.util.bcel.classfile.AnnotationElementValue)

Aggregations

HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 ElementValuePair (org.apache.tomcat.util.bcel.classfile.ElementValuePair)6 Map (java.util.Map)4 FilterMap (org.apache.catalina.deploy.FilterMap)2 AnnotationElementValue (org.apache.tomcat.util.bcel.classfile.AnnotationElementValue)2 AnnotationEntry (org.apache.tomcat.util.bcel.classfile.AnnotationEntry)2 ArrayElementValue (org.apache.tomcat.util.bcel.classfile.ArrayElementValue)2 ElementValue (org.apache.tomcat.util.bcel.classfile.ElementValue)2 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)2 Entry (java.util.Map.Entry)1 FilterDef (org.apache.catalina.deploy.FilterDef)1 ServletDef (org.apache.catalina.deploy.ServletDef)1 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)1 ServletDef (org.apache.tomcat.util.descriptor.web.ServletDef)1