-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1727 added new functions to the functions library:
* fn:trim() * fn:url-encode() * fn:url-decode() * fn:base64-encode() * fn:base64-decode() Signed-off-by: Thomas Jäckle <thomas.jaeckle@beyonnex.io>
- Loading branch information
Showing
11 changed files
with
728 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
76 changes: 76 additions & 0 deletions
76
placeholders/src/main/java/org/eclipse/ditto/placeholders/PipelineFunctionBase64Decode.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,76 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.placeholders; | ||
|
||
import java.util.Base64; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* Provides the {@code fn:base64-decode()} function implementation. | ||
*/ | ||
@Immutable | ||
final class PipelineFunctionBase64Decode implements PipelineFunction { | ||
|
||
private static final String FUNCTION_NAME = "base64-decode"; | ||
|
||
@Override | ||
public String getName() { | ||
return FUNCTION_NAME; | ||
} | ||
|
||
@Override | ||
public Base64DecodeSignature getSignature() { | ||
return Base64DecodeSignature.INSTANCE; | ||
} | ||
|
||
@Override | ||
public PipelineElement apply(final PipelineElement value, final String paramsIncludingParentheses, | ||
final ExpressionResolver expressionResolver) { | ||
|
||
// check if signature matches (empty params!) | ||
validateOrThrow(paramsIncludingParentheses); | ||
return value.map(v -> new String(Base64.getDecoder().decode(v))); | ||
} | ||
|
||
private void validateOrThrow(final String paramsIncludingParentheses) { | ||
if (!PipelineFunctionParameterResolverFactory.forEmptyParameters().test(paramsIncludingParentheses)) { | ||
throw PlaceholderFunctionSignatureInvalidException.newBuilder(paramsIncludingParentheses, this) | ||
.build(); | ||
} | ||
} | ||
|
||
/** | ||
* Describes the signature of the {@code base64-decode()} function. | ||
*/ | ||
private static final class Base64DecodeSignature implements Signature { | ||
|
||
private static final Base64DecodeSignature INSTANCE = new Base64DecodeSignature(); | ||
|
||
private Base64DecodeSignature() { | ||
} | ||
|
||
@Override | ||
public List<ParameterDefinition<?>> getParameterDefinitions() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return renderSignature(); | ||
} | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
placeholders/src/main/java/org/eclipse/ditto/placeholders/PipelineFunctionBase64Encode.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,77 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.placeholders; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* Provides the {@code fn:base64-encode()} function implementation. | ||
*/ | ||
@Immutable | ||
final class PipelineFunctionBase64Encode implements PipelineFunction { | ||
|
||
private static final String FUNCTION_NAME = "base64-encode"; | ||
|
||
@Override | ||
public String getName() { | ||
return FUNCTION_NAME; | ||
} | ||
|
||
@Override | ||
public Base64EncodeSignature getSignature() { | ||
return Base64EncodeSignature.INSTANCE; | ||
} | ||
|
||
@Override | ||
public PipelineElement apply(final PipelineElement value, final String paramsIncludingParentheses, | ||
final ExpressionResolver expressionResolver) { | ||
|
||
// check if signature matches (empty params!) | ||
validateOrThrow(paramsIncludingParentheses); | ||
return value.map(v -> Base64.getEncoder().encodeToString(v.getBytes(StandardCharsets.UTF_8))); | ||
} | ||
|
||
private void validateOrThrow(final String paramsIncludingParentheses) { | ||
if (!PipelineFunctionParameterResolverFactory.forEmptyParameters().test(paramsIncludingParentheses)) { | ||
throw PlaceholderFunctionSignatureInvalidException.newBuilder(paramsIncludingParentheses, this) | ||
.build(); | ||
} | ||
} | ||
|
||
/** | ||
* Describes the signature of the {@code base64-encode()} function. | ||
*/ | ||
private static final class Base64EncodeSignature implements Signature { | ||
|
||
private static final Base64EncodeSignature INSTANCE = new Base64EncodeSignature(); | ||
|
||
private Base64EncodeSignature() { | ||
} | ||
|
||
@Override | ||
public List<ParameterDefinition<?>> getParameterDefinitions() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return renderSignature(); | ||
} | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
placeholders/src/main/java/org/eclipse/ditto/placeholders/PipelineFunctionTrim.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,75 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.placeholders; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* Provides the {@code fn:trim()} function implementation. | ||
*/ | ||
@Immutable | ||
final class PipelineFunctionTrim implements PipelineFunction { | ||
|
||
private static final String FUNCTION_NAME = "trim"; | ||
|
||
@Override | ||
public String getName() { | ||
return FUNCTION_NAME; | ||
} | ||
|
||
@Override | ||
public TrimFunctionSignature getSignature() { | ||
return TrimFunctionSignature.INSTANCE; | ||
} | ||
|
||
@Override | ||
public PipelineElement apply(final PipelineElement value, final String paramsIncludingParentheses, | ||
final ExpressionResolver expressionResolver) { | ||
|
||
// check if signature matches (empty params!) | ||
validateOrThrow(paramsIncludingParentheses); | ||
return value.map(String::trim); | ||
} | ||
|
||
private void validateOrThrow(final String paramsIncludingParentheses) { | ||
if (!PipelineFunctionParameterResolverFactory.forEmptyParameters().test(paramsIncludingParentheses)) { | ||
throw PlaceholderFunctionSignatureInvalidException.newBuilder(paramsIncludingParentheses, this) | ||
.build(); | ||
} | ||
} | ||
|
||
/** | ||
* Describes the signature of the {@code trim()} function. | ||
*/ | ||
private static final class TrimFunctionSignature implements Signature { | ||
|
||
private static final TrimFunctionSignature INSTANCE = new TrimFunctionSignature(); | ||
|
||
private TrimFunctionSignature() { | ||
} | ||
|
||
@Override | ||
public List<ParameterDefinition<?>> getParameterDefinitions() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return renderSignature(); | ||
} | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
placeholders/src/main/java/org/eclipse/ditto/placeholders/PipelineFunctionUrlDecode.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,84 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.placeholders; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLDecoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* Provides the {@code fn:url-decode()} function implementation. | ||
*/ | ||
@Immutable | ||
final class PipelineFunctionUrlDecode implements PipelineFunction { | ||
|
||
private static final String FUNCTION_NAME = "url-decode"; | ||
|
||
@Override | ||
public String getName() { | ||
return FUNCTION_NAME; | ||
} | ||
|
||
@Override | ||
public UrlDecodeSignature getSignature() { | ||
return UrlDecodeSignature.INSTANCE; | ||
} | ||
|
||
@Override | ||
public PipelineElement apply(final PipelineElement value, final String paramsIncludingParentheses, | ||
final ExpressionResolver expressionResolver) { | ||
|
||
// check if signature matches (empty params!) | ||
validateOrThrow(paramsIncludingParentheses); | ||
return value.map(v -> { | ||
try { | ||
return URLDecoder.decode(v, StandardCharsets.UTF_8.name()); | ||
} catch (final UnsupportedEncodingException e) { | ||
return URLDecoder.decode(v); | ||
} | ||
}); | ||
} | ||
|
||
private void validateOrThrow(final String paramsIncludingParentheses) { | ||
if (!PipelineFunctionParameterResolverFactory.forEmptyParameters().test(paramsIncludingParentheses)) { | ||
throw PlaceholderFunctionSignatureInvalidException.newBuilder(paramsIncludingParentheses, this) | ||
.build(); | ||
} | ||
} | ||
|
||
/** | ||
* Describes the signature of the {@code url-decode()} function. | ||
*/ | ||
private static final class UrlDecodeSignature implements Signature { | ||
|
||
private static final UrlDecodeSignature INSTANCE = new UrlDecodeSignature(); | ||
|
||
private UrlDecodeSignature() { | ||
} | ||
|
||
@Override | ||
public List<ParameterDefinition<?>> getParameterDefinitions() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return renderSignature(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.