1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.commons.resourcehandler.webapp.config.impl;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.faces.webapp.FacesServlet;
26 import javax.servlet.FilterRegistration;
27 import javax.servlet.ServletContext;
28 import javax.servlet.ServletRegistration;
29
30 import org.apache.myfaces.commons.resourcehandler.webapp.config.WebRegistration;
31 import org.apache.myfaces.commons.resourcehandler.webapp.config.element.impl.FilterRegistrationWrapper;
32 import org.apache.myfaces.commons.resourcehandler.webapp.config.element.impl.ServletRegistrationWrapper;
33
34
35
36
37
38
39 final class _Servlet30Utils
40 {
41
42 public static WebRegistration getWebRegistrationFromServlet30Api(ServletContext servletContext)
43 {
44 WebRegistrationImpl provider = new WebRegistrationImpl();
45 Map<String, ? extends ServletRegistration> servletRegistrations = servletContext.getServletRegistrations();
46 Map<String, ? extends FilterRegistration> filterRegistrations = servletContext.getFilterRegistrations();
47 if (servletRegistrations != null && !servletRegistrations.isEmpty())
48 {
49 for (Map.Entry<String, ? extends ServletRegistration> entry : servletRegistrations.entrySet())
50 {
51 provider.getServletRegistrations().put(entry.getKey(),
52 (org.apache.myfaces.commons.resourcehandler.webapp.config.element.ServletRegistration)
53 new ServletRegistrationWrapper(entry.getValue()));
54 }
55 }
56 if (filterRegistrations != null && !filterRegistrations.isEmpty())
57 {
58 for (Map.Entry<String, ? extends FilterRegistration> entry : filterRegistrations.entrySet())
59 {
60 provider.getFilterRegistrations().put(entry.getKey(), new FilterRegistrationWrapper(entry.getValue()));
61 }
62 }
63 return provider;
64 }
65
66 public static String[] getFacesServletPrefixMappings(ServletContext servletContext)
67 {
68 Map<String, ? extends ServletRegistration> registrations = servletContext.getServletRegistrations();
69 List<String> prefixMappings = null;
70 if (registrations != null)
71 {
72 for (Map.Entry<String, ? extends ServletRegistration> entry : registrations.entrySet())
73 {
74 ServletRegistration registration = entry.getValue();
75
76 if (FacesServlet.class.getName().equals(registration.getClassName()))
77 {
78 for (String urlPattern :entry.getValue().getMappings())
79 {
80
81 String prefix;
82 String extension = urlPattern != null && urlPattern.startsWith("*.") ? urlPattern.substring(urlPattern
83 .indexOf('.')) : null;
84 if (extension == null)
85 {
86 int index = urlPattern.indexOf("/*");
87 if (index != -1)
88 {
89 prefix = urlPattern.substring(0, urlPattern.indexOf("/*"));
90 }
91 else
92 {
93 prefix = urlPattern;
94 }
95 }
96 else
97 {
98 prefix = null;
99 }
100
101 if (prefix != null)
102 {
103 if (prefixMappings == null)
104 {
105 prefixMappings = new ArrayList<String>();
106 }
107 prefixMappings.add(prefix);
108 }
109 }
110 }
111 }
112 }
113 return prefixMappings == null ? new String[]{} : prefixMappings.toArray(new String[prefixMappings.size()]);
114 }
115
116 }