Skip to content

Commit

Permalink
Added: Http Method and Params for Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
djuarezgf committed Dec 30, 2023
1 parent 99ba986 commit 52b7fe5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- FrontendSiteModule repeatable
- Action names
- Module names
- Http Method and Params for Actions
5 changes: 3 additions & 2 deletions src/main/java/de/samply/frontend/Action.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package de.samply.frontend;

public record Action(
String action,
String path
String path,
String method,
String[] params
) {
}
17 changes: 14 additions & 3 deletions src/main/java/de/samply/frontend/FrontendService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.samply.aop.ConstraintsService;
import de.samply.app.ProjectManagerController;
import de.samply.user.roles.RolesExtractor;
import de.samply.utils.AspectUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;
Expand Down Expand Up @@ -58,20 +59,30 @@ private Map<String, ModuleActionsPackage> fetchModuleActionsPackages(Map<String,
responseEntity = this.constraintsService.checkStateConstraints(stateConstraints, projectCode, bridgehead);
}
if (responseEntity.isEmpty()) { // If there are no restrictions
addAction(moduleModuleActionsPackageMap, frontendSiteModule, frontendAction, rootPath, path);
addAction(moduleModuleActionsPackageMap, frontendSiteModule, frontendAction, rootPath, path, method);
}
}
return moduleModuleActionsPackageMap;
}

private void addAction(Map<String, ModuleActionsPackage> moduleModuleActionsPackageMap, FrontendSiteModule frontendSiteModule, FrontendAction frontendAction, String rootPath, Optional<String> path) {
private void addAction(Map<String, ModuleActionsPackage> moduleModuleActionsPackageMap, FrontendSiteModule frontendSiteModule, FrontendAction frontendAction, String rootPath, Optional<String> path, Method method) {
ModuleActionsPackage moduleActionsPackage = moduleModuleActionsPackageMap.get(frontendSiteModule.module());
if (moduleActionsPackage == null) {
moduleActionsPackage = new ModuleActionsPackage();
moduleActionsPackage.setModule(frontendSiteModule.module());
moduleModuleActionsPackageMap.put(frontendSiteModule.module(), moduleActionsPackage);
}
moduleActionsPackage.addAction(new Action(frontendAction.action(), rootPath + path.get()));
moduleActionsPackage.addAction(frontendAction.action(),
new Action(rootPath + path.get(), fetchHttpMethod(method), fetchHttpParams(method)));
}

private String fetchHttpMethod(Method method) {
Optional<String> result = AspectUtils.fetchHttpMethod(method);
return result.isPresent() ? result.get() : null;
}

private String[] fetchHttpParams(Method method) {
return AspectUtils.fetchRequestParamNames(method);
}

public String fetchUrl(String site, Map<String, String> parameters) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/de/samply/frontend/ModuleActionsPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

@Getter
public class ModuleActionsPackage {
private String module;
private List<Action> actions = new ArrayList<>();
private Map<String, Action> actions = new HashMap<>();

public void setModule(String module) {
this.module = module;
}

public void addAction(Action action) {
actions.add(action);
public void addAction(String actionName, Action action) {
actions.put(actionName, action);
}

}
21 changes: 2 additions & 19 deletions src/main/java/de/samply/user/roles/RolesExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.samply.annotations.RoleConstraints;
import de.samply.app.ProjectManagerController;
import de.samply.utils.AspectUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.annotation.Annotation;
Expand All @@ -20,31 +21,13 @@ public static Map<String, MethodRoles> extractPathRolesMap() {
Arrays.stream(ProjectManagerController.class.getDeclaredMethods()).forEach(method -> {
RoleConstraints roleConstraints = method.getAnnotation(RoleConstraints.class);
if (roleConstraints != null && roleConstraints.organisationRoles().length > 0) {
fetchPath(method).ifPresent(path -> fetchHttpMethod(method).ifPresent(httpMethod -> result.put(rootPath + path,
fetchPath(method).ifPresent(path -> AspectUtils.fetchHttpMethod(method).ifPresent(httpMethod -> result.put(rootPath + path,
new MethodRoles(httpMethod, Arrays.stream(roleConstraints.organisationRoles()).map(role -> role.name()).toArray(String[]::new)))));
}
});
return result;
}

private static Optional<String> fetchHttpMethod(Method method) {
RequestMapping annotation = method.getAnnotation(RequestMapping.class);
if (annotation == null) {
for (Annotation methodAnnotation : method.getDeclaredAnnotations()) {
for (Annotation methodAnnotationAnnotation : methodAnnotation.annotationType().getDeclaredAnnotations()) {
if (methodAnnotationAnnotation.annotationType() == RequestMapping.class) {
return fetchHttpMetod((RequestMapping) methodAnnotationAnnotation);
}
}
}
}
return fetchHttpMetod(annotation);
}

private static Optional<String> fetchHttpMetod(RequestMapping requestMapping) {
return Optional.of(requestMapping.method()[0].name());
}

public static Optional<String> fetchPath(Method method) {
RequestMapping annotation = method.getAnnotation(RequestMapping.class);
if (annotation == null) {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/de/samply/utils/AspectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
import de.samply.db.repository.ProjectRepository;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

public class AspectUtils {

Expand Down Expand Up @@ -53,5 +58,31 @@ public static Optional<Project> fetchProject(ProjectRepository projectRepository
return (projectCode.isPresent()) ? projectRepository.findByCode(projectCode.get()) : Optional.empty();
}

public static String[] fetchRequestParamNames(Method method) {
Set<String> result = new HashSet<>();
Arrays.stream(method.getParameters()).forEach(parameter -> Arrays.stream(parameter.getAnnotations())
.filter(annotation -> annotation.annotationType() == RequestParam.class)
.map(annotation -> (RequestParam) annotation)
.forEach(requestParam -> result.add(requestParam.name())));
return result.toArray(String[]::new);
}

public static Optional<String> fetchHttpMethod(Method method) {
RequestMapping annotation = method.getAnnotation(RequestMapping.class);
if (annotation == null) {
for (Annotation methodAnnotation : method.getDeclaredAnnotations()) {
for (Annotation methodAnnotationAnnotation : methodAnnotation.annotationType().getDeclaredAnnotations()) {
if (methodAnnotationAnnotation.annotationType() == RequestMapping.class) {
return fetchHttpMetod((RequestMapping) methodAnnotationAnnotation);
}
}
}
}
return fetchHttpMetod(annotation);
}

private static Optional<String> fetchHttpMetod(RequestMapping requestMapping) {
return Optional.of(requestMapping.method()[0].name());
}

}

0 comments on commit 52b7fe5

Please sign in to comment.