Skip to content

Commit

Permalink
#1727 added new functions to the functions library:
Browse files Browse the repository at this point in the history
* 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
thjaeckle committed Sep 15, 2023
1 parent c6fbf8b commit e94ca9f
Show file tree
Hide file tree
Showing 11 changed files with 728 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ final class ImmutableFunctionExpression implements FunctionExpression {
new PipelineFunctionSubstringAfter(), // fn:substring-after(':')
new PipelineFunctionLower(), // fn:lower()
new PipelineFunctionUpper(), // fn:upper()
new PipelineFunctionTrim(), // fn:trim()
new PipelineFunctionUrlEncode(), // fn:url-encode()
new PipelineFunctionUrlDecode(), // fn:url-decode()
new PipelineFunctionBase64Encode(), // fn:base64-encode()
new PipelineFunctionBase64Decode(), // fn:base64-decode()
new PipelineFunctionDelete(), // fn:delete()
new PipelineFunctionReplace(), // fn:replace('from', 'to')
new PipelineFunctionSplit() // fn:split(' ')
Expand Down
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();
}
}

}
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();
}
}

}
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();
}
}

}
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();
}
}

}
Loading

0 comments on commit e94ca9f

Please sign in to comment.