Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added example of using script with parameters #406

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -106,6 +105,7 @@ class RequestParameterInjector : Injector, StaticInjectAnnotationProcessorFactor
return annotation.value
}

@Deprecated("")
override fun isOptional(): Boolean {
return annotation.optional
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -147,16 +146,15 @@ 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)
}

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))
}
Expand All @@ -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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 21 additions & 0 deletions app/aem/core/src/test/resources/macro.apm
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions app/aem/core/src/test/resources/run-macro.apm
Original file line number Diff line number Diff line change
@@ -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'
Loading