Author: alexsmirnov
Date: 2007-12-06 19:06:29 -0500 (Thu, 06 Dec 2007)
New Revision: 4583
Modified:
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/webapp/WebXml.java
Log:
Merge fix for a
http://jira.jboss.com/jira/browse/RF-1513
Modified: branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/webapp/WebXml.java
===================================================================
--- branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/webapp/WebXml.java 2007-12-06
23:59:35 UTC (rev 4582)
+++ branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/webapp/WebXml.java 2007-12-07
00:06:29 UTC (rev 4583)
@@ -29,6 +29,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
+import java.util.Map.Entry;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
@@ -58,16 +59,17 @@
public static final String CONTEXT_ATTRIBUTE = WebXml.class.getName();
/**
- *
- */
- private static final long serialVersionUID = -9042908418843695017L;
+ *
+ */
+ private static final long serialVersionUID = -9042908418843695017L;
private static final Log _log = LogFactory.getLog(WebXml.class);
static final String WEB_XML = "/WEB-INF/web.xml";
-
- public static final String RESOURCE_URI_PREFIX =
"a4j_"+VersionBean.MAJOR_VERSION+"_"+VersionBean.MINOR_VERSION+"_"+VersionBean.REVISION;
+ public static final String RESOURCE_URI_PREFIX = "a4j_"
+ + VersionBean.MAJOR_VERSION + "_" + VersionBean.MINOR_VERSION +
"_"
+ + VersionBean.REVISION;
static final String RESOURCE_URI_PREFIX_WITH_SLASH = "/"
+ RESOURCE_URI_PREFIX;
@@ -86,6 +88,10 @@
*/
private String _facesFilterSuffix = null;
+ private String _facesServletPrefix = null;
+
+ private String _facesServletSuffix = null;
+
private boolean _prefixMapping = false;
private String _filterName;
@@ -99,91 +105,109 @@
*/
private String _resourcePrefix = RESOURCE_URI_PREFIX_WITH_SLASH;
-
/**
* Parse application web.xml configuration and detect mapping for resources
* and logs.
+ *
* @param context
* @param filterName
* @throws ServletException
*/
- public void init(ServletContext context, String filterName) throws ServletException {
- InputStream webXml = context.getResourceAsStream(WEB_XML);
- if (null == webXml) {
- throw new ServletException(Messages.getMessage(
- Messages.GET_RESOURCE_AS_STREAM_ERROR, WEB_XML));
- }
- Digester dig = new Digester();
- dig.setDocumentLocator(new LocatorImpl());
- // Disable xml validations at all - web.xml already validated by
- // container
- dig.setValidating(false);
- dig.setEntityResolver(new EntityResolver() {
- // Dummi resolver - alvays do nothing
- public InputSource resolveEntity(String publicId, String systemId)
- throws SAXException, IOException {
- return new InputSource(new StringReader(""));
- }
+ public void init(ServletContext context, String filterName)
+ throws ServletException {
+ InputStream webXml = context.getResourceAsStream(WEB_XML);
+ if (null == webXml) {
+ throw new ServletException(Messages.getMessage(
+ Messages.GET_RESOURCE_AS_STREAM_ERROR, WEB_XML));
+ }
+ Digester dig = new Digester();
+ dig.setDocumentLocator(new LocatorImpl());
+ // Disable xml validations at all - web.xml already validated by
+ // container
+ dig.setValidating(false);
+ dig.setEntityResolver(new EntityResolver() {
+ // Dummi resolver - alvays do nothing
+ public InputSource resolveEntity(String publicId, String systemId)
+ throws SAXException, IOException {
+ return new InputSource(new StringReader(""));
+ }
- });
- dig.setNamespaceAware(false);
- // dig.setUseContextClassLoader(true);
- dig.setClassLoader(this.getClass().getClassLoader());
- // Parsing rules.
- // Servlets.
- String path = "web-app/servlet";
- dig.addObjectCreate(path, ServletBean.class);
- dig.addBeanPropertySetter(path + "/servlet-name",
"servletName");
- dig.addBeanPropertySetter(path + "/servlet-class",
"servletClass");
- dig.addBeanPropertySetter(path + "/display-name",
"displayName");
- dig.addBeanPropertySetter(path + "/description");
- dig.addSetNext(path, "addServlet");
- // Filters
- path = "web-app/filter";
- dig.addObjectCreate(path, FilterBean.class);
- dig.addBeanPropertySetter(path + "/filter-name", "filterName");
- dig.addBeanPropertySetter(path + "/filter-class",
"filterClass");
- dig.addBeanPropertySetter(path + "/display-name",
"displayName");
- dig.addBeanPropertySetter(path + "/description");
- dig.addSetNext(path, "addFilter");
- // Servlet mappings
- path = "web-app/servlet-mapping";
- dig.addCallMethod(path, "addServletMapping", 2);
- dig.addCallParam(path + "/servlet-name", 0);
- dig.addCallParam(path + "/url-pattern", 1);
- // Filter mappings
- // TODO - parse dispatcher.
- path = "web-app/filter-mapping";
- dig.addCallMethod(path, "addFilterMapping", 3);
- dig.addCallParam(path + "/filter-name", 0);
- dig.addCallParam(path + "/url-pattern", 1);
- dig.addCallParam(path + "/servlet-name", 2);
- dig.push(this);
- try {
- dig.parse(webXml);
- this.setFilterName(filterName, context);
- // Store Instance to context attribute.
- context.setAttribute(CONTEXT_ATTRIBUTE,this);
- } catch (IOException e) {
- String message = Messages
- .getMessage(Messages.PARSING_WEB_XML_IO_ERROR);
- _log.error(message, e);
- throw new ServletException(message, e);
- } catch (SAXException e) {
- String message = Messages
- .getMessage(Messages.PARSING_WEB_XML_SAX_ERROR);
- _log.error(message, e);
- throw new ServletException(message, e);
- } finally {
- try {
- webXml.close();
- } catch (IOException e) {
- // this exception don't affect any aspects of work and can be
- // ignored.
- }
- }
+ });
+ dig.setNamespaceAware(false);
+ // dig.setUseContextClassLoader(true);
+ dig.setClassLoader(this.getClass().getClassLoader());
+ // Parsing rules.
+ // Servlets.
+ String path = "web-app/servlet";
+ dig.addObjectCreate(path, ServletBean.class);
+ dig.addBeanPropertySetter(path + "/servlet-name", "servletName");
+ dig.addBeanPropertySetter(path + "/servlet-class",
"servletClass");
+ dig.addBeanPropertySetter(path + "/display-name", "displayName");
+ dig.addBeanPropertySetter(path + "/description");
+ dig.addSetNext(path, "addServlet");
+ // Filters
+ path = "web-app/filter";
+ dig.addObjectCreate(path, FilterBean.class);
+ dig.addBeanPropertySetter(path + "/filter-name", "filterName");
+ dig.addBeanPropertySetter(path + "/filter-class", "filterClass");
+ dig.addBeanPropertySetter(path + "/display-name", "displayName");
+ dig.addBeanPropertySetter(path + "/description");
+ dig.addSetNext(path, "addFilter");
+ // Servlet mappings
+ path = "web-app/servlet-mapping";
+ dig.addCallMethod(path, "addServletMapping", 2);
+ dig.addCallParam(path + "/servlet-name", 0);
+ dig.addCallParam(path + "/url-pattern", 1);
+ // Filter mappings
+ // TODO - parse dispatcher.
+ path = "web-app/filter-mapping";
+ dig.addCallMethod(path, "addFilterMapping", 3);
+ dig.addCallParam(path + "/filter-name", 0);
+ dig.addCallParam(path + "/url-pattern", 1);
+ dig.addCallParam(path + "/servlet-name", 2);
+ dig.push(this);
+ try {
+ dig.parse(webXml);
+ this.setFilterName(filterName, context);
+ this.findFacesServlet(context);
+ // Store Instance to context attribute.
+ context.setAttribute(CONTEXT_ATTRIBUTE, this);
+ } catch (IOException e) {
+ String message = Messages
+ .getMessage(Messages.PARSING_WEB_XML_IO_ERROR);
+ _log.error(message, e);
+ throw new ServletException(message, e);
+ } catch (SAXException e) {
+ String message = Messages
+ .getMessage(Messages.PARSING_WEB_XML_SAX_ERROR);
+ _log.error(message, e);
+ throw new ServletException(message, e);
+ } finally {
+ try {
+ webXml.close();
+ } catch (IOException e) {
+ // this exception don't affect any aspects of work and can be
+ // ignored.
+ }
+ }
}
+ private void findFacesServlet(ServletContext context) {
+ for (Iterator<Entry<String, ServletBean>> servletsIterator = _servlets
+ .entrySet().iterator(); servletsIterator.hasNext();) {
+ Entry<String, ServletBean> servletEntry = servletsIterator.next();
+ String servletClass = servletEntry.getValue().getServletClass();
+ if("javax.faces.webapp.FacesServlet".equals(servletClass)){
+ Mapping mapping = checkMapping(servletEntry.getValue().getMappings());
+ if(null != mapping){
+ this._facesServletPrefix = mapping.getPrefix();
+ this._facesServletSuffix = mapping.getSuffix();
+ }
+ }
+ }
+
+ }
+
public void addServlet(ServletBean bean) {
String name = bean.getServletName();
if (null != name) {
@@ -220,12 +244,11 @@
}
/**
- * Convert {@link org.ajax4jsf.resource.InternetResource } key to
- * real URL for handle by chameleon filter, depend of mapping in WEB.XML .
- * For prefix or * mapping, prepend servlet prefix and default Resource
- * prefix to key. For suffix mapping, prepend with resource prefix and
- * append default faces suffix to URL ( before request param ). After
- * conversion, call
+ * Convert {@link org.ajax4jsf.resource.InternetResource } key to real URL
+ * for handle by chameleon filter, depend of mapping in WEB.XML . For prefix
+ * or * mapping, prepend servlet prefix and default Resource prefix to key.
+ * For suffix mapping, prepend with resource prefix and append default faces
+ * suffix to URL ( before request param ). After conversion, call
* {@link
javax.faces.application.ViewHandler#getResourceURL(javax.faces.context.FacesContext,
java.lang.String)}
* and
* {@link javax.faces.context.ExternalContext#encodeResourceURL(java.lang.String)} .
@@ -360,7 +383,8 @@
*
* @param filterName
* The filterName to set.
- * @param context TODO
+ * @param context
+ * TODO
*/
void setFilterName(String filterName, ServletContext context) {
if (null == filterName) {
@@ -377,21 +401,30 @@
Messages.FILTER_NOT_FOUND_ERROR, filterName));
}
// find faces servlet
- checkMapping(filter.getMappings());
+ Mapping mapping = checkMapping(filter.getMappings());
// Filter mapped only to servlet.
- if (_facesFilterPrefix == null && _facesFilterSuffix == null) {
+ if (null == mapping) {
for (Iterator sevlets = filter.getServlets().iterator(); sevlets
.hasNext()
&& _facesFilterPrefix == null && _facesFilterSuffix == null;) {
String servletname = (String) sevlets.next();
ServletBean servlet = (ServletBean) _servlets.get(servletname);
if (null != servlet) {
- checkMapping(servlet.getMappings());
+ mapping = checkMapping(servlet.getMappings());
}
}
}
- String resourcePrefix = (String) context.getAttribute(RESOURCE_URI_PREFIX_PARAM);
- if(null == resourcePrefix){
+ if (null != mapping) {
+ setFacesFilterPrefix(mapping.getPrefix());
+ setFacesFilterSuffix(mapping.getSuffix());
+ } else {
+ throw new IllegalStateException(Messages.getMessage(
+ Messages.NO_PREFIX_OR_SUFFIX_IN_FILTER_MAPPING_ERROR,
+ filterName));
+ }
+ String resourcePrefix = (String) context
+ .getInitParameter(RESOURCE_URI_PREFIX_PARAM);
+ if (null == resourcePrefix) {
resourcePrefix = RESOURCE_URI_PREFIX;
}
if (null != _facesFilterPrefix) {
@@ -399,37 +432,42 @@
if (_facesFilterPrefix.endsWith("/")) {
setResourcePrefix(resourcePrefix);
} else {
- setResourcePrefix("/"+resourcePrefix);
+ setResourcePrefix("/" + resourcePrefix);
}
} else if (null != _facesFilterSuffix) {
_prefixMapping = false;
- setResourcePrefix("/"+resourcePrefix);
- } else {
- throw new IllegalStateException(Messages.getMessage(
- Messages.NO_PREFIX_OR_SUFFIX_IN_FILTER_MAPPING_ERROR,
- filterName));
+ setResourcePrefix("/" + resourcePrefix);
}
}
- private void checkMapping(Set mappings) {
+ private Mapping checkMapping(Set mappings) {
+ Mapping mapping = null;
if (null != mappings) {
for (Iterator iter = mappings.iterator(); iter.hasNext();) {
- String mapping = (String) iter.next();
+ String mappingPattern = (String) iter.next();
// first test - for prefix, like /xxx/*
// TODO - select correct dispatcher.
- if (mapping.endsWith("*")) {
- setFacesFilterPrefix(mapping.substring(0,
- mapping.length() - 1));
+ if (mappingPattern.endsWith("*")) {
+ if (null == mapping) {
+ mapping = new Mapping();
+ }
+ int cut = mappingPattern.endsWith("/*")?2:1;
+ mapping.setPrefix(mappingPattern.substring(0,
+ mappingPattern.length() - cut));
break;
} else
// test for suffix mapping, eg *.xxx
- if (mapping.startsWith("*")) {
- setFacesFilterSuffix(mapping.substring(1));
+ if (mappingPattern.startsWith("*")) {
+ if (null == mapping) {
+ mapping = new Mapping();
+ }
+ mapping.setSuffix(mappingPattern.substring(1));
} else {
// Fixed mapping - do not use it.
}
}
}
+ return mapping;
}
/**
@@ -438,4 +476,53 @@
public boolean isPrefixMapping() {
return _prefixMapping;
}
+
+ private static class Mapping {
+ private String prefix;
+ private String suffix;
+
+ /**
+ * @return the prefix
+ */
+ public String getPrefix() {
+ return prefix;
+ }
+
+ /**
+ * @param prefix
+ * the prefix to set
+ */
+ public void setPrefix(String prefix) {
+ this.prefix = prefix;
+ }
+
+ /**
+ * @return the suffix
+ */
+ public String getSuffix() {
+ return suffix;
+ }
+
+ /**
+ * @param suffix
+ * the suffix to set
+ */
+ public void setSuffix(String suffix) {
+ this.suffix = suffix;
+ }
+ }
+
+ /**
+ * @return the facesServletPrefix
+ */
+ public String getFacesServletPrefix() {
+ return _facesServletPrefix;
+ }
+
+ /**
+ * @return the facesServletSuffix
+ */
+ public String getFacesServletSuffix() {
+ return _facesServletSuffix;
+ }
}