package org.gcube.application.framework.core.cache.factories; import java.io.ByteArrayInputStream; import java.util.concurrent.atomic.AtomicInteger; import javax.xml.parsers.DocumentBuilderFactory; import org.gcube.application.framework.core.commons.ProfileService; import org.gcube.application.framework.core.util.CacheEntryConstants; import org.gcube.application.framework.core.util.QueryString; import org.gcube.common.core.scope.GCUBEScope; import org.gcube.common.core.types.VOID; import org.gcube.common.core.utils.logging.GCUBELog; import org.gcube.personalization.userprofileaccess.stubs.UserProfileAccessPortType; import org.gridforum.jgss.ExtendedGSSCredential; import org.w3c.dom.Document; import org.xml.sax.InputSource; import net.sf.ehcache.constructs.blocking.CacheEntryFactory; /** * @author Valia Tsagkalidou (NKUA) */ public class ProfileCacheEntryFactory implements CacheEntryFactory { /** * Document factory instance */ protected static final DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); /** * An atomic integer to get the Profile EPRs round-robin */ protected static AtomicInteger profileId = new AtomicInteger(0); /** Object logger. */ protected final GCUBELog logger = new GCUBELog(this); /** * @param key a QueryString representing pairs of keys and values. Needed keys are: "vre", "username" * @return the user profile as a DOM object (Document) */ public Object createEntry(Object key) throws Exception { QueryString query = (QueryString) key; String profile = ""; if(query.containsKey(CacheEntryConstants.vre) && query.containsKey(CacheEntryConstants.username)) { ProfileService profileService = new ProfileService(query.get(CacheEntryConstants.username), GCUBEScope.getScope(query.get(CacheEntryConstants.vre))); ExtendedGSSCredential cred = ApplicationCredentials.getInstance().getCredential(query.get(CacheEntryConstants.vre)); UserProfileAccessPortType port = profileService.getUserProfileAccess(cred); profile = port.getUserProfile(new VOID()); // this is the profile!!! logger.debug(profile); InputSource profileIn = new InputSource(new ByteArrayInputStream(profile.getBytes())); Document profileDoc = dfactory.newDocumentBuilder().parse(profileIn); return profileDoc; } return null; } }