aslcore/src/org/gcube/application/framework/core/commons/ProfileService.java

244 lines
7.5 KiB
Java

package org.gcube.application.framework.core.commons;
import java.rmi.RemoteException;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.axis.message.addressing.Address;
import org.apache.axis.message.addressing.EndpointReferenceType;
import org.gcube.application.framework.core.cache.HarvestersManager;
import org.gcube.application.framework.core.security.PortalSecurityManager;
import org.gcube.application.framework.core.util.CacheEntryConstants;
import org.gcube.common.core.contexts.GCUBERemotePortTypeContext;
import org.gcube.common.core.faults.GCUBEFault;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.security.GCUBESecurityManager;
import org.gcube.common.core.types.VOID;
import org.gcube.personalization.profileadministration.stubs.ProfileAdministrationPortType;
import org.gcube.personalization.profileadministration.stubs.service.ProfileAdministrationServiceAddressingLocator;
import org.gcube.personalization.userprofileaccess.stubs.UserProfileAccessFactoryPortType;
import org.gcube.personalization.userprofileaccess.stubs.UserProfileAccessPortType;
import org.gcube.personalization.userprofileaccess.stubs.service.UserProfileAccessFactoryServiceAddressingLocator;
import org.gcube.personalization.userprofileaccess.stubs.service.UserProfileAccessServiceAddressingLocator;
import org.gcube.searchservice.searchlibrary.isharvester.ISInfo;
import org.gridforum.jgss.ExtendedGSSCredential;
import org.ietf.jgss.GSSCredential;
/**
* @author Valia Tsagkalidou (NKUA)
*
*/
public class ProfileService {
protected static AtomicInteger profileId = new AtomicInteger(0);
protected String username;
protected GCUBEScope scope;
/**
* The constructor
* @param username the user for each it will get the profile (Resource)
* @param scope the scope of the active VRE
*/
public ProfileService( String username, GCUBEScope scope) {
super();
this.username = username;
this.scope = scope;
}
/**
* @return
*/
public UserProfileAccessFactoryPortType getUserProfileAccessFactory()
{
ISInfo disInfo = HarvestersManager.getInstance().getISInfo(scope);
String[] profileURIs;
try {
profileURIs = disInfo.getEndPointForPersonalizationUPAFactoryService();
} catch (Exception e1) {
e1.printStackTrace();
return null;
}
System.out.println("getUserProfileAccessFactory: " + profileURIs.length);
EndpointReferenceType serviceEPR = new EndpointReferenceType();
UserProfileAccessFactoryPortType upa = null;
try
{
serviceEPR.setAddress(new Address(profileURIs[profileId.getAndIncrement()%profileURIs.length]));
UserProfileAccessFactoryServiceAddressingLocator upalocator = new UserProfileAccessFactoryServiceAddressingLocator();
upa = upalocator.getUserProfileAccessFactoryPortTypePort(serviceEPR);
return upa;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param cred
* @return
*/
public UserProfileAccessPortType getUserProfileAccess(ExtendedGSSCredential cred)
{
UserProfileAccessFactoryPortType upa = getUserProfileAccessFactory();
GCUBESecurityManager secManager = new PortalSecurityManager(scope);
if(secManager.isSecurityEnabled())
{
try {
secManager.useCredentials(cred);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
upa = GCUBERemotePortTypeContext.getProxy(upa, scope, secManager);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EndpointReferenceType epr = null;
try {
System.out.println("\n\n\n\n" + username + " scope= " + scope + "\n\n\n\n");
epr = upa.createResource(username);
} catch (GCUBEFault e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
if(epr != null)
{
try {
return getUserProfileAccessServicePortType(epr, scope, cred);
} catch (RemoteException e) {
e.printStackTrace();
}
}
return null;
}
/**
* @param upa
* @param cred
* @return
*/
public UserProfileAccessPortType getUserProfileAccess(UserProfileAccessFactoryPortType upa, ExtendedGSSCredential cred)
{
GCUBESecurityManager secManager = new PortalSecurityManager(scope);
if(secManager.isSecurityEnabled())
{
try {
secManager.useCredentials(cred);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
upa = GCUBERemotePortTypeContext.getProxy(upa, scope, secManager);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EndpointReferenceType epr = null;
try {
epr = upa.createResource(username);
} catch (GCUBEFault e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
if(epr != null)
{
try {
return getUserProfileAccessServicePortType(epr, scope, cred);
} catch (RemoteException e) {
e.printStackTrace();
}
}
return null;
}
/**
* This method returns the port type of the service User Profile Access.
*
* @param epr the endpoint reference of a WS resource of this service
* @return port for UserProfileAccess service
* @throws RemoteException an exception
*/
private UserProfileAccessPortType getUserProfileAccessServicePortType(EndpointReferenceType epr,GCUBEScope scope, GSSCredential cred) throws RemoteException {
UserProfileAccessServiceAddressingLocator instanceLocator = new UserProfileAccessServiceAddressingLocator();
UserProfileAccessPortType port = null;
try {
port = instanceLocator.getUserProfileAccessPortTypePort(epr);
GCUBESecurityManager secManager = new PortalSecurityManager(scope);
if(secManager.isSecurityEnabled())
{
try {
secManager.useCredentials(cred);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
port = GCUBERemotePortTypeContext.getProxy(port, scope, secManager);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
throw new RemoteException("A service exception was thrown. Failed to get the port type of the service user profile access");
}
return port;
}
/**
* @param cred
* @return
*/
public ProfileAdministrationPortType getProfileAdministration(GSSCredential cred)
{
ISInfo disInfo = HarvestersManager.getInstance().getISInfo(scope);
String[] profileURIs;
try {
profileURIs = disInfo.getEndPointForPersonalizationPAService();
} catch (Exception e1) {
e1.printStackTrace();
return null;
}
EndpointReferenceType serviceEPR = new EndpointReferenceType();
ProfileAdministrationPortType pa = null;
System.out.println("profile administration services: " + profileURIs.length);
try
{
serviceEPR.setAddress(new Address(profileURIs[profileId.getAndIncrement()%profileURIs.length]));
ProfileAdministrationServiceAddressingLocator palocator = new ProfileAdministrationServiceAddressingLocator();
pa = palocator.getProfileAdministrationPortTypePort(serviceEPR);
GCUBESecurityManager secManager = new PortalSecurityManager(scope);
if(secManager.isSecurityEnabled())
{
secManager.useCredentials(cred);
}
pa = GCUBERemotePortTypeContext.getProxy(pa, scope, secManager);
return pa;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
}