Package org.wso2.carbon.identity.core.dao

Source Code of org.wso2.carbon.identity.core.dao.PPIDValueDAO

/*
*  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.core.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.IdentityRegistryResources;
import org.wso2.carbon.identity.core.model.PPIDValueDO;
import org.wso2.carbon.identity.core.model.RelyingPartyDO;
import org.wso2.carbon.registry.core.Association;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.RegistryConstants;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.jdbc.utils.Transaction;
import org.wso2.carbon.registry.core.exceptions.RegistryException;

public class PPIDValueDAO extends AbstractDAO<PPIDValueDO> {

    protected Log log = LogFactory.getLog(PPIDValueDAO.class);

    /**
     *
     * @param registry
     */
    public PPIDValueDAO(Registry registry) {
        this.registry = registry;
    }

    /**
     *
     * @param ppid
     * @throws IdentityException
     */
    public void createPPIDValue(PPIDValueDO ppid) throws IdentityException {
        String path = null;
        Resource resource = null;
        Collection userResource = null;

        if (log.isDebugEnabled()) {
            log.debug("Creating or updating PPID value");
        }

        try {
            ppid.setPpid(removeSpecialCharacters(ppid.getPpid()));
            path = IdentityRegistryResources.PPID_ROOT + ppid.getPpid();

            if (registry.resourceExists(path)) {
                resource = registry.get(path);
            } else {
                resource = registry.newResource();
            }

            if (ppid.getRelyingParty() != null) {
                resource.addProperty(IdentityRegistryResources.PROP_HOST_NAME, ppid
                        .getRelyingParty().getHostName());
            } else if (ppid.getPersonalRelyingParty() != null) {
                resource.addProperty(IdentityRegistryResources.PROP_HOST_NAME, ppid
                        .getPersonalRelyingParty().getHostName());
            }

            resource.addProperty(IdentityRegistryResources.PROP_USER_ID, ppid.getUserId());
            resource.addProperty(IdentityRegistryResources.PROP_PPID, ppid.getPpid());
            boolean transactionStarted = Transaction.isStarted();
            try {
                if (!transactionStarted) {
                    registry.beginTransaction();
                }
                registry.put(path, resource);

                if (!registry.resourceExists(RegistryConstants.PROFILES_PATH + ppid.getUserId())) {
                    userResource = registry.newCollection();
                    registry.put(RegistryConstants.PROFILES_PATH + ppid.getUserId(), userResource);
                } else {
                   //userResource = (Collection) registry.get(RegistryConstants.PROFILES_PATH
                   //         + ppid.getUserId());
                }

                registry.addAssociation(RegistryConstants.PROFILES_PATH + ppid.getUserId(), path,
                        IdentityRegistryResources.ASSOCIATION_USER_PPID);
                if (!transactionStarted) {
                    registry.commitTransaction();
                }
            } catch (Exception e) {
                if (!transactionStarted) {
                    registry.rollbackTransaction();
                }
                if (e instanceof RegistryException) {
                    throw (RegistryException)e;
                } else {
                    throw new IdentityException("Error while creating or updating PPID value", e);
                }
            }
        } catch (RegistryException e) {
            log.error("Error while creating or updating PPID value", e);
            throw new IdentityException("Error while creating or updating PPID value", e);
        }
    }

    /**
     *
     * @param userid
     * @return
     * @throws IdentityException
     */
    public PPIDValueDO[] getPPIDValuesForUser(String userId) throws IdentityException {
        Association[] assoc = null;
        PPIDValueDO ppid = null;
        List<PPIDValueDO> ppids = null;

        if (log.isDebugEnabled()) {
            log.debug("Retreiving PPID for user " + userId);
        }

        ppids = new ArrayList<PPIDValueDO>();

        try {
            if (registry.resourceExists(RegistryConstants.PROFILES_PATH + userId)) {
                assoc = registry.getAssociations(RegistryConstants.PROFILES_PATH + userId,
                        IdentityRegistryResources.ASSOCIATION_USER_PPID);
                for (Association association : assoc) {
                    ppid = resourceToObject(registry.get(association.getDestinationPath()));
                    ppid.setUserId(userId);
                    ppids.add(ppid);
                }
            }
        } catch (RegistryException e) {
            log.error("Error while retreiving PPID for user " + userId, e);
            throw new IdentityException("Error while retreiving PPID for user " + userId, e);
        }

        return ppids.toArray(new PPIDValueDO[ppids.size()]);
    }

    /**
     *
     * @param ppid
     * @return
     * @throws IdentityException
     */
    public String getUserByPPID(String ppid) throws IdentityException {
        String path = null;
        Resource resource = null;

        if (log.isDebugEnabled()) {
            log.debug("Retreiving user for PPID value " + ppid);
        }

        try {
            path = IdentityRegistryResources.PPID_ROOT + removeSpecialCharacters(ppid);
            if (registry.resourceExists(path)) {
                resource = registry.get(path);
                return resource.getProperty(IdentityRegistryResources.PROP_USER_ID);
            } else {
                return null;
            }
        } catch (RegistryException e) {
            log.error("Error while retreiving user for PPID value  " + ppid, e);
            throw new IdentityException("Error while retreiving user for PPID value  " + ppid, e);
        }
    }
   
    /**
     * {@inheritDoc}
     */
    protected PPIDValueDO resourceToObject(Resource resource) {
        PPIDValueDO ppid = null;
        RelyingPartyDO rp = null;
        if (resource != null) {
            ppid = new PPIDValueDO();
            rp = new RelyingPartyDO();
            ppid.setHostName(resource.getProperty(IdentityRegistryResources.PROP_HOST_NAME));
            rp.setHostName(resource.getProperty(IdentityRegistryResources.PROP_HOST_NAME));
            ppid.setRelyingParty(rp);
            ppid.setUserId(resource.getProperty(IdentityRegistryResources.PROP_USER_ID));
            ppid.setPpid(addSpecialCharacters(resource
                    .getProperty(IdentityRegistryResources.PROP_PPID)));
        }
        return ppid;
    }
    /**
     * Getting rid of special character not permitted by remote registry.
     *
     * @param ppid
     * @return
     */
    private String removeSpecialCharacters(String ppid) {
        if (ppid == null) {
            return null;
        }

        if (ppid.indexOf("/") > 0) {
            ppid = ppid.replace("/", "WSO2_FORWARD_SLASH");
        }

        if (ppid.indexOf("+") > 0) {
            ppid = ppid.replace("+", "WSO2_PLUS");
        }
       
        if (ppid.indexOf("=") > 0) {
            ppid = ppid.replace("=", "WSO2_EQUAL");
        }
        return ppid;
    }

    private String addSpecialCharacters(String ppid) {
        if (ppid == null) {
            return null;
        }

        if (ppid.indexOf("WSO2_FORWARD_SLASH") > 0) {
            ppid = ppid.replace("WSO2_FORWARD_SLASH", "/");
        }

        if (ppid.indexOf("WSO2_PLUS") > 0) {
            ppid = ppid.replace("WSO2_PLUS", "+");
        }
       
        if (ppid.indexOf("WSO2_EQUAL") > 0) {
            ppid = ppid.replace("WSO2_EQUAL", "=");
        }

        return ppid;
    }
}
TOP

Related Classes of org.wso2.carbon.identity.core.dao.PPIDValueDAO

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.