public synchronized T get(String template) { PathTemplate pathTemplate = PathTemplate.create(template); Set<PathTemplateHolder> values = pathTemplateMap.get(trimBase(pathTemplate)); if(values == null) { return null; } for (PathTemplateHolder next : values) { if (next.template.getTemplateString().equals(template)) { return next.value; } } return null; }
@Override public int hashCode() { int result = getTemplateString() != null ? getTemplateString().hashCode() : 0; result = 31 * result + (template ? 1 : 0); result = 31 * result + (getBase() != null ? getBase().hashCode() : 0); result = 31 * result + (parts != null ? parts.hashCode() : 0); result = 31 * result + (getParameterNames() != null ? getParameterNames().hashCode() : 0); return result; }
return PathTemplate.create("/" + inputPath); return new PathTemplate(path, state > 1 && !base.contains("*"), base, parts, templates, trailingSlash);
private String trimBase(PathTemplate template) { String retval = template.getBase(); if (template.getBase().endsWith("/") && !template.getParameterNames().isEmpty()) { return retval.substring(0, retval.length() - 1); } if (retval.endsWith("*")) { return retval.substring(0, retval.length() - 1); } return retval; }
private PathMatchResult<T> handleStemMatch(Set<PathTemplateHolder> entry, final String path, final Map<String, String> params) { for (PathTemplateHolder val : entry) { if (val.template.matches(path, params)) { return new PathMatchResult<>(params, val.template.getTemplateString(), val.value); } else { params.clear(); } } return null; }
public PathTemplatePredicate(final String template, final ExchangeAttribute attribute) { this.attribute = attribute; this.value = PathTemplate.create(template); }
private synchronized PathTemplateMatcher<T> remove(PathTemplate template) { Set<PathTemplateHolder> values = pathTemplateMap.get(trimBase(template)); Set<PathTemplateHolder> newValues; if (values == null) { return this; } else { newValues = new TreeSet<>(values); } Iterator<PathTemplateHolder> it = newValues.iterator(); while (it.hasNext()) { PathTemplateHolder next = it.next(); if (next.template.getTemplateString().equals(template.getTemplateString())) { it.remove(); break; } } if (newValues.size() == 0) { pathTemplateMap.remove(trimBase(template)); } else { pathTemplateMap.put(trimBase(template), newValues); } buildLengths(); return this; }
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof PathTemplate)) return false; PathTemplate that = (PathTemplate) o; return this.compareTo(that) == 0; }
@Override public boolean resolve(final HttpServerExchange exchange) { final Map<String, String> params = new HashMap<>(); String path = attribute.readAttribute(exchange); if(path == null) { return false; } boolean result = this.value.matches(path, params); if (result) { Map<String, Object> context = exchange.getAttachment(PREDICATE_CONTEXT); if(context == null) { exchange.putAttachment(PREDICATE_CONTEXT, context = new TreeMap<>()); } context.putAll(params); } return result; }
public synchronized PathTemplateMatcher<T> add(final String pathTemplate, final T value) { final PathTemplate template = PathTemplate.create(pathTemplate); return add(template, value); }
private String trimBase(PathTemplate template) { String retval = template.getBase(); if (template.getBase().endsWith("/") && !template.getParameterNames().isEmpty()) { return retval.substring(0, retval.length() - 1); } if (retval.endsWith("*")) { return retval.substring(0, retval.length() - 1); } return retval; }
public synchronized PathTemplateMatcher<T> add(final PathTemplate template, final T value) { Set<PathTemplateHolder> values = pathTemplateMap.get(trimBase(template)); Set<PathTemplateHolder> newValues; if (values == null) { newValues = new TreeSet<>(); } else { newValues = new TreeSet<>(values); } PathTemplateHolder holder = new PathTemplateHolder(value, template); if (newValues.contains(holder)) { PathTemplate equivalent = null; for (PathTemplateHolder item : newValues) { if (item.compareTo(holder) == 0) { equivalent = item.template; break; } } throw UndertowMessages.MESSAGES.matcherAlreadyContainsTemplate(template.getTemplateString(), equivalent.getTemplateString()); } newValues.add(holder); pathTemplateMap.put(trimBase(template), newValues); buildLengths(); return this; }
private PathMatchResult<T> handleStemMatch(Set<PathTemplateHolder> entry, final String path, final Map<String, String> params) { for (PathTemplateHolder val : entry) { if (val.template.matches(path, params)) { return new PathMatchResult<>(params, val.template.getTemplateString(), val.value); } else { params.clear(); } } return null; }
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof PathTemplate)) return false; PathTemplate that = (PathTemplate) o; return this.compareTo(that) == 0; }
@Override public boolean resolve(final HttpServerExchange exchange) { final Map<String, String> params = new HashMap<>(); String path = attribute.readAttribute(exchange); if(path == null) { return false; } boolean result = this.value.matches(path, params); if (result) { Map<String, Object> context = exchange.getAttachment(PREDICATE_CONTEXT); if(context == null) { exchange.putAttachment(PREDICATE_CONTEXT, context = new TreeMap<>()); } context.putAll(params); } return result; }
@Override public int hashCode() { int result = getTemplateString() != null ? getTemplateString().hashCode() : 0; result = 31 * result + (template ? 1 : 0); result = 31 * result + (getBase() != null ? getBase().hashCode() : 0); result = 31 * result + (parts != null ? parts.hashCode() : 0); result = 31 * result + (getParameterNames() != null ? getParameterNames().hashCode() : 0); return result; }
public synchronized T get(String template) { PathTemplate pathTemplate = PathTemplate.create(template); Set<PathTemplateHolder> values = pathTemplateMap.get(trimBase(pathTemplate)); if(values == null) { return null; } for (PathTemplateHolder next : values) { if (next.template.getTemplateString().equals(template)) { return next.value; } } return null; }
public synchronized PathTemplateMatcher<T> remove(final String pathTemplate) { final PathTemplate template = PathTemplate.create(pathTemplate); return remove(template); }