Search in sources :

Example 6 with NestedAnnotationAttributeValue

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

the class ReferenceField method setJoinAnnotations.

/**
 * Fill {@link #joinTableAttributes} for building @JoinTable annotation. The annotation
 * would have some nested @JoinColumn annotations in each of its "joinColumns" and
 * "inverseJoinColumns" attributes.
 *
 * @param joinTableName
 * @param joinColumns
 * @param referencedColumns
 * @param inverseJoinColumns
 * @param inverseReferencedColumns
 */
public void setJoinAnnotations(String joinTableName, String[] joinColumns, String[] referencedColumns, String[] inverseJoinColumns, String[] inverseReferencedColumns) {
    final List<AnnotationMetadataBuilder> joinColumnsBuilders = new ArrayList<AnnotationMetadataBuilder>();
    if (joinColumns != null) {
        // Build joinColumns attribute
        for (int i = 0; i < joinColumns.length; i++) {
            // Build @JoinColumn annotation for owner side of the relation
            final AnnotationMetadataBuilder joinColumnAnnotation = new AnnotationMetadataBuilder(JOIN_COLUMN);
            joinColumnAnnotation.addStringAttribute("name", joinColumns[i]);
            if (referencedColumns != null) {
                joinColumnAnnotation.addStringAttribute("referencedColumnName", referencedColumns[i]);
            }
            joinColumnsBuilders.add(joinColumnAnnotation);
        }
    }
    final List<AnnotationMetadataBuilder> inverseJoinColumnsBuilders = new ArrayList<AnnotationMetadataBuilder>();
    if (inverseJoinColumns != null) {
        // Build inverseJoinColumns attribute
        for (int i = 0; i < inverseJoinColumns.length; i++) {
            // Build @JoinColumn annotation for the not owner side of the relation
            final AnnotationMetadataBuilder inverseJoinColumnsAnnotation = new AnnotationMetadataBuilder(JOIN_COLUMN);
            inverseJoinColumnsAnnotation.addStringAttribute("name", inverseJoinColumns[i]);
            inverseJoinColumnsAnnotation.addStringAttribute("referencedColumnName", inverseReferencedColumns[i]);
            inverseJoinColumnsBuilders.add(inverseJoinColumnsAnnotation);
        }
    }
    if (StringUtils.isNotBlank(joinTableName) || !inverseJoinColumnsBuilders.isEmpty()) {
        // add @JoinTable annotation
        // Add attributes for @JoinTable annotation
        final List<AnnotationAttributeValue<?>> joinTableAttributes = new ArrayList<AnnotationAttributeValue<?>>();
        // If name not specified, use default name value
        joinTableAttributes.add(new StringAttributeValue(new JavaSymbolName("name"), joinTableName));
        // If joinColumns options were not specified, use default @JoinColumn values
        if (joinColumns != null) {
            final List<AnnotationAttributeValue<?>> joinColumnsAnnotations = new ArrayList<AnnotationAttributeValue<?>>();
            for (AnnotationMetadataBuilder joinColumnAnnotation : joinColumnsBuilders) {
                joinColumnsAnnotations.add(new NestedAnnotationAttributeValue(new JavaSymbolName("joinColumns"), joinColumnAnnotation.build()));
            }
            joinTableAttributes.add(new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("joinColumns"), joinColumnsAnnotations));
        }
        // If inverseJoinColumns options were not specified, use default @JoinColumn values
        if (inverseJoinColumns != null) {
            final List<AnnotationAttributeValue<?>> inverseJoinColumnsAnnotations = new ArrayList<AnnotationAttributeValue<?>>();
            for (AnnotationMetadataBuilder inverseJoinColumnsAnnotation : inverseJoinColumnsBuilders) {
                inverseJoinColumnsAnnotations.add(new NestedAnnotationAttributeValue(new JavaSymbolName("inverseJoinColumns"), inverseJoinColumnsAnnotation.build()));
            }
            joinTableAttributes.add(new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("inverseJoinColumns"), inverseJoinColumnsAnnotations));
        }
        // Add @JoinTable to additonalAnnotations
        additionaAnnotations.add(new AnnotationMetadataBuilder(JOIN_TABLE, joinTableAttributes));
    } else if (!joinColumnsBuilders.isEmpty()) {
        if (joinColumnsBuilders.size() == 1) {
            // Just one @JoinColumn
            additionaAnnotations.add(joinColumnsBuilders.iterator().next());
        } else {
            // Multiple @JoinColumn, wrap with @JoinColumns
            final AnnotationMetadataBuilder joinColumnsAnnotation = new AnnotationMetadataBuilder(JOIN_COLUMNS);
            final List<AnnotationAttributeValue<?>> joinColumnsAnnotations = new ArrayList<AnnotationAttributeValue<?>>();
            for (AnnotationMetadataBuilder joinColumnAnnotation : joinColumnsBuilders) {
                joinColumnsAnnotations.add(new NestedAnnotationAttributeValue(new JavaSymbolName("value"), joinColumnAnnotation.build()));
            }
            joinColumnsAnnotation.addAttribute(new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("value"), joinColumnsAnnotations));
            // Add @JoinColumns
            additionaAnnotations.add(joinColumnsAnnotation);
        }
    }
}
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) ArrayList(java.util.ArrayList) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) List(java.util.List) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 7 with NestedAnnotationAttributeValue

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

the class SecurityFiltersMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    // Getting the annotated entity type
    JavaType annotatedService = governorPhysicalTypeMetadata.getType();
    // Getting the service details
    MemberDetails serviceDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), getTypeLocationService().getTypeDetails(annotatedService));
    Map<MethodMetadata, String> prefilterMethods = new LinkedHashMap<MethodMetadata, String>();
    Map<MethodMetadata, String> postfilterMethods = new LinkedHashMap<MethodMetadata, String>();
    // Get methods defined in each annotation @RooSecurityFilter
    AnnotationMetadata annotationFilters = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_FILTERS);
    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();
            // Get attributes from the annotation @RooSecurityFilter
            String methodName = (String) filterAnnotation.getValue().getAttribute("method").getValue();
            String when = (String) filterAnnotation.getValue().getAttribute("when").getValue();
            List<?> methodParameters = (List<?>) filterAnnotation.getValue().getAttribute("parameters").getValue();
            List<MethodMetadata> methods = serviceDetails.getMethods(new JavaSymbolName(methodName));
            for (MethodMetadata method : methods) {
                // check the parameters to get corresponding method
                if (checkParameters(method, methodParameters)) {
                    String roles = null;
                    if (filterAnnotation.getValue().getAttribute("roles") != null) {
                        roles = (String) filterAnnotation.getValue().getAttribute("roles").getValue();
                    }
                    String usernames = null;
                    if (filterAnnotation.getValue().getAttribute("usernames") != null) {
                        usernames = (String) filterAnnotation.getValue().getAttribute("usernames").getValue();
                    }
                    // Create the content based on defined roles and
                    // usernames to include it in the security annotation
                    String secAnnotationValue = getSecurityOperations().getSpringSecurityAnnotationValue(roles, usernames);
                    if (PRE_FILTER.equals(when)) {
                        prefilterMethods.put(method, secAnnotationValue);
                    } else {
                        if (POST_FILTER.equals(when)) {
                            postfilterMethods.put(method, secAnnotationValue);
                        }
                    }
                    break;
                }
            }
        }
    }
    return new SecurityFiltersMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, prefilterMethods, postfilterMethods);
}
Also used : AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) LinkedHashMap(java.util.LinkedHashMap) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) List(java.util.List) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 8 with NestedAnnotationAttributeValue

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

the class SecurityOperationsImpl method checkRooSecurityFilterMaintainAnnotation.

/**
 * Check if {@link RooSecurityFilter} annotation should be kept
 * or should be replaced because is defined in the annotations list to add.
 *
 * @param rooSecurityFiltersToAdd Annotations list to add
 * @param filterAnnotation Annotation to check
 * @return
 */
private boolean checkRooSecurityFilterMaintainAnnotation(List<AnnotationAttributeValue<?>> rooSecurityFiltersToAdd, NestedAnnotationAttributeValue filterAnnotation) {
    boolean maintainAnnotation = true;
    String annotationMethod = (String) filterAnnotation.getValue().getAttribute("method").getValue();
    List<?> annotationParameters = (List<?>) filterAnnotation.getValue().getAttribute("parameters").getValue();
    String annotationWhen = (String) filterAnnotation.getValue().getAttribute("when").getValue();
    Iterator<AnnotationAttributeValue<?>> iterParamTypes = rooSecurityFiltersToAdd.iterator();
    while (iterParamTypes.hasNext()) {
        NestedAnnotationAttributeValue rooSecurityFilterToAdd = (NestedAnnotationAttributeValue) iterParamTypes.next();
        String annotationMethodToAdd = (String) rooSecurityFilterToAdd.getValue().getAttribute("method").getValue();
        List<?> annotationParametersToAdd = (List<?>) rooSecurityFilterToAdd.getValue().getAttribute("parameters").getValue();
        String annotationWhenToAdd = (String) rooSecurityFilterToAdd.getValue().getAttribute("when").getValue();
        boolean parametersAreEquals = true;
        if (annotationParametersToAdd.size() != annotationParameters.size()) {
            parametersAreEquals = false;
        } else {
            for (int i = 0; i < annotationParametersToAdd.size(); i++) {
                ClassAttributeValue classAnnotationParametersToAdd = (ClassAttributeValue) annotationParametersToAdd.get(i);
                ClassAttributeValue classAnnotationParameters = (ClassAttributeValue) annotationParameters.get(i);
                if (!classAnnotationParametersToAdd.getValue().getSimpleTypeName().equals(classAnnotationParameters.getValue().getSimpleTypeName())) {
                    parametersAreEquals = false;
                    break;
                }
            }
        }
        if (annotationMethodToAdd.equals(annotationMethod) && annotationWhenToAdd.equals(annotationWhen) && parametersAreEquals) {
            maintainAnnotation = false;
            break;
        }
    }
    return maintainAnnotation;
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) List(java.util.List) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 9 with NestedAnnotationAttributeValue

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

the class SecurityOperationsImpl method checkRooSecurityAuthorizationMaintainAnnotation.

/**
 * Check if {@link RooSecurityAuthorization} annotation should be kept
 * or should be replaced because is defined in the annotations list to add.
 *
 * @param rooSecurityAuthorizationsToAdd Annotations list to add
 * @param authorizationAnnotation Annotation to check
 * @return
 */
private boolean checkRooSecurityAuthorizationMaintainAnnotation(List<AnnotationAttributeValue<?>> rooSecurityAuthorizationsToAdd, NestedAnnotationAttributeValue authorizationAnnotation) {
    boolean maintainAnnotation = true;
    String annotationMethod = (String) authorizationAnnotation.getValue().getAttribute("method").getValue();
    List<?> annotationParameters = (List<?>) authorizationAnnotation.getValue().getAttribute("parameters").getValue();
    Iterator<AnnotationAttributeValue<?>> iterParamTypes = rooSecurityAuthorizationsToAdd.iterator();
    while (iterParamTypes.hasNext()) {
        NestedAnnotationAttributeValue rooSecurityAuthorizationToAdd = (NestedAnnotationAttributeValue) iterParamTypes.next();
        String annotationMethodToAdd = (String) rooSecurityAuthorizationToAdd.getValue().getAttribute("method").getValue();
        List<?> annotationParametersToAdd = (List<?>) rooSecurityAuthorizationToAdd.getValue().getAttribute("parameters").getValue();
        boolean parametersAreEquals = true;
        if (annotationParametersToAdd.size() != annotationParameters.size()) {
            parametersAreEquals = false;
        } else {
            for (int i = 0; i < annotationParametersToAdd.size(); i++) {
                ClassAttributeValue classAnnotationParametersToAdd = (ClassAttributeValue) annotationParametersToAdd.get(i);
                ClassAttributeValue classAnnotationParameters = (ClassAttributeValue) annotationParameters.get(i);
                if (!classAnnotationParametersToAdd.getValue().getSimpleTypeName().equals(classAnnotationParameters.getValue().getSimpleTypeName())) {
                    parametersAreEquals = false;
                    break;
                }
            }
        }
        if (annotationMethodToAdd.equals(annotationMethod) && parametersAreEquals) {
            maintainAnnotation = false;
            break;
        }
    }
    return maintainAnnotation;
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) List(java.util.List) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 10 with NestedAnnotationAttributeValue

use of org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue 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)

Aggregations

NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)19 ArrayList (java.util.ArrayList)16 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)15 List (java.util.List)14 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)14 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)14 JavaType (org.springframework.roo.model.JavaType)10 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)9 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)9 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)8 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)8 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)5 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)5 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)5 EnumDetails (org.springframework.roo.model.EnumDetails)5 RooJavaType (org.springframework.roo.model.RooJavaType)5 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)4 LinkedHashMap (java.util.LinkedHashMap)3 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)2 ArrayInitializerExpr (com.github.antlrjavaparser.api.expr.ArrayInitializerExpr)2