if (instance.getWidget() == null || instance.getIdKey() == null) {
throw new Exception(localizedMessages.getString("OpenSocialUtils.1")); //$NON-NLS-1$
}
BasicBlobCrypter crypter = new BasicBlobCrypter(key.getBytes());
BlobCrypterSecurityToken token = new BlobCrypterSecurityToken(crypter, CONTAINER_ID, DOMAIN_ID);
token.setAppUrl(getUrl(instance));
// The ModuleId only takes a long, so we just have to hash the idKey for now. We could use the instance id,
// but this would involve updating the instance object in a two-step create, which is more fiddly and expensive.
token.setModuleId(instance.getIdKey().hashCode());
token.setOwnerId(getOwnerId(instance));
token.setViewerId(getUserId(instance));
// The URL used in the active request
token.setActiveUrl(getUrl(instance));
String encryptedToken = token.encrypt();
encryptedToken = URLEncoder.encode(encryptedToken, "UTF-8"); //$NON-NLS-1$
return encryptedToken;
}