2013-05-08 17:17:55 +02:00
package org.gcube.portlets.user.socialprofile.server ;
2014-09-04 18:08:31 +02:00
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor ;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor ;
2014-09-02 19:08:38 +02:00
import java.io.InputStream ;
import java.util.ArrayList ;
2013-05-08 18:16:43 +02:00
import java.util.HashMap ;
2014-09-02 19:08:38 +02:00
import java.util.LinkedHashMap ;
import java.util.LinkedList ;
import java.util.List ;
import java.util.Map ;
2013-05-08 18:16:43 +02:00
2016-11-15 15:18:08 +01:00
import javax.servlet.http.HttpServletRequest ;
2014-09-03 18:34:35 +02:00
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
2014-09-02 19:08:38 +02:00
import org.apache.commons.io.IOUtils ;
import org.apache.http.HttpEntity ;
import org.apache.http.HttpResponse ;
import org.apache.http.NameValuePair ;
import org.apache.http.client.HttpClient ;
import org.apache.http.client.entity.UrlEncodedFormEntity ;
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 ;
2014-09-04 18:08:31 +02:00
import org.gcube.common.encryption.StringEncrypter ;
2016-11-15 15:18:08 +01:00
import org.gcube.common.portal.PortalContext ;
2014-09-04 18:08:31 +02:00
import org.gcube.common.resources.gcore.ServiceEndpoint ;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint ;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property ;
import org.gcube.common.resources.gcore.utils.XPathHelper ;
import org.gcube.common.scope.api.ScopeProvider ;
2014-09-09 14:48:47 +02:00
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.common.scope.impl.ScopeBean.Type ;
2016-03-29 11:23:59 +02:00
import org.gcube.portal.custom.communitymanager.SiteManagerUtil ;
2013-05-08 18:16:43 +02:00
import org.gcube.portal.databook.shared.UserInfo ;
2013-05-08 17:17:55 +02:00
import org.gcube.portlets.user.socialprofile.client.SocialService ;
2013-05-09 18:00:47 +02:00
import org.gcube.portlets.user.socialprofile.shared.UserContext ;
2014-09-04 18:08:31 +02:00
import org.gcube.resources.discovery.client.api.DiscoveryClient ;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery ;
2016-03-29 11:23:59 +02:00
import org.gcube.vomanagement.usermanagement.UserManager ;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager ;
2016-06-28 17:36:13 +02:00
import org.gcube.vomanagement.usermanagement.model.CustomAttributeKeys ;
2016-03-29 11:23:59 +02:00
import org.gcube.vomanagement.usermanagement.model.GCubeUser ;
2014-09-02 19:08:38 +02:00
import org.json.simple.parser.ContainerFactory ;
import org.json.simple.parser.JSONParser ;
2014-09-03 18:34:35 +02:00
import org.w3c.dom.Document ;
2013-05-08 18:16:43 +02:00
2013-05-08 17:17:55 +02:00
import com.google.gwt.user.server.rpc.RemoteServiceServlet ;
2016-11-15 15:18:08 +01:00
import com.liferay.portal.kernel.log.Log ;
import com.liferay.portal.kernel.log.LogFactoryUtil ;
2014-09-23 17:56:40 +02:00
import com.liferay.portal.model.Contact ;
import com.liferay.portal.service.ContactLocalServiceUtil ;
2013-05-08 18:16:43 +02:00
import com.liferay.portal.service.UserLocalServiceUtil ;
2013-05-08 17:17:55 +02:00
/ * *
* The server side implementation of the RPC service .
* /
@SuppressWarnings ( " serial " )
public class SocialServiceImpl extends RemoteServiceServlet implements SocialService {
2013-10-10 15:40:23 +02:00
2016-11-15 15:18:08 +01:00
//private static final Logger logger = LoggerFactory.getLogger(SocialServiceImpl.class);
private static final Log logger = LogFactoryUtil . getLog ( SocialServiceImpl . class ) ;
2013-05-09 18:00:47 +02:00
2014-09-04 18:08:31 +02:00
private static final String LINKEDIN_HOST_SERVICE_NAME = " host " ;
private static final String LINKEDIN_CLIEND_ID_PROPNAME = " client_id " ;
private static final String LINKEDIN_CLIEND_SECRET_PROPNAME = " client_secret " ;
2014-09-23 17:56:40 +02:00
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) " ;
2013-05-08 18:16:43 +02:00
/ * *
2016-11-15 15:18:08 +01:00
* Retrieve the current user by using the portal manager
* @return a GcubeUser object
2013-05-08 18:16:43 +02:00
* /
2016-11-15 15:18:08 +01:00
private GCubeUser getCurrentUser ( HttpServletRequest request ) {
if ( request = = null )
throw new IllegalArgumentException ( " HttpServletRequest is null! " ) ;
2014-09-09 18:47:39 +02:00
2016-11-15 15:18:08 +01:00
PortalContext pContext = PortalContext . getConfiguration ( ) ;
GCubeUser user = pContext . getCurrentUser ( request ) ;
logger . debug ( " Returning user " + user ) ;
return user ;
}
2014-06-09 10:03:01 +02:00
/ * *
2016-11-15 15:18:08 +01:00
* Retrieve the current scope by using the portal manager
* @return a string representing the context
2014-06-09 10:03:01 +02:00
* /
2016-11-15 15:18:08 +01:00
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 ;
2014-06-09 10:03:01 +02:00
}
2016-11-15 15:18:08 +01:00
2014-09-03 18:34:35 +02:00
/ * *
*
* @return true if you ' re running into the portal , false if in development
* /
private boolean isWithinPortal ( ) {
try {
UserLocalServiceUtil . getService ( ) ;
return true ;
}
catch ( com . liferay . portal . kernel . bean . BeanLocatorException ex ) {
2016-11-15 15:18:08 +01:00
logger . trace ( " Development Mode ON " ) ;
2014-09-03 18:34:35 +02:00
return false ;
}
}
2016-11-15 15:18:08 +01:00
2013-05-08 18:16:43 +02:00
@Override
2013-05-09 18:00:47 +02:00
public UserContext getUserContext ( String userid ) {
2016-11-15 15:18:08 +01:00
if ( userid = = null | | userid . equals ( " " ) | | userid . equals ( getCurrentUser ( this . getThreadLocalRequest ( ) ) . getUsername ( ) ) ) {
2013-05-09 18:00:47 +02:00
System . out . println ( " Own Profile " ) ;
2016-11-15 15:18:08 +01:00
logger . info ( " Own Profile " ) ;
2013-05-09 18:00:47 +02:00
return getOwnProfile ( ) ;
}
else {
System . out . println ( " Reading Profile " ) ;
2016-11-15 15:18:08 +01:00
logger . info ( userid + " Reading Profile " ) ;
2013-07-17 18:32:55 +02:00
return getUserProfile ( userid ) ;
2013-05-09 18:00:47 +02:00
}
}
2016-05-05 18:19:00 +02:00
@Override
2016-05-06 12:35:29 +02:00
public String saveProfessionalBackground ( String summary ) {
2016-11-15 15:18:08 +01:00
String username = getCurrentUser ( this . getThreadLocalRequest ( ) ) . getUsername ( ) ;
2016-05-06 12:35:29 +02:00
// parse (html sanitize)
2016-05-11 14:26:38 +02:00
String toReturn = transformSummary ( summary ) ;
2016-05-05 18:19:00 +02:00
if ( isWithinPortal ( ) ) {
UserManager um = new LiferayUserManager ( ) ;
try {
2016-11-15 15:18:08 +01:00
GCubeUser user = um . getUserByUsername ( username ) ;
2016-05-11 14:26:38 +02:00
um . setUserProfessionalBackground ( user . getUserId ( ) , summary ) ; // save as it is
return toReturn ; // sanitized
2016-05-05 18:19:00 +02:00
} catch ( Exception e ) {
2016-11-15 15:18:08 +01:00
logger . error ( " Unable to save the professional background " + summary + " for user " + username ) ;
2016-05-06 12:35:29 +02:00
return null ;
2016-05-05 18:19:00 +02:00
}
} else
2016-05-11 14:26:38 +02:00
return toReturn ; // development mode
2016-05-05 18:19:00 +02:00
}
2013-07-17 18:32:55 +02:00
private UserContext getUserProfile ( String username ) {
String email = username + " @isti.cnr.it " ;
String fullName = username + " FULL " ;
String thumbnailURL = " images/Avatar_default.png " ;
2016-11-15 15:18:08 +01:00
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String contextName = pContext . getCurrentGroupName ( this . getThreadLocalRequest ( ) ) ;
2014-09-03 18:34:35 +02:00
if ( isWithinPortal ( ) ) {
2013-07-17 18:32:55 +02:00
try {
2016-03-29 11:23:59 +02:00
UserManager um = new LiferayUserManager ( ) ;
GCubeUser user = um . getUserByUsername ( username ) ;
thumbnailURL = user . getUserAvatarURL ( ) ;
fullName = user . getFullname ( ) ;
email = user . getEmail ( ) ;
2013-07-17 18:32:55 +02:00
HashMap < String , String > vreNames = new HashMap < String , String > ( ) ;
2016-03-29 11:23:59 +02:00
UserInfo userInfo = new UserInfo ( username , fullName , thumbnailURL , user . getEmail ( ) , " " , true , false , vreNames ) ;
String headline = user . getJobTitle ( ) ;
String company = user . getLocation_industry ( ) ;
String summary = transformSummary ( um . getUserProfessionalBackground ( user . getUserId ( ) ) ) ;
2014-07-23 16:39:04 +02:00
2016-11-15 15:18:08 +01:00
return new UserContext ( userInfo , headline , company , summary , contextName , false , isInfrastructureScope ( ) ) ;
2013-07-17 18:32:55 +02:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
return new UserContext ( ) ;
}
} else {
2016-11-15 15:18:08 +01:00
logger . info ( " Returning test USER " ) ;
2013-07-17 18:32:55 +02:00
HashMap < String , String > fakeVreNames = new HashMap < String , String > ( ) ;
fakeVreNames . put ( " /gcube/devsec/devVRE " , " devVRE " ) ;
//fakeVreNames.put("/gcube/devNext/NexNext","NexNext");
UserInfo user = new UserInfo ( username , username + " FULL " , thumbnailURL , email , " fakeAccountUrl " , true , false , fakeVreNames ) ;
2014-07-23 16:39:04 +02:00
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. "
2016-11-15 15:18:08 +01:00
+ " " , contextName , false , isInfrastructureScope ( ) ) ;
2013-07-17 18:32:55 +02:00
}
}
2013-05-09 18:00:47 +02:00
private UserContext getOwnProfile ( ) {
2013-05-08 18:16:43 +02:00
try {
2016-11-15 15:18:08 +01:00
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String contextName = pContext . getCurrentGroupName ( this . getThreadLocalRequest ( ) ) ;
String username = getCurrentUser ( this . getThreadLocalRequest ( ) ) . getUsername ( ) ;
2013-05-08 18:16:43 +02:00
String email = username + " @isti.cnr.it " ;
String fullName = username + " FULL " ;
String thumbnailURL = " images/Avatar_default.png " ;
2014-09-03 18:34:35 +02:00
if ( isWithinPortal ( ) ) {
2013-05-09 18:00:47 +02:00
2016-03-29 11:23:59 +02:00
UserManager um = new LiferayUserManager ( ) ;
GCubeUser user = um . getUserByUsername ( username ) ;
thumbnailURL = user . getUserAvatarURL ( ) ;
fullName = user . getFullname ( ) ;
email = user . getEmail ( ) ;
2016-05-05 18:19:00 +02:00
// ThemeDisplay themeDisplay = (ThemeDisplay) this.getThreadLocalRequest().getSession().getAttribute(WebKeys.THEME_DISPLAY);
2016-03-29 11:23:59 +02:00
String accountURL = " TODO " ; //TODO: //themeDisplay.getURLMyAccount().toString();
2013-05-08 18:16:43 +02:00
HashMap < String , String > vreNames = new HashMap < String , String > ( ) ;
2016-03-29 11:23:59 +02:00
UserInfo userInfo = new UserInfo ( username , fullName , thumbnailURL , user . getEmail ( ) , accountURL , true , false , vreNames ) ;
String headline = user . getJobTitle ( ) ;
String company = user . getLocation_industry ( ) ;
String summary = transformSummary ( um . getUserProfessionalBackground ( user . getUserId ( ) ) ) ;
2014-07-23 16:39:04 +02:00
2016-11-15 15:18:08 +01:00
return new UserContext ( userInfo , headline , company , summary , contextName , true , isInfrastructureScope ( ) ) ;
2013-05-08 18:16:43 +02:00
}
else {
2016-11-15 15:18:08 +01:00
logger . info ( " Returning test USER " ) ;
2013-05-08 18:16:43 +02:00
HashMap < String , String > fakeVreNames = new HashMap < String , String > ( ) ;
fakeVreNames . put ( " /gcube/devsec/devVRE " , " devVRE " ) ;
//fakeVreNames.put("/gcube/devNext/NexNext","NexNext");
2016-11-15 15:18:08 +01:00
UserInfo user = new UserInfo ( username , fullName , thumbnailURL , email , " fakeAccountUrl " , true , false , fakeVreNames ) ;
2014-07-23 16:39:04 +02:00
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. "
2016-11-15 15:18:08 +01:00
+ " " , contextName , true , isInfrastructureScope ( ) ) ;
2013-05-08 18:16:43 +02:00
}
2013-05-08 17:17:55 +02:00
2013-05-08 18:16:43 +02:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2013-05-09 18:00:47 +02:00
return new UserContext ( ) ;
}
2016-03-29 11:23:59 +02:00
private String transformSummary ( String professionalBackground ) {
String toReturn = escapeHtml ( professionalBackground ) ;
2014-07-23 16:39:04 +02:00
// replace all the line breaks by <br/>
toReturn = toReturn . replaceAll ( " ( \ r \ n| \ n) " , " <br/> " ) ;
// then replace all the double spaces by the html version
toReturn = toReturn . replaceAll ( " \\ s \\ s " , " " ) ;
return toReturn ;
2013-05-08 18:16:43 +02:00
}
2013-07-17 18:32:55 +02:00
@Override
public Boolean saveHeadline ( String newHeadline ) {
try {
2016-06-28 17:36:13 +02:00
UserManager um = new LiferayUserManager ( ) ;
2016-11-15 15:18:08 +01:00
return um . updateJobTitle ( um . getUserId ( getCurrentUser ( this . getThreadLocalRequest ( ) ) . getUsername ( ) ) , newHeadline ) ;
2013-07-17 18:32:55 +02:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
return false ;
}
}
@Override
public Boolean saveIsti ( String institution ) {
2016-06-28 17:36:13 +02:00
//try save the location/industry
2013-07-17 18:32:55 +02:00
try {
2016-06-28 17:36:13 +02:00
UserManager um = new LiferayUserManager ( ) ;
2016-11-15 15:18:08 +01:00
um . saveCustomAttr ( um . getUserId ( getCurrentUser ( this . getThreadLocalRequest ( ) ) . getUsername ( ) ) , CustomAttributeKeys . USER_LOCATION_INDUSTRY . getKeyName ( ) , institution ) ;
2016-06-28 17:36:13 +02:00
return true ;
2013-07-17 18:32:55 +02:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
return false ;
}
}
/ * *
2014-09-04 18:08:31 +02:00
* This method fetches the user profile by authenticating with LinkedIn through OAuth2 protocol
* once authenitcated it call another methos to get the profile and parse it .
* @return true if everything goes ok , false otherwise .
2013-07-17 18:32:55 +02:00
* /
2014-09-02 19:08:38 +02:00
@Override
2014-09-23 17:56:40 +02:00
public String fetchUserProfile ( String authCode , String redirectURI ) {
2014-09-02 19:08:38 +02:00
try {
2014-09-23 17:56:40 +02:00
HashMap < String , String > infoMap = getLinkedInUASInfo ( ) ;
2014-09-02 19:08:38 +02:00
HttpClient httpClient = HttpClients . createDefault ( ) ;
2014-09-04 18:08:31 +02:00
HttpPost httpPost = new HttpPost ( infoMap . get ( LINKEDIN_HOST_SERVICE_NAME ) ) ;
2014-09-02 19:08:38 +02:00
// Request parameters and other properties.
ArrayList < NameValuePair > params = new ArrayList < NameValuePair > ( 5 ) ;
params . add ( new BasicNameValuePair ( " grant_type " , " authorization_code " ) ) ;
params . add ( new BasicNameValuePair ( " code " , authCode ) ) ;
params . add ( new BasicNameValuePair ( " redirect_uri " , redirectURI ) ) ;
2014-09-04 18:08:31 +02:00
params . add ( new BasicNameValuePair ( " client_id " , infoMap . get ( LINKEDIN_CLIEND_ID_PROPNAME ) ) ) ;
params . add ( new BasicNameValuePair ( " client_secret " , infoMap . get ( LINKEDIN_CLIEND_SECRET_PROPNAME ) ) ) ;
2014-09-02 19:08:38 +02:00
httpPost . setEntity ( new UrlEncodedFormEntity ( params , " UTF-8 " ) ) ;
//Execute and get the response.
HttpResponse httpResponse = httpClient . execute ( httpPost ) ;
HttpEntity entity = httpResponse . getEntity ( ) ;
if ( entity ! = null ) {
InputStream myInputStream = entity . getContent ( ) ;
try {
String jsonText = IOUtils . toString ( myInputStream , " UTF-8 " ) ;
2016-11-15 15:18:08 +01:00
logger . debug ( " LinkedIn response: " + jsonText ) ;
2014-09-02 19:08:38 +02:00
if ( jsonText = = null )
2014-09-23 17:56:40 +02:00
return null ;
2014-09-02 19:08:38 +02:00
JSONParser parser = new JSONParser ( ) ;
@SuppressWarnings ( " rawtypes " )
ContainerFactory containerFactory = new ContainerFactory ( ) {
public List creatArrayContainer ( ) {
return new LinkedList ( ) ;
}
public Map createObjectContainer ( ) {
return new LinkedHashMap ( ) ;
}
} ;
@SuppressWarnings ( " unchecked " )
Map < String , String > json = ( Map < String , String > ) parser . parse ( jsonText , containerFactory ) ;
if ( json . get ( " error " ) ! = null )
2014-09-23 17:56:40 +02:00
return null ;
2014-09-02 19:08:38 +02:00
String token = json . get ( " access_token " ) ;
if ( token = = null )
2014-09-23 17:56:40 +02:00
return null ;
2014-09-04 18:08:31 +02:00
//here we got authorized by the user
2014-09-02 19:08:38 +02:00
return parseProfile ( httpClient , token ) ;
}
finally {
myInputStream . close ( ) ;
}
2014-09-23 17:56:40 +02:00
} else
return null ;
2014-09-02 19:08:38 +02:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2014-09-23 17:56:40 +02:00
return null ;
2014-09-02 19:08:38 +02:00
}
}
2014-09-04 18:08:31 +02:00
/ * *
* Ask the basic profile to LinkedIn API for the authenticated user , parse rge profile and write info into local DB .
* @param httpClient
* @param token
2014-09-23 17:56:40 +02:00
* @return the LinkedIn public URL of the user , or null in case of errors
2014-09-04 18:08:31 +02:00
* /
2014-09-23 17:56:40 +02:00
private String parseProfile ( HttpClient httpClient , String token ) {
2014-09-04 18:08:31 +02:00
HttpGet request = new HttpGet ( LINKEDIN_API_REQUEST ) ;
// add request header as in the documentation, @see https://developer.linkedin.com/documents/authentication
2014-09-02 19:08:38 +02:00
request . addHeader ( " Authorization " , " Bearer " + token ) ;
try {
2016-11-15 15:18:08 +01:00
logger . debug ( " Asking LinkedIn profile via http GET for " + getCurrentUser ( this . getThreadLocalRequest ( ) ) . getUsername ( ) ) ;
2014-09-02 19:08:38 +02:00
HttpResponse httpResponse = httpClient . execute ( request ) ;
HttpEntity entity = httpResponse . getEntity ( ) ;
if ( entity ! = null ) {
InputStream myInputStream = entity . getContent ( ) ;
try {
String xmlResponse = IOUtils . toString ( myInputStream , " UTF-8 " ) ;
2014-09-09 18:47:39 +02:00
//xmlResponse = testParsing();
2016-11-15 15:18:08 +01:00
logger . debug ( " LinkedIn xmlResponse: " + xmlResponse ) ;
2014-09-03 18:34:35 +02:00
2014-09-09 18:47:39 +02:00
2014-09-03 18:34:35 +02:00
DocumentBuilderFactory dbFactory = DocumentBuilderFactory . newInstance ( ) ;
DocumentBuilder dBuilder = dbFactory . newDocumentBuilder ( ) ;
Document doc = dBuilder . parse ( IOUtils . toInputStream ( xmlResponse ) ) ;
2016-11-15 15:18:08 +01:00
logger . debug ( " Parsing LinkedIn profile xmlResponse for " + getCurrentUser ( this . getThreadLocalRequest ( ) ) . getUsername ( ) ) ;
2014-09-03 18:34:35 +02:00
String headline = " " ;
String summary = " " ;
String location = " " ;
String industry = " " ;
2014-09-04 18:08:31 +02:00
List < String > currValue = null ;
XPathHelper helper = new XPathHelper ( doc . getDocumentElement ( ) ) ;
currValue = helper . evaluate ( " /person/headline/text() " ) ;
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
headline = currValue . get ( 0 ) ;
}
currValue = helper . evaluate ( " /person/summary/text() " ) ;
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
summary = currValue . get ( 0 ) ;
}
currValue = helper . evaluate ( " /person/location/name/text() " ) ;
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
location = currValue . get ( 0 ) ;
}
currValue = helper . evaluate ( " /person/industry/text() " ) ;
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
industry = currValue . get ( 0 ) ;
}
//positions
String positions = " " ;
currValue = helper . evaluate ( " /person/positions/position " ) ;
int positionsNo = currValue . size ( ) ;
2016-11-15 15:18:08 +01:00
logger . debug ( " Number of positions: " + positions ) ;
2014-09-04 18:08:31 +02:00
if ( positionsNo > 0 ) {
positions = ( positionsNo > 1 ) ? " \ n \ nCurrent Positions: " : " \ n \ nCurrent Position: " ;
2014-09-09 18:47:39 +02:00
for ( int i = 0 ; i < positionsNo ; i + + ) {
List < String > positionTitle = null ;
List < String > companyName = null ;
List < String > companType = null ;
List < String > companSize = null ;
List < String > companyIndustry = null ;
List < String > positionSummary = null ;
2014-09-23 17:56:40 +02:00
2014-09-09 18:47:39 +02:00
positionTitle = helper . evaluate ( " /person/positions/position[ " + ( i + 1 ) + " ]/title/text() " ) ;
if ( positionTitle ! = null & & positionTitle . size ( ) > 0 ) {
positions + = " \ n \ n " + positionTitle . get ( 0 ) ;
2014-09-04 18:08:31 +02:00
}
2014-09-09 18:47:39 +02:00
companyName = helper . evaluate ( " /person/positions/position[ " + ( i + 1 ) + " ]/company/name/text() " ) ;
if ( companyName ! = null & & companyName . size ( ) > 0 ) {
positions + = " \ n " + companyName . get ( 0 ) ;
2014-09-04 18:08:31 +02:00
}
2014-09-09 18:47:39 +02:00
companType = helper . evaluate ( " /person/positions/position[ " + ( i + 1 ) + " ]/company/type/text() " ) ;
if ( companType ! = null & & companType . size ( ) > 0 ) {
positions + = " \ n " + companType . get ( 0 ) + " ; " ;
2014-09-04 18:08:31 +02:00
}
2014-09-09 18:47:39 +02:00
companSize = helper . evaluate ( " /person/positions/position[ " + ( i + 1 ) + " ]/company/size/text() " ) ;
if ( companSize ! = null & & companSize . size ( ) > 0 ) {
positions + = " " + companSize . get ( 0 ) + " ; " ;
2014-09-04 18:08:31 +02:00
}
2014-09-09 18:47:39 +02:00
companyIndustry = helper . evaluate ( " /person/positions/position[ " + ( i + 1 ) + " ]/company/industry/text() " ) ;
if ( companyIndustry ! = null & & companyIndustry . size ( ) > 0 ) {
positions + = " \ n " + companyIndustry . get ( 0 ) ;
2014-09-04 18:08:31 +02:00
}
2014-09-09 18:47:39 +02:00
positionSummary = helper . evaluate ( " /person/positions/position[ " + ( i + 1 ) + " ]/summary/text() " ) ;
if ( positionSummary ! = null & & positionSummary . size ( ) > 0 ) {
positions + = " \ n \ n " + positionSummary . get ( 0 ) ;
2014-09-04 18:08:31 +02:00
}
2014-09-23 17:56:40 +02:00
2014-09-04 18:08:31 +02:00
}
}
2014-09-09 18:47:39 +02:00
String pictureURL = " " ;
currValue = helper . evaluate ( " /person/picture-urls/picture-url/text() " ) ;
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
pictureURL = currValue . get ( 0 ) ;
}
2014-09-23 17:56:40 +02:00
String publicProfileURL = " " ;
currValue = helper . evaluate ( " /person/public-profile-url/text() " ) ;
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
publicProfileURL = currValue . get ( 0 ) ;
}
2014-09-04 18:08:31 +02:00
//add the positions to the summary
2014-09-09 18:47:39 +02:00
summary + = positions ;
2014-09-04 18:08:31 +02:00
2014-09-03 18:34:35 +02:00
if ( isWithinPortal ( ) ) {
2016-11-15 15:18:08 +01:00
logger . debug ( " LinkedIn Profile gotten correctly for " + getCurrentUser ( this . getThreadLocalRequest ( ) ) . getUsername ( ) + " attempting to write into DB ... " ) ;
2014-09-03 18:34:35 +02:00
com . liferay . portal . model . User user ;
2016-11-15 15:18:08 +01:00
user = UserLocalServiceUtil . getUserByScreenName ( SiteManagerUtil . getCompany ( ) . getCompanyId ( ) , getCurrentUser ( this . getThreadLocalRequest ( ) ) . getUsername ( ) ) ;
2014-09-03 18:34:35 +02:00
//headline
2015-01-07 12:17:57 +01:00
if ( headline . compareTo ( " " ) ! = 0 ) {
String checkedHeadline = headline ;
if ( headline . length ( ) > = 75 )
checkedHeadline = headline . substring ( 0 , 70 ) + " ... " ;
user . setJobTitle ( escapeHtml ( checkedHeadline ) ) ;
}
2014-09-03 18:34:35 +02:00
//location and industry
2016-06-28 17:36:13 +02:00
String locationOrIndustry = " " ;
if ( location . compareTo ( " " ) ! = 0 & & industry . compareTo ( " " ) ! = 0 ) {
locationOrIndustry = escapeHtml ( location + " | " + industry ) ;
}
2014-09-03 18:34:35 +02:00
else if ( location . compareTo ( " " ) ! = 0 | | industry . compareTo ( " " ) ! = 0 )
2016-06-28 17:36:13 +02:00
locationOrIndustry = escapeHtml ( location + industry ) ;
2014-09-03 18:34:35 +02:00
//summary
if ( summary . compareTo ( " " ) ! = 0 )
user . setComments ( escapeHtml ( summary ) ) ;
2014-09-23 17:56:40 +02:00
//public profile URL
if ( publicProfileURL . compareTo ( " " ) ! = 0 ) {
Contact contact = user . getContact ( ) ;
contact . setMySpaceSn ( publicProfileURL ) ;
ContactLocalServiceUtil . updateContact ( contact ) ;
}
2014-09-09 18:47:39 +02:00
boolean toReturn = ( UserLocalServiceUtil . updateUser ( user ) ! = null ) ;
//set the picture
if ( pictureURL . compareTo ( " " ) ! = 0 & & pictureURL . startsWith ( " http " ) ) {
byte [ ] pictureData = getUserPictureFromURL ( httpClient , pictureURL ) ;
if ( pictureData ! = null ) {
2016-11-15 15:18:08 +01:00
logger . debug ( " Updating Image Profile with this one: " + pictureURL ) ;
2014-09-09 18:47:39 +02:00
UserLocalServiceUtil . updatePortrait ( user . getUserId ( ) , pictureData ) ;
}
}
2016-07-19 15:09:13 +02:00
2016-06-28 17:36:13 +02:00
//update the location/industry
2016-07-19 15:09:13 +02:00
if ( locationOrIndustry ! = null )
new LiferayUserManager ( ) . saveCustomAttr ( user . getUserId ( ) , CustomAttributeKeys . USER_LOCATION_INDUSTRY . getKeyName ( ) , locationOrIndustry ) ;
2014-09-23 17:56:40 +02:00
if ( toReturn )
return publicProfileURL ;
else return null ;
2014-09-03 18:34:35 +02:00
} else {
2016-11-15 15:18:08 +01:00
logger . warn ( " Development Mode ON, not attempting to write into DB " ) ;
2014-09-23 17:56:40 +02:00
return " fakePublicURL " ;
2014-09-03 18:34:35 +02:00
}
2014-09-02 19:08:38 +02:00
}
finally {
myInputStream . close ( ) ;
}
}
2014-09-23 17:56:40 +02:00
else
return null ;
2014-09-02 19:08:38 +02:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2014-09-23 17:56:40 +02:00
return null ;
2014-09-02 19:08:38 +02:00
}
2016-05-05 18:19:00 +02:00
2014-09-23 17:56:40 +02:00
}
2014-09-09 18:47:39 +02:00
/ * *
*
* @param httpClient
* @param pictureURL
* @return a byte array for the picture
* /
private byte [ ] getUserPictureFromURL ( HttpClient httpClient , String pictureURL ) {
HttpGet request = new HttpGet ( pictureURL ) ;
try {
return IOUtils . toByteArray ( httpClient . execute ( request ) . getEntity ( ) . getContent ( ) ) ;
} catch ( Exception e ) {
2016-11-15 15:18:08 +01:00
logger . error ( " Could not get bytes from picture URL " + e . getMessage ( ) ) ;
2014-09-09 18:47:39 +02:00
e . printStackTrace ( ) ;
return null ;
}
}
2013-07-17 18:32:55 +02:00
2014-09-23 17:56:40 +02:00
2014-09-04 18:08:31 +02:00
/ * *
2014-09-09 18:47:39 +02:00
* this method return the URL and the keys to access the LinkedIn User AuthN Service ( UAS )
2014-09-04 18:08:31 +02:00
* @return an hashmap containing the 3 info needed
* /
private HashMap < String , String > getLinkedInUASInfo ( ) {
2016-11-15 15:18:08 +01:00
String scope = getCurrentContext ( this . getThreadLocalRequest ( ) ) ;
logger . info ( " Looking for a LinkedIn UAS in " + scope ) ;
2014-09-04 18:08:31 +02:00
String previousScope = ScopeProvider . instance . get ( ) ;
ScopeProvider . instance . set ( scope ) ;
SimpleQuery query = queryFor ( ServiceEndpoint . class ) ;
query . addCondition ( " $resource/Profile/Category/string() eq 'Service' " ) ;
query . addCondition ( " $resource/Profile/Name/string() eq 'LinkedIn-user-authorization' " ) ;
DiscoveryClient < ServiceEndpoint > client = clientFor ( ServiceEndpoint . class ) ;
List < ServiceEndpoint > list = client . submit ( query ) ;
ScopeProvider . instance . set ( previousScope ) ;
if ( list . size ( ) > 1 ) {
2016-11-15 15:18:08 +01:00
logger . warn ( " Multiple LinkedIn-user-authorization Service Endpoints available in the scope, should be only one. " ) ;
2014-09-04 18:08:31 +02:00
return null ;
}
else if ( list . size ( ) = = 1 ) {
ServiceEndpoint se = list . get ( 0 ) ;
AccessPoint ap = se . profile ( ) . accessPoints ( ) . iterator ( ) . next ( ) ;
String authServiceURL = ap . address ( ) ;
String clientId = " " ;
String clientSecret = " " ;
try {
for ( Property property : ap . properties ( ) ) {
if ( property . name ( ) . compareTo ( LINKEDIN_CLIEND_ID_PROPNAME ) = = 0 )
clientId = StringEncrypter . getEncrypter ( ) . decrypt ( property . value ( ) ) ;
if ( property . name ( ) . compareTo ( LINKEDIN_CLIEND_SECRET_PROPNAME ) = = 0 )
clientSecret = StringEncrypter . getEncrypter ( ) . decrypt ( property . value ( ) ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2014-09-23 17:56:40 +02:00
2014-09-04 18:08:31 +02:00
HashMap < String , String > map = new HashMap < String , String > ( ) ;
map . put ( LINKEDIN_HOST_SERVICE_NAME , authServiceURL ) ;
map . put ( LINKEDIN_CLIEND_ID_PROPNAME , clientId ) ;
map . put ( LINKEDIN_CLIEND_SECRET_PROPNAME , clientSecret ) ;
return map ;
}
else return null ;
}
2014-09-09 14:48:47 +02:00
/ * *
* Indicates whether the scope is the whole infrastructure .
* @return < code > true < / code > if it is , < code > false < / code > otherwise .
* /
private boolean isInfrastructureScope ( ) {
2016-11-15 15:18:08 +01:00
ScopeBean scope = new ScopeBean ( getCurrentContext ( this . getThreadLocalRequest ( ) ) ) ;
2014-09-09 14:48:47 +02:00
return scope . is ( Type . INFRASTRUCTURE ) ;
}
2014-09-04 18:08:31 +02:00
/ * *
* Escape an html string . Escaping data received from the client helps to
* prevent cross - site script vulnerabilities .
*
* @param html the html string to escape
* @return the escaped string
* /
private String escapeHtml ( String html ) {
if ( html = = null ) {
return null ;
}
return html . replaceAll ( " & " , " & " ) . replaceAll ( " < " , " < " )
. replaceAll ( " > " , " > " ) ;
}
2013-05-08 17:17:55 +02:00
}