Search in sources :

Example 31 with RemotePartner

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);
}
Also used : PermissionException(org.jbei.ice.lib.access.PermissionException) RemotePartner(org.jbei.ice.storage.model.RemotePartner) RegistryPartner(org.jbei.ice.lib.dto.web.RegistryPartner)

Example 32 with RemotePartner

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();
}
Also used : PermissionException(org.jbei.ice.lib.access.PermissionException) RemotePartner(org.jbei.ice.storage.model.RemotePartner)

Example 33 with RemotePartner

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;
}
Also used : PermissionException(org.jbei.ice.lib.access.PermissionException) RemotePartner(org.jbei.ice.storage.model.RemotePartner)

Example 34 with RemotePartner

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();
}
Also used : RemotePartner(org.jbei.ice.storage.model.RemotePartner) Date(java.util.Date)

Aggregations

RemotePartner (org.jbei.ice.storage.model.RemotePartner)34 RegistryPartner (org.jbei.ice.lib.dto.web.RegistryPartner)10 Account (org.jbei.ice.storage.model.Account)8 PermissionException (org.jbei.ice.lib.access.PermissionException)7 AccountTransfer (org.jbei.ice.lib.account.AccountTransfer)6 RemoteUser (org.jbei.ice.lib.dto.web.RemoteUser)4 Folder (org.jbei.ice.storage.model.Folder)4 RemoteAccessModel (org.jbei.ice.storage.model.RemoteAccessModel)4 Test (org.junit.Test)4 PartData (org.jbei.ice.lib.dto.entry.PartData)3 FolderDetails (org.jbei.ice.lib.dto.folder.FolderDetails)3 UserGroup (org.jbei.ice.lib.dto.group.UserGroup)3 Entry (org.jbei.ice.storage.model.Entry)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HibernateException (org.hibernate.HibernateException)2 AccountController (org.jbei.ice.lib.account.AccountController)2 IceRestClient (org.jbei.ice.services.rest.IceRestClient)2 DAOException (org.jbei.ice.storage.DAOException)2 RemoteClientModel (org.jbei.ice.storage.model.RemoteClientModel)2