1 7 package winstone; 8 9 16 public class Mapping implements java.util.Comparator { 17 public static final int EXACT_PATTERN = 1; 18 public static final int FOLDER_PATTERN = 2; 19 public static final int EXTENSION_PATTERN = 3; 20 public static final int DEFAULT_SERVLET = 4; 21 22 public static final String STAR = "*"; 23 public static final String SLASH = "/"; 24 25 private String urlPattern; 26 private String linkName; private String mappedTo; 29 private int patternType; 30 private boolean isPatternFirst; 33 protected Mapping(String mappedTo) { 34 this.mappedTo = mappedTo; 35 } 36 37 41 public static Mapping createFromURL(String mappedTo, String pattern) { 42 if ((pattern == null) || (mappedTo == null)) 43 throw new WinstoneException(Launcher.RESOURCES.getString( 44 "Mapping.InvalidMount", new String [] { mappedTo, pattern })); 45 46 if (!pattern.equals("") && !pattern.startsWith(STAR) && 49 !pattern.startsWith(SLASH)) { 50 pattern = SLASH + pattern; 51 } else if (pattern.equals(STAR)) { 52 Logger.log(Logger.WARNING, Launcher.RESOURCES, "Mapping.RewritingStarMount"); 53 pattern = SLASH + STAR; 54 } 55 56 Mapping me = new Mapping(mappedTo); 57 58 int firstStarPos = pattern.indexOf(STAR); 59 int lastStarPos = pattern.lastIndexOf(STAR); 60 int patternLength = pattern.length(); 61 62 if (pattern.equals(SLASH)) { 64 me.urlPattern = ""; 65 me.patternType = DEFAULT_SERVLET; 66 } 67 68 else if (firstStarPos == -1) { 69 me.urlPattern = pattern; 70 me.patternType = EXACT_PATTERN; 71 } 72 73 else if (firstStarPos != lastStarPos) 75 throw new WinstoneException(Launcher.RESOURCES.getString( 76 "Mapping.InvalidMount", new String [] { mappedTo, pattern })); 77 78 else if (pattern.indexOf(SLASH + STAR) == (patternLength - (SLASH + STAR).length())) { 80 me.urlPattern = pattern.substring(0, pattern.length() 81 - (SLASH + STAR).length()); 82 me.patternType = FOLDER_PATTERN; 83 } 84 85 else if (pattern.indexOf(SLASH) != -1) 87 throw new WinstoneException(Launcher.RESOURCES.getString( 88 "Mapping.InvalidMount", new String [] { mappedTo, pattern })); 89 90 else if (firstStarPos == 0) { 92 me.urlPattern = pattern.substring(STAR.length()); 93 me.patternType = EXTENSION_PATTERN; 94 me.isPatternFirst = false; 95 } 96 else if (firstStarPos == (patternLength - STAR.length())) { 98 me.urlPattern = pattern.substring(0, patternLength - STAR.length()); 99 me.patternType = EXTENSION_PATTERN; 100 me.isPatternFirst = true; 101 } else 102 throw new WinstoneException(Launcher.RESOURCES.getString( 103 "Mapping.InvalidMount", new String [] { mappedTo, pattern })); 104 105 Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES, "Mapping.MappedPattern", 106 new String [] { mappedTo, pattern }); 107 return me; 108 } 109 110 113 public static Mapping createFromLink(String mappedTo, String linkName) { 114 if ((linkName == null) || (mappedTo == null)) 115 throw new WinstoneException(Launcher.RESOURCES.getString( 116 "Mapping.InvalidLink", new String [] { mappedTo, linkName })); 117 118 Mapping me = new Mapping(mappedTo); 119 me.linkName = linkName; 120 return me; 121 } 122 123 public int getPatternType() { 124 return this.patternType; 125 } 126 127 public String getUrlPattern() { 128 return this.urlPattern; 129 } 130 131 public String getMappedTo() { 132 return this.mappedTo; 133 } 134 135 public String getLinkName() { 136 return this.linkName; 137 } 138 139 147 public boolean match(String inputPattern, StringBuffer servletPath, 148 StringBuffer pathInfo) { 149 switch (this.patternType) { 152 case FOLDER_PATTERN: 153 if (inputPattern.startsWith(this.urlPattern + '/') || 154 inputPattern.equals(this.urlPattern)) { 155 if (servletPath != null) 156 servletPath.append(this.urlPattern); 157 if (pathInfo != null) 158 pathInfo.append(inputPattern.substring(this.urlPattern.length())); 159 return true; 160 } else 161 return false; 162 163 case EXTENSION_PATTERN: 164 int slashPos = inputPattern.lastIndexOf(SLASH); 166 if ((slashPos == -1) || (slashPos == inputPattern.length() - 1)) 167 return false; 168 String fileName = inputPattern.substring(slashPos + 1); 169 if ((this.isPatternFirst && fileName.startsWith(this.urlPattern)) 170 || (!this.isPatternFirst && fileName.endsWith(this.urlPattern))) { 171 if (servletPath != null) 172 servletPath.append(inputPattern); 173 return true; 174 } else 175 return false; 176 177 case EXACT_PATTERN: 178 if (inputPattern.equals(this.urlPattern)) { 179 if (servletPath != null) 180 servletPath.append(inputPattern); 181 return true; 182 } else 183 return false; 184 185 case DEFAULT_SERVLET: 186 if (servletPath != null) 187 servletPath.append(inputPattern); 188 return true; 189 190 default: 191 return false; 192 } 193 } 194 195 199 public int compare(Object objOne, Object objTwo) { 200 Mapping one = (Mapping) objOne; 201 Mapping two = (Mapping) objTwo; 202 203 Integer intOne = new Integer (one.getPatternType()); 204 Integer intTwo = new Integer (two.getPatternType()); 205 int order = -1 * intOne.compareTo(intTwo); 206 if (order != 0) { 207 return order; 208 } 209 if (one.getLinkName() != null) { 210 return one.getLinkName().compareTo(two.getLinkName()); 212 } else { 213 return -1 * one.getUrlPattern().compareTo(two.getUrlPattern()); 214 } 215 } 216 217 public String toString() { 218 return this.linkName != null ? "Link:" + this.linkName 219 : "URLPattern:type=" + this.patternType + ",pattern=" 220 + this.urlPattern; 221 } 222 } 223 | Popular Tags |