View | Details | Raw Unified | Return to bug 53737
Collapse All | Expand All

(-)test/org/apache/jasper/servlet/TestJspCServletContext.java (+97 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package org.apache.jasper.servlet;
18
19
import java.io.File;
20
import java.util.Collection;
21
import java.util.Iterator;
22
23
import javax.servlet.descriptor.JspConfigDescriptor;
24
import javax.servlet.descriptor.JspPropertyGroupDescriptor;
25
26
import org.junit.Assert;
27
import org.junit.Test;
28
29
public class TestJspCServletContext {
30
31
    @Test
32
    public void testWebapp() throws Exception {
33
        File appDir = new File("test/webapp");
34
        JspCServletContext context = new JspCServletContext(null, appDir.toURI().toURL());
35
        Assert.assertEquals(3, context.getEffectiveMajorVersion());
36
        Assert.assertEquals(1, context.getEffectiveMinorVersion());
37
        JspConfigDescriptor jspConfigDescriptor = context.getJspConfigDescriptor();
38
        Assert.assertTrue(jspConfigDescriptor.getTaglibs().isEmpty());
39
        Collection<JspPropertyGroupDescriptor> propertyGroups = jspConfigDescriptor.getJspPropertyGroups();
40
        Assert.assertEquals(1, propertyGroups.size());
41
        JspPropertyGroupDescriptor groupDescriptor = propertyGroups.iterator().next();
42
        Assert.assertEquals("text/plain", groupDescriptor.getDefaultContentType());
43
        Collection<String> urlPatterns = groupDescriptor.getUrlPatterns();
44
        Assert.assertEquals(2, urlPatterns.size());
45
        Iterator<String> iterator = urlPatterns.iterator();
46
        Assert.assertEquals("/bug49nnn/bug49726a.jsp", iterator.next());
47
        Assert.assertEquals("/bug49nnn/bug49726b.jsp", iterator.next());
48
    }
49
50
    @Test
51
    public void testWebapp_2_3() throws Exception {
52
        File appDir = new File("test/webapp-2.3");
53
        JspCServletContext context = new JspCServletContext(null, appDir.toURI().toURL());
54
        Assert.assertEquals(2, context.getEffectiveMajorVersion());
55
        Assert.assertEquals(3, context.getEffectiveMinorVersion());
56
    }
57
58
    @Test
59
    public void testWebapp_2_4() throws Exception {
60
        File appDir = new File("test/webapp-2.4");
61
        JspCServletContext context = new JspCServletContext(null, appDir.toURI().toURL());
62
        Assert.assertEquals(2, context.getEffectiveMajorVersion());
63
        Assert.assertEquals(4, context.getEffectiveMinorVersion());
64
    }
65
66
    @Test
67
    public void testWebapp_2_5() throws Exception {
68
        File appDir = new File("test/webapp-2.5");
69
        JspCServletContext context = new JspCServletContext(null, appDir.toURI().toURL());
70
        Assert.assertEquals(2, context.getEffectiveMajorVersion());
71
        Assert.assertEquals(5, context.getEffectiveMinorVersion());
72
    }
73
74
    @Test
75
    public void testWebapp_3_0() throws Exception {
76
        File appDir = new File("test/webapp-3.0");
77
        JspCServletContext context = new JspCServletContext(null, appDir.toURI().toURL());
78
        Assert.assertEquals(3, context.getEffectiveMajorVersion());
79
        Assert.assertEquals(0, context.getEffectiveMinorVersion());
80
    }
81
82
    @Test
83
    public void testWebapp_3_1() throws Exception {
84
        File appDir = new File("test/webapp-3.1");
85
        JspCServletContext context = new JspCServletContext(null, appDir.toURI().toURL());
86
        Assert.assertEquals(3, context.getEffectiveMajorVersion());
87
        Assert.assertEquals(1, context.getEffectiveMinorVersion());
88
    }
89
90
    @Test
91
    public void testWebresources() throws Exception {
92
        File appDir = new File("test/webresources/dir1");
93
        JspCServletContext context = new JspCServletContext(null, appDir.toURI().toURL());
94
        Assert.assertEquals(3, context.getEffectiveMajorVersion());
95
        Assert.assertEquals(1, context.getEffectiveMinorVersion());
96
    }
97
}
(-)java/org/apache/jasper/JspC.java (-10 / +5 lines)
Lines 30-36 Link Here
30
import java.io.PrintWriter;
30
import java.io.PrintWriter;
31
import java.io.Reader;
31
import java.io.Reader;
32
import java.io.Writer;
32
import java.io.Writer;
33
import java.net.MalformedURLException;
34
import java.net.URL;
33
import java.net.URL;
35
import java.net.URLClassLoader;
34
import java.net.URLClassLoader;
36
import java.util.ArrayList;
35
import java.util.ArrayList;
Lines 1414-1428 Link Here
1414
        }
1413
        }
1415
    }
1414
    }
1416
1415
1417
    protected void initServletContext() {
1416
    protected void initServletContext() throws IOException, JasperException {
1418
        try {
1417
        // TODO: should we use the Ant Project's log?
1419
            context =new JspCServletContext
1418
        PrintWriter log = new PrintWriter(System.out);
1420
                (new PrintWriter(System.out),
1419
        URL resourceBase = new File(uriRoot).getCanonicalFile().toURI().toURL();
1421
                 new URL("file:" + uriRoot.replace('\\','/') + '/'));
1420
        context = new JspCServletContext(log, resourceBase);
1422
            tldLocationsCache = TldLocationsCache.getInstance(context);
1423
        } catch (MalformedURLException me) {
1424
            System.out.println("**" + me);
1425
        }
1426
        rctxt = new JspRuntimeContext(context, this);
1421
        rctxt = new JspRuntimeContext(context, this);
1427
        jspConfig = new JspConfig(context);
1422
        jspConfig = new JspConfig(context);
1428
        tagPluginManager = new TagPluginManager(context);
1423
        tagPluginManager = new TagPluginManager(context);
(-)java/org/apache/jasper/servlet/JspCServletContext.java (-4 / +351 lines)
Lines 19-33 Link Here
19
19
20
20
21
import java.io.File;
21
import java.io.File;
22
import java.io.IOException;
22
import java.io.InputStream;
23
import java.io.InputStream;
23
import java.io.PrintWriter;
24
import java.io.PrintWriter;
24
import java.net.MalformedURLException;
25
import java.net.MalformedURLException;
25
import java.net.URL;
26
import java.net.URL;
27
import java.util.ArrayList;
28
import java.util.Collection;
29
import java.util.Collections;
26
import java.util.EnumSet;
30
import java.util.EnumSet;
27
import java.util.Enumeration;
31
import java.util.Enumeration;
28
import java.util.EventListener;
32
import java.util.EventListener;
29
import java.util.HashSet;
33
import java.util.HashSet;
30
import java.util.Hashtable;
34
import java.util.Hashtable;
35
import java.util.Iterator;
36
import java.util.List;
31
import java.util.Map;
37
import java.util.Map;
32
import java.util.Set;
38
import java.util.Set;
33
import java.util.Vector;
39
import java.util.Vector;
Lines 43-50 Link Here
43
import javax.servlet.SessionCookieConfig;
49
import javax.servlet.SessionCookieConfig;
44
import javax.servlet.SessionTrackingMode;
50
import javax.servlet.SessionTrackingMode;
45
import javax.servlet.descriptor.JspConfigDescriptor;
51
import javax.servlet.descriptor.JspConfigDescriptor;
52
import javax.servlet.descriptor.JspPropertyGroupDescriptor;
53
import javax.servlet.descriptor.TaglibDescriptor;
46
54
55
import org.apache.jasper.JasperException;
47
import org.apache.jasper.util.ExceptionUtils;
56
import org.apache.jasper.util.ExceptionUtils;
57
import org.apache.jasper.xmlparser.ParserUtils;
58
import org.apache.jasper.xmlparser.TreeNode;
48
59
49
60
50
/**
61
/**
Lines 78-88 Link Here
78
    private final URL myResourceBaseURL;
89
    private final URL myResourceBaseURL;
79
90
80
91
92
81
    /**
93
    /**
82
     * Web application class loader.
94
     * Web application class loader.
83
     */
95
     */
84
    private ClassLoader loader;
96
    private ClassLoader loader;
85
97
98
    private final int effectiveMajorVersion;
99
    private final int effectiveMinorVersion;
100
    private final JspConfigDescriptor jspConfigDescriptor;
86
101
87
    // ----------------------------------------------------------- Constructors
102
    // ----------------------------------------------------------- Constructors
88
103
Lines 92-106 Link Here
92
     * @param aLogWriter PrintWriter which is used for <code>log()</code> calls
107
     * @param aLogWriter PrintWriter which is used for <code>log()</code> calls
93
     * @param aResourceBaseURL Resource base URL
108
     * @param aResourceBaseURL Resource base URL
94
     */
109
     */
95
    public JspCServletContext(PrintWriter aLogWriter, URL aResourceBaseURL) {
110
    public JspCServletContext(PrintWriter aLogWriter, URL aResourceBaseURL) throws IOException, JasperException {
96
111
97
        myAttributes = new Hashtable<>();
112
        myAttributes = new Hashtable<>();
98
        myLogWriter = aLogWriter;
113
        myLogWriter = aLogWriter;
99
        myResourceBaseURL = aResourceBaseURL;
114
        myResourceBaseURL = aResourceBaseURL;
100
115
116
        TreeNode mergedWebApp = loadAndMerge();
117
118
        // set effective version for this application
119
        Version version = Version.fromString(mergedWebApp.findAttribute("version"));
120
        effectiveMajorVersion = version.getMajor();
121
        effectiveMinorVersion = version.getMinor();
122
123
        jspConfigDescriptor = new JspCJspConfigDescriptor(mergedWebApp, version);
101
    }
124
    }
102
125
126
    private TreeNode loadAndMerge() throws IOException, JasperException {
127
        ParserUtils pu = new ParserUtils();
128
        TreeNode root = load(pu);
129
        List<TreeNode> libs = getFragments();
130
        for (TreeNode lib : libs) {
131
            merge(root, lib);
132
        }
133
        return root;
134
    }
103
135
136
    private TreeNode load(ParserUtils pu) throws JasperException, IOException {
137
        URL webUrl = getResource("/WEB-INF/web.xml");
138
        if (webUrl == null) {
139
            // No web.xml in the application, use a empty one as default to support Servlet 3.0 applications.
140
            // This avoids inconsistency between compilation triggered by JspServlet which would use the
141
            // container's specification level and JspC which would default to Servlet 2.3 behaviour (including
142
            // unanticipated consequences such as disabling EL evaluation).
143
            webUrl = JspCServletContext.class.getResource("web.xml");
144
        }
145
        try (InputStream is = webUrl.openStream()) {
146
            return pu.parseXMLDocument(webUrl.toExternalForm(), is);
147
        }
148
    }
149
150
    private List<TreeNode> getFragments() {
151
        // TODO: find, parse and order web fragments
152
        // Perhaps the logic for this in ContextConfig could be moved to o.a.tomcat.util and shared.
153
        return Collections.emptyList();
154
    }
155
156
    private void merge(TreeNode root, TreeNode fragment) {
157
        // TODO: merge a web-fragment into the main web.xml
158
    }
159
104
    // --------------------------------------------------------- Public Methods
160
    // --------------------------------------------------------- Public Methods
105
161
106
162
Lines 628-640 Link Here
628
684
629
    @Override
685
    @Override
630
    public int getEffectiveMajorVersion() {
686
    public int getEffectiveMajorVersion() {
631
        return 3;
687
        return effectiveMajorVersion;
632
    }
688
    }
633
689
634
690
635
    @Override
691
    @Override
636
    public int getEffectiveMinorVersion() {
692
    public int getEffectiveMinorVersion() {
637
        return 0;
693
        return effectiveMinorVersion;
638
    }
694
    }
639
695
640
696
Lines 646-652 Link Here
646
702
647
    @Override
703
    @Override
648
    public JspConfigDescriptor getJspConfigDescriptor() {
704
    public JspConfigDescriptor getJspConfigDescriptor() {
649
        return null;
705
        return jspConfigDescriptor;
650
    }
706
    }
651
707
652
708
Lines 660-663 Link Here
660
    public String getVirtualServerName() {
716
    public String getVirtualServerName() {
661
        return null;
717
        return null;
662
    }
718
    }
719
720
    private static enum Version {
721
        VERSION_2_3(2, 3),
722
        VERSION_2_4(2, 4),
723
        VERSION_2_5(2, 5),
724
        VERSION_3_0(3, 0),
725
        VERSION_3_1(3, 1);
726
        private final int major;
727
        private final int minor;
728
        private Version(int major, int minor) {
729
            this.major = major;
730
            this.minor = minor;
731
        }
732
733
        private int getMajor() {
734
            return major;
735
        }
736
737
        private int getMinor() {
738
            return minor;
739
        }
740
741
        private static Version fromString(String version) {
742
            if (version == null) {
743
                return VERSION_2_3;
744
            }
745
            switch (version) {
746
                case "2.4":
747
                    return VERSION_2_4;
748
                case "2.5":
749
                    return VERSION_2_5;
750
                case "3.0":
751
                    return VERSION_3_0;
752
                case "3.1":
753
                    return VERSION_3_1;
754
                default:
755
                    return VERSION_2_3;
756
            }
757
        }
758
    }
759
760
    private static class JspCJspConfigDescriptor implements JspConfigDescriptor {
761
        private final Collection<TaglibDescriptor> taglibs;
762
        private final Collection<JspPropertyGroupDescriptor> jspPropertyGroups;
763
764
        private JspCJspConfigDescriptor(TreeNode webapp, Version version) {
765
            // In Servlet 2.3, <taglib> elements were under the root and there were no property groups
766
            if (version == Version.VERSION_2_3) {
767
                taglibs = taglibDescriptors(webapp);
768
                jspPropertyGroups = Collections.emptyList();
769
                return;
770
            }
771
772
            // In later versions, JSP configuration is under the <jsp-config> element
773
            TreeNode jspConfig = webapp.findChild("jsp-config");
774
            if (jspConfig == null) {
775
                taglibs = Collections.emptyList();
776
                jspPropertyGroups = Collections.emptyList();
777
                return;
778
            }
779
            taglibs = taglibDescriptors(jspConfig);
780
            jspPropertyGroups = propertyGroups(jspConfig);
781
        }
782
783
        private Collection<TaglibDescriptor> taglibDescriptors(TreeNode parent) {
784
            Collection<TaglibDescriptor> descriptors = new ArrayList<>();
785
            Iterator<TreeNode> taglibs = parent.findChildren("taglib");
786
            while (taglibs.hasNext()) {
787
                TreeNode taglib = taglibs.next();
788
                final String tagUri = optionalChild(taglib, "taglib-uri");
789
                final String tagLoc = optionalChild(taglib, "taglib-location");
790
                descriptors.add(new JspCTaglibDescriptor(tagUri, tagLoc));
791
            }
792
            return Collections.unmodifiableCollection(descriptors);
793
        }
794
795
        private Collection<JspPropertyGroupDescriptor> propertyGroups(TreeNode parent) {
796
            List<JspPropertyGroupDescriptor> descriptors = new ArrayList<>();
797
            Iterator<TreeNode> groups = parent.findChildren("jsp-property-group");
798
            while (groups.hasNext()) {
799
                TreeNode group = groups.next();
800
                String buffer = null;
801
                String defaultContentType = null;
802
                String deferedSyntaxAllowedAsLiteral = null;
803
                String elIgnored = null;
804
                String errorOnUndeclaredNamespace = null;
805
                List<String> includeCodas = new ArrayList<>();
806
                List<String> includePreludes = new ArrayList<>();
807
                String isXml = null;
808
                String pageEncoding = null;
809
                String scriptingInvalid = null;
810
                String trimDirectiveWhitespaces = null;
811
                List<String> urlPatterns = new ArrayList<>();
812
                Iterator<TreeNode> child = group.findChildren();
813
                while (child.hasNext()) {
814
                    TreeNode node = child.next();
815
                    String body = node.getBody();
816
                    switch (node.getName()) {
817
                        case "buffer":
818
                            buffer = body;
819
                            break;
820
                        case "default-content-type":
821
                            defaultContentType = body;
822
                            break;
823
                        case "deferred-syntax-allowed-as-literal":
824
                            deferedSyntaxAllowedAsLiteral = body;
825
                            break;
826
                        case "el-ignored":
827
                            elIgnored = body;
828
                            break;
829
                        case "error-on-undeclared-namespace":
830
                            errorOnUndeclaredNamespace = body;
831
                            break;
832
                        case "include-coda":
833
                            includeCodas.add(body);
834
                            break;
835
                        case "include-prelude":
836
                            includePreludes.add(body);
837
                            break;
838
                        case "is-xml":
839
                            isXml = body;
840
                            break;
841
                        case "page-encoding":
842
                            pageEncoding = body;
843
                            break;
844
                        case "scripting-invalid":
845
                            scriptingInvalid = body;
846
                            break;
847
                        case "trim-directive-whitespaces":
848
                            buffer = body;
849
                            break;
850
                        case "url-pattern":
851
                            urlPatterns.add(body);
852
                            break;
853
                    }
854
                }
855
                descriptors.add(new JspCPropertyGroupDescriptor(
856
                        buffer,
857
                        defaultContentType,
858
                        deferedSyntaxAllowedAsLiteral,
859
                        elIgnored,
860
                        errorOnUndeclaredNamespace,
861
                        Collections.unmodifiableCollection(includeCodas),
862
                        Collections.unmodifiableCollection(includePreludes),
863
                        isXml,
864
                        pageEncoding,
865
                        scriptingInvalid,
866
                        trimDirectiveWhitespaces,
867
                        Collections.unmodifiableCollection(urlPatterns)));
868
            }
869
            return Collections.unmodifiableList(descriptors);
870
        }
871
872
        private static String optionalChild(TreeNode parent, String name) {
873
            TreeNode child = parent.findChild(name);
874
            return child == null ? null : child.getBody();
875
        }
876
877
        @Override
878
        public Collection<TaglibDescriptor> getTaglibs() {
879
            return taglibs;
880
        }
881
882
        @Override
883
        public Collection<JspPropertyGroupDescriptor> getJspPropertyGroups() {
884
            return jspPropertyGroups;
885
        }
886
887
        // TODO: share with org.apache.catalina.core.ApplicationTaglibDescriptor
888
        private static class JspCTaglibDescriptor implements TaglibDescriptor {
889
            private final String tagUri;
890
            private final String tagLoc;
891
892
            public JspCTaglibDescriptor(String tagUri, String tagLoc) {
893
                this.tagUri = tagUri;
894
                this.tagLoc = tagLoc;
895
            }
896
897
            @Override
898
            public String getTaglibURI() {
899
                return tagUri;
900
            }
901
902
            @Override
903
            public String getTaglibLocation() {
904
                return tagLoc;
905
            }
906
        }
907
908
        // TODO: share with org.apache.catalina.core.ApplicationJspPropertyGroupDescriptor
909
        private static class JspCPropertyGroupDescriptor implements JspPropertyGroupDescriptor {
910
            private final String buffer;
911
            private final String defaultContentType;
912
            private final String deferedSyntaxAllowedAsLiteral;
913
            private final String elIgnored;
914
            private final String errorOnUndeclaredNamespace;
915
            private final Collection<String> includeCodas;
916
            private final Collection<String> includePreludes;
917
            private final String isXml;
918
            private final String pageEncoding;
919
            private final String scriptingInvalid;
920
            private final String trimDirectiveWhitespaces;
921
            private final Collection<String> urlPatterns;
922
923
            private JspCPropertyGroupDescriptor(String buffer,
924
                                                String defaultContentType,
925
                                                String deferedSyntaxAllowedAsLiteral,
926
                                                String elIgnored,
927
                                                String errorOnUndeclaredNamespace,
928
                                                Collection<String> includeCodas,
929
                                                Collection<String> includePreludes,
930
                                                String isXml,
931
                                                String pageEncoding,
932
                                                String scriptingInvalid,
933
                                                String trimDirectiveWhitespaces,
934
                                                Collection<String> urlPatterns) {
935
                this.buffer = buffer;
936
                this.defaultContentType = defaultContentType;
937
                this.deferedSyntaxAllowedAsLiteral = deferedSyntaxAllowedAsLiteral;
938
                this.elIgnored = elIgnored;
939
                this.errorOnUndeclaredNamespace = errorOnUndeclaredNamespace;
940
                this.includeCodas = includeCodas;
941
                this.includePreludes = includePreludes;
942
                this.isXml = isXml;
943
                this.pageEncoding = pageEncoding;
944
                this.scriptingInvalid = scriptingInvalid;
945
                this.trimDirectiveWhitespaces = trimDirectiveWhitespaces;
946
                this.urlPatterns = urlPatterns;
947
            }
948
949
            @Override
950
            public Collection<String> getUrlPatterns() {
951
                return urlPatterns;
952
            }
953
954
            @Override
955
            public String getElIgnored() {
956
                return elIgnored;
957
            }
958
959
            @Override
960
            public String getPageEncoding() {
961
                return pageEncoding;
962
            }
963
964
            @Override
965
            public String getScriptingInvalid() {
966
                return scriptingInvalid;
967
            }
968
969
            @Override
970
            public String getIsXml() {
971
                return isXml;
972
            }
973
974
            @Override
975
            public Collection<String> getIncludePreludes() {
976
                return includePreludes;
977
            }
978
979
            @Override
980
            public Collection<String> getIncludeCodas() {
981
                return includeCodas;
982
            }
983
984
            @Override
985
            public String getDeferredSyntaxAllowedAsLiteral() {
986
                return deferedSyntaxAllowedAsLiteral;
987
            }
988
989
            @Override
990
            public String getTrimDirectiveWhitespaces() {
991
                return trimDirectiveWhitespaces;
992
            }
993
994
            @Override
995
            public String getDefaultContentType() {
996
                return defaultContentType;
997
            }
998
999
            @Override
1000
            public String getBuffer() {
1001
                return buffer;
1002
            }
1003
1004
            @Override
1005
            public String getErrorOnUndeclaredNamespace() {
1006
                return errorOnUndeclaredNamespace;
1007
            }
1008
        }
1009
    }
663
}
1010
}
(-)java/org/apache/jasper/servlet/web.xml (+25 lines)
Line 0 Link Here
1
<?xml version="1.0" encoding="ISO-8859-1"?>
2
<!--
3
  Licensed to the Apache Software Foundation (ASF) under one or more
4
  contributor license agreements.  See the NOTICE file distributed with
5
  this work for additional information regarding copyright ownership.
6
  The ASF licenses this file to You under the Apache License, Version 2.0
7
  (the "License"); you may not use this file except in compliance with
8
  the License.  You may obtain a copy of the License at
9
10
      http://www.apache.org/licenses/LICENSE-2.0
11
12
  Unless required by applicable law or agreed to in writing, software
13
  distributed under the License is distributed on an "AS IS" BASIS,
14
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
  See the License for the specific language governing permissions and
16
  limitations under the License.
17
-->
18
<!--
19
  Default web.xml used by Jasper if one is not provided in the application.
20
-->
21
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
22
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
24
         version="3.1">
25
</web-app>
(-)java/org/apache/jasper/compiler/JspConfig.java (-247 / +116 lines)
Lines 17-30 Link Here
17
17
18
package org.apache.jasper.compiler;
18
package org.apache.jasper.compiler;
19
19
20
import java.util.Iterator;
20
import java.util.ArrayList;
21
import java.util.Collection;
22
import java.util.List;
21
import java.util.Vector;
23
import java.util.Vector;
22
24
23
import javax.servlet.ServletContext;
25
import javax.servlet.ServletContext;
26
import javax.servlet.descriptor.JspConfigDescriptor;
27
import javax.servlet.descriptor.JspPropertyGroupDescriptor;
24
28
25
import org.apache.jasper.JasperException;
26
import org.apache.jasper.xmlparser.ParserUtils;
27
import org.apache.jasper.xmlparser.TreeNode;
28
import org.apache.juli.logging.Log;
29
import org.apache.juli.logging.Log;
29
import org.apache.juli.logging.LogFactory;
30
import org.apache.juli.logging.LogFactory;
30
31
Lines 41-249 Link Here
41
    // Logger
42
    // Logger
42
    private final Log log = LogFactory.getLog(JspConfig.class);
43
    private final Log log = LogFactory.getLog(JspConfig.class);
43
44
44
    private Vector<JspPropertyGroup> jspProperties = null;
45
    private final JspProperty defaultJspProperty;
45
    private final ServletContext ctxt;
46
    private final List<JspPropertyGroup> jspProperties;
46
    private volatile boolean initialized = false;
47
47
48
    private static final String defaultIsXml = null;    // unspecified
49
    private String defaultIsELIgnored = null;           // unspecified
50
    private static final String defaultIsScriptingInvalid = null;
51
    private String defaultDeferedSyntaxAllowedAsLiteral = null;
52
    private static final String defaultTrimDirectiveWhitespaces = null;
53
    private static final String defaultDefaultContentType = null;
54
    private static final String defaultBuffer = null;
55
    private static final String defaultErrorOnUndeclaredNamespace = "false";
56
    private JspProperty defaultJspProperty;
57
58
    public JspConfig(ServletContext ctxt) {
48
    public JspConfig(ServletContext ctxt) {
59
        this.ctxt = ctxt;
49
        // set up default behaviour based on effective Servlet specification version
60
    }
50
        int major = ctxt.getEffectiveMajorVersion();
51
        int minor = ctxt.getEffectiveMinorVersion();
61
52
62
    private double getVersion(TreeNode webApp) {
53
        if (major < 2 || major == 2 && minor < 4) {
63
        String v = webApp.findAttribute("version");
54
            // < 2.4 : elIgnored, deferedSyntaxAllowedAsLiteral, and no jsp-config
64
        if (v != null) {
55
            defaultJspProperty = new JspProperty("true", "true");
65
            try {
56
            jspProperties = null;
66
                return Double.parseDouble(v);
57
            return;
67
            } catch (NumberFormatException e) {
58
        } else if (major == 2 && minor == 4) {
68
            }
59
            // 2.4 : deferedSyntaxAllowedAsLiteral
60
            defaultJspProperty = new JspProperty(null, "true");
61
        } else {
62
            defaultJspProperty = new JspProperty(null, null);
69
        }
63
        }
70
        return 2.3;
64
        jspProperties = processJspConfig(ctxt);
71
    }
65
    }
72
66
73
    private void processWebDotXml() throws JasperException {
67
    private List<JspPropertyGroup> processJspConfig(ServletContext ctxt) {
68
        JspConfigDescriptor jspConfig = ctxt.getJspConfigDescriptor();
69
        if (jspConfig == null) {
70
            // no jsp-config for this application
71
            return null;
72
        }
74
73
75
        WebXml webXml = null;
74
        Collection<JspPropertyGroupDescriptor> jspPropertyGroups = jspConfig.getJspPropertyGroups();
75
        List<JspPropertyGroup> jspProperties = new ArrayList<>();
76
        for (JspPropertyGroupDescriptor jspPropertyGroup : jspPropertyGroups) {
76
77
77
        try {
78
            Collection<String> urlPatterns = jspPropertyGroup.getUrlPatterns();
78
            webXml = new WebXml(ctxt);
79
            if (urlPatterns.isEmpty()) {
79
80
                continue;
80
            TreeNode webApp = null;
81
            if (webXml.getInputSource() != null) {
82
                ParserUtils pu = new ParserUtils();
83
                webApp = pu.parseXMLDocument(webXml.getSystemId(),
84
                        webXml.getInputSource());
85
            }
81
            }
86
82
87
            if (webApp == null
83
            JspProperty property = new JspProperty(jspPropertyGroup.getIsXml(),
88
                    || getVersion(webApp) < 2.4) {
84
                    jspPropertyGroup.getElIgnored(),
89
                defaultIsELIgnored = "true";
85
                    jspPropertyGroup.getScriptingInvalid(),
90
                defaultDeferedSyntaxAllowedAsLiteral = "true";
86
                    jspPropertyGroup.getPageEncoding(),
91
                return;
87
                    new Vector<>(jspPropertyGroup.getIncludePreludes()),
92
            }
88
                    new Vector<>(jspPropertyGroup.getIncludeCodas()),
93
            if (getVersion(webApp) < 2.5) {
89
                    jspPropertyGroup.getDeferredSyntaxAllowedAsLiteral(),
94
                defaultDeferedSyntaxAllowedAsLiteral = "true";
90
                    jspPropertyGroup.getTrimDirectiveWhitespaces(),
95
            }
91
                    jspPropertyGroup.getDefaultContentType(),
96
            TreeNode jspConfig = webApp.findChild("jsp-config");
92
                    jspPropertyGroup.getBuffer(),
97
            if (jspConfig == null) {
93
                    jspPropertyGroup.getErrorOnUndeclaredNamespace());
98
                return;
99
            }
100
94
101
            jspProperties = new Vector<>();
95
            // Add one JspPropertyGroup for each URL Pattern.  This makes
102
            Iterator<TreeNode> jspPropertyList =
96
            // the matching logic easier.
103
                jspConfig.findChildren("jsp-property-group");
97
            for (String urlPattern : urlPatterns) {
104
            while (jspPropertyList.hasNext()) {
98
                String path = null;
99
                String extension = null;
105
100
106
                TreeNode element = jspPropertyList.next();
101
                if (urlPattern.indexOf('*') < 0) {
107
                Iterator<TreeNode> list = element.findChildren();
102
                    // Exact match
108
103
                    path = urlPattern;
109
                Vector<String> urlPatterns = new Vector<>();
104
                } else {
110
                String pageEncoding = null;
105
                    int i = urlPattern.lastIndexOf('/');
111
                String scriptingInvalid = null;
106
                    String file;
112
                String elIgnored = null;
107
                    if (i >= 0) {
113
                String isXml = null;
108
                        path = urlPattern.substring(0, i + 1);
114
                Vector<String> includePrelude = new Vector<>();
109
                        file = urlPattern.substring(i + 1);
115
                Vector<String> includeCoda = new Vector<>();
116
                String deferredSyntaxAllowedAsLiteral = null;
117
                String trimDirectiveWhitespaces = null;
118
                String defaultContentType = null;
119
                String buffer = null;
120
                String errorOnUndeclaredNamespace = null;
121
122
                while (list.hasNext()) {
123
124
                    element = list.next();
125
                    String tname = element.getName();
126
127
                    if ("url-pattern".equals(tname))
128
                        urlPatterns.addElement( element.getBody() );
129
                    else if ("page-encoding".equals(tname))
130
                        pageEncoding = element.getBody();
131
                    else if ("is-xml".equals(tname))
132
                        isXml = element.getBody();
133
                    else if ("el-ignored".equals(tname))
134
                        elIgnored = element.getBody();
135
                    else if ("scripting-invalid".equals(tname))
136
                        scriptingInvalid = element.getBody();
137
                    else if ("include-prelude".equals(tname))
138
                        includePrelude.addElement(element.getBody());
139
                    else if ("include-coda".equals(tname))
140
                        includeCoda.addElement(element.getBody());
141
                    else if ("deferred-syntax-allowed-as-literal".equals(tname))
142
                        deferredSyntaxAllowedAsLiteral = element.getBody();
143
                    else if ("trim-directive-whitespaces".equals(tname))
144
                        trimDirectiveWhitespaces = element.getBody();
145
                    else if ("default-content-type".equals(tname))
146
                        defaultContentType = element.getBody();
147
                    else if ("buffer".equals(tname))
148
                        buffer = element.getBody();
149
                    else if ("error-on-undeclared-namespace".equals(tname))
150
                        errorOnUndeclaredNamespace = element.getBody();
151
                }
152
153
                if (urlPatterns.size() == 0) {
154
                    continue;
155
                }
156
157
                // Add one JspPropertyGroup for each URL Pattern.  This makes
158
                // the matching logic easier.
159
                for( int p = 0; p < urlPatterns.size(); p++ ) {
160
                    String urlPattern = urlPatterns.elementAt( p );
161
                    String path = null;
162
                    String extension = null;
163
164
                    if (urlPattern.indexOf('*') < 0) {
165
                        // Exact match
166
                        path = urlPattern;
167
                    } else {
110
                    } else {
168
                        int i = urlPattern.lastIndexOf('/');
111
                        file = urlPattern;
169
                        String file;
112
                    }
170
                        if (i >= 0) {
171
                            path = urlPattern.substring(0,i+1);
172
                            file = urlPattern.substring(i+1);
173
                        } else {
174
                            file = urlPattern;
175
                        }
176
113
177
                        // pattern must be "*", or of the form "*.jsp"
114
                    // pattern must be "*", or of the form "*.jsp"
178
                        if (file.equals("*")) {
115
                    if (file.equals("*")) {
179
                            extension = "*";
116
                        extension = "*";
180
                        } else if (file.startsWith("*.")) {
117
                    } else if (file.startsWith("*.")) {
181
                            extension = file.substring(file.indexOf('.')+1);
118
                        extension = file.substring(file.indexOf('.') + 1);
182
                        }
119
                    }
183
120
184
                        // The url patterns are reconstructed as the following:
121
                    // The url patterns are reconstructed as the following:
185
                        // path != null, extension == null:  / or /foo/bar.ext
122
                    // path != null, extension == null:  / or /foo/bar.ext
186
                        // path == null, extension != null:  *.ext
123
                    // path == null, extension != null:  *.ext
187
                        // path != null, extension == "*":   /foo/*
124
                    // path != null, extension == "*":   /foo/*
188
                        boolean isStar = "*".equals(extension);
125
                    boolean isStar = "*".equals(extension);
189
                        if ((path == null && (extension == null || isStar))
126
                    if ((path == null && (extension == null || isStar))
190
                                || (path != null && !isStar)) {
127
                            || (path != null && !isStar)) {
191
                            if (log.isWarnEnabled()) {
128
                        if (log.isWarnEnabled()) {
192
                                log.warn(Localizer.getMessage(
129
                            log.warn(Localizer.getMessage(
193
                                        "jsp.warning.bad.urlpattern.propertygroup",
130
                                    "jsp.warning.bad.urlpattern.propertygroup",
194
                                        urlPattern));
131
                                    urlPattern));
195
                            }
196
                            continue;
197
                        }
132
                        }
133
                        continue;
198
                    }
134
                    }
135
                }
199
136
200
                    JspProperty property = new JspProperty(isXml,
137
                JspPropertyGroup propertyGroup =
201
                            elIgnored,
202
                            scriptingInvalid,
203
                            pageEncoding,
204
                            includePrelude,
205
                            includeCoda,
206
                            deferredSyntaxAllowedAsLiteral,
207
                            trimDirectiveWhitespaces,
208
                            defaultContentType,
209
                            buffer,
210
                            errorOnUndeclaredNamespace);
211
                    JspPropertyGroup propertyGroup =
212
                        new JspPropertyGroup(path, extension, property);
138
                        new JspPropertyGroup(path, extension, property);
213
139
214
                    jspProperties.addElement(propertyGroup);
140
                jspProperties.add(propertyGroup);
215
                }
216
            }
141
            }
217
        } catch (Exception ex) {
218
            throw new JasperException(ex);
219
        } finally {
220
            if (webXml != null) {
221
                webXml.close();
222
            }
223
        }
142
        }
143
        return jspProperties;
224
    }
144
    }
225
145
226
    private void init() throws JasperException {
227
228
        if (!initialized) {
229
            synchronized (this) {
230
                if (!initialized) {
231
                    processWebDotXml();
232
                    defaultJspProperty = new JspProperty(defaultIsXml,
233
                            defaultIsELIgnored,
234
                            defaultIsScriptingInvalid,
235
                            null, null, null,
236
                            defaultDeferedSyntaxAllowedAsLiteral,
237
                            defaultTrimDirectiveWhitespaces,
238
                            defaultDefaultContentType,
239
                            defaultBuffer,
240
                            defaultErrorOnUndeclaredNamespace);
241
                    initialized = true;
242
                }
243
            }
244
        }
245
    }
246
247
    /**
146
    /**
248
     * Select the property group that has more restrictive url-pattern.
147
     * Select the property group that has more restrictive url-pattern.
249
     * In case of tie, select the first.
148
     * In case of tie, select the first.
Lines 268-277 Link Here
268
            // Both specifies a *.ext, keep the first one
167
            // Both specifies a *.ext, keep the first one
269
            return prev;
168
            return prev;
270
        }
169
        }
271
        if (prevPath == null && currPath != null) {
170
        if (prevPath == null) {
272
            return curr;
171
            return curr;
273
        }
172
        }
274
        if (prevPath != null && currPath == null) {
173
        if (currPath == null) {
275
            return prev;
174
            return prev;
276
        }
175
        }
277
        if (prevPath.length() >= currPath.length()) {
176
        if (prevPath.length() >= currPath.length()) {
Lines 286-295 Link Here
286
     * @param uri the resource supplied.
185
     * @param uri the resource supplied.
287
     * @return a JspProperty indicating the best match, or some default.
186
     * @return a JspProperty indicating the best match, or some default.
288
     */
187
     */
289
    public JspProperty findJspProperty(String uri) throws JasperException {
188
    public JspProperty findJspProperty(String uri) {
290
189
291
        init();
292
293
        // JSP Configuration settings do not apply to tag files
190
        // JSP Configuration settings do not apply to tag files
294
        if (jspProperties == null || uri.endsWith(".tag")
191
        if (jspProperties == null || uri.endsWith(".tag")
295
                || uri.endsWith(".tagx")) {
192
                || uri.endsWith(".tagx")) {
Lines 320-329 Link Here
320
        JspPropertyGroup bufferMatch = null;
217
        JspPropertyGroup bufferMatch = null;
321
        JspPropertyGroup errorOnUndeclaredNamespaceMatch = null;
218
        JspPropertyGroup errorOnUndeclaredNamespaceMatch = null;
322
219
323
        Iterator<JspPropertyGroup> iter = jspProperties.iterator();
220
        for (JspPropertyGroup jpg : jspProperties) {
324
        while (iter.hasNext()) {
325
221
326
            JspPropertyGroup jpg = iter.next();
327
            JspProperty jp = jpg.getJspProperty();
222
            JspProperty jp = jpg.getJspProperty();
328
223
329
            // (arrays will be the same length)
224
            // (arrays will be the same length)
Lines 339-345 Link Here
339
            } else {
234
            } else {
340
                // Matching patterns *.ext or /p/*
235
                // Matching patterns *.ext or /p/*
341
                if (path != null && uriPath != null &&
236
                if (path != null && uriPath != null &&
342
                        ! uriPath.startsWith(path)) {
237
                        !uriPath.startsWith(path)) {
343
                    // not matched
238
                    // not matched
344
                    continue;
239
                    continue;
345
                }
240
                }
Lines 368-457 Link Here
368
            }
263
            }
369
            if (jp.isScriptingInvalid() != null) {
264
            if (jp.isScriptingInvalid() != null) {
370
                scriptingInvalidMatch =
265
                scriptingInvalidMatch =
371
                    selectProperty(scriptingInvalidMatch, jpg);
266
                        selectProperty(scriptingInvalidMatch, jpg);
372
            }
267
            }
373
            if (jp.getPageEncoding() != null) {
268
            if (jp.getPageEncoding() != null) {
374
                pageEncodingMatch = selectProperty(pageEncodingMatch, jpg);
269
                pageEncodingMatch = selectProperty(pageEncodingMatch, jpg);
375
            }
270
            }
376
            if (jp.isDeferedSyntaxAllowedAsLiteral() != null) {
271
            if (jp.isDeferedSyntaxAllowedAsLiteral() != null) {
377
                deferedSyntaxAllowedAsLiteralMatch =
272
                deferedSyntaxAllowedAsLiteralMatch =
378
                    selectProperty(deferedSyntaxAllowedAsLiteralMatch, jpg);
273
                        selectProperty(deferedSyntaxAllowedAsLiteralMatch, jpg);
379
            }
274
            }
380
            if (jp.isTrimDirectiveWhitespaces() != null) {
275
            if (jp.isTrimDirectiveWhitespaces() != null) {
381
                trimDirectiveWhitespacesMatch =
276
                trimDirectiveWhitespacesMatch =
382
                    selectProperty(trimDirectiveWhitespacesMatch, jpg);
277
                        selectProperty(trimDirectiveWhitespacesMatch, jpg);
383
            }
278
            }
384
            if (jp.getDefaultContentType() != null) {
279
            if (jp.getDefaultContentType() != null) {
385
                defaultContentTypeMatch =
280
                defaultContentTypeMatch =
386
                    selectProperty(defaultContentTypeMatch, jpg);
281
                        selectProperty(defaultContentTypeMatch, jpg);
387
            }
282
            }
388
            if (jp.getBuffer() != null) {
283
            if (jp.getBuffer() != null) {
389
                bufferMatch = selectProperty(bufferMatch, jpg);
284
                bufferMatch = selectProperty(bufferMatch, jpg);
390
            }
285
            }
391
            if (jp.isErrorOnUndeclaredNamespace() != null) {
286
            if (jp.isErrorOnUndeclaredNamespace() != null) {
392
                errorOnUndeclaredNamespaceMatch =
287
                errorOnUndeclaredNamespaceMatch =
393
                    selectProperty(errorOnUndeclaredNamespaceMatch, jpg);
288
                        selectProperty(errorOnUndeclaredNamespaceMatch, jpg);
394
            }
289
            }
395
        }
290
        }
396
291
397
292
398
        String isXml = defaultIsXml;
293
        String isXml = defaultProperty(isXmlMatch).isXml();
399
        String isELIgnored = defaultIsELIgnored;
294
        String isELIgnored = defaultProperty(elIgnoredMatch).isELIgnored();
400
        String isScriptingInvalid = defaultIsScriptingInvalid;
295
        String isScriptingInvalid = defaultProperty(scriptingInvalidMatch).isScriptingInvalid();
401
        String pageEncoding = null;
296
        String pageEncoding = defaultProperty(pageEncodingMatch).getPageEncoding();
402
        String isDeferedSyntaxAllowedAsLiteral =
297
        String isDeferedSyntaxAllowedAsLiteral = defaultProperty(defaultContentTypeMatch).isDeferedSyntaxAllowedAsLiteral();
403
            defaultDeferedSyntaxAllowedAsLiteral;
298
        String isTrimDirectiveWhitespaces = defaultProperty(trimDirectiveWhitespacesMatch).isTrimDirectiveWhitespaces();
404
        String isTrimDirectiveWhitespaces = defaultTrimDirectiveWhitespaces;
299
        String defaultContentType = defaultProperty(defaultContentTypeMatch).getDefaultContentType();
405
        String defaultContentType = defaultDefaultContentType;
300
        String buffer = defaultProperty(bufferMatch).getBuffer();
406
        String buffer = defaultBuffer;
301
        String errorOnUndeclaredNamespace = defaultProperty(errorOnUndeclaredNamespaceMatch).isErrorOnUndeclaredNamespace();
407
        String errorOnUndelcaredNamespace = defaultErrorOnUndeclaredNamespace;
408
302
409
        if (isXmlMatch != null) {
410
            isXml = isXmlMatch.getJspProperty().isXml();
411
        }
412
        if (elIgnoredMatch != null) {
413
            isELIgnored = elIgnoredMatch.getJspProperty().isELIgnored();
414
        }
415
        if (scriptingInvalidMatch != null) {
416
            isScriptingInvalid =
417
                scriptingInvalidMatch.getJspProperty().isScriptingInvalid();
418
        }
419
        if (pageEncodingMatch != null) {
420
            pageEncoding = pageEncodingMatch.getJspProperty().getPageEncoding();
421
        }
422
        if (deferedSyntaxAllowedAsLiteralMatch != null) {
423
            isDeferedSyntaxAllowedAsLiteral =
424
                deferedSyntaxAllowedAsLiteralMatch.getJspProperty().isDeferedSyntaxAllowedAsLiteral();
425
        }
426
        if (trimDirectiveWhitespacesMatch != null) {
427
            isTrimDirectiveWhitespaces =
428
                trimDirectiveWhitespacesMatch.getJspProperty().isTrimDirectiveWhitespaces();
429
        }
430
        if (defaultContentTypeMatch != null) {
431
            defaultContentType =
432
                defaultContentTypeMatch.getJspProperty().getDefaultContentType();
433
        }
434
        if (bufferMatch != null) {
435
            buffer = bufferMatch.getJspProperty().getBuffer();
436
        }
437
        if (errorOnUndeclaredNamespaceMatch != null) {
438
            errorOnUndelcaredNamespace =
439
                errorOnUndeclaredNamespaceMatch.getJspProperty().isErrorOnUndeclaredNamespace();
440
        }
441
442
        return new JspProperty(isXml, isELIgnored, isScriptingInvalid,
303
        return new JspProperty(isXml, isELIgnored, isScriptingInvalid,
443
                pageEncoding, includePreludes, includeCodas,
304
                pageEncoding, includePreludes, includeCodas,
444
                isDeferedSyntaxAllowedAsLiteral, isTrimDirectiveWhitespaces,
305
                isDeferedSyntaxAllowedAsLiteral, isTrimDirectiveWhitespaces,
445
                defaultContentType, buffer, errorOnUndelcaredNamespace);
306
                defaultContentType, buffer, errorOnUndeclaredNamespace);
446
    }
307
    }
447
308
309
    private JspProperty defaultProperty(JspPropertyGroup match) {
310
        if (match == null) {
311
            return defaultJspProperty;
312
        } else {
313
            return match.getJspProperty();
314
        }
315
    }
316
448
    /**
317
    /**
449
     * To find out if an uri matches an url pattern in jsp config.  If so,
318
     * To find out if an uri matches an url pattern in jsp config.  If so,
450
     * then the uri is a JSP page.  This is used primarily for jspc.
319
     * then the uri is a JSP page.  This is used primarily for jspc.
451
     */
320
     */
452
    public boolean isJspPage(String uri) throws JasperException {
321
    public boolean isJspPage(String uri) {
453
322
454
        init();
455
        if (jspProperties == null) {
323
        if (jspProperties == null) {
456
            return false;
324
            return false;
457
        }
325
        }
Lines 467-477 Link Here
467
            uriExtension = uri.substring(index+1);
335
            uriExtension = uri.substring(index+1);
468
        }
336
        }
469
337
470
        Iterator<JspPropertyGroup> iter = jspProperties.iterator();
338
        for (JspPropertyGroup jpg : jspProperties) {
471
        while (iter.hasNext()) {
472
339
473
            JspPropertyGroup jpg = iter.next();
474
475
            String extension = jpg.getExtension();
340
            String extension = jpg.getExtension();
476
            String path = jpg.getPath();
341
            String path = jpg.getPath();
477
342
Lines 530-535 Link Here
530
        private final String buffer;
395
        private final String buffer;
531
        private final String errorOnUndeclaredNamespace;
396
        private final String errorOnUndeclaredNamespace;
532
397
398
        private JspProperty(String elIgnored, String deferedSyntaxAllowedAsLiteral) {
399
            this(null, elIgnored, null, null, null, null, deferedSyntaxAllowedAsLiteral, null, null, null, null);
400
        }
401
533
        public JspProperty(String isXml, String elIgnored,
402
        public JspProperty(String isXml, String elIgnored,
534
                String scriptingInvalid, String pageEncoding,
403
                String scriptingInvalid, String pageEncoding,
535
                Vector<String> includePrelude, Vector<String> includeCoda,
404
                Vector<String> includePrelude, Vector<String> includeCoda,
(-)java/org/apache/jasper/compiler/TldLocationsCache.java (-46 / +25 lines)
Lines 27-32 Link Here
27
import java.util.Set;
27
import java.util.Set;
28
28
29
import javax.servlet.ServletContext;
29
import javax.servlet.ServletContext;
30
import javax.servlet.descriptor.JspConfigDescriptor;
31
import javax.servlet.descriptor.TaglibDescriptor;
30
32
31
import org.apache.jasper.JasperException;
33
import org.apache.jasper.JasperException;
32
import org.apache.jasper.util.ExceptionUtils;
34
import org.apache.jasper.util.ExceptionUtils;
Lines 235-294 Link Here
235
    /*
237
    /*
236
     * Populates taglib map described in web.xml.
238
     * Populates taglib map described in web.xml.
237
     *
239
     *
238
     * This is not kept in sync with o.a.c.startup.TldConfig as the Jasper only
240
     * This is not kept in sync with o.a.c.startup.TldConfig as a) Jasper only
239
     * needs the URI to TLD mappings from scan web.xml whereas TldConfig needs
241
     * needs the URI to TLD mappings and b) Jasper can obtain the information
240
     * to scan the actual TLD files.
242
     * from the ServletContext.
241
     */
243
     */
242
    private void tldScanWebXml() throws Exception {
244
    private void tldScanWebXml() throws Exception {
243
245
244
        WebXml webXml = null;
246
        JspConfigDescriptor jspConfig = ctxt.getJspConfigDescriptor();
245
        try {
247
        if (jspConfig == null) {
246
            webXml = new WebXml(ctxt);
248
            // no jsp-config for this application
247
            if (webXml.getInputSource() == null) {
249
            return;
248
                return;
250
        }
249
            }
250
251
251
            // Parse the web application deployment descriptor
252
        for (TaglibDescriptor taglib : jspConfig.getTaglibs()) {
252
            TreeNode webtld = null;
253
            webtld = new ParserUtils().parseXMLDocument(webXml.getSystemId(),
254
                    webXml.getInputSource());
255
253
256
            // Allow taglib to be an element of the root or jsp-config (JSP2.0)
254
            String tagUri = taglib.getTaglibURI();
257
            TreeNode jspConfig = webtld.findChild("jsp-config");
255
            String tagLoc = taglib.getTaglibLocation();
258
            if (jspConfig != null) {
259
                webtld = jspConfig;
260
            }
261
            Iterator<TreeNode> taglibs = webtld.findChildren("taglib");
262
            while (taglibs.hasNext()) {
263
256
264
                // Parse the next <taglib> element
257
            // Save this location if appropriate
265
                TreeNode taglib = taglibs.next();
258
            if (tagLoc == null) {
266
                String tagUri = null;
259
                continue;
267
                String tagLoc = null;
268
                TreeNode child = taglib.findChild("taglib-uri");
269
                if (child != null)
270
                    tagUri = child.getBody();
271
                child = taglib.findChild("taglib-location");
272
                if (child != null)
273
                    tagLoc = child.getBody();
274
275
                // Save this location if appropriate
276
                if (tagLoc == null)
277
                    continue;
278
                if (uriType(tagLoc) == NOROOT_REL_URI)
279
                    tagLoc = "/WEB-INF/" + tagLoc;
280
                TldLocation location;
281
                if (tagLoc.endsWith(JAR_EXT)) {
282
                    location = new TldLocation("META-INF/taglib.tld", ctxt.getResource(tagLoc).toString());
283
                } else {
284
                    location = new TldLocation(tagLoc);
285
                }
286
                mappings.put(tagUri, location);
287
            }
260
            }
288
        } finally {
261
            if (uriType(tagLoc) == NOROOT_REL_URI) {
289
            if (webXml != null) {
262
                tagLoc = "/WEB-INF/" + tagLoc;
290
                webXml.close();
291
            }
263
            }
264
            TldLocation location;
265
            if (tagLoc.endsWith(JAR_EXT)) {
266
                location = new TldLocation("META-INF/taglib.tld", ctxt.getResource(tagLoc).toString());
267
            } else {
268
                location = new TldLocation(tagLoc);
269
            }
270
            mappings.put(tagUri, location);
292
        }
271
        }
293
    }
272
    }
294
273
(-)java/org/apache/jasper/compiler/WebXml.java (+3 lines)
Lines 39-45 Link Here
39
 * annotations with the main web.xml
39
 * annotations with the main web.xml
40
 *
40
 *
41
 * Clients *must* ensure that they call {@link #close()} to clean up resources.
41
 * Clients *must* ensure that they call {@link #close()} to clean up resources.
42
 *
43
 * @deprecated  Unused - will be removed in Tomcat 8.0.x
42
 */
44
 */
45
@Deprecated
43
public class WebXml {
46
public class WebXml {
44
    private static final String FILE_PROTOCOL = "file:";
47
    private static final String FILE_PROTOCOL = "file:";
45
    private static final String WEB_XML = "/WEB-INF/web.xml";
48
    private static final String WEB_XML = "/WEB-INF/web.xml";

Return to bug 53737