Skip to content

Commit

Permalink
Merge pull request #416 from wttech/de-kotlin
Browse files Browse the repository at this point in the history
rewritten Kotlin source code to Java
  • Loading branch information
dprzybyl authored Oct 24, 2023
2 parents cb12bc8 + 194079a commit 35f8366
Show file tree
Hide file tree
Showing 108 changed files with 4,551 additions and 3,649 deletions.
11 changes: 0 additions & 11 deletions app/aem/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import org.gradle.jvm.tasks.Jar

plugins {
id("com.cognifide.aem.bundle")
kotlin("jvm")
id("org.jetbrains.kotlin.plugin.noarg") version "1.6.21"
antlr
groovy
java
Expand Down Expand Up @@ -33,8 +31,6 @@ aem {
).joinToString(","))
excludePackage("org.antlr.stringtemplate", "org.antlr.v4.gui")
embedPackage("org.antlr:antlr4-runtime:4.7.2", "org.antlr.v4.runtime.*")
embedPackage("org.jetbrains.kotlin:kotlin-reflect:1.6.21", "kotlin.reflect.*")
embedPackage("org.jetbrains.kotlin:kotlin-stdlib:1.6.21", "kotlin.*")
}
}
}
Expand All @@ -45,9 +41,6 @@ dependencies {
implementation(project(":app:aem:actions.main"))

antlr("org.antlr:antlr4:4.7.2")

compileOnly(kotlin("stdlib-jdk8"))
compileOnly(kotlin("reflect"))
}

sourceSets {
Expand All @@ -59,10 +52,6 @@ sourceSets {
}

tasks {
named("compileKotlin").configure {
dependsOn("generateGrammarSource")
}

named("generateGrammarSource", AntlrTask::class).configure {
maxHeapSize = "64m"
arguments = arguments + listOf("-visitor", "-long-messages", "-package", "com.cognifide.apm.core.grammar.antlr")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import com.cognifide.apm.core.actions.ParameterDescriptor.FlagsParameterDescriptor;
import com.cognifide.apm.core.actions.ParameterDescriptor.NamedParameterDescriptor;
import com.cognifide.apm.core.actions.ParameterDescriptor.RequiredParameterDescriptor;
import com.cognifide.apm.core.grammar.ApmInteger;
import com.cognifide.apm.core.grammar.ApmList;
import com.cognifide.apm.core.grammar.ApmMap;
import com.cognifide.apm.core.grammar.ApmString;
import com.cognifide.apm.core.grammar.ApmType;
import com.cognifide.apm.core.grammar.ApmType.ApmInteger;
import com.cognifide.apm.core.grammar.ApmType.ApmList;
import com.cognifide.apm.core.grammar.ApmType.ApmMap;
import com.cognifide.apm.core.grammar.ApmType.ApmString;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.lang.annotation.Annotation;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*-
* ========================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.crypto;

import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.cognifide.apm.core.Property;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang.text.StrSubstitutor;
import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(
service = DecryptionService.class,
property = {
Property.DESCRIPTION + "APM Service for decryption encrypted values",
Property.VENDOR
}
)
public class DecryptionService {

@Reference
private CryptoSupport cryptoSupport;

public String decrypt(String text) {
Map<String, String> tokens = Optional.ofNullable(StringUtils.substringsBetween(text, "{", "}"))
.map(Arrays::stream)
.orElse(Stream.empty())
.distinct()
.collect(Collectors.toMap(Function.identity(), token -> unprotect("{" + token + "}")));
StrSubstitutor strSubstitutor = new StrSubstitutor(tokens, "{", "}");
return tokens.isEmpty() ? text : strSubstitutor.replace(text);
}

protected String unprotect(String text) {
try {
return cryptoSupport.unprotect(text);
} catch (CryptoException e) {
throw new IllegalArgumentException(String.format("Unable to decrypt '%s', wrong hmac or master key", text), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* ========================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.crypto;

import com.cognifide.apm.core.endpoints.params.RequestParameter;
import javax.inject.Inject;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;

@Model(adaptables = SlingHttpServletRequest.class)
public class ProtectTextForm {

@Inject
@RequestParameter(value = "text", optional = false)
private String text;

public String getText() {
return text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* ========================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.crypto;

import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.cognifide.apm.core.Property;
import com.cognifide.apm.core.endpoints.response.ResponseEntity;
import com.cognifide.apm.core.endpoints.utils.RequestProcessor;
import java.io.IOException;
import javax.servlet.Servlet;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.models.factory.ModelFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(
service = Servlet.class,
property = {
Property.PATH + "/bin/apm/scripts/protect",
Property.METHOD + "POST",
Property.DESCRIPTION + "APM Encrypt Text Servlet",
Property.VENDOR
}
)
public class ProtectTextServlet extends SlingAllMethodsServlet {

@Reference
private CryptoSupport cryptoSupport;

@Reference
private ModelFactory modelFactory;

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
RequestProcessor.process(modelFactory, ProtectTextForm.class, request, response, (form, resourceResolver) -> {
try {
return ResponseEntity.ok("Text successfully encrypted")
.addEntry("text", cryptoSupport.protect(form.getText()));
} catch (CryptoException e) {
return ResponseEntity.badRequest(StringUtils.defaultString(e.getMessage(), "Errors while encrypting text"));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,45 @@
* 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
package com.cognifide.apm.core.endpoints;

import com.cognifide.apm.api.services.ScriptManager
import com.cognifide.apm.core.Property
import com.cognifide.apm.core.utils.ServletUtils
import org.apache.sling.api.SlingHttpServletRequest
import org.apache.sling.api.SlingHttpServletResponse
import org.apache.sling.api.servlets.SlingAllMethodsServlet
import org.osgi.service.component.annotations.Component
import org.osgi.service.component.annotations.Reference
import javax.servlet.Servlet
import com.cognifide.apm.api.services.ScriptManager;
import com.cognifide.apm.core.Property;
import com.cognifide.apm.core.utils.ServletUtils;
import java.io.IOException;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(
service = [Servlet::class],
property = [
service = Servlet.class,
property = {
Property.PATH + "/bin/apm/definitions",
Property.METHOD + "GET",
Property.DESCRIPTION + "APM Definitions Servlet",
Property.VENDOR
]
}
)
class DefinitionsServlet : SlingAllMethodsServlet() {
public class DefinitionsServlet extends SlingAllMethodsServlet {

@Reference
@Transient
private lateinit var scriptManager: ScriptManager
@Reference
private ScriptManager scriptManager;

override fun doGet(request: SlingHttpServletRequest, response: SlingHttpServletResponse) {
ServletUtils.writeJson(response, scriptManager.predefinedDefinitions)
}
}
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
ServletUtils.writeJson(response, scriptManager.getPredefinedDefinitions());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,35 @@
* limitations under the License.
* =========================LICENSE_END==================================
*/
package com.cognifide.apm.core.endpoints
package com.cognifide.apm.core.endpoints;

import com.cognifide.apm.core.Property
import com.cognifide.apm.core.actions.ActionFactory
import com.cognifide.apm.core.utils.ServletUtils
import org.apache.sling.api.SlingHttpServletRequest
import org.apache.sling.api.SlingHttpServletResponse
import org.apache.sling.api.servlets.SlingAllMethodsServlet
import org.osgi.service.component.annotations.Component
import org.osgi.service.component.annotations.Reference
import javax.servlet.Servlet
import com.cognifide.apm.core.Property;
import com.cognifide.apm.core.actions.ActionFactory;
import com.cognifide.apm.core.utils.ServletUtils;
import java.io.IOException;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(
service = [Servlet::class],
property = [
service = Servlet.class,
property = {
Property.PATH + "/bin/apm/references",
Property.METHOD + "GET",
Property.DESCRIPTION + "APM References Servlet",
Property.VENDOR
]
}
)
class ReferencesServlet : SlingAllMethodsServlet() {
public class ReferencesServlet extends SlingAllMethodsServlet {

@Reference
@Transient
private lateinit var actionFactory: ActionFactory
@Reference
private ActionFactory actionFactory;

override fun doGet(request: SlingHttpServletRequest, response: SlingHttpServletResponse) {
ServletUtils.writeJson(response, actionFactory.commandDescriptions)
}
}
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
ServletUtils.writeJson(response, actionFactory.getCommandDescriptions());
}
}
Loading

0 comments on commit 35f8366

Please sign in to comment.