diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 940e556..b6f946d 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -4,6 +4,12 @@ + + uses + + + uses + diff --git a/src/main/java/org/gcube/portlets/user/socialprofile/server/SocialServiceImpl.java b/src/main/java/org/gcube/portlets/user/socialprofile/server/SocialServiceImpl.java index bf58e3a..f13ee6a 100644 --- a/src/main/java/org/gcube/portlets/user/socialprofile/server/SocialServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/socialprofile/server/SocialServiceImpl.java @@ -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 vreNames = new HashMap(); @@ -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 positionTitle = null; + List companyName = null; + List companType = null; + List companSize = null; + List companyIndustry = null; + List 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 getLinkedInUASInfo(){ @@ -489,4 +536,25 @@ public class SocialServiceImpl extends RemoteServiceServlet implements SocialSer return html.replaceAll("&", "&").replaceAll("<", "<") .replaceAll(">", ">"); } + //test metho + private String testParsing() { + return + "KbR7LHuOerTechnical Director at D4Science Hybrid Data InfrastructureSenior Researcher at the Networked Multimedia Information Systems Laboratory of the "Istituto di Scienza e Tecnologie della Informazione A. Faedo" (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." + +"Pisa Area, ItalyResearch" + +"" + + "421485155Technical DirectorSummary D4Science.org .... the D4Science.org Agreement" + +"20121trueD4Science Hybrid Data InfrastructureResearch" + +"" + + "421485155Senior ResearcherSummary CNR The aim of my research is the study and experimentation of models" + +"20134true" + +"565371ISTI-CNR201-500 employeesPublic CompanyResearch" + +"" + + "421485155Technical DirectorSummary iMarine launches an initiative to establi" + +"20147trueD4Science Hybrid Data InfrastructureResearch" + +"" + +"" + +"http://m.c.lnkd.licdn.com/mpr/mprx/0_mK4hvD9T0-r5MTo1elJa92JfJvOLMh61WlQSPuFDc3x6j3NgI8M2AQ6jLEE" + +""; + } } diff --git a/src/main/resources/clientlog4j.properties b/src/main/resources/clientlog4j.properties index fd367a5..834ba2c 100644 --- a/src/main/resources/clientlog4j.properties +++ b/src/main/resources/clientlog4j.properties @@ -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 \ No newline at end of file