use of org.jbei.ice.storage.model.RemotePartner in project ice by JBEI.
the class WebPartners method addNewPartner.
/**
* Adds the registry instance specified by the url to the list of existing partners (if not already in there)
* and sends a request to the remote instance that includes a security token that the remote instance
* can use to communicate with this instance.
* <p>
* Information about the remote instance is still saved even when it cannot be communicated with. This
* allows a future communication attempt.
*
* @param userId id of user performing action (must have admin privileges)
* @param partner registry partner object that contains unique uniform resource identifier & name for the registry
* @return add partner ofr
*/
public RegistryPartner addNewPartner(String userId, RegistryPartner partner, String thisUrl) {
if (!isInWebOfRegistries())
return null;
// check for admin privileges before granting request
if (!accountController.isAdministrator(userId))
throw new PermissionException("Non admin attempting to add remote partner " + partner.getUrl());
if (StringUtils.isEmpty(partner.getUrl()))
throw new IllegalArgumentException("Cannot add partner without valid url");
// check if there is a partner with that url
RemotePartner remotePartner = dao.getByUrl(partner.getUrl());
if (remotePartner != null) {
// if so just update the api key
return updateAPIKey(userId, remotePartner.getId());
}
Logger.info(userId + ": adding WoR partner [" + partner.getUrl() + "]");
// create information about this instance to send to potential partner
// including a random token for use when contacting this instance
RegistryPartner thisPartner = getThisInstanceWithNewApiKey();
// validate this url
if (!isValidUrl(thisPartner.getUrl())) {
if (isValidUrl(thisUrl))
thisPartner.setUrl(thisUrl);
else {
Logger.error("Could not obtain a valid url for this instance.");
thisPartner = null;
}
}
if (thisPartner == null) {
// will not contact
Logger.error("Cannot exchange api token with remote host due to invalid local url");
partner.setStatus(RemotePartnerStatus.NOT_CONTACTED);
} else {
RegistryPartner newPartner = remoteContact.contactPotentialPartner(thisPartner, partner.getUrl());
if (newPartner == null) {
// contact failed
Logger.error("Remote contact of partner " + partner.getUrl() + " failed");
partner.setStatus(RemotePartnerStatus.CONTACT_FAILED);
} else {
// contact succeeded with return of api key
partner.setStatus(RemotePartnerStatus.APPROVED);
partner.setApiKey(newPartner.getApiKey());
}
}
// if status is not approved, then the token is irrelevant since it is not stored and was not
// successfully transmitted
String apiKey = thisPartner != null ? thisPartner.getApiKey() : null;
return createRemotePartnerObject(partner, apiKey);
}
use of org.jbei.ice.storage.model.RemotePartner in project ice by JBEI.
the class WebPartners method update.
// only updates the status
public RegistryPartner update(String userId, long id, RegistryPartner partner) {
if (!isInWebOfRegistries())
return null;
if (!accountController.isAdministrator(userId))
throw new PermissionException(userId + " is not an admin");
RemotePartner existing = dao.get(id);
if (existing == null)
throw new IllegalArgumentException("Cannot retrieve partner with id " + id);
Logger.info(userId + ": updating partner (" + existing.getUrl() + ") to " + partner.toString());
existing.setPartnerStatus(partner.getStatus());
return dao.update(existing).toDataTransferObject();
}
use of org.jbei.ice.storage.model.RemotePartner in project ice by JBEI.
the class WebPartners method remove.
public boolean remove(String userId, long id) {
if (!accountController.isAdministrator(userId))
throw new PermissionException(userId + " is not an admin");
RemotePartner partner = dao.get(id);
if (partner == null)
return false;
dao.delete(partner);
// todo : contact deleted partner since they cannot contact anymore?
return true;
}
use of org.jbei.ice.storage.model.RemotePartner in project ice by JBEI.
the class WebPartners method createRemotePartnerObject.
protected RegistryPartner createRemotePartnerObject(RegistryPartner newPartner, String token) {
RemotePartner remotePartner = new RemotePartner();
remotePartner.setName(newPartner.getName());
remotePartner.setUrl(newPartner.getUrl());
remotePartner.setPartnerStatus(newPartner.getStatus());
if (newPartner.getStatus() == RemotePartnerStatus.APPROVED) {
remotePartner.setSalt(tokenHash.generateSalt());
String hash = tokenHash.encrypt(token + newPartner.getUrl(), remotePartner.getSalt());
remotePartner.setAuthenticationToken(hash);
remotePartner.setApiKey(newPartner.getApiKey());
}
remotePartner.setAdded(new Date());
return dao.create(remotePartner).toDataTransferObject();
}
Aggregations