View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with 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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Leonardo Uribe
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                         //look for prefix mapping
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 }