diff --git a/pom.xml b/pom.xml index 4185805..4cb3752 100644 --- a/pom.xml +++ b/pom.xml @@ -131,6 +131,11 @@ jtidy r938 + + org.jsoup + jsoup + 1.6.1 + net.eliasbalasis tibcopagebus4gwt diff --git a/src/main/java/org/gcube/portlets/user/shareupdates/client/form/ShareUpdateForm.java b/src/main/java/org/gcube/portlets/user/shareupdates/client/form/ShareUpdateForm.java index 69439b1..b19bd9b 100644 --- a/src/main/java/org/gcube/portlets/user/shareupdates/client/form/ShareUpdateForm.java +++ b/src/main/java/org/gcube/portlets/user/shareupdates/client/form/ShareUpdateForm.java @@ -24,6 +24,7 @@ import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.Composite; +import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HTMLPanel; import com.google.gwt.user.client.ui.Image; import com.google.gwt.user.client.ui.ListBox; @@ -128,36 +129,37 @@ public class ShareUpdateForm extends Composite { } @UiHandler("submitButton") - void onClick(ClickEvent e) { - Window.alert("Please wait, Implementation to be completed"); -// shareupdateService.getUserSettings(new AsyncCallback() { -// public void onFailure(Throwable caught) { -// Window.alert("Ops! we encountered some problems delivering your message, server is not responding, please try again in a short while."); -// } -// public void onSuccess(UserSettings result) { -// if (result.getUserInfo().getUsername().equals("test.user")) { -// Window.alert("Your session has expired, please log out and login again"); -// return; -// } -// myUserInfo = result.getUserInfo(); -// String toShare = shareTextArea.getText().trim(); -// if (toShare.equals(SHARE_UPDATE_TEXT) || toShare.equals(ERROR_UPDATE_TEXT) || toShare.equals("")) { -// shareTextArea.addStyleName("error"); -// shareTextArea.setText(ERROR_UPDATE_TEXT); -// return; -// } -// //then you can post -// postTweet(toShare); -// } -// }); + void onClick(ClickEvent e) { + shareupdateService.getUserSettings(new AsyncCallback() { + public void onFailure(Throwable caught) { + Window.alert("Ops! we encountered some problems delivering your message, server is not responding, please try again in a short while."); + } + public void onSuccess(UserSettings result) { + if (result.getUserInfo().getUsername().equals("test.user")) { + Window.alert("Your session has expired, please log out and login again"); + return; + } + myUserInfo = result.getUserInfo(); + String toShare = shareTextArea.getText().trim(); + if (toShare.equals(SHARE_UPDATE_TEXT) || toShare.equals(ERROR_UPDATE_TEXT) || toShare.equals("")) { + shareTextArea.addStyleName("error"); + shareTextArea.setText(ERROR_UPDATE_TEXT); + return; + } + //then you can post, but pass html + String toPost = shareTextArea.getHTML().trim(); + postTweet(toPost); + } + }); } /** * * @param textToPost */ private void postTweet(String textToPost) { - String toShare = escapeHtml(textToPost); - if (! checkTextLength(toShare)) { + //String toShare = escapeHtml(textToPost); + String toShare = textToPost; + if (! checkTextLength(new HTML(toShare).getText())) { //need to convert it to text Window.alert("We found a single word containing more than 50 chars and it's not a link, is it meaningful?"); return; } diff --git a/src/main/java/org/gcube/portlets/user/shareupdates/client/form/SmartTextArea.java b/src/main/java/org/gcube/portlets/user/shareupdates/client/form/SmartTextArea.java index 6141fdb..28da4c2 100644 --- a/src/main/java/org/gcube/portlets/user/shareupdates/client/form/SmartTextArea.java +++ b/src/main/java/org/gcube/portlets/user/shareupdates/client/form/SmartTextArea.java @@ -22,6 +22,7 @@ import com.google.gwt.event.shared.HandlerManager; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Timer; +import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.HTML; @@ -82,7 +83,7 @@ public class SmartTextArea extends ContentEditDiv { } }); } - + /** * paste and other events overridden */ @@ -95,7 +96,7 @@ public class SmartTextArea extends ContentEditDiv { Timer t = new Timer() { @Override public void run() { - String toCheck = extractLink(getText()); + String toCheck = extractLink(getHTML()); if (toCheck != null) { GWT.log("toCheck1:" + toCheck); ShareUpdateForm.get().checkLink(new HTML(toCheck).getText()); @@ -134,12 +135,29 @@ public class SmartTextArea extends ContentEditDiv { * @return null if there's no link present */ private String extractLink(String textToCheck) { + GWT.log("textToCheck:" + textToCheck); String [] parts = textToCheck.split("\\s"); // Attempt to convert each item into an URL. for( String item : parts ) { if (item.startsWith("http")) { return item; } + if (item.startsWith("href") || item.startsWith("HREF")) { //this is for (damned) safari because sometimes it paste " + postText + ""; + + HtmlCleaner cleaner = new HtmlCleaner(); + // parse the string HTML + TagNode pageData = cleaner.clean(toParse); + TagNode[] inputElements = pageData.getElementsByName("input", true); + if (inputElements != null) { + for (int i = 0; i < inputElements.length; i++) { + System.out.println("Found input " + inputElements[i].getAttributes().get("value")); + toReturn.add(inputElements[i].getAttributes().get("value")); + } + } else { + _log.trace("No person tags in this post"); + } + return toReturn; + } + + private static String html2text(String html) { + return Jsoup.parse(html).text(); + } + + + /** + * this method is used when posting a feed + * + * It converts the tagged people etc etc + * + * @return a String ready to be posted + */ + private String transformPost(String postText) { + ArrayList taggedPeople = extractPeopleTags(postText); + if (taggedPeople == null || taggedPeople.size() == 0) { //there are no tagged people, remove html and go + String escapedFeedText = escapeHtml(postText); //here escape html to avoid xss attacks + String html = "" + escapedFeedText + ""; + return html2text(html); + } else { + // this is needed to reconstruct the place of people tags, selfexplaining i think + int i = 0; + while (postText.contains("" + postText + ""; + String postWithPlaceHolders = html2text(html); + _log.trace("before cure: " + postWithPlaceHolders); + /* + * at this point you have the html removed and somthing like "text text text _usr_place_holder_[0] text text text _usr_place_holder_[1]" + * you need to replace _usr_place_holder_[i](s) with the people with same index in taggedPeople ArrayList + */ + String escapedFeedText = escapeHtml(postWithPlaceHolders); //here escape html to avoid xss attacks + ArrayList usernames = getSelectedUserIds(taggedPeople); + i = 0; + for (String tagged : taggedPeople) { + String username = (i < usernames.size()) ? usernames.get(i) : ""; + String taggedHTML = ""+tagged+" "; + escapedFeedText = escapedFeedText.replace("_usr_place_holder_["+i+"]", taggedHTML); + i++; + } + _log.trace("After cure: " + escapedFeedText); + return escapedFeedText; + } + } + /** * */ - public ClientFeed share(String feedText, FeedType feedType, PrivacyLevel pLevel, String vreId, String linkTitle, - String linkDesc, String url, String urlThumbnail, String host) { - - String escapedFeedText = escapeHtml(feedText); + public ClientFeed share(String postText, FeedType feedType, PrivacyLevel pLevel, String vreId, String linkTitle, String linkDesc, String url, String urlThumbnail, String host) { + + String escapedFeedText = transformPost(postText); String username = getASLSession().getUsername(); String email = username+"@isti.cnr.it"; String fullName = username+" FULL"; @@ -179,6 +255,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar replaceAmpersand(toShare.getDescription()), fullName, email, thumbnailURL, toShare.getLinkTitle(), toShare.getLinkDescription(), toShare.getUriThumbnail(), toShare.getLinkHost()); return cf; + } private UserSettings getUserSettingsFromSession() { @@ -256,7 +333,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar HashMap fakeVreNames = new HashMap(); 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); return new UserSettings(user, 0, session.getScopeName(), isInfrastructureScope()); } @@ -283,7 +360,7 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar toReturn.put(vre.getGroupId(), vre.getGroupName()); } } - + return toReturn; } /** @@ -597,6 +674,27 @@ public class ShareUpdateServiceImpl extends RemoteServiceServlet implements Shar return getASLSession().getScope().isInfrastructure(); } + /** + * + * @return the screennames of the addressee (user logins e.g. pino.pini) + */ + public ArrayList getSelectedUserIds(ArrayList fullNames) { + if (fullNames == null) + return new ArrayList(); + else { + ArrayList allUsers = getPortalUsers(); + ArrayList toReturn = new ArrayList(); + for (String fullName : fullNames) + for (PickingUser puser : allUsers) { + if (puser.getFullName().compareTo(fullName) == 0) { + toReturn.add(puser.getUsername()); + break; + } + } + return toReturn; + } + } + @Override public ArrayList getPortalUsers() { ArrayList portalUsers = new ArrayList();