diff --git a/app/aem/core/src/main/kotlin/com/cognifide/apm/core/endpoints/params/RequestParameterInjector.kt b/app/aem/core/src/main/kotlin/com/cognifide/apm/core/endpoints/params/RequestParameterInjector.kt index a83e9ed1..195d29d0 100644 --- a/app/aem/core/src/main/kotlin/com/cognifide/apm/core/endpoints/params/RequestParameterInjector.kt +++ b/app/aem/core/src/main/kotlin/com/cognifide/apm/core/endpoints/params/RequestParameterInjector.kt @@ -21,7 +21,6 @@ package com.cognifide.apm.core.endpoints.params import com.cognifide.apm.core.Property -import com.google.common.primitives.Ints import org.apache.commons.lang3.StringUtils import org.apache.sling.api.SlingHttpServletRequest import org.apache.sling.models.spi.DisposalCallbackRegistry @@ -69,7 +68,7 @@ class RequestParameterInjector : Injector, StaticInjectAnnotationProcessorFactor val parameterValue = request.getRequestParameter(fieldName) ?: return null return when { annotatedElement.isAnnotationPresent(FileName::class.java) -> parameterValue.fileName - fieldClass.name in listOf("java.lang.Integer", "int") -> Ints.tryParse(parameterValue.string) + fieldClass.name in listOf("java.lang.Integer", "int") -> parameterValue.string.toIntOrNull() fieldClass.name in listOf("java.lang.Boolean", "boolean") -> "true" == parameterValue.string fieldClass == InputStream::class.java -> parameterValue.inputStream fieldClass == LocalDateTime::class.java -> toLocalDateTime(annotatedElement, parameterValue) @@ -106,6 +105,7 @@ class RequestParameterInjector : Injector, StaticInjectAnnotationProcessorFactor return annotation.value } + @Deprecated("") override fun isOptional(): Boolean { return annotation.optional } diff --git a/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/ScriptRunner.kt b/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/ScriptRunner.kt index 15a466e8..4912e1b0 100644 --- a/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/ScriptRunner.kt +++ b/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/ScriptRunner.kt @@ -92,7 +92,9 @@ class ScriptRunner( override fun visitRequireVariable(ctx: RequireVariableContext): Status { val variableName = ctx.IDENTIFIER().toString() - if (executionContext.getVariable(variableName) == null) { + try { + executionContext.getVariable(variableName) + } catch (e: ArgumentResolverException) { val status = if (validateOnly) Status.WARNING else Status.ERROR progress(ctx, status, "require", "Variable \"$variableName\" is required") } diff --git a/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/argument/ArgumentResolver.kt b/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/argument/ArgumentResolver.kt index 6307c37b..421f0696 100644 --- a/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/argument/ArgumentResolver.kt +++ b/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/argument/ArgumentResolver.kt @@ -26,7 +26,6 @@ import com.cognifide.apm.core.grammar.common.getIdentifier import com.cognifide.apm.core.grammar.common.getKey import com.cognifide.apm.core.grammar.common.getPath import com.cognifide.apm.core.grammar.executioncontext.VariableHolder -import com.google.common.primitives.Ints import org.apache.commons.lang.text.StrSubstitutor import org.apache.commons.lang3.StringUtils @@ -147,7 +146,7 @@ class ArgumentResolver(private val variableHolder: VariableHolder) { override fun visitNumberValue(ctx: NumberValueContext): ApmType { val value = ctx.NUMBER_LITERAL().toString() - val number = Ints.tryParse(value) + val number = value.toIntOrNull() ?: throw ArgumentResolverException("Found invalid number value $value") return ApmInteger(number) } @@ -155,8 +154,7 @@ class ArgumentResolver(private val variableHolder: VariableHolder) { private fun determineStringValue(value: String): ApmString { val tokens = StringUtils.substringsBetween(value, "\${", "}") .orEmpty() - .map { it to variableHolder[it]!!.string } - .toMap() + .associateWith { variableHolder[it].string } val strSubstitutor = StrSubstitutor(tokens, "\${", "}") return ApmString(if (tokens.isEmpty()) value else strSubstitutor.replace(value)) } @@ -177,7 +175,6 @@ class ArgumentResolver(private val variableHolder: VariableHolder) { override fun visitVariable(ctx: VariableContext): ApmType { val name = getIdentifier(ctx.variableIdentifier()) return variableHolder[name] - ?: throw ArgumentResolverException("Variable \"$name\" not found") } } } diff --git a/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/executioncontext/ExecutionContext.kt b/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/executioncontext/ExecutionContext.kt index e8664291..a499a014 100644 --- a/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/executioncontext/ExecutionContext.kt +++ b/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/executioncontext/ExecutionContext.kt @@ -90,7 +90,7 @@ class ExecutionContext private constructor( variableHolder[key] = value } - override fun getVariable(key: String): ApmType? { + override fun getVariable(key: String): ApmType { return variableHolder[key] } diff --git a/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/executioncontext/ExternalExecutionContext.kt b/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/executioncontext/ExternalExecutionContext.kt index de99216c..1d5bac50 100644 --- a/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/executioncontext/ExternalExecutionContext.kt +++ b/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/executioncontext/ExternalExecutionContext.kt @@ -27,7 +27,7 @@ import org.apache.jackrabbit.api.security.user.Authorizable interface ExternalExecutionContext { val progress: Progress fun setVariable(key: String, value: ApmType) - fun getVariable(key: String): ApmType? + fun getVariable(key: String): ApmType fun setAuthorizable(authorizable: Authorizable?) fun getAuthorizable(): Authorizable? } \ No newline at end of file diff --git a/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/executioncontext/VariableHolder.kt b/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/executioncontext/VariableHolder.kt index 9f7fec93..e62e012b 100644 --- a/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/executioncontext/VariableHolder.kt +++ b/app/aem/core/src/main/kotlin/com/cognifide/apm/core/grammar/executioncontext/VariableHolder.kt @@ -64,8 +64,8 @@ class VariableHolder { for (key in keys) { result = when (result) { is ApmList -> result.list.getOrNull(key.toIntOrNull() ?: -1) - is ApmMap -> result.map.get(key) - else -> context.get(key) + is ApmMap -> result.map[key] + else -> context[key] } result ?: break } diff --git a/app/aem/core/src/test/groovy/com/cognifide/apm/core/grammar/ScriptRunnerTest.groovy b/app/aem/core/src/test/groovy/com/cognifide/apm/core/grammar/ScriptRunnerTest.groovy index 25b1e6dc..66b99fe0 100644 --- a/app/aem/core/src/test/groovy/com/cognifide/apm/core/grammar/ScriptRunnerTest.groovy +++ b/app/aem/core/src/test/groovy/com/cognifide/apm/core/grammar/ScriptRunnerTest.groovy @@ -95,6 +95,22 @@ class ScriptRunnerTest extends Specification { "Executing command SHOW [[\"a\", \"b\"], [\"c\", \"d\"]]"] } + def "run macro"() { + given: + Script script = createScript("/run-macro.apm") + scriptFinder.find("/macro.apm", resourceResolver) >> createScript("/macro.apm") + + when: + def result = scriptExecutor.execute(script, new ProgressImpl("")) + + then: + def commands = result.entries + .collect { it.command } + .findAll { it.startsWith("Executing") } + commands == ["Executing command SHOW \"abc\"", + "Executing command SHOW \"123\""] + } + def "run import"() { given: Script script = createScript("/import.apm") diff --git a/app/aem/core/src/test/resources/macro.apm b/app/aem/core/src/test/resources/macro.apm new file mode 100644 index 00000000..e0b748e2 --- /dev/null +++ b/app/aem/core/src/test/resources/macro.apm @@ -0,0 +1,21 @@ + + # ========================LICENSE_START================================= + # AEM Permission Management + # %% + # Copyright (C) 2013 Wunderman Thompson Technology + # %% + # Licensed 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. + # =========================LICENSE_END================================== + +REQUIRE param +SHOW $param diff --git a/app/aem/core/src/test/resources/run-macro.apm b/app/aem/core/src/test/resources/run-macro.apm new file mode 100644 index 00000000..8d862de6 --- /dev/null +++ b/app/aem/core/src/test/resources/run-macro.apm @@ -0,0 +1,21 @@ + + # ========================LICENSE_START================================= + # AEM Permission Management + # %% + # Copyright (C) 2013 Wunderman Thompson Technology + # %% + # Licensed 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. + # =========================LICENSE_END================================== + +RUN '/macro.apm' param='abc' +RUN '/macro.apm' param='123'