Skip to content

Commit

Permalink
SQL: fix URI path being lost in case of hosted ES scenario (elastic#4…
Browse files Browse the repository at this point in the history
  • Loading branch information
astefan authored Jul 25, 2019
1 parent 5b9ccd7 commit 06dea85
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.sql.rowset.serial.SerialException;

import static java.util.Collections.emptyMap;
import static org.elasticsearch.xpack.sql.client.UriUtils.appendSegmentToPath;
import static org.elasticsearch.xpack.sql.proto.Protocol.SQL_QUERY_REST_ENDPOINT;

/**
Expand All @@ -52,7 +53,7 @@ public class JreHttpUrlConnection implements Closeable {
+ "?error_trace] and method [POST], allowed:";

public static <R> R http(String path, String query, ConnectionConfiguration cfg, Function<JreHttpUrlConnection, R> handler) {
final URI uriPath = cfg.baseUri().resolve(path); // update path if needed
final URI uriPath = appendSegmentToPath(cfg.baseUri(), path); // update path if needed
final String uriQuery = query == null ? uriPath.getQuery() : query; // update query if needed
final URL url;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,33 @@ public static URI removeQuery(URI uri, String connectionString, URI defaultURI)
throw new IllegalArgumentException("Invalid connection configuration [" + connectionString + "]: " + e.getMessage(), e);
}
}

public static URI appendSegmentToPath(URI uri, String segment) {
if (uri == null) {
throw new IllegalArgumentException("URI must not be null");
}
if (segment == null || segment.isEmpty() || "/".equals(segment)) {
return uri;
}

String path = uri.getPath();
String concatenatedPath = "";
String cleanSegment = segment.startsWith("/") ? segment.substring(1) : segment;

if (path == null || path.isEmpty()) {
path = "/";
}

if (path.charAt(path.length() - 1) == '/') {
concatenatedPath = path + cleanSegment;
} else {
concatenatedPath = path + "/" + cleanSegment;
}
try {
return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), concatenatedPath,
uri.getQuery(), uri.getFragment());
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid segment [" + segment + "] for URI [" + uri + "]: " + e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.net.URI;

import static org.elasticsearch.xpack.sql.client.UriUtils.appendSegmentToPath;
import static org.elasticsearch.xpack.sql.client.UriUtils.parseURI;
import static org.elasticsearch.xpack.sql.client.UriUtils.removeQuery;

Expand Down Expand Up @@ -84,4 +85,56 @@ public void testRemoveQueryNoQuery() throws Exception {
assertEquals(URI.create("http://server:9100"),
removeQuery(URI.create("http://server:9100"), "http://server:9100", DEFAULT_URI));
}

public void testAppendEmptySegmentToPath() throws Exception {
assertEquals(URI.create("http://server:9100"),
appendSegmentToPath(URI.create("http://server:9100"), ""));
}

public void testAppendNullSegmentToPath() throws Exception {
assertEquals(URI.create("http://server:9100"),
appendSegmentToPath(URI.create("http://server:9100"), null));
}

public void testAppendSegmentToNullPath() throws Exception {
assertEquals(
"URI must not be null",
expectThrows(IllegalArgumentException.class, () -> appendSegmentToPath(null, "/_sql")).getMessage()
);
}

public void testAppendSegmentToEmptyPath() throws Exception {
assertEquals(URI.create("/_sql"),
appendSegmentToPath(URI.create(""), "/_sql"));
}

public void testAppendSlashSegmentToPath() throws Exception {
assertEquals(URI.create("http://server:9100"),
appendSegmentToPath(URI.create("http://server:9100"), "/"));
}

public void testAppendSqlSegmentToPath() throws Exception {
assertEquals(URI.create("http://server:9100/_sql"),
appendSegmentToPath(URI.create("http://server:9100"), "/_sql"));
}

public void testAppendSqlSegmentNoSlashToPath() throws Exception {
assertEquals(URI.create("http://server:9100/_sql"),
appendSegmentToPath(URI.create("http://server:9100"), "_sql"));
}

public void testAppendSegmentToPath() throws Exception {
assertEquals(URI.create("http://server:9100/es_rest/_sql"),
appendSegmentToPath(URI.create("http://server:9100/es_rest"), "/_sql"));
}

public void testAppendSegmentNoSlashToPath() throws Exception {
assertEquals(URI.create("http://server:9100/es_rest/_sql"),
appendSegmentToPath(URI.create("http://server:9100/es_rest"), "_sql"));
}

public void testAppendSegmentTwoSlashesToPath() throws Exception {
assertEquals(URI.create("https://server:9100/es_rest/_sql"),
appendSegmentToPath(URI.create("https://server:9100/es_rest/"), "/_sql"));
}
}

0 comments on commit 06dea85

Please sign in to comment.