Search in sources :

Example 1 with DefaultMethodMetadata

use of org.springframework.roo.classpath.details.DefaultMethodMetadata in project spring-roo by spring-projects.

the class SecurityOperationsImpl method generateAuthorizeAnnotations.

@Override
public void generateAuthorizeAnnotations(JavaType klass, String methodName, String roles, String usernames) {
    Validate.notNull(klass, "ERROR: klass parameter is mandatory on 'generateAuthorizeAnnotations' method");
    Validate.notNull(methodName, "ERROR: method parameter is mandatory on 'generateAuthorizeAnnotations' method");
    // Get methods to annotate.
    // With the last parameter to false, we avoid that push in action occurs.
    List<Object> pushedElements = getPushInOperations().pushIn(klass.getPackage(), klass, methodName, false);
    List<AnnotationAttributeValue<?>> rooSecurityAuthorizationsToAdd = new ArrayList<AnnotationAttributeValue<?>>();
    for (Object pushedElement : pushedElements) {
        if (pushedElement instanceof DefaultMethodMetadata) {
            DefaultMethodMetadata method = (DefaultMethodMetadata) pushedElement;
            // Get parameters
            List<AnnotationAttributeValue<?>> lstParamTypes = new ArrayList<AnnotationAttributeValue<?>>();
            List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
            Iterator<AnnotatedJavaType> iterParamTypes = parameterTypes.iterator();
            while (iterParamTypes.hasNext()) {
                ClassAttributeValue parameterAttributeValue = new ClassAttributeValue(new JavaSymbolName("value"), iterParamTypes.next().getJavaType());
                lstParamTypes.add(parameterAttributeValue);
            }
            // Generate new annotations @RooSecurityAuthorization
            NestedAnnotationAttributeValue newFilter = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getRooSecurityAuthorizationsAnnotation(method.getMethodName().getSymbolName(), lstParamTypes, roles, usernames).build());
            rooSecurityAuthorizationsToAdd.add(newFilter);
        }
    }
    // Get actual values of @RooSecurityAuthorizations
    ClassOrInterfaceTypeDetails serviceDetails = getTypeLocationService().getTypeDetails(klass);
    ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceDetails);
    // Check annotation @RooSecurityAuthorizations to delete defined annotations
    // that will be redefined
    AnnotationMetadata annotationAuthorizations = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
    AnnotationMetadataBuilder annotationAuthorizationsMetadataBuilder;
    if (annotationAuthorizations != null) {
        // Getting authorizations from annotation
        AnnotationAttributeValue<?> attributeAuthorizations = annotationAuthorizations.getAttribute("authorizations");
        List<?> values = (List<?>) attributeAuthorizations.getValue();
        if (values != null && !values.isEmpty()) {
            Iterator<?> valuesIt = values.iterator();
            while (valuesIt.hasNext()) {
                NestedAnnotationAttributeValue authorizationAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
                if (checkRooSecurityAuthorizationMaintainAnnotation(rooSecurityAuthorizationsToAdd, authorizationAnnotation)) {
                    // Maintain annotation if 'method' or 'parameters' are different
                    rooSecurityAuthorizationsToAdd.add(authorizationAnnotation);
                }
            }
        }
        annotationAuthorizationsMetadataBuilder = new AnnotationMetadataBuilder(annotationAuthorizations);
        // remove annotation
        cidBuilder.removeAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
    } else {
        // Doesn't exist @RooSecurityAuthorizations, create it
        annotationAuthorizationsMetadataBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
    }
    // Add authorizations attribute
    ArrayAttributeValue<AnnotationAttributeValue<?>> newAuthorizations = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("authorizations"), rooSecurityAuthorizationsToAdd);
    annotationAuthorizationsMetadataBuilder.addAttribute(newAuthorizations);
    // Include new @RooSecurityAuthorizations annotation
    cidBuilder.addAnnotation(annotationAuthorizationsMetadataBuilder);
    // Write on disk
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    // Add Spring Security dependency
    getProjectOperations().addDependency(klass.getModule(), SPRING_SECURITY_CORE, false);
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) DefaultMethodMetadata(org.springframework.roo.classpath.details.DefaultMethodMetadata) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 2 with DefaultMethodMetadata

use of org.springframework.roo.classpath.details.DefaultMethodMetadata in project spring-roo by spring-projects.

the class SecurityOperationsImpl method generateFilterAnnotations.

public void generateFilterAnnotations(JavaType klass, String methodName, String roles, String usernames, String when) {
    // Get methods to annotate.
    // With the last parameter to false, we avoid that push in action occurs.
    List<Object> pushedElements = getPushInOperations().pushIn(klass.getPackage(), klass, methodName, false);
    List<AnnotationAttributeValue<?>> rooSecurityFiltersToAdd = new ArrayList<AnnotationAttributeValue<?>>();
    for (Object pushedElement : pushedElements) {
        if (pushedElement instanceof DefaultMethodMetadata) {
            DefaultMethodMetadata method = (DefaultMethodMetadata) pushedElement;
            // Get parameters
            List<AnnotationAttributeValue<?>> lstParamTypes = new ArrayList<AnnotationAttributeValue<?>>();
            List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
            Iterator<AnnotatedJavaType> iterParamTypes = parameterTypes.iterator();
            while (iterParamTypes.hasNext()) {
                ClassAttributeValue parameterAttributeValue = new ClassAttributeValue(new JavaSymbolName("value"), iterParamTypes.next().getJavaType());
                lstParamTypes.add(parameterAttributeValue);
            }
            // Generate new annotations @RooSecurityFilter
            NestedAnnotationAttributeValue newFilter = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getRooSecurityFilterAnnotation(method.getMethodName().getSymbolName(), lstParamTypes, roles, usernames, when).build());
            rooSecurityFiltersToAdd.add(newFilter);
        }
    }
    // Get actual values of @RooSecurityFilters
    ClassOrInterfaceTypeDetails serviceDetails = getTypeLocationService().getTypeDetails(klass);
    ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(serviceDetails);
    // Check annotation @RooSecurityFilters to delete defined annotations
    // that will be redefined
    AnnotationMetadata annotationFilters = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_FILTERS);
    AnnotationMetadataBuilder annotationFiltersMetadataBuilder;
    if (annotationFilters != null) {
        // Getting filters from annotation
        AnnotationAttributeValue<?> attributeFilters = annotationFilters.getAttribute("filters");
        List<?> values = (List<?>) attributeFilters.getValue();
        if (values != null && !values.isEmpty()) {
            Iterator<?> valuesIt = values.iterator();
            while (valuesIt.hasNext()) {
                NestedAnnotationAttributeValue filterAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
                if (checkRooSecurityFilterMaintainAnnotation(rooSecurityFiltersToAdd, filterAnnotation)) {
                    // Maintain annotation if 'method', 'parameters' or 'when' are different
                    rooSecurityFiltersToAdd.add(filterAnnotation);
                }
            }
        }
        annotationFiltersMetadataBuilder = new AnnotationMetadataBuilder(annotationFilters);
        // remove annotation
        cidBuilder.removeAnnotation(RooJavaType.ROO_SECURITY_FILTERS);
    } else {
        // Doesn't exist @RooSecurityFilters, create it
        annotationFiltersMetadataBuilder = new AnnotationMetadataBuilder(RooJavaType.ROO_SECURITY_FILTERS);
    }
    // Add filters attribute
    ArrayAttributeValue<AnnotationAttributeValue<?>> newFilters = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("filters"), rooSecurityFiltersToAdd);
    annotationFiltersMetadataBuilder.addAttribute(newFilters);
    // Include new @RooSecurityFilters annotation
    cidBuilder.addAnnotation(annotationFiltersMetadataBuilder);
    // Write on disk
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    // Add Spring Security dependency
    getProjectOperations().addDependency(klass.getModule(), SPRING_SECURITY_CORE, false);
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) DefaultMethodMetadata(org.springframework.roo.classpath.details.DefaultMethodMetadata) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)2 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)2 DefaultMethodMetadata (org.springframework.roo.classpath.details.DefaultMethodMetadata)2 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)2 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)2 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)2 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)2 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)2 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)2 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)2 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)2