1 24 package org.riotfamily.common.web.util; 25 26 import javax.servlet.ServletContext ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.springframework.beans.factory.InitializingBean; 31 import org.springframework.util.Assert; 32 import org.springframework.web.context.ServletContextAware; 33 34 35 42 public class PathCompleter implements ServletContextAware, InitializingBean { 43 44 private static final Log log = LogFactory.getLog(PathCompleter.class); 45 46 private String servletName; 47 48 private String servletMapping; 49 50 private ServletContext servletContext; 51 52 private boolean prefixMapping; 53 54 private String servletPrefix; 55 56 private boolean suffixMapping; 57 58 private String servletSuffix; 59 60 public void setServletContext(ServletContext servletContext) { 61 this.servletContext = servletContext; 62 } 63 64 public void setServletMapping(String servletMapping) { 65 this.servletMapping = servletMapping; 66 } 67 68 public void setServletName(String servletName) { 69 this.servletName = servletName; 70 } 71 72 public void afterPropertiesSet() throws Exception { 73 if (servletMapping == null) { 74 Assert.notNull(servletName, "Either servletMapping or " 75 + "servletName must be set."); 76 77 servletMapping = ServletUtils.getServletMapping(servletName, 78 servletContext); 79 80 Assert.notNull(servletMapping, "Could not determine mapping " 81 + "for servlet '" + servletName + "'."); 82 83 log.info("Servlet '" + servletName + "' is mapped to " 84 + servletMapping + " in web.xml"); 85 } 86 87 int i = servletMapping.indexOf('*'); 88 if (i == 0) { 89 suffixMapping = true; 90 servletSuffix = servletMapping.substring(1); 91 log.info("Servlet suffix: '" + servletSuffix + "'"); 92 } 93 if (i > 0) { 94 prefixMapping = true; 95 servletPrefix = servletMapping.substring(0, i - 1); 96 log.info("Servlet prefix: '" + servletPrefix + "'"); 97 } 98 } 99 100 public boolean isPrefixMapping() { 101 return this.prefixMapping; 102 } 103 104 public boolean isSuffixMapping() { 105 return this.suffixMapping; 106 } 107 108 public String addServletMapping(String path) { 109 if (suffixMapping) { 110 int i = path.indexOf('?'); 111 if (i != -1) { 112 return path.substring(0, i) + servletSuffix + path.substring(i); 113 } 114 else { 115 return path + servletSuffix; 116 } 117 } 118 if (prefixMapping) { 119 return servletPrefix + path; 120 } 121 return path; 122 } 123 124 public void addServletMapping(StringBuffer path) { 125 if (prefixMapping) { 126 path.insert(0, servletPrefix); 127 } 128 else if (suffixMapping) { 129 int i = path.indexOf("?"); 130 if (i != -1) { 131 path.insert(i, servletSuffix); 132 } 133 else { 134 path.append(servletSuffix); 135 } 136 } 137 } 138 139 } 140 | Popular Tags |