-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
---- |
140 changes: 140 additions & 0 deletions
140
plugins/src/main/java/org/apache/hop/expression/operator/ParseUrlFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|