@ -11,6 +11,7 @@ import java.util.LinkedList;
import java.util.List ;
import java.util.Map ;
import javax.servlet.http.HttpServletRequest ;
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
@ -24,9 +25,8 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost ;
import org.apache.http.impl.client.HttpClients ;
import org.apache.http.message.BasicNameValuePair ;
import org.gcube.application.framework.core.session.ASLSession ;
import org.gcube.application.framework.core.session.SessionManager ;
import org.gcube.common.encryption.StringEncrypter ;
import org.gcube.common.portal.PortalContext ;
import org.gcube.common.resources.gcore.ServiceEndpoint ;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint ;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property ;
@ -35,7 +35,6 @@ import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.common.scope.impl.ScopeBean.Type ;
import org.gcube.portal.custom.communitymanager.SiteManagerUtil ;
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper ;
import org.gcube.portal.databook.shared.UserInfo ;
import org.gcube.portlets.user.socialprofile.client.SocialService ;
import org.gcube.portlets.user.socialprofile.shared.UserContext ;
@ -47,11 +46,11 @@ import org.gcube.vomanagement.usermanagement.model.CustomAttributeKeys;
import org.gcube.vomanagement.usermanagement.model.GCubeUser ;
import org.json.simple.parser.ContainerFactory ;
import org.json.simple.parser.JSONParser ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.w3c.dom.Document ;
import com.google.gwt.user.server.rpc.RemoteServiceServlet ;
import com.liferay.portal.kernel.log.Log ;
import com.liferay.portal.kernel.log.LogFactoryUtil ;
import com.liferay.portal.model.Contact ;
import com.liferay.portal.service.ContactLocalServiceUtil ;
import com.liferay.portal.service.UserLocalServiceUtil ;
@ -62,7 +61,8 @@ import com.liferay.portal.service.UserLocalServiceUtil;
@SuppressWarnings ( "serial" )
public class SocialServiceImpl extends RemoteServiceServlet implements SocialService {
private static final Logger _log = LoggerFactory . getLogger ( SocialServiceImpl . class ) ;
//private static final Logger logger = LoggerFactory.getLogger(SocialServiceImpl.class);
private static final Log logger = LogFactoryUtil . getLog ( SocialServiceImpl . class ) ;
private static final String LINKEDIN_HOST_SERVICE_NAME = "host" ;
private static final String LINKEDIN_CLIEND_ID_PROPNAME = "client_id" ;
@ -71,29 +71,36 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
private static final String LINKEDIN_API_REQUEST = "https://api.linkedin.com/v1/people/~:(id,headline,summary,location:(name),industry,positions,picture-urls::(original),public-profile-url)" ;
/ * *
* the current ASLSession
* @return the session
* Retrieve the current user by using the portal manager
* @return a GcubeUser object
* /
private ASLSession getASLSession ( ) {
String sessionID = this . getThreadLocalRequest ( ) . getSession ( ) . getId ( ) ;
String user = ( String ) this . getThreadLocalRequest ( ) . getSession ( ) . getAttribute ( ScopeHelper . USERNAME_ATTRIBUTE ) ;
if ( user = = null ) {
_log . warn ( "USER IS NULL setting test.user and Running OUTSIDE PORTAL" ) ;
user = getDevelopmentUser ( ) ;
SessionManager . getInstance ( ) . getASLSession ( sessionID , user ) . setScope ( "/gcube" ) ;
}
return SessionManager . getInstance ( ) . getASLSession ( sessionID , user ) ;
}
private GCubeUser getCurrentUser ( HttpServletRequest request ) {
if ( request = = null )
throw new IllegalArgumentException ( "HttpServletRequest is null!" ) ;
PortalContext pContext = PortalContext . getConfiguration ( ) ;
GCubeUser user = pContext . getCurrentUser ( request ) ;
logger . debug ( "Returning user " + user ) ;
return user ;
}
/ * *
* when packaging test will fail if the user is not set to test . user
* @return .
* Retrieve the current scope by using the portal manag er
* @return a string representing the context
* /
public String getDevelopmentUser ( ) {
String user = "test.user" ;
// user = "costantino.perciante";
return user ;
private String getCurrentContext ( HttpServletRequest request ) {
if ( request = = null )
throw new IllegalArgumentException ( "HttpServletRequest is null!" ) ;
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String context = pContext . getCurrentScope ( request ) ;
logger . debug ( "Returning context " + context ) ;
return context ;
}
/ * *
*
* @return true if you ' re running into the portal , false if in development
@ -104,37 +111,39 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
return true ;
}
catch ( com . liferay . portal . kernel . bean . BeanLocatorException ex ) {
_ log. trace ( "Development Mode ON" ) ;
logger . trace ( "Development Mode ON" ) ;
return false ;
}
}
@Override
public UserContext getUserContext ( String userid ) {
if ( userid = = null | | userid . equals ( "" ) | | userid . equals ( get ASLSession( ) . getUsername ( ) ) ) {
if ( userid = = null | | userid . equals ( "" ) | | userid . equals ( get CurrentUser( this . getThreadLocalRequest ( ) ) . getUsername ( ) ) ) {
System . out . println ( "Own Profile" ) ;
_ log. info ( "Own Profile" ) ;
logger . info ( "Own Profile" ) ;
return getOwnProfile ( ) ;
}
else {
System . out . println ( "Reading Profile" ) ;
_ log. info ( userid + " Reading Profile" ) ;
logger . info ( userid + " Reading Profile" ) ;
return getUserProfile ( userid ) ;
}
}
@Override
public String saveProfessionalBackground ( String summary ) {
String username = getCurrentUser ( this . getThreadLocalRequest ( ) ) . getUsername ( ) ;
// parse (html sanitize)
String toReturn = transformSummary ( summary ) ;
if ( isWithinPortal ( ) ) {
UserManager um = new LiferayUserManager ( ) ;
ASLSession session = getASLSession ( ) ;
try {
GCubeUser user = um . getUserByUsername ( session. getUsername ( ) ) ;
GCubeUser user = um . getUserByUsername ( username ) ;
um . setUserProfessionalBackground ( user . getUserId ( ) , summary ) ; // save as it is
return toReturn ; // sanitized
} catch ( Exception e ) {
_ log. error ( "Unable to save the professional background " + summary + " for user " + session. getUsername ( ) ) ;
logger . error ( "Unable to save the professional background " + summary + " for user " + username ) ;
return null ;
}
} else
@ -142,10 +151,11 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
}
private UserContext getUserProfile ( String username ) {
ASLSession session = getASLSession ( ) ;
String email = username + "@isti.cnr.it" ;
String fullName = username + " FULL" ;
String thumbnailURL = "images/Avatar_default.png" ;
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String contextName = pContext . getCurrentGroupName ( this . getThreadLocalRequest ( ) ) ;
if ( isWithinPortal ( ) ) {
try {
UserManager um = new LiferayUserManager ( ) ;
@ -160,14 +170,14 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
String company = user . getLocation_industry ( ) ;
String summary = transformSummary ( um . getUserProfessionalBackground ( user . getUserId ( ) ) ) ;
return new UserContext ( userInfo , headline , company , summary , session. getScopeName ( ) , false , isInfrastructureScope ( ) ) ;
return new UserContext ( userInfo , headline , company , summary , contextName , false , isInfrastructureScope ( ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
return new UserContext ( ) ;
}
} else {
_ log. info ( "Returning test USER" ) ;
logger . info ( "Returning test USER" ) ;
HashMap < String , String > fakeVreNames = new HashMap < String , String > ( ) ;
fakeVreNames . put ( "/gcube/devsec/devVRE" , "devVRE" ) ;
//fakeVreNames.put("/gcube/devNext/NexNext","NexNext");
@ -175,14 +185,15 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
UserInfo user = new UserInfo ( username , username + "FULL" , thumbnailURL , email , "fakeAccountUrl" , true , false , fakeVreNames ) ;
return new UserContext ( user , "" , "" , ""
+ "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam."
+ "" , session. getScopeName ( ) , false , isInfrastructureScope ( ) ) ;
+ "" , contextName , false , isInfrastructureScope ( ) ) ;
}
}
private UserContext getOwnProfile ( ) {
try {
ASLSession session = getASLSession ( ) ;
String username = session . getUsername ( ) ;
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String contextName = pContext . getCurrentGroupName ( this . getThreadLocalRequest ( ) ) ;
String username = getCurrentUser ( this . getThreadLocalRequest ( ) ) . getUsername ( ) ;
String email = username + "@isti.cnr.it" ;
String fullName = username + " FULL" ;
String thumbnailURL = "images/Avatar_default.png" ;
@ -203,18 +214,18 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
String company = user . getLocation_industry ( ) ;
String summary = transformSummary ( um . getUserProfessionalBackground ( user . getUserId ( ) ) ) ;
return new UserContext ( userInfo , headline , company , summary , session. getScopeName ( ) , true , isInfrastructureScope ( ) ) ;
return new UserContext ( userInfo , headline , company , summary , contextName , true , isInfrastructureScope ( ) ) ;
}
else {
_ log. info ( "Returning test USER" ) ;
logger . info ( "Returning test USER" ) ;
HashMap < String , String > fakeVreNames = new HashMap < String , String > ( ) ;
fakeVreNames . put ( "/gcube/devsec/devVRE" , "devVRE" ) ;
//fakeVreNames.put("/gcube/devNext/NexNext","NexNext");
UserInfo user = new UserInfo ( getASLSession( ) . getUsername ( ) , fullName , thumbnailURL , email , "fakeAccountUrl" , true , false , fakeVreNames ) ;
UserInfo user = new UserInfo ( username , fullName , thumbnailURL , email , "fakeAccountUrl" , true , false , fakeVreNames ) ;
return new UserContext ( user , "" , "" , ""
+ "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam."
+ "" , session. getScopeName ( ) , true , isInfrastructureScope ( ) ) ;
+ "" , contextName , true , isInfrastructureScope ( ) ) ;
}
} catch ( Exception e ) {
@ -237,7 +248,7 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
public Boolean saveHeadline ( String newHeadline ) {
try {
UserManager um = new LiferayUserManager ( ) ;
return um . updateJobTitle ( um . getUserId ( get ASLSession( ) . getUsername ( ) ) , newHeadline ) ;
return um . updateJobTitle ( um . getUserId ( get CurrentUser( this . getThreadLocalRequest ( ) ) . getUsername ( ) ) , newHeadline ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
return false ;
@ -249,7 +260,7 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
//try save the location/industry
try {
UserManager um = new LiferayUserManager ( ) ;
um . saveCustomAttr ( um . getUserId ( get ASLSession( ) . getUsername ( ) ) , CustomAttributeKeys . USER_LOCATION_INDUSTRY . getKeyName ( ) , institution ) ;
um . saveCustomAttr ( um . getUserId ( get CurrentUser( this . getThreadLocalRequest ( ) ) . getUsername ( ) ) , CustomAttributeKeys . USER_LOCATION_INDUSTRY . getKeyName ( ) , institution ) ;
return true ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
@ -287,7 +298,7 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
InputStream myInputStream = entity . getContent ( ) ;
try {
String jsonText = IOUtils . toString ( myInputStream , "UTF-8" ) ;
_ log. debug ( "LinkedIn response: " + jsonText ) ;
logger . debug ( "LinkedIn response: " + jsonText ) ;
if ( jsonText = = null )
return null ;
@ -336,7 +347,7 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
// add request header as in the documentation, @see https://developer.linkedin.com/documents/authentication
request . addHeader ( "Authorization" , "Bearer " + token ) ;
try {
_ log. debug ( "Asking LinkedIn profile via http GET for " + get ASLSession( ) . getUsername ( ) ) ;
logger . debug ( "Asking LinkedIn profile via http GET for " + get CurrentUser( this . getThreadLocalRequest ( ) ) . getUsername ( ) ) ;
HttpResponse httpResponse = httpClient . execute ( request ) ;
HttpEntity entity = httpResponse . getEntity ( ) ;
@ -345,14 +356,14 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
try {
String xmlResponse = IOUtils . toString ( myInputStream , "UTF-8" ) ;
//xmlResponse = testParsing();
_ log. debug ( "LinkedIn xmlResponse: " + xmlResponse ) ;
logger . debug ( "LinkedIn xmlResponse: " + xmlResponse ) ;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory . newInstance ( ) ;
DocumentBuilder dBuilder = dbFactory . newDocumentBuilder ( ) ;
Document doc = dBuilder . parse ( IOUtils . toInputStream ( xmlResponse ) ) ;
_ log. debug ( "Parsing LinkedIn profile xmlResponse for " + get ASLSession( ) . getUsername ( ) ) ;
logger . debug ( "Parsing LinkedIn profile xmlResponse for " + get CurrentUser( this . getThreadLocalRequest ( ) ) . getUsername ( ) ) ;
String headline = "" ;
String summary = "" ;
String location = "" ;
@ -380,7 +391,7 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
String positions = "" ;
currValue = helper . evaluate ( "/person/positions/position" ) ;
int positionsNo = currValue . size ( ) ;
_ log. debug ( "Number of positions: " + positions ) ;
logger . debug ( "Number of positions: " + positions ) ;
if ( positionsNo > 0 ) {
positions = ( positionsNo > 1 ) ? "\n\nCurrent Positions:" : "\n\nCurrent Position:" ;
for ( int i = 0 ; i < positionsNo ; i + + ) {
@ -434,9 +445,9 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
summary + = positions ;
if ( isWithinPortal ( ) ) {
_ log. debug ( "LinkedIn Profile gotten correctly for " + get ASLSession( ) . getUsername ( ) + " attempting to write into DB ..." ) ;
logger . debug ( "LinkedIn Profile gotten correctly for " + get CurrentUser( this . getThreadLocalRequest ( ) ) . getUsername ( ) + " attempting to write into DB ..." ) ;
com . liferay . portal . model . User user ;
user = UserLocalServiceUtil . getUserByScreenName ( SiteManagerUtil . getCompany ( ) . getCompanyId ( ) , get ASLSession( ) . getUsername ( ) ) ;
user = UserLocalServiceUtil . getUserByScreenName ( SiteManagerUtil . getCompany ( ) . getCompanyId ( ) , get CurrentUser( this . getThreadLocalRequest ( ) ) . getUsername ( ) ) ;
//headline
if ( headline . compareTo ( "" ) ! = 0 ) {
String checkedHeadline = headline ;
@ -467,7 +478,7 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
if ( pictureURL . compareTo ( "" ) ! = 0 & & pictureURL . startsWith ( "http" ) ) {
byte [ ] pictureData = getUserPictureFromURL ( httpClient , pictureURL ) ;
if ( pictureData ! = null ) {
_ log. debug ( "Updating Image Profile with this one: " + pictureURL ) ;
logger . debug ( "Updating Image Profile with this one: " + pictureURL ) ;
UserLocalServiceUtil . updatePortrait ( user . getUserId ( ) , pictureData ) ;
}
}
@ -479,7 +490,7 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
return publicProfileURL ;
else return null ;
} else {
_ log. warn ( "Development Mode ON, not attempting to write into DB" ) ;
logger . warn ( "Development Mode ON, not attempting to write into DB" ) ;
return "fakePublicURL" ;
}
}
@ -508,7 +519,7 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
try {
return IOUtils . toByteArray ( httpClient . execute ( request ) . getEntity ( ) . getContent ( ) ) ;
} catch ( Exception e ) {
_ log. error ( "Could not get bytes from picture URL " + e . getMessage ( ) ) ;
logger . error ( "Could not get bytes from picture URL " + e . getMessage ( ) ) ;
e . printStackTrace ( ) ;
return null ;
}
@ -520,8 +531,8 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
* @return an hashmap containing the 3 info needed
* /
private HashMap < String , String > getLinkedInUASInfo ( ) {
String scope = get ASLSession( ) . getScope ( ) ;
_ log. info ( "Looking for a LinkedIn UAS in " + scope ) ;
String scope = get CurrentContext( this . getThreadLocalRequest ( ) ) ;
logger . info ( "Looking for a LinkedIn UAS in " + scope ) ;
String previousScope = ScopeProvider . instance . get ( ) ;
ScopeProvider . instance . set ( scope ) ;
SimpleQuery query = queryFor ( ServiceEndpoint . class ) ;
@ -533,7 +544,7 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
ScopeProvider . instance . set ( previousScope ) ;
if ( list . size ( ) > 1 ) {
_ log. warn ( "Multiple LinkedIn-user-authorization Service Endpoints available in the scope, should be only one." ) ;
logger . warn ( "Multiple LinkedIn-user-authorization Service Endpoints available in the scope, should be only one." ) ;
return null ;
}
else if ( list . size ( ) = = 1 ) {
@ -566,7 +577,7 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
* @return < code > true < / code > if it is , < code > false < / code > otherwise .
* /
private boolean isInfrastructureScope ( ) {
ScopeBean scope = new ScopeBean ( get ASLSession( ) . getScope ( ) ) ;
ScopeBean scope = new ScopeBean ( get CurrentContext( this . getThreadLocalRequest ( ) ) ) ;
return scope . is ( Type . INFRASTRUCTURE ) ;
}
/ * *