All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cxf.rt.security.xacml.RequestComponentBuilder Maven / Gradle / Ivy

There is a newer version: 2.7.18
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.apache.cxf.rt.security.xacml;

import java.util.List;

import org.opensaml.Configuration;
import org.opensaml.xacml.XACMLObjectBuilder;
import org.opensaml.xacml.ctx.ActionType;
import org.opensaml.xacml.ctx.AttributeType;
import org.opensaml.xacml.ctx.AttributeValueType;
import org.opensaml.xacml.ctx.EnvironmentType;
import org.opensaml.xacml.ctx.RequestType;
import org.opensaml.xacml.ctx.ResourceContentType;
import org.opensaml.xacml.ctx.ResourceType;
import org.opensaml.xacml.ctx.SubjectType;
import org.opensaml.xml.XMLObjectBuilderFactory;

/**
 * A set of utility methods to construct XACML 2.0 Request statements
 */
public final class RequestComponentBuilder {
    private static volatile XACMLObjectBuilder attributeValueTypeBuilder;
    
    private static volatile XACMLObjectBuilder attributeTypeBuilder;
    
    private static volatile XACMLObjectBuilder subjectTypeBuilder;
    
    private static volatile XACMLObjectBuilder resourceTypeBuilder;
    
    private static volatile XACMLObjectBuilder actionTypeBuilder;
    
    private static volatile XACMLObjectBuilder environmentTypeBuilder;
    
    private static volatile XACMLObjectBuilder requestTypeBuilder;
    
    private static volatile XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
    
    private RequestComponentBuilder() {
        // complete
    }

    @SuppressWarnings("unchecked")
    public static AttributeValueType createAttributeValueType(
        String value
    ) {
        if (attributeValueTypeBuilder == null) {
            attributeValueTypeBuilder = (XACMLObjectBuilder)
                builderFactory.getBuilder(AttributeValueType.DEFAULT_ELEMENT_NAME);
        }
        AttributeValueType attributeValue = attributeValueTypeBuilder.buildObject();
        attributeValue.setValue(value);
        
        return attributeValue;
    }
    
    @SuppressWarnings("unchecked")
    public static AttributeType createAttributeType(
        String attributeId,
        String dataType,
        String issuer,
        List attributeValues
    ) {
        if (attributeTypeBuilder == null) {
            attributeTypeBuilder = (XACMLObjectBuilder)
                builderFactory.getBuilder(AttributeType.DEFAULT_ELEMENT_NAME);
        }
        AttributeType attributeType = attributeTypeBuilder.buildObject();
        attributeType.setAttributeID(attributeId);
        attributeType.setDataType(dataType);
        attributeType.setIssuer(issuer);
        attributeType.getAttributeValues().addAll(attributeValues);
        
        return attributeType;
    }
    
    @SuppressWarnings("unchecked")
    public static SubjectType createSubjectType(
        List attributes,
        String subjectCategory
    ) {
        if (subjectTypeBuilder == null) {
            subjectTypeBuilder = (XACMLObjectBuilder)
                builderFactory.getBuilder(SubjectType.DEFAULT_ELEMENT_NAME);
        }
        SubjectType subject = subjectTypeBuilder.buildObject();
        if (attributes != null) {
            subject.getAttributes().addAll(attributes);
        }
        subject.setSubjectCategory(subjectCategory);
        
        return subject;
    }
    
    @SuppressWarnings("unchecked")
    public static ResourceType createResourceType(
        List attributes,
        ResourceContentType resourceContent
    ) {
        if (resourceTypeBuilder == null) {
            resourceTypeBuilder = (XACMLObjectBuilder)
                builderFactory.getBuilder(ResourceType.DEFAULT_ELEMENT_NAME);
        }
        ResourceType resource = resourceTypeBuilder.buildObject();
        if (attributes != null) {
            resource.getAttributes().addAll(attributes);
        }
        resource.setResourceContent(resourceContent);
        
        return resource;
    }
    
    @SuppressWarnings("unchecked")
    public static ActionType createActionType(
        List attributes
    ) {
        if (actionTypeBuilder == null) {
            actionTypeBuilder = (XACMLObjectBuilder)
                builderFactory.getBuilder(ActionType.DEFAULT_ELEMENT_NAME);
        }
        ActionType action = actionTypeBuilder.buildObject();
        if (attributes != null) {
            action.getAttributes().addAll(attributes);
        }
        
        return action;
    }
    
    @SuppressWarnings("unchecked")
    public static EnvironmentType createEnvironmentType(
        List attributes
    ) {
        if (environmentTypeBuilder == null) {
            environmentTypeBuilder = (XACMLObjectBuilder)
                builderFactory.getBuilder(EnvironmentType.DEFAULT_ELEMENT_NAME);
        }
        EnvironmentType enviroment = environmentTypeBuilder.buildObject();
        if (attributes != null) {
            enviroment.getAttributes().addAll(attributes);
        }
        
        return enviroment;
    }
    
    @SuppressWarnings("unchecked")
    public static RequestType createRequestType(
        List subjects,
        List resources,
        ActionType action,
        EnvironmentType environment
    ) {
        if (requestTypeBuilder == null) {
            requestTypeBuilder = (XACMLObjectBuilder)
                builderFactory.getBuilder(RequestType.DEFAULT_ELEMENT_NAME);
        }
        RequestType request = requestTypeBuilder.buildObject();
        request.getSubjects().addAll(subjects);
        request.getResources().addAll(resources);
        request.setAction(action);
        request.setEnvironment(environment);
        
        return request;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy