/*
* Copyright 2005,2006 WSO2, Inc. http://www.wso2.org
*
* Licensed 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.axiom.om.util.UUIDGenerator;
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.Collection;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.RegistryConstants;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
public class RelyingPartyDAO extends AbstractDAO<RelyingPartyDO> {
protected Log log = LogFactory.getLog(RelyingPartyDAO.class);
/**
*
* @param registry
*/
public RelyingPartyDAO(Registry registry) {
this.registry = registry;
}
/**
*
* @param rp
* @throws IdentityException
*/
public void create(RelyingPartyDO rp) throws IdentityException {
String uuid = null;
String path = null;
RelyingPartyDO rpdo = null;
Resource resource = null;
if (log.isDebugEnabled()) {
log.debug("Creating new relying party");
}
try {
uuid = UUIDGenerator.getUUID();
path = IdentityRegistryResources.RELYING_PARTY + uuid;
rpdo = this.getFirstObjectWithPropertyValue(IdentityRegistryResources.RELYING_PARTY,
IdentityRegistryResources.PROP_HOST_NAME, rp.getHostName());
if (rpdo == null) {
if (log.isDebugEnabled()) {
log.debug("Rrelying party already exists");
}
return;
}
resource = registry.newResource();
resource.addProperty(IdentityRegistryResources.PROP_ALIAS, rp.getAlias());
resource.addProperty(IdentityRegistryResources.PROP_HOST_NAME, rp.getHostName());
registry.put(path, resource);
} catch (RegistryException e) {
log.error("Error while creating new relying party", e);
throw new IdentityException("Error while creating new relying party", e);
}
}
/**
*
* @param rp
* @throws IdentityException
*/
public void delete(RelyingPartyDO rp) throws IdentityException {
String path = null;
if (log.isDebugEnabled()) {
log.debug("Deleting relying party");
}
try {
path = IdentityRegistryResources.RELYING_PARTY + "/" + rp.getUuid();
registry.delete(path);
} catch (RegistryException e) {
log.error("Error while deleting relying party", e);
throw new IdentityException("Error while deleting relying party", e);
}
}
/**
*
* @param hostName
* @return
* @throws IdentityException
*/
public RelyingPartyDO getRelyingPartyDO(String hostName) throws IdentityException {
RelyingPartyDO rp = null;
if (log.isDebugEnabled()) {
log.debug("Retreiving relying party for host " + hostName);
}
rp = getFirstObjectWithPropertyValue(IdentityRegistryResources.RELYING_PARTY,
IdentityRegistryResources.PROP_HOST_NAME, hostName);
return rp;
}
/**
*
* @return
* @throws IdentityException
*/
public RelyingPartyDO[] getAllRelyingParties() throws IdentityException {
List<RelyingPartyDO> lst = null;
RelyingPartyDO[] rps = null;
if (log.isDebugEnabled()) {
log.debug("Retreiving all relying parties");
}
lst = getAllObjects(IdentityRegistryResources.RELYING_PARTY);
rps = lst.toArray(new RelyingPartyDO[lst.size()]);
return rps;
}
/**
*
* @param ppidValue
* @param hostName
* @throws IdentityException
*/
public void createPPIDValueForRP(PPIDValueDO ppidValue, String hostName)
throws IdentityException {
RelyingPartyDO rp = null;
String path = null;
Resource resource = null;
if (log.isDebugEnabled()) {
log.debug("Creating PPID for user relying party");
}
try {
rp = getFirstObjectWithPropertyValue(IdentityRegistryResources.RELYING_PARTY,
IdentityRegistryResources.PROP_HOST_NAME, hostName);
if (rp == null) {
if (log.isDebugEnabled()) {
log.debug("Rrelying party already exists");
}
return;
}
path = IdentityRegistryResources.RELYING_PARTY + rp.getUuid() + "/"
+ ppidValue.getUserId() + "/" + ppidValue.getPpid();
resource = registry.get(path);
registry.put(path, resource);
} catch (RegistryException e) {
log.error("Error while creating PPID for user relying party", e);
throw new IdentityException("Error while creating PPID for user relying party", e);
}
}
/**
*
* @param userId
* @param hostName
* @return
* @throws IdentityException
*/
public PPIDValueDO[] getIssuedPPIDvaluesForUser(String userId, String hostName)
throws IdentityException {
PPIDValueDO[] vals = new PPIDValueDO[0];
RelyingPartyDO rp = null;
List<PPIDValueDO> lst = null;
String path = null;
Collection collection = null;
String[] children = null;
if (log.isDebugEnabled()) {
log.debug("Retreiving PPIDs for user " + userId);
}
try {
rp = getFirstObjectWithPropertyValue(IdentityRegistryResources.RELYING_PARTY,
IdentityRegistryResources.PROP_HOST_NAME, hostName);
if (rp == null) {
// TODO
return vals;
}
lst = new ArrayList<PPIDValueDO>();
path = IdentityRegistryResources.RELYING_PARTY + rp.getUuid() + "/" + userId;
collection = (Collection) registry.get(path);
children = collection.getChildren();
for (String child : children) {
int index = child.lastIndexOf("/");
String ppid = child.substring(index);
PPIDValueDO temp = new PPIDValueDO();
temp.setHostName(hostName);
temp.setUserId(userId);
temp.setPpid(ppid);
lst.add(temp);
}
vals = lst.toArray(new PPIDValueDO[lst.size()]);
} catch (RegistryException e) {
log.error("Error while retreiving PPIDs for user " + userId, e);
throw new IdentityException("Error while retreiving PPIDs for user " + userId, e);
}
return vals;
}
/**
* {@inheritDoc}
*/
protected RelyingPartyDO resourceToObject(Resource resource) {
RelyingPartyDO rp = null;
if (resource != null) {
rp = new RelyingPartyDO();
String path = resource.getPath();
int index = path.lastIndexOf("/");
String uuid = path.substring(index);
String hostName = resource.getProperty(IdentityRegistryResources.PROP_HOST_NAME);
String alias = resource.getProperty(IdentityRegistryResources.PROP_ALIAS);
rp.setUuid(uuid);
rp.setHostName(hostName);
rp.setHostName(alias);
}
return rp;
}
}