Skip to content

Commit

Permalink
Refactor uri_parts processor so it can be exposed in Painless (elasti…
Browse files Browse the repository at this point in the history
  • Loading branch information
danhermann committed Jun 1, 2021
1 parent f23babf commit 735e789
Showing 1 changed file with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,8 @@ public boolean getKeepOriginal() {
@Override
public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
String value = ingestDocument.getFieldValue(field, String.class);
Map<String, Object> uriParts = apply(value);

URI uri = null;
URL url = null;
try {
uri = new URI(value);
} catch (URISyntaxException e) {
try {
url = new URL(value);
} catch (MalformedURLException e2) {
throw new IllegalArgumentException("unable to parse URI [" + value + "]");
}
}
Map<String, Object> uriParts = getUriParts(uri, url);
if (keepOriginal) {
uriParts.put("original", value);
}
Expand All @@ -81,6 +70,21 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
return ingestDocument;
}

public static Map<String, Object> apply(String urlString) {
URI uri = null;
URL url = null;
try {
uri = new URI(urlString);
} catch (URISyntaxException e) {
try {
url = new URL(urlString);
} catch (MalformedURLException e2) {
throw new IllegalArgumentException("unable to parse URI [" + urlString + "]");
}
}
return getUriParts(uri, url);
}

@SuppressForbidden(reason = "URL.getPath is used only if URI.getPath is unavailable")
private static Map<String, Object> getUriParts(URI uri, URL fallbackUrl) {
Map<String, Object> uriParts = new HashMap<>();
Expand Down

0 comments on commit 735e789

Please sign in to comment.