From bb5fea160be8f3baa97c90763c9bbfc87e184fe0 Mon Sep 17 00:00:00 2001 From: Dan Hermann Date: Fri, 28 May 2021 08:41:20 -0500 Subject: [PATCH] Refactor uri_parts processor so it can be exposed in Painless (#73344) --- .../ingest/common/UriPartsProcessor.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/UriPartsProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/UriPartsProcessor.java index 11204235822fb..ae252fbf6240e 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/UriPartsProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/UriPartsProcessor.java @@ -58,18 +58,7 @@ public boolean getKeepOriginal() { public IngestDocument execute(IngestDocument ingestDocument) throws Exception { String value = ingestDocument.getFieldValue(field, String.class); - 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 + "]"); - } - } - var uriParts = getUriParts(uri, url); + var uriParts = apply(value); if (keepOriginal) { uriParts.put("original", value); } @@ -81,6 +70,21 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception { return ingestDocument; } + public static Map 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 getUriParts(URI uri, URL fallbackUrl) { var uriParts = new HashMap();