Skip to content

Commit

Permalink
feat(SPEL): new base64 encode and decode methods (#1339)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaslin committed May 12, 2017
1 parent eaa5399 commit 70e42a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import org.springframework.expression.spel.support.StandardEvaluationContext
import org.springframework.expression.spel.support.StandardTypeLocator

import java.lang.reflect.Method
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.concurrent.atomic.AtomicReference

Expand Down Expand Up @@ -153,6 +152,8 @@ class ContextParameterProcessor {
evaluationContext.registerFunction('toInt', ContextUtilities.getDeclaredMethod("toInt", String))
evaluationContext.registerFunction('toFloat', ContextUtilities.getDeclaredMethod("toFloat", String))
evaluationContext.registerFunction('toBoolean', ContextUtilities.getDeclaredMethod("toBoolean", String))
evaluationContext.registerFunction('toBase64', ContextUtilities.getDeclaredMethod("toBase64", String))
evaluationContext.registerFunction('fromBase64', ContextUtilities.getDeclaredMethod("fromBase64", String))

// only add methods that are context sensitive at stage evaluation time
if (allowUnknownKeys) {
Expand Down Expand Up @@ -313,6 +314,14 @@ abstract class ContextUtilities {
context.stages?.find { it.name == id && it.type == 'manualJudgment' }?.context?.judgmentInput
}

static String toBase64(String text) {
Base64.getEncoder().encodeToString(text.getBytes())
}

static String fromBase64(String text) {
new String(Base64.getDecoder().decode(text), 'UTF-8')
}

}

class MapPropertyAccessor extends ReflectivePropertyAccessor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class ContextParameterProcessorSpec extends Specification {
//successful case: 'http://captive.apple.com/' | 'this should work'
}

def "base64 encode and decode"() {
expect:
contextParameterProcessor.process([test: '${#toBase64("Yo Dawg")}'], [:], true).test == 'WW8gRGF3Zw=='
contextParameterProcessor.process([test: '${#fromBase64("WW8gRGF3Zw==")}'], [:], true).test == 'Yo Dawg'
}

@Unroll
def "should not System.exit"() {
when:
Expand Down

0 comments on commit 70e42a8

Please sign in to comment.