Skip to content

Commit

Permalink
Add PARSE_URL function #90
Browse files Browse the repository at this point in the history
  • Loading branch information
nadment committed Jun 30, 2024
1 parent 9bf4f61 commit 4836058
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/src/main/doc/functions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Expression support most of the standard scalar and aggregate functions.
* https://github.com/nadment/hop-expression/blob/master/plugins/src/main/doc/nullifzero.adoc[NULLIFZERO]
* https://github.com/nadment/hop-expression/blob/master/plugins/src/main/doc/numberformat.adoc[NUMBERFORMAT]
* https://github.com/nadment/hop-expression/blob/master/plugins/src/main/doc/nvl2.adoc[NVL2]
* https://github.com/nadment/hop-expression/blob/master/plugins/src/main/doc/parse_url.adoc[PARSE_URL]
* https://github.com/nadment/hop-expression/blob/master/plugins/src/main/doc/pi.adoc[PI]
* https://github.com/nadment/hop-expression/blob/master/plugins/src/main/doc/power.adoc[POWER]
* https://github.com/nadment/hop-expression/blob/master/plugins/src/main/doc/previous_day.adoc[PREVIOUS_DAY]
Expand Down
44 changes: 44 additions & 0 deletions plugins/src/main/doc/parse_url.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
////
= PARSE_URL

Returns the specified part of the URL or the value for the specified QUERY key.

== Syntax
----
PARSE_URL(url, part [, key])
----

Return:: String

== Usage

* `part` must be one of HOST, PORT, PATH, QUERY, REF, PROTOCOL, FRAGMENT, AUTHORITY, FILE, USERINFO.
* Use `key` specifies which query to extract (case-sensitive).
* If a requested part or key is not found, NULL is returned.
* Returns NULL if one of the arguments is NULL.

== Example

----
PARSE_URL('http://hop.apache.org:80/path?id=1','HOST') → hop.apache.org
PARSE_URL('http://hop.apache.org:80/path?id=1','PATH') → /path
PARSE_URL('http://hop.apache.org:80/path?id=1&lang=fr','FILE') → /path?id=1&lang=fr
PARSE_URL('http://hop.apache.org:80/path?id=1&lang=fr','QUERY') → id=1&lang=fr
PARSE_URL('http://hop.apache.org:80/path?id=1&lang=fr','QUERY','id') → 1
PARSE_URL('http://hop.apache.org:80/path?id=1#fragment','FRAGMENT') → fragment
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hop.expression.operator;

import com.jayway.jsonpath.internal.Utils;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.hop.expression.Function;
import org.apache.hop.expression.FunctionPlugin;
import org.apache.hop.expression.IExpression;
import org.apache.hop.expression.OperatorCategory;
import org.apache.hop.expression.type.OperandTypes;
import org.apache.hop.expression.type.ReturnTypes;

/** Returns the specified part of the URL or the value for the specified QUERY key. */
@FunctionPlugin
public class ParseUrlFunction extends Function {

public enum UrlPart {
HOST,
PORT,
PATH,
QUERY,
REF,
PROTOCOL,
FILE,
AUTHORITY,
USERINFO;
}

static Pattern keyToPattern(final String keyToExtract) {
return Pattern.compile("(&|^)" + keyToExtract + "=([^&]*)");
}

private static final Map<String, Pattern> cache = new ConcurrentHashMap<>();

public ParseUrlFunction() {
super(
"PARSE_URL",
ReturnTypes.STRING_NULLABLE,
OperandTypes.STRING_STRING.or(OperandTypes.STRING_STRING_STRING),
OperatorCategory.STRING,
"/docs/parse_url.html");
}

@Override
public Object eval(final IExpression[] operands) {
String url = operands[0].getValue(String.class);
if (url == null) return null;

String part = operands[1].getValue(String.class);
if (part == null) return null;

if (operands.length == 3) {
String key = operands[2].getValue(String.class);
if (key == null) {
return null;
}

return parseUrl(url, part, key);
}

return parseUrl(url, part);
}

public String parseUrl(String url, String partStr) {
UrlPart part;

URI uri;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
return null;
}

try {
part = UrlPart.valueOf(partStr);
} catch (IllegalArgumentException e) {
return null;
}

switch (part) {
case HOST:
return uri.getHost();
case PORT:
return (uri.getPort() < 0) ? null : String.valueOf(uri.getPort());
case PATH:
String path = uri.getRawPath();
return (Utils.isEmpty(path)) ? null : path;
case QUERY:
return uri.getRawQuery();
case REF:
return uri.getRawFragment();
case PROTOCOL:
return uri.getScheme();
case FILE:
if (uri.getRawQuery() != null) {
return uri.getRawPath() + "?" + uri.getRawQuery();
} else {
return uri.getRawPath();
}
case AUTHORITY:
return uri.getRawAuthority();
case USERINFO:
return uri.getRawUserInfo();
default:
return null;
}
}

public String parseUrl(String url, String part, String keyToExtract) {
if (part.equals("QUERY")) {
String query = parseUrl(url, part);
if (query != null) {
Pattern pattern = cache.computeIfAbsent(keyToExtract, ParseUrlFunction::keyToPattern);
Matcher matcher = pattern.matcher(query);
return matcher.find() ? matcher.group(2) : null;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,36 @@ public void Decode() throws Exception {
"DECODE(FIELD_NUMBER,FIELD_INTEGER,1,2,2,4)");
}

@Test
public void ParseUrl() throws Exception {
evalEquals("Parse_Url('http://hop.apache.org:80/path?query=1','PROTOCOL')", "http")
.returnType(Types.STRING);
evalEquals("Parse_Url('http://hop.apache.org:80/path?query=1','HOST')", "hop.apache.org");
evalEquals("Parse_Url('http://hop.apache.org:80/path?query=1','PORT')", "80");
evalEquals(
"Parse_Url('http://user:[email protected]:80/path?query=1','USERINFO')",
"user:password");
evalEquals(
"Parse_Url('http://user:[email protected]:80/path?query=1','AUTHORITY')",
"user:[email protected]:80");

evalEquals("Parse_Url('http://hop.apache.org:80/path?query=1','PATH')", "/path");
evalEquals(
"Parse_Url('http://hop.apache.org:80/path?query=1#fragment','FILE')", "/path?query=1");
evalEquals(
"Parse_Url('http://hop.apache.org:80/path?query=1&lang=fr','QUERY')", "query=1&lang=fr");
evalEquals("Parse_Url('http://hop.apache.org:80/path?query=1&id=2','QUERY','id')", "2");
evalEquals("Parse_Url('http://hop.apache.org:80/path?query=1#fragment', 'REF')", "fragment");

evalNull("Parse_Url(NULL_STRING,'PATH')");
evalNull("Parse_Url('http://hop.apache.org:80',NULL_STRING)");
evalNull("Parse_Url('http://hop.apache.org:80','PATH')");
evalNull("Parse_Url('http://hop.apache.org/path?query=1','PORT')");
evalNull("Parse_Url('http://hop.apache.org/path','QUERY')");
evalNull("Parse_Url('http://hop.apache.org/path?query=1','QUERY','xxx')");
evalNull("Parse_Url('http://hop.apache.org/path?query=1','QUERY',NULL_STRING)");
}

@Test
public void Pi() throws Exception {
evalEquals("Pi()", PI).returnType(Types.NUMBER);
Expand Down

0 comments on commit 4836058

Please sign in to comment.