Skip to content

Commit

Permalink
Change the Springdog agent path prefix to be applied at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
PENEKhun committed Sep 1, 2024
1 parent 655033f commit 10057fb
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 236 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 the original author or authors.
*
* 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.
*/

package org.easypeelsecurity.springdog.agent;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
* Annotation for Springdog agent controller.
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface SpringdogAgentController {
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
Expand All @@ -41,6 +42,8 @@
/**
* Controller for the agent's view.
*/
@Controller
@SpringdogAgentController
@SuppressWarnings("checkstyle:MissingJavadocMethod")
public class SpringdogAgentView {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2024 the original author or authors.
*
* 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.
*/

package org.easypeelsecurity.springdog.agent;

import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerTypePredicate;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import org.easypeelsecurity.springdog.shared.configuration.SpringdogProperties;
import org.easypeelsecurity.springdog.shared.util.Assert;

/**
* Springdog dynamic URL mapping configuration.
* Enable agent access to the springdog path user determine at runtime.
*/
@Component
public class SpringdogDynamicUrlMappingConfig implements WebMvcRegistrations {

private final SpringdogProperties springdogProperties;

/**
* Constructor.
*/
public SpringdogDynamicUrlMappingConfig(SpringdogProperties springdogProperties) {
this.springdogProperties = springdogProperties;
}

@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new CustomRequestMappingHandlerMapping(springdogProperties);
}

private static class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
private final SpringdogProperties springdogProperties;

CustomRequestMappingHandlerMapping(SpringdogProperties springdogProperties) {
this.springdogProperties = springdogProperties;
}

@Override
protected void initHandlerMethods() {
String basePath = springdogProperties.computeAbsolutePath("");
Assert.notNull(basePath, "Springdog Agent's base path must not be null");

setPathPrefixes(
java.util.Collections.singletonMap(
basePath,
HandlerTypePredicate.forAnnotation(SpringdogAgentController.class)
)
);
super.initHandlerMethods();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;

import org.easypeelsecurity.springdog.autoconfigure.PropertiesLoader;

/**
* Processes the SpringDogEnable annotation to enable configurations necessary for SpringDog functionalities.
*/
Expand Down Expand Up @@ -57,20 +55,17 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
private void generateConfigurations(RoundEnvironment roundEnv) {
roundEnv.getElementsAnnotatedWith(SpringDogEnable.class).forEach(element -> {
String fullPackageName = element.getEnclosingElement().toString();
PropertiesLoader propertiesLoader = new PropertiesLoader(processingEnv);
String agentBasePath = propertiesLoader.getPropertyOrDefault("springdog.agent.basePath", "springdog");

List<CodeGenerator> generatedCodes = Arrays.asList(
new SpringdogStorageApplier(),
new SpringdogDynamicTemplateResolver(),
new SpringdogBannerPrinter(),
new SpringdogAgentSecurityApplier(),
new SpringdogManagerApplier(),
new SpringdogAutoConfigurationApplier(),
new SpringdogSharedApplier(),
new SpringdogNotificationApplier(),
new SpringdogDomainApplier(),
new SpringdogAgentApplier(agentBasePath)
new SpringdogAgentApplier()
);

generatedCodes.forEach(generator -> generator.writeTo(fullPackageName, processingEnv));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

import javax.lang.model.element.Modifier;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.easypeelsecurity.springdog.agent.SpringdogAgentView;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.TypeSpec;
Expand All @@ -32,21 +30,13 @@
*/
public class SpringdogAgentApplier extends CodeGenerator {

private final String basePath;

@SuppressWarnings("checkstyle:MissingJavadocMethod")
public SpringdogAgentApplier(String basePath) {
this.basePath = basePath;
}

@Override
public Builder typeSpec() {
return TypeSpec.classBuilder("SpringdogAgentApplier")
.addAnnotation(Controller.class)
.addModifiers(Modifier.PUBLIC)
.superclass(SpringdogAgentView.class)
.addAnnotation(AnnotationSpec.builder(RequestMapping.class)
.addMember(ANNOTATION_DEFAULT_FIELD, "$S", basePath)
.build());
.addAnnotation(Configuration.class)
.addAnnotation(AnnotationSpec.builder(ComponentScan.class)
.addMember("basePackages", "$S", "org.easypeelsecurity.springdog.agent")
.build())
.addModifiers(Modifier.PUBLIC);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class SpringdogEnableProcessorTests {
@DisplayName("Should generate code correctly with @SpringdogEnable annotation.")
@CsvSource({
"test.annotation.springdog.SpringdogAgentApplier",
"test.annotation.springdog.SpringdogAgentSecurityApplier",
"test.annotation.springdog.SpringdogAutoConfigurationApplier",
"test.annotation.springdog.SpringdogBannerPrinter",
"test.annotation.springdog.SpringdogDynamicTemplateResolver",
Expand Down
Loading

0 comments on commit 10057fb

Please sign in to comment.