Package org.wso2.carbon.identity.entitlement

Source Code of org.wso2.carbon.identity.entitlement.EntitlementService

/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. 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.wso2.carbon.identity.entitlement;

import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.core.AbstractAdmin;

import org.wso2.carbon.identity.entitlement.internal.EntitlementServiceComponent;
import org.wso2.carbon.identity.entitlement.pdp.EntitlementEngine;
import org.wso2.carbon.identity.entitlement.pip.PIPExtension;
import org.wso2.carbon.identity.entitlement.policy.PolicyRequestBuilder;
import org.wso2.carbon.identity.entitlement.policy.PolicyResponseBuilder;

import com.sun.xacml.attr.AttributeValue;
import com.sun.xacml.ctx.RequestCtx;
import com.sun.xacml.ctx.ResponseCtx;
import com.sun.xacml.ctx.Subject;
import com.sun.xacml.ctx.Attribute;

public class EntitlementService extends AbstractAdmin {

    private static Log log = LogFactory.getLog(EntitlementService.class);

    /**
     *
     * @param request
     * @return
     * @throws Exception
     */
    public String getDecision(String request) throws Exception {
        PolicyRequestBuilder policyRequestBuilder = null;
        PolicyResponseBuilder policyResponseBuilder = null;
        Element xacmlReq = null;
        ResponseCtx response = null;

        try {

            if (log.isDebugEnabled()) {
                log.debug("XACML request" + request);
            }

            EntitlementEngine entitlementEngine = EntitlementEngine
                    .getInstance(getGovernanceUserRegistry(), CarbonContext.getCurrentContext().getTenantId());
            policyRequestBuilder = new PolicyRequestBuilder();
            policyResponseBuilder = new PolicyResponseBuilder();
            xacmlReq = policyRequestBuilder.getXacmlRequest(request);
            RequestCtx req = RequestCtx.getInstance(xacmlReq);
            List<PIPExtension> extensions = EntitlementServiceComponent.getPipConfig()
                    .getExtensions();
            for (Iterator iterator = extensions.iterator(); iterator.hasNext();) {
                PIPExtension pipExtension = (PIPExtension) iterator.next();
                pipExtension.update(req);
            }
            response = entitlementEngine.evaluate(req);
            return policyResponseBuilder.getXacmlResponse(response);
        } catch (Exception e) {
            log.error("Error occured while eveluating XACML request", e);
            throw new Exception("Error occured while eveluating XACML request");
        }
    }

    /**
     *
     * @param subject
     * @param resource
     * @param action
     * @param env
     * @return
     * @throws Exception
     */
    public String getDecisionByAttributes(String subject, String resource, String action,
            String[] env) throws Exception {

        Set<Subject> subjects = null;
        Set<Attribute> resources = null;
        Set<Attribute> actions = null;
        Set<Attribute> environment = null;

        try {
            subjects = new HashSet<Subject>();
            resources = new HashSet<Attribute>();
            actions = new HashSet<Attribute>();
            environment = new HashSet<Attribute>();

            subjects.add(new Subject(getAttributes(
                    "urn:oasis:names:tc:xacml:1.0:subject:subject-id",
                    "http://www.w3.org/2001/XMLSchema#string", subject)));
            resources.add(getAttribute("urn:oasis:names:tc:xacml:1.0:resource:resource-id",
                    "http://www.w3.org/2001/XMLSchema#string", resource));
            actions.add(getAttribute("urn:oasis:names:tc:xacml:1.0:action:action-id",
                    "http://www.w3.org/2001/XMLSchema#string", action));

            ByteArrayOutputStream requestOut = new ByteArrayOutputStream();

            RequestCtx request = new RequestCtx(subjects, resources, actions, environment);
            request.encode(requestOut);
            return getDecision(requestOut.toString());

        } catch (Exception e) {
            log.error("Error occured while eveluating XACML request", e);
            throw new Exception("Error occured while eveluating XACML request");
        }
    }

    /**
     *
     * @param uri
     * @param value
     * @return
     * @throws URISyntaxException
     */
    private Set<Attribute> getAttributes(String uri, String type, final String value)
            throws URISyntaxException {
        Set<Attribute> attrs = new HashSet<Attribute>();

        AttributeValue attrValues = new AttributeValue(new URI(type)) {
            @Override
            public String encode() {
                return value;
            }
        };
        Attribute attribute = new Attribute(new URI(uri), null, null, attrValues);
        attrs.add(attribute);
        return attrs;
    }

    /**
     *
     * @param uri
     * @param value
     * @return
     * @throws URISyntaxException
     */
    private Attribute getAttribute(String uri, String type, final String value)
            throws URISyntaxException {

        AttributeValue attrValues = new AttributeValue(new URI(type)) {
            @Override
            public String encode() {
                return value;
            }
        };
        return new Attribute(new URI(uri), null, null, attrValues);
    }

}
TOP

Related Classes of org.wso2.carbon.identity.entitlement.EntitlementService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.