Skip to content

Commit

Permalink
Fixed open/close index api when using wildcard only
Browse files Browse the repository at this point in the history
Named wildcards were not always properly replaced with proper values by PathTrie.
Delete index (curl -XDELETE localhost:9200/*) worked anyway as the named wildcard is the last path element (and even if {index} didn't get replaced with '*', the empty string would have mapped to all indices anyway). When the named wildcard wasn't the last path element (e.g. curl -XPOST localhost:29200/*/_close), the variable didn't get replaced with the current '*' value, but with the empty string, which leads to an error as empty index is not allowed by open/close index.

Closes #4564
  • Loading branch information
javanna committed Jan 8, 2014
1 parent c6fefac commit 6c23ace
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 16 deletions.
82 changes: 82 additions & 0 deletions rest-api-spec/test/indices.open/20_multiple_indices.yaml
@@ -0,0 +1,82 @@
setup:
- do:
indices.create:
index: test_index1
- do:
indices.create:
index: test_index2
- do:
indices.create:
index: test_index3
- do:
cluster.health:
wait_for_status: yellow

---
"All indices":
- do:
indices.close:
index: _all

- do:
catch: forbidden
search:
index: test_index2

- do:
indices.open:
index: _all

- do:
cluster.health:
wait_for_status: yellow

- do:
search:
index: test_index2

---
"Trailing wildcard":
- do:
indices.close:
index: test_*

- do:
catch: forbidden
search:
index: test_index2

- do:
indices.open:
index: test_*

- do:
cluster.health:
wait_for_status: yellow

- do:
search:
index: test_index2

---
"Only wildcard":
- do:
indices.close:
index: '*'

- do:
catch: forbidden
search:
index: test_index3

- do:
indices.open:
index: '*'

- do:
cluster.health:
wait_for_status: yellow

- do:
search:
index: test_index3
25 changes: 11 additions & 14 deletions src/main/java/org/elasticsearch/common/path/PathTrie.java
Expand Up @@ -157,42 +157,39 @@ public T retrieve(String[] path, int index, Map<String, String> params) {

String token = path[index];
TrieNode<T> node = children.get(token);
boolean usedWildcard = false;
boolean usedWildcard;
if (node == null) {
node = children.get(wildcard);
if (node == null) {
return null;
} else {
usedWildcard = true;
if (params != null && node.isNamedWildcard()) {
put(params, node.namedWildcard(), token);
}
}
usedWildcard = true;
} else {
usedWildcard = token.equals(wildcard);
}

put(params, node, token);

if (index == (path.length - 1)) {
if (params != null && node.isNamedWildcard()) {
put(params, node.namedWildcard(), token);
}
return node.value;
}

T res = node.retrieve(path, index + 1, params);
if (res == null && !usedWildcard) {
node = children.get(wildcard);
if (node != null) {
if (params != null && node.isNamedWildcard()) {
put(params, node.namedWildcard(), token);
}
put(params, node, token);
res = node.retrieve(path, index + 1, params);
}
}

return res;
}

private void put(Map<String, String> params, String key, String value) {
params.put(key, decoder.decode(value));
private void put(Map<String, String> params, TrieNode<T> node, String value) {
if (params != null && node.isNamedWildcard()) {
params.put(node.namedWildcard(), decoder.decode(value));
}
}
}

Expand Down
24 changes: 22 additions & 2 deletions src/test/java/org/elasticsearch/common/path/PathTrieTests.java
Expand Up @@ -25,7 +25,6 @@
import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

Expand Down Expand Up @@ -113,11 +112,32 @@ public void testPreferNonWildcardExecution() {
}

@Test
public void testEndWithNamedWildcardAndLookupWithWildcard() {
public void testNamedWildcardAndLookupWithWildcard() {
PathTrie<String> trie = new PathTrie<String>();
trie.insert("x/{test}", "test1");
trie.insert("{test}/a", "test2");
trie.insert("/{test}", "test3");
trie.insert("/{test}/_endpoint", "test4");
trie.insert("/*/{test}/_endpoint", "test5");

Map<String, String> params = newHashMap();
assertThat(trie.retrieve("/x/*", params), equalTo("test1"));
assertThat(params.get("test"), equalTo("*"));

params = newHashMap();
assertThat(trie.retrieve("/b/a", params), equalTo("test2"));
assertThat(params.get("test"), equalTo("b"));

params = newHashMap();
assertThat(trie.retrieve("/*", params), equalTo("test3"));
assertThat(params.get("test"), equalTo("*"));

params = newHashMap();
assertThat(trie.retrieve("/*/_endpoint", params), equalTo("test4"));
assertThat(params.get("test"), equalTo("*"));

params = newHashMap();
assertThat(trie.retrieve("a/*/_endpoint", params), equalTo("test5"));
assertThat(params.get("test"), equalTo("*"));
}
}

0 comments on commit 6c23ace

Please sign in to comment.