From 5ce5e61bdd15c88f96c992f006a5eda933dd13f7 Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Wed, 1 Nov 2023 16:45:48 +0100 Subject: [PATCH 1/3] fixed apm editor --- .../apm/core/endpoints/ScriptUploadForm.java | 6 +- .../apm/core/endpoints/params/DateFormat.java | 33 ----------- .../params/RequestParameterInjector.java | 13 ++--- .../apm/core/history/HistoryEntryWriter.java | 9 ++- .../apm/core/scripts/LaunchMetadata.java | 55 +++++++++--------- .../apm/core/scripts/ScriptStorageImpl.java | 20 +++---- .../clientlibs/views/editor/js/apm-editor.js | 9 ++- .../apm/clientlibs/views/scripts/.content.xml | 2 +- .../apm/components/scriptLaunch/.content.xml | 5 -- .../scriptLaunch/scriptDetails.html | 58 ------------------- .../apm/fragments/scriptLauncher/.content.xml | 3 +- .../apps/apm/views/scripts/.content.xml | 2 +- 12 files changed, 58 insertions(+), 157 deletions(-) delete mode 100644 app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/params/DateFormat.java delete mode 100644 app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/components/scriptLaunch/.content.xml delete mode 100644 app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/components/scriptLaunch/scriptDetails.html diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptUploadForm.java b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptUploadForm.java index 0d01a343f..4489a7a8d 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptUploadForm.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptUploadForm.java @@ -22,13 +22,12 @@ import com.cognifide.apm.api.scripts.LaunchEnvironment; import com.cognifide.apm.api.scripts.LaunchMode; -import com.cognifide.apm.core.endpoints.params.DateFormat; import com.cognifide.apm.core.endpoints.params.FileName; import com.cognifide.apm.core.endpoints.params.RequestParameter; import com.cognifide.apm.core.scripts.LaunchMetadata; import com.cognifide.apm.core.scripts.ScriptNode; import java.io.InputStream; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import javax.inject.Inject; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.models.annotations.Model; @@ -75,8 +74,7 @@ public class ScriptUploadForm { @Inject @RequestParameter(ScriptNode.APM_LAUNCH_SCHEDULE) - @DateFormat("yyyy-MM-dd'T'HH:mm:ss") - private LocalDateTime launchSchedule; + private OffsetDateTime launchSchedule; @Inject @RequestParameter(ScriptNode.APM_LAUNCH_HOOK) diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/params/DateFormat.java b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/params/DateFormat.java deleted file mode 100644 index faf19aad5..000000000 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/params/DateFormat.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * ========================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================================== - */ - -package com.cognifide.apm.core.endpoints.params; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.FIELD, ElementType.PARAMETER}) -@Retention(RetentionPolicy.RUNTIME) -public @interface DateFormat { - - String value(); -} diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/params/RequestParameterInjector.java b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/params/RequestParameterInjector.java index 90db59490..8e3743772 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/params/RequestParameterInjector.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/params/RequestParameterInjector.java @@ -26,7 +26,7 @@ import java.lang.reflect.AnnotatedElement; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Map; @@ -84,8 +84,8 @@ private Object getValue(SlingHttpServletRequest request, Class type, String p return BooleanUtils.toBoolean(parameterValue.getString()); } else if (type == InputStream.class) { return toInputStream(parameterValue); - } else if (type == LocalDateTime.class) { - return toLocalDateTime(annotatedElement, parameterValue); + } else if (type == OffsetDateTime.class) { + return toOffsetDateTime(parameterValue); } else if (type.isEnum()) { return toEnum(type, parameterValue); } else if (type == String[].class) { @@ -120,11 +120,8 @@ private Map extractParams(SlingHttpServletRequest request, Strin )); } - private LocalDateTime toLocalDateTime(AnnotatedElement annotatedElement, org.apache.sling.api.request.RequestParameter parameterValue) { - String dateFormat = Optional.ofNullable(annotatedElement.getAnnotation(DateFormat.class)) - .map(DateFormat::value) - .orElse(DateTimeFormatter.ISO_LOCAL_DATE_TIME.toString()); - return LocalDateTime.parse(parameterValue.getString(), DateTimeFormatter.ofPattern(dateFormat)); + private OffsetDateTime toOffsetDateTime(org.apache.sling.api.request.RequestParameter parameterValue) { + return OffsetDateTime.parse(parameterValue.getString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME); } @Override diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/history/HistoryEntryWriter.java b/app/aem/core/src/main/java/com/cognifide/apm/core/history/HistoryEntryWriter.java index 34f7047a7..d400c76d6 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/history/HistoryEntryWriter.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/history/HistoryEntryWriter.java @@ -73,8 +73,13 @@ public void writeTo(Resource historyLogResource) throws IOException { valueMap.put(HistoryEntryImpl.SCRIPT_PATH, filePath); valueMap.put(HistoryEntryImpl.AUTHOR, author); valueMap.put(HistoryEntryImpl.MODE, mode); - try (InputStream progressLogInput = IOUtils.toInputStream(progressLog, StandardCharsets.UTF_8)) { - valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLogInput); + int logWarnStringSizeThreshold = Integer.getInteger("oak.repository.node.property.logWarnStringSizeThreshold", 102400); + if (progressLog.length() > logWarnStringSizeThreshold) { + try (InputStream progressLogInput = IOUtils.toInputStream(progressLog, StandardCharsets.UTF_8)) { + valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLogInput); + } + } else { + valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLog); } valueMap.put(HistoryEntryImpl.IS_RUN_SUCCESSFUL, isRunSuccessful); valueMap.put(HistoryEntryImpl.EXECUTION_TIME, executionTime); diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/LaunchMetadata.java b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/LaunchMetadata.java index 4f4254c51..296d205b1 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/LaunchMetadata.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/LaunchMetadata.java @@ -22,35 +22,37 @@ import com.cognifide.apm.api.scripts.LaunchEnvironment; import com.cognifide.apm.api.scripts.LaunchMode; -import java.time.LocalDateTime; -import java.util.Arrays; -import java.util.Optional; -import java.util.stream.Stream; -import org.apache.commons.lang3.StringUtils; +import java.time.OffsetDateTime; public class LaunchMetadata { - private final boolean executionEnabled; + private final boolean launchEnabled; + private final LaunchMode launchMode; + private final LaunchEnvironment launchEnvironment; + private final String[] launchRunModes; - private final String executionHook; - private final LocalDateTime executionSchedule; - private final String cronExpression; - public LaunchMetadata(boolean executionEnabled, LaunchMode launchMode, LaunchEnvironment launchEnvironment, - String[] launchRunModes, String executionHook, LocalDateTime executionSchedule, String cronExpression) { - this.executionEnabled = executionEnabled; + private final String launchHook; + + private final OffsetDateTime launchSchedule; + + private final String launchCronExpression; + + public LaunchMetadata(boolean launchEnabled, LaunchMode launchMode, LaunchEnvironment launchEnvironment, + String[] launchRunModes, String launchHook, OffsetDateTime launchSchedule, String launchCronExpression) { + this.launchEnabled = launchEnabled; this.launchMode = launchMode; this.launchEnvironment = launchEnvironment; this.launchRunModes = launchRunModes; - this.executionHook = executionHook; - this.executionSchedule = executionSchedule; - this.cronExpression = cronExpression; + this.launchHook = launchHook; + this.launchSchedule = launchSchedule; + this.launchCronExpression = launchCronExpression; } - public boolean isExecutionEnabled() { - return executionEnabled; + public boolean isLaunchEnabled() { + return launchEnabled; } public LaunchMode getLaunchMode() { @@ -62,23 +64,18 @@ public LaunchEnvironment getLaunchEnvironment() { } public String[] getLaunchRunModes() { - return Optional.ofNullable(launchRunModes) - .map(Arrays::stream) - .orElse(Stream.empty()) - .filter(StringUtils::isNotBlank) - .distinct() - .toArray(String[]::new); + return launchRunModes; } - public String getExecutionHook() { - return executionHook; + public String getLaunchHook() { + return launchHook; } - public LocalDateTime getExecutionSchedule() { - return executionSchedule; + public OffsetDateTime getLaunchSchedule() { + return launchSchedule; } - public String getCronExpression() { - return cronExpression; + public String getLaunchCronExpression() { + return launchCronExpression; } } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java index 34471ae3b..7dc0104bb 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java @@ -29,11 +29,12 @@ import com.day.cq.commons.jcr.JcrConstants; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -108,13 +109,13 @@ private Script saveScript(FileDescriptor descriptor, LaunchMetadata launchMetada contentNode.setProperty(JcrConstants.JCR_DATA, binary); contentNode.setProperty(JcrConstants.JCR_ENCODING, SCRIPT_ENCODING.name()); fileNode.addMixin(ScriptNode.APM_SCRIPT); - fileNode.setProperty(ScriptNode.APM_LAUNCH_ENABLED, launchMetadata.isExecutionEnabled()); + fileNode.setProperty(ScriptNode.APM_LAUNCH_ENABLED, launchMetadata.isLaunchEnabled()); setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_MODE, launchMetadata.getLaunchMode()); setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_ENVIRONMENT, launchMetadata.getLaunchEnvironment()); setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_RUN_MODES, launchMetadata.getLaunchRunModes()); - setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_HOOK, launchMetadata.getExecutionHook()); - setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_SCHEDULE, launchMetadata.getExecutionSchedule()); - setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_CRON_EXPRESSION, launchMetadata.getCronExpression()); + setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_HOOK, launchMetadata.getLaunchHook()); + setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_SCHEDULE, launchMetadata.getLaunchSchedule()); + setOrRemoveProperty(fileNode, ScriptNode.APM_LAUNCH_CRON_EXPRESSION, launchMetadata.getLaunchCronExpression()); removeProperty(fileNode, ScriptNode.APM_LAST_EXECUTED); JcrUtils.setLastModified(fileNode, Calendar.getInstance()); session.save(); @@ -128,12 +129,11 @@ private Script saveScript(FileDescriptor descriptor, LaunchMetadata launchMetada private void setOrRemoveProperty(Node node, String name, Object value) throws RepositoryException { if (value == null) { removeProperty(node, name); - } else if (value instanceof LocalDateTime) { - LocalDateTime localDateTime = (LocalDateTime) value; + } else if (value instanceof OffsetDateTime) { + OffsetDateTime offsetDateTime = (OffsetDateTime) value; + Date date = Date.from(offsetDateTime.toInstant()); Calendar calendar = Calendar.getInstance(); - calendar.clear(); - calendar.set(localDateTime.getYear(), localDateTime.getMonthValue() - 1, localDateTime.getDayOfMonth(), - localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond()); + calendar.setTime(date); node.setProperty(name, calendar); } else if (value instanceof String[]) { node.setProperty(name, (String[]) value); diff --git a/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/clientlibs/views/editor/js/apm-editor.js b/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/clientlibs/views/editor/js/apm-editor.js index 0ffdf4664..a6d3f3f15 100644 --- a/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/clientlibs/views/editor/js/apm-editor.js +++ b/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/clientlibs/views/editor/js/apm-editor.js @@ -35,7 +35,6 @@ $(document).on('cui-contentloaded', function () { const fieldNames = [ - 'apm:launchEnabled', 'apm:launchMode', 'apm:launchEnvironment', 'apm:launchRunModes', @@ -101,9 +100,9 @@ const formData = new FormData(); $.each(fieldNames, (index, fieldName) => { const originalValue = fieldName === 'apm:launchRunModes' - ? originalFormData.getAll(fieldName) - : originalFormData.get(fieldName); - if (originalValue) { + ? originalFormData.getAll(fieldName).filter((item) => item.trim().length) + : (originalFormData.get(fieldName) || '').trim(); + if (originalValue.length) { formData.set(fieldName, originalValue); } }); @@ -182,7 +181,7 @@ let message = response.message; if (response.errors) { message += ''; } self.uiHelper.notify('error', message, 'error'); diff --git a/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/clientlibs/views/scripts/.content.xml b/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/clientlibs/views/scripts/.content.xml index 9e34b997b..8f0185fba 100644 --- a/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/clientlibs/views/scripts/.content.xml +++ b/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/clientlibs/views/scripts/.content.xml @@ -3,4 +3,4 @@ xmlns:jcr="http://www.jcp.org/jcr/1.0" jcr:primaryType="cq:ClientLibraryFolder" sling:resourceType="widgets/clientlib" - categories="[apm.scripts]"/> + categories="[apm-scripts]"/> diff --git a/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/components/scriptLaunch/.content.xml b/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/components/scriptLaunch/.content.xml deleted file mode 100644 index 77dd00bb8..000000000 --- a/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/components/scriptLaunch/.content.xml +++ /dev/null @@ -1,5 +0,0 @@ - - diff --git a/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/components/scriptLaunch/scriptDetails.html b/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/components/scriptLaunch/scriptDetails.html deleted file mode 100644 index be2cbcfc2..000000000 --- a/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/components/scriptLaunch/scriptDetails.html +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - - - - - diff --git a/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/fragments/scriptLauncher/.content.xml b/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/fragments/scriptLauncher/.content.xml index a587613c1..62ba3fa0e 100644 --- a/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/fragments/scriptLauncher/.content.xml +++ b/app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/fragments/scriptLauncher/.content.xml @@ -52,7 +52,8 @@ fieldLabel="Schedule" name="apm:launchSchedule" type="datetime" - valueFormat="yyyy-MM-dd[T]HH:mm:ss"/> + displayedFormat="YYYY-MM-DD HH:mm" + valueFormat="YYYY-MM-DD[T]HH:mm:ssZ"/> + categories="[apm-scripts,cq.common.wcm,cq.sites.collectionpage,cq.siteadmin.admin.page.row,cq.experiencefragments.components.experiencefragment,cq.siteadmin.common]"/> Date: Wed, 1 Nov 2023 22:33:22 +0100 Subject: [PATCH 2/3] fixed CRON expression launcher --- .../com/cognifide/apm/api/scripts/Script.java | 2 +- .../apm/api/scripts/TransientScript.java | 2 +- .../apm/core/endpoints/ScriptUploadForm.java | 6 ++-- .../apm/core/grammar/ReferenceFinder.java | 2 +- .../apm/core/scripts/ScriptFilters.java | 2 +- .../apm/core/scripts/ScriptModel.java | 9 +++-- .../ScriptsResourceChangeListener.java | 35 +++++++++++-------- 7 files changed, 31 insertions(+), 27 deletions(-) diff --git a/app/aem/api/src/main/java/com/cognifide/apm/api/scripts/Script.java b/app/aem/api/src/main/java/com/cognifide/apm/api/scripts/Script.java index 68e0ecb84..59dead262 100644 --- a/app/aem/api/src/main/java/com/cognifide/apm/api/scripts/Script.java +++ b/app/aem/api/src/main/java/com/cognifide/apm/api/scripts/Script.java @@ -53,7 +53,7 @@ public interface Script { /** * Get CRON expression */ - String getCronExpression(); + String getLaunchCronExpression(); /** * Get last execution date diff --git a/app/aem/api/src/main/java/com/cognifide/apm/api/scripts/TransientScript.java b/app/aem/api/src/main/java/com/cognifide/apm/api/scripts/TransientScript.java index cfd84bd77..2a5d660d3 100644 --- a/app/aem/api/src/main/java/com/cognifide/apm/api/scripts/TransientScript.java +++ b/app/aem/api/src/main/java/com/cognifide/apm/api/scripts/TransientScript.java @@ -83,7 +83,7 @@ public Date getLaunchSchedule() { } @Override - public String getCronExpression() { + public String getLaunchCronExpression() { return null; } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptUploadForm.java b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptUploadForm.java index 4489a7a8d..f43e0519a 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptUploadForm.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptUploadForm.java @@ -77,11 +77,11 @@ public class ScriptUploadForm { private OffsetDateTime launchSchedule; @Inject - @RequestParameter(ScriptNode.APM_LAUNCH_HOOK) - private String cronExpression; + @RequestParameter(ScriptNode.APM_LAUNCH_CRON_EXPRESSION) + private String launchCronExpression; public LaunchMetadata toLaunchMetadata() { - return new LaunchMetadata(launchEnabled, launchMode, launchEnvironment, launchRunModes, launchHook, launchSchedule, cronExpression); + return new LaunchMetadata(launchEnabled, launchMode, launchEnvironment, launchRunModes, launchHook, launchSchedule, launchCronExpression); } public String getFileName() { diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ReferenceFinder.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ReferenceFinder.java index 646a11aba..57f87fd24 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ReferenceFinder.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ReferenceFinder.java @@ -209,7 +209,7 @@ public Date getLaunchSchedule() { } @Override - public String getCronExpression() { + public String getLaunchCronExpression() { return null; } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptFilters.java b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptFilters.java index 0b45bd2d2..820e28a39 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptFilters.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptFilters.java @@ -98,7 +98,7 @@ private static Predicate