fixed parsing of profile

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/social-profile@99642 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2014-09-09 16:47:39 +00:00
parent 7d97a0c112
commit 25f5325d0c
3 changed files with 105 additions and 30 deletions

View File

@ -4,6 +4,12 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/target/generated-sources/gwt"/>
<dependent-module archiveName="wsmail-widget-1.7.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/wsmail-widget/wsmail-widget">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="session-checker-0.2.5-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/session-checker/session-checker">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="java-output-path" value="/${module}/target/www/WEB-INF/classes"/>
<property name="context-root" value="social-profile"/>
</wb-module>

View File

@ -3,6 +3,7 @@ package org.gcube.portlets.user.socialprofile.server;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
@ -18,6 +19,7 @@ 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.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
@ -65,7 +67,7 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
private static final String LINKEDIN_CLIEND_ID_PROPNAME = "client_id";
private static final String LINKEDIN_CLIEND_SECRET_PROPNAME = "client_secret";
private static final String LINKEDIN_API_REQUEST = "https://api.linkedin.com/v1/people/~:(id,headline,summary,location:(name),industry,positions)";
private static final String LINKEDIN_API_REQUEST = "https://api.linkedin.com/v1/people/~:(id,headline,summary,location:(name),industry,positions,picture-urls::(original))";
/**
* the current ASLSession
@ -77,10 +79,11 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
if (user == null) {
_log.warn("USER IS NULL setting test.user and Running OUTSIDE PORTAL");
user = getDevelopmentUser();
SessionManager.getInstance().getASLSession(sessionID, user).setScope("/gcube/devsec");
SessionManager.getInstance().getASLSession(sessionID, user).setScope("/gcube");
}
return SessionManager.getInstance().getASLSession(sessionID, user);
}
/**
* when packaging test will fail if the user is not set to test.user
* @return .
@ -125,8 +128,8 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
String thumbnailURL = "images/Avatar_default.png";
if (isWithinPortal()) {
try {
com.liferay.portal.model.UserModel user = UserLocalServiceUtil.getUserByScreenName(OrganizationsUtil.getCompany().getCompanyId(), username);
thumbnailURL = "/image/user_male_portrait?img_id="+user.getPortraitId();
com.liferay.portal.model.User user = UserLocalServiceUtil.getUserByScreenName(OrganizationsUtil.getCompany().getCompanyId(), username);
thumbnailURL = user.isMale() ? "/image/user_male_portrait?img_id="+user.getPortraitId() : "/image/user_female_portrait?img_id="+user.getPortraitId();
fullName = user.getFirstName() + " " + user.getLastName();
email = user.getEmailAddress();
HashMap<String, String> vreNames = new HashMap<String, String>();
@ -165,8 +168,8 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
if (isWithinPortal()) {
com.liferay.portal.model.UserModel user = UserLocalServiceUtil.getUserByScreenName(OrganizationsUtil.getCompany().getCompanyId(), username);
thumbnailURL = "/image/user_male_portrait?img_id="+user.getPortraitId();
com.liferay.portal.model.User user = UserLocalServiceUtil.getUserByScreenName(OrganizationsUtil.getCompany().getCompanyId(), username);
thumbnailURL = user.isMale() ? "/image/user_male_portrait?img_id="+user.getPortraitId() : "/image/user_female_portrait?img_id="+user.getPortraitId();
fullName = user.getFirstName() + " " + user.getLastName();
email = user.getEmailAddress();
ThemeDisplay themeDisplay = (ThemeDisplay) this.getThreadLocalRequest().getSession().getAttribute(WebKeys.THEME_DISPLAY);
@ -322,8 +325,10 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
InputStream myInputStream = entity.getContent();
try {
String xmlResponse = IOUtils.toString(myInputStream, "UTF-8");
//xmlResponse = testParsing();
_log.debug("LinkedIn xmlResponse: " + xmlResponse);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(IOUtils.toInputStream(xmlResponse));
@ -356,40 +361,55 @@ 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);
if (positionsNo > 0) {
positions = (positionsNo > 1) ? "\n\nCurrent Positions:" : "\n\nCurrent Position:";
for (int i = 0; i < positionsNo; i++) {
currValue = helper.evaluate("/person/positions/position/title/text()");
if (currValue != null && currValue.size() > 0) {
positions += "\n\n"+currValue.get(i);
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;
positionTitle = helper.evaluate("/person/positions/position["+(i+1)+"]/title/text()");
if (positionTitle != null && positionTitle.size() > 0) {
positions += "\n\n"+positionTitle.get(0);
}
currValue = helper.evaluate("/person/positions/position/company/name/text()");
if (currValue != null && currValue.size() > 0) {
positions += "\n"+currValue.get(i);
companyName = helper.evaluate("/person/positions/position["+(i+1)+"]/company/name/text()");
if (companyName != null && companyName.size() > 0) {
positions += "\n"+companyName.get(0);
}
currValue = helper.evaluate("/person/positions/position/company/type/text()");
if (currValue != null && currValue.size() > 0) {
positions += "\n" + currValue.get(i)+"; ";
companType = helper.evaluate("/person/positions/position["+(i+1)+"]/company/type/text()");
if (companType != null && companType.size() > 0) {
positions += "\n" + companType.get(0)+";";
}
currValue = helper.evaluate("/person/positions/position/company/size/text()");
if (currValue != null && currValue.size() > 0) {
positions += currValue.get(i)+"; ";
companSize = helper.evaluate("/person/positions/position["+(i+1)+"]/company/size/text()");
if (companSize != null && companSize.size() > 0) {
positions += " " + companSize.get(0)+";";
}
currValue = helper.evaluate("/person/positions/position/company/industry/text()");
if (currValue != null && currValue.size() > 0) {
positions += currValue.get(i) + " industry.";
companyIndustry = helper.evaluate("/person/positions/position["+(i+1)+"]/company/industry/text()");
if (companyIndustry != null && companyIndustry.size() > 0) {
positions += "\n" +companyIndustry.get(0);
}
currValue = helper.evaluate("/person/positions/position/summary/text()");
if (currValue != null && currValue.size() > 0) {
positions += "\n\n" + currValue.get(i);
positionSummary = helper.evaluate("/person/positions/position["+(i+1)+"]/summary/text()");
if (positionSummary != null && positionSummary.size() > 0 ) {
positions += "\n\n" + positionSummary.get(0);
}
}
}
String pictureURL = "";
currValue = helper.evaluate("/person/picture-urls/picture-url/text()");
if (currValue != null && currValue.size() > 0) {
pictureURL = currValue.get(0);
}
//add the positions to the summary
summary += positions;
_log.debug("LinkedIn Profile gotten correctly for " + getASLSession().getUsername() + " attempting to write into DB ...");
summary += positions;
if (isWithinPortal()) {
_log.debug("LinkedIn Profile gotten correctly for " + getASLSession().getUsername() + " attempting to write into DB ...");
com.liferay.portal.model.User user;
user = UserLocalServiceUtil.getUserByScreenName(OrganizationsUtil.getCompany().getCompanyId(), getASLSession().getUsername());
//headline
@ -403,8 +423,17 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
//summary
if (summary.compareTo("") != 0)
user.setComments(escapeHtml(summary));
return (UserLocalServiceUtil.updateUser(user) != null);
boolean toReturn = (UserLocalServiceUtil.updateUser(user) != null);
//set the picture
if (pictureURL.compareTo("") != 0 && pictureURL.startsWith("http")) {
byte[] pictureData = getUserPictureFromURL(httpClient, pictureURL);
if (pictureData != null) {
_log.debug("Updating Image Profile with this one: " + pictureURL);
UserLocalServiceUtil.updatePortrait(user.getUserId(), pictureData);
}
}
return toReturn;
} else {
_log.warn("Development Mode ON, not attempting to write into DB");
}
@ -419,10 +448,28 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
return false;
}
}
/**
*
* @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) {
_log.error("Could not get bytes from picture URL " + e.getMessage());
e.printStackTrace();
return null;
}
}
/**
* this method return the URL and the keys to access the LinkedIn User AuthN Service
* this method return the URL and the keys to access the LinkedIn User AuthN Service (UAS)
* @return an hashmap containing the 3 info needed
*/
private HashMap<String, String> getLinkedInUASInfo(){
@ -489,4 +536,25 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer
return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
.replaceAll(">", "&gt;");
}
//test metho
private String testParsing() {
return
"<person><id>KbR7LHuOer</id><headline>Technical Director at D4Science Hybrid Data Infrastructure</headline><summary>Senior Researcher at the Networked Multimedia Information Systems Laboratory of the &quot;Istituto di Scienza e Tecnologie della Informazione A. Faedo&quot; (ISTI) of the Italian National Research Council (CNR). I received my M.Sc. in Information Systems Technologies from the Department of Computer Science of the University of Pisa (1998), and the Ph.D. degree in Information Engineering from the Department of Information Engineering: Electronics, Information Theory, Telecommunications of the same university (2006). The aim of my research is the study and experimentation of models, methodologies and techniques for the design and development of distributed virtual research environments (VREs) which require the handling of heterogeneous computational and storage resources, provided by Grid and Cloud based e-Infrastructures, for the management of heterogenous data sources. I have a strong background on distributed architectures. I participated to the design of the most relevant distributed systems and e-Infrastructure enabling middleware developed by ISTI - CNR.I am currently the Technical Director of the Data e-Infrastructure Initiative for Fisheries Management and Conservation of Marine Living Resources (iMarine). I am also serving EUBrazilOpenBio initiative and ENVRI - Environmental Research Infrastructure - as consultant."
+"In the past, I have been involved in the Venus-C, GRDI2020, D4Science-II, D4Science, Diligent, DRIVER, DRIVER II, BELIEF, BELIEF II, Scholnet, Cyclades, and ARCA European projects.</summary>"
+"<location><name>Pisa Area, Italy</name></location><industry>Research</industry>"
+"<positions total=\"3\">"
+ "<position><id>421485155</id><title>Technical Director</title><summary>Summary D4Science.org .... the D4Science.org Agreement</summary>"
+"<start-date><year>2012</year><month>1</month></start-date><is-current>true</is-current><company><name>D4Science Hybrid Data Infrastructure</name><industry>Research</industry></company>"
+"</position>"
+ "<position><id>421485155</id><title>Senior Researcher</title><summary>Summary CNR The aim of my research is the study and experimentation of models</summary>"
+"<start-date><year>2013</year><month>4</month></start-date><is-current>true</is-current>"
+"<company><id>565371</id><name>ISTI-CNR</name><size>201-500 employees</size><type>Public Company</type><industry>Research</industry></company>"
+"</position>"
+ "<position><id>421485155</id><title>Technical Director</title><summary>Summary iMarine launches an initiative to establi</summary>"
+"<start-date><year>2014</year><month>7</month></start-date><is-current>true</is-current><company><name>D4Science Hybrid Data Infrastructure</name><industry>Research</industry></company>"
+"</position>"
+"</positions>"
+"<picture-urls total=\"1\"><picture-url key=\"original\">http://m.c.lnkd.licdn.com/mpr/mprx/0_mK4hvD9T0-r5MTo1elJa92JfJvOLMh61WlQSPuFDc3x6j3NgI8M2AQ6jLEE</picture-url></picture-urls>"
+"</person>";
}
}

View File

@ -16,3 +16,4 @@ log4j.logger.org.gcube.common.clients=ERROR
log4j.logger.org.gcube.common.homelibrary.jcr=ERROR
log4j.logger.org.gcube.application.framework.accesslogger=ERROR
log4j.logger.org.apache.pdfbox.util.PDFStreamEngine=ERROR
log4j.logger.org.apache.http=INFO