Search in sources :

Example 1 with Step

use of com.mercedesbenz.sechub.sharedkernel.Step in project sechub by mercedes-benz.

the class ProjectCreationService method createProject.

@Validated
/* @formatter:off */
@UseCaseAdminCreatesProject(@Step(number = 2, name = "Create project", description = "The service will create the project when not already existing with such name."))
public /* @formatter:on */
void createProject(@NotNull String projectId, @NotNull String description, @NotNull String owner, @NotNull Set<URI> whitelist, @NotNull ProjectMetaData metaData) {
    LOG.info("Administrator {} triggers create of project:{}, having owner:{}", userContext.getUserId(), projectId, owner);
    assertion.assertIsValidProjectId(projectId);
    assertion.assertIsValidUserId(owner);
    assertion.assertIsValidProjectDescription(description);
    /* assert found */
    Optional<Project> foundProject = projectRepository.findById(projectId);
    if (foundProject.isPresent()) {
        throw new AlreadyExistsException("Project '" + projectId + "' already exists");
    }
    Optional<User> foundOwner = userRepository.findById(owner);
    if (!foundOwner.isPresent()) {
        throw new NotFoundException("Owner '" + owner + "' not found");
    }
    /* setup */
    Project project = new Project();
    project.id = projectId;
    project.description = description;
    User ownerUser = foundOwner.get();
    project.owner = ownerUser;
    /**
     * add only accepted/valid URIs - sanitize
     */
    whitelist.stream().filter(uri -> uriValidation.validate(uri).isValid()).forEach(project.getWhiteList()::add);
    List<ProjectMetaDataEntity> metaDataEntities = metaData.getMetaDataMap().entrySet().stream().map(entry -> new ProjectMetaDataEntity(projectId, entry.getKey(), entry.getValue())).collect(Collectors.toList());
    project.metaData.addAll(metaDataEntities);
    /* store */
    persistenceService.saveInOwnTransaction(project);
    sendProjectCreatedEvent(projectId, whitelist);
    sendRefreshUserAuth(ownerUser);
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) User(com.mercedesbenz.sechub.domain.administration.user.User) MessageID(com.mercedesbenz.sechub.sharedkernel.messaging.MessageID) ProjectMessage(com.mercedesbenz.sechub.sharedkernel.messaging.ProjectMessage) DomainMessage(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessage) Service(org.springframework.stereotype.Service) NotFoundException(com.mercedesbenz.sechub.sharedkernel.error.NotFoundException) Step(com.mercedesbenz.sechub.sharedkernel.Step) URI(java.net.URI) AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) MessageDataKeys(com.mercedesbenz.sechub.sharedkernel.messaging.MessageDataKeys) UserRepository(com.mercedesbenz.sechub.domain.administration.user.UserRepository) DomainMessageFactory(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageFactory) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject) UserInputAssertion(com.mercedesbenz.sechub.sharedkernel.validation.UserInputAssertion) Validated(org.springframework.validation.annotation.Validated) Logger(org.slf4j.Logger) UserContextService(com.mercedesbenz.sechub.sharedkernel.UserContextService) Set(java.util.Set) URIValidation(com.mercedesbenz.sechub.sharedkernel.validation.URIValidation) RoleConstants(com.mercedesbenz.sechub.sharedkernel.RoleConstants) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) IsSendingAsyncMessage(com.mercedesbenz.sechub.sharedkernel.messaging.IsSendingAsyncMessage) List(java.util.List) DomainMessageService(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageService) ProjectMetaData(com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData) Optional(java.util.Optional) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject) User(com.mercedesbenz.sechub.domain.administration.user.User) AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) NotFoundException(com.mercedesbenz.sechub.sharedkernel.error.NotFoundException) Validated(org.springframework.validation.annotation.Validated) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject)

Example 2 with Step

use of com.mercedesbenz.sechub.sharedkernel.Step in project sechub by mercedes-benz.

the class UsecaseStepsWithRestDocAreDocumentedTest method inspectMethodAnnotatedWithUseCase.

private void inspectMethodAnnotatedWithUseCase(Map<Class<?>, InspData> map, Class<? extends Annotation> usecaseClass, Method methodOfUseCase, Annotation annot) throws IllegalAccessException, InvocationTargetException {
    for (Method methodInAnnotation : annot.getClass().getDeclaredMethods()) {
        /*
             * each usecase annotation should have a step inside, so we extract STEP
             * information in next steps
             */
        if (Step.class.equals(methodInAnnotation.getReturnType())) {
            /* found method */
            Step step = (Step) methodInAnnotation.invoke(annot);
            if (step.needsRestDoc()) {
                InspData data = map.computeIfAbsent(usecaseClass, key -> InspData.create(key));
                data.add(methodOfUseCase.toString());
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) Step(com.mercedesbenz.sechub.sharedkernel.Step)

Example 3 with Step

use of com.mercedesbenz.sechub.sharedkernel.Step in project sechub by mercedes-benz.

the class UseCaseModelDataCollector method collectAnnotationInfo.

private <T extends Annotation> void collectAnnotationInfo(UseCaseModel model, Class<T> useCaseAnnotationClazz) {
    if (DEBUG) {
        LOG.info("start collecting annotation info:{}", useCaseAnnotationClazz);
    }
    Set<Method> methodsAnnotated = reflections.getMethodsAnnotatedWith(useCaseAnnotationClazz);
    if (DEBUG) {
        LOG.info("found methods annotated with:{} - {}", useCaseAnnotationClazz, methodsAnnotated);
    }
    for (Method method : methodsAnnotated) {
        List<RolesAllowed> rolesAllowed = fetchAllAllowedRoles(method);
        T[] annosFound = method.getAnnotationsByType(useCaseAnnotationClazz);
        if (DEBUG) {
            LOG.info("inspect method:{}\n - roles found:{}\n - annos found:{}", method, rolesAllowed, annosFound);
        }
        for (T anno : annosFound) {
            UseCaseEntry useCaseEntry = model.ensureUseCase(anno.getClass());
            try {
                Method valueMethod = anno.getClass().getMethod("value");
                Object value = valueMethod.invoke(anno);
                if (value instanceof Step) {
                    String location = "method `" + method.getName() + "` in class `" + method.getDeclaringClass().getName() + "`";
                    useCaseEntry.addStep((Step) value, rolesAllowed, location);
                }
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        }
    }
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) UseCaseEntry(com.mercedesbenz.sechub.docgen.usecase.UseCaseModel.UseCaseEntry) Method(java.lang.reflect.Method) Step(com.mercedesbenz.sechub.sharedkernel.Step)

Aggregations

Step (com.mercedesbenz.sechub.sharedkernel.Step)3 Method (java.lang.reflect.Method)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 UseCaseEntry (com.mercedesbenz.sechub.docgen.usecase.UseCaseModel.UseCaseEntry)1 ProjectMetaData (com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData)1 User (com.mercedesbenz.sechub.domain.administration.user.User)1 UserRepository (com.mercedesbenz.sechub.domain.administration.user.UserRepository)1 RoleConstants (com.mercedesbenz.sechub.sharedkernel.RoleConstants)1 UserContextService (com.mercedesbenz.sechub.sharedkernel.UserContextService)1 AlreadyExistsException (com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException)1 NotFoundException (com.mercedesbenz.sechub.sharedkernel.error.NotFoundException)1 DomainMessage (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessage)1 DomainMessageFactory (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageFactory)1 DomainMessageService (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageService)1 IsSendingAsyncMessage (com.mercedesbenz.sechub.sharedkernel.messaging.IsSendingAsyncMessage)1 MessageDataKeys (com.mercedesbenz.sechub.sharedkernel.messaging.MessageDataKeys)1 MessageID (com.mercedesbenz.sechub.sharedkernel.messaging.MessageID)1 ProjectMessage (com.mercedesbenz.sechub.sharedkernel.messaging.ProjectMessage)1 UseCaseAdminCreatesProject (com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject)1 URIValidation (com.mercedesbenz.sechub.sharedkernel.validation.URIValidation)1