Creato da pastuweb.com
Share My Page
My Social Accounts
Account FaceBook Account LinkedIn Account Twitter Account Google Plus Account Git Hub
MySocialButtons Portlet (on MarketPlace Liferay)

MySocialButtons View

 

This is the View JSP of my portlet: MySocialButtons. You can find and dowload this portlet from Liferay Market.

You can share the current page of your Liferay website and you can also add Your Social Account, like Facebook, Twitter, LikedIn, Google Plus and GitHub.

 

 

MySocialButtons Edit

This is the Edit JSP of MySocialButtons.

In this form you can edit the default Social Accounts.

 

 

 

 

 

 

 

 

The project:

MySocialButtons projectThe most important files are:

  • MySocialButtons.java
  • liferay-display.xml
  • liferay-plugin-package.properties
  • liferay-portlet.xml
  • portlet.xml
  • edit.jsp
  • view.jsp

Now i show you the source code of every file:

liferay-display.xml

<?xml version="1.0"?>

<!DOCTYPE display PUBLIC "-//Liferay//DTD Display 6.0.0//EN"

"http://www.liferay.com/dtd/liferay-display_6_0_0.dtd">

<display>

<category name="Pasturenzi Portlets">

<portlet id="MySocialButtons" />

</category>

</display>

 

liferay-plugin-package.properties

name=MySocialButtons

module-group-id=liferay

module-incremental-version=1

tags=

short-description=Liferay 6.1

change-log=

page-url=http://www.appuntivari.net/informatica/programmazione/liferay/mysocialbuttons-portlet-code

author=Pasturenzi Francesco

licenses=LGPL

liferay-versions=6.1.1+

liferay-portlet.xml

<?xml version="1.0"?>

<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.0.0//EN"

"http://www.liferay.com/dtd/liferay-portlet-app_6_0_0.dtd">

<liferay-portlet-app>

<portlet>

<portlet-name>MySocialButtons</portlet-name>

<icon>/icon.png</icon>

<instanceable>false</instanceable>

<header-portlet-css>/css/main.css</header-portlet-css>

<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>

<css-class-wrapper>MySocialButtons-portlet</css-class-wrapper>

</portlet>

<role-mapper>

<role-name>administrator</role-name>

<role-link>Administrator</role-link>

</role-mapper>

<role-mapper>

<role-name>guest</role-name>

<role-link>Guest</role-link>

</role-mapper>

<role-mapper>

<role-name>power-user</role-name>

<role-link>Power User</role-link>

</role-mapper>

<role-mapper>

<role-name>user</role-name>

<role-link>User</role-link>

</role-mapper>

 

</liferay-portlet-app>

portlet.xml

<?xml version="1.0"?>

 

<portlet-app

version="2.0"

xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation=

"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"

>

<portlet>

<portlet-name>MySocialButtons</portlet-name>

<display-name>MySocialButtons</display-name>

<portlet-class>com.appuntivarinet.MySocialButtons</portlet-class>

<init-param>

<name>view-jsp</name>

<value>/view.jsp</value>

</init-param>

<init-param>

<name>edit-jsp</name>

<value>/edit.jsp</value>

</init-param>

<expiration-cache>0</expiration-cache>

<supports>

<mime-type>text/html</mime-type>

<portlet-mode>view</portlet-mode>

<portlet-mode>edit</portlet-mode>

</supports>

<portlet-info>

<title>MySocialButtons</title>

<short-title>MySocialButtons</short-title>

<keywords>MySocialButtons</keywords>

</portlet-info>

<security-role-ref>

<role-name>administrator</role-name>

</security-role-ref>

<security-role-ref>

<role-name>guest</role-name>

</security-role-ref>

<security-role-ref>

<role-name>power-user</role-name>

</security-role-ref>

<security-role-ref>

<role-name>user</role-name>

</security-role-ref>

</portlet>

</portlet-app>

 

Now I'll show you the view.jsp (Portlet VIEW) and edit.jsp (Portlet Preferences):

view.jsp

<%

/**

 * Copyright (c) Pasturenzi Francesco

 * This is the VIEW of the Portlet.

 */

%>

 

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<jsp:useBean id="urlAccountFacebook" class="java.lang.String" scope="request" />

<jsp:useBean id="urlAccountLinkedIn" class="java.lang.String" scope="request" />

<jsp:useBean id="urlAccountGooglePlus" class="java.lang.String" scope="request" />

<jsp:useBean id="urlAccountTwitter" class="java.lang.String" scope="request" />

<jsp:useBean id="urlAccountGitHub" class="java.lang.String" scope="request" />
 

<jsp:useBean id="currentUrl" class="java.lang.String" scope="request" />

 

<portlet:defineObjects />

 

<div class="MySocialButtons-portlet-bordiRotondi"

style="position:relative;margin:auto;margin-top:5px;margin-bottom:5px;width:90%;">

 

<div style="position:absolute;bottom:3px;left:3px;">

<a href="http://www.pastuweb.com" target="_blank"><img

src="http://www.pastuweb.com/loghi_pw/icone/pastuweb.png"

width="30" alt="Creato da pastuweb.com" title="Creato da pastuweb.com" /></a>

</div>

 

<div style="text-align:center;">

<strong>Share My Page</strong>

</div>

<div style="text-align:center;margin:auto;">

<!-- Social Buttons -->

 

<a href="https://twitter.com/share" class="twitter-share-button"

data-lang="it" data-url="<%=currentUrl %>">Tweet</a>

<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?

'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;

js.src=p+'://platform.twitter.com/widgets.js';

fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>

 

<div class="g-plusone" data-size="medium" data-href="<%=currentUrl %>"></div>

 

<!-- Inserisci questo tag dopo l'ultimo tag di pulsante +1. -->

<script type="text/javascript">

  window.___gcfg = {lang: 'it'};

 

  (function() {

    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;

    po.src = 'https://apis.google.com/js/plusone.js';

    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);

  })();

</script>

 

<div id="fb-root"></div>

<script>(function(d, s, id) {

  var js, fjs = d.getElementsByTagName(s)[0];

  if (d.getElementById(id)) return;

  js = d.createElement(s); js.id = id;

  js.src = "//connect.facebook.net/it_IT/all.js#xfbml=1";

  fjs.parentNode.insertBefore(js, fjs);

}(document, 'script', 'facebook-jssdk'));</script>

 

<div class="fb-like" data-href="<%=currentUrl %>" data-send="true"

data-layout="button_count" data-width="200" data-show-faces="true"></div>

 

<script src="//platform.linkedin.com/in.js" type="text/javascript">

lang: en_US

</script>

<script type="IN/Share" data-counter="right" data-url="<%=currentUrl %>"></script>

 

<div style="clear:left;"></div>

 

</div>

<div style="text-align:center;">

<strong>My Social Accounts</strong>

</div>

<div style="text-align:center;margin:auto;width:100%;">

<a href="<%=urlAccountFacebook%>" target="_blank"><img

src="http://www.pastuweb.com/special_butt/facebook.png"

width="30" title="Account FaceBook" alt="Account FaceBook" /></a>  

<a href="<%=urlAccountLinkedIn%>" target="_blank"><img

src="http://www.pastuweb.com/special_butt/linkedin.png"

width="30" title="Account LinkedIn" alt="Account LinkedIn" /></a> 

<a href="<%=urlAccountTwitter%>" target="_blank"><img

src="http://www.pastuweb.com/special_butt/twitter.png"

width="31" title="Account Twitter" alt="Account Twitter" /></a> 

<a href="<%=urlAccountGooglePlus%>" target="_blank"><img

src="http://www.pastuweb.com/special_butt/google_plus.png"

width="39" title="Account Google Plus" alt="Account Google Plus" /></a> 

<a href="<%=urlAccountGitHub%>" target="_blank"><img
src="<%=request.getContextPath() %>/images/github.png"
width="30" title="Account Git Hub" alt="Account Git Hub" /></a> 

 

 

</div>

 

</div>

edit.jsp

<%
/**
 * Copyright (c) Pasturenzi Francesco
 * This is the form that you can see click on button "Preferences" of the Portlet
 */
%>
 
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<jsp:useBean id="saveAccountsURL" class="java.lang.String" scope="request" />
<jsp:useBean id="urlAccountFacebook" class="java.lang.String" scope="request" />
<jsp:useBean id="urlAccountLinkedIn" class="java.lang.String" scope="request" />
<jsp:useBean id="urlAccountGooglePlus" class="java.lang.String" scope="request" />
<jsp:useBean id="urlAccountTwitter" class="java.lang.String" scope="request" />
<jsp:useBean id="urlAccountGitHub" class="java.lang.String" scope="request" />
<jsp:useBean id="currentUrl" class="java.lang.String" scope="request" />
 
<portlet:defineObjects />
 
 
<form id="<portlet:namespace/>accountForm" action="<%=saveAccountsURL%>" method="POST">
<span style="color:#000000;">URL Account <img src="<%=request.getContextPath() %>/images/facebook.png"
     width="30" title="Account FaceBook" alt="Account FaceBook" />:</span><br>
<input type="text" name="inUrlAccountFacebook" size="50" value="<%=urlAccountFacebook%>"><br>
<span style="color:#000000;">URL Account <img src="<%=request.getContextPath() %>/images/linkedin.png"
     width="30" title="Account LinkedIn" alt="Account LinkedIn" />:</span><br>
<input type="text" name="inUrlAccountLinkedIn" size="50" value="<%=urlAccountLinkedIn%>"><br>
<span style="color:#000000;">URL Account <img src="<%=request.getContextPath() %>/images/googleplus.png"
     width="39" title="Account Google Plus" alt="Account Google Plus" />:</span><br>
<input type="text" name="inUrlAccountGooglePlus" size="50" value="<%=urlAccountGooglePlus%>"><br>
<span style="color:#000000;">URL Account <img src="<%=request.getContextPath() %>/images/twitter.png"
     width="30" title="Account Twitter" alt="Account Twitter" />:</span><br>
<input type="text" name="inUrlAccountTwitter" size="50" value="<%=urlAccountTwitter%>"><br>
<span style="color:#000000;">URL Account <img src="<%=request.getContextPath() %>/images/github.png"
     width="30" title="Account GitHub" alt="Account GitHub" />:</span><br>
<input type="text" name="inUrlAccountGitHub" size="50" value="<%=urlAccountGitHub%>"><br>
<p style="text-align:right;">
<input type="submit" id="inviaAccountsForm" title="Save" value="Save">
</p>
<br>
</form>

 

Now the most important file: MySocialButtons.java
package com.appuntivarinet;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Logger;
 
import javax.portlet.*;
 
import com.liferay.portal.util.PortalUtil;
 
 
public class MySocialButtons extends GenericPortlet {
 
/* initialize the default parameter of "portlet.xml" */
protected String editJSP;
protected String viewJSP;
private String googUrl = "https://www.googleapis.com/urlshortener/v1/url?
   shortUrl=http://goo.gl/fbsS&key=AIzaSyCwkdNYB0IVPOaHfqTxb7XNcUOmYIpUkZ4";
 
public void init() throws PortletException{
editJSP = getInitParameter("edit-jsp");
viewJSP = getInitParameter("view-jsp");
}
 
 
//set the Portlet's default View
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
    throws IOException, PortletException{
PortletPreferences prefs = renderRequest.getPreferences();
String accountFb = (String) prefs.getValue("urlAccountFb", "none");
String accountLIn = (String) prefs.getValue("urlAccountLIn", "none");
String accountGoo = (String) prefs.getValue("urlAccountGoo", "none");
String accountTw = (String) prefs.getValue("urlAccountTw", "none");
String accountGit = (String) prefs.getValue("urlAccountGit", "none");
 
/* Default value */
if(accountFb.equalsIgnoreCase("none")){
accountFb = "https://www.facebook.com/francesco.pasturenzi";
}
if(accountLIn.equalsIgnoreCase("none")){
accountLIn = "http://it.linkedin.com/in/francescopasturenzi";
}
if(accountGoo.equalsIgnoreCase("none")){
accountGoo = "https://plus.google.com/u/0/115914974766338057909/about";
}
if(accountTw.equalsIgnoreCase("none")){
accountTw = "https://twitter.com/pasturenzi";
}
if(accountGit.equalsIgnoreCase("none")){
accountGit = "https://github.com/pastuweb";
}
 
String dominio = renderRequest.getServerName();
String currentUrl = "http://" + dominio + "" +PortalUtil.getCurrentURL(renderRequest);
 
/*Accedo alla API di Google: Shorter Url*/
String currentUrlShort = shorten(currentUrl);
 
renderRequest.setAttribute("urlAccountFacebook", accountFb);
renderRequest.setAttribute("urlAccountLinkedIn", accountLIn);
renderRequest.setAttribute("urlAccountGooglePlus", accountGoo);
renderRequest.setAttribute("urlAccountTwitter", accountTw);
renderRequest.setAttribute("urlAccountGitHub", accountGit);
renderRequest.setAttribute("currentUrl", currentUrlShort);
 
include(viewJSP, renderRequest, renderResponse);
}
 
 
/* special method: used to dispatch to right JSP */
protected void include(String path, RenderRequest renderRequest, RenderResponse renderResponse)
  throws IOException, PortletException{
PortletRequestDispatcher portletRequestDispatcher = getPortletContext().getRequestDispatcher(path);
if(portletRequestDispatcher == null){
System.out.println("path : "+path+" non e' valido.");
}else{
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}
 
/* set the Portlet's default Edit: it's a simple <form> */
public void doEdit(RenderRequest renderRequest, RenderResponse renderResponse)
   throws IOException, PortletException{
 
renderResponse.setContentType("text/html");
 
PortletPreferences prefs = renderRequest.getPreferences();
String accountFb = (String) prefs.getValue("urlAccountFb", "none");
String accountLIn = (String) prefs.getValue("urlAccountLIn", "none");
String accountGoo = (String) prefs.getValue("urlAccountGoo", "none");
String accountTw = (String) prefs.getValue("urlAccountTw", "none");
String accountGit = (String) prefs.getValue("urlAccountGit", "none");
 
/* Default value */
if(accountFb.equalsIgnoreCase("none")){
accountFb = "https://www.facebook.com/francesco.pasturenzi";
}
if(accountLIn.equalsIgnoreCase("none")){
accountLIn = "http://it.linkedin.com/in/francescopasturenzi";
}
if(accountGoo.equalsIgnoreCase("none")){
accountGoo = "https://plus.google.com/u/0/115914974766338057909/about";
}
if(accountTw.equalsIgnoreCase("none")){
accountTw = "https://twitter.com/pasturenzi";
}
if(accountGit.equalsIgnoreCase("none")){
accountGit = "https://github.com/pastuweb";
}
 
renderRequest.setAttribute("urlAccountFacebook", accountFb);
renderRequest.setAttribute("urlAccountLinkedIn", accountLIn);
renderRequest.setAttribute("urlAccountGooglePlus", accountGoo);
renderRequest.setAttribute("urlAccountTwitter", accountTw);
renderRequest.setAttribute("urlAccountGitHub", accountGit);
 
PortletURL saveAccountsURL = renderResponse.createActionURL();
saveAccountsURL.setParameter("saveAccounts", "saveAccounts");
renderRequest.setAttribute("saveAccountsURL", saveAccountsURL.toString());
 
/*You can add other ACTION URL for the EDIT JSP*/
 
include(editJSP, renderRequest, renderResponse);
 
}
 
 
/* ACTION call from Portlet's <form> of EDIT JSP */
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
   throws IOException, PortletException{
 
String saveAccounts = actionRequest.getParameter("saveAccounts");
/*You can add other getParameter of EDIT JSP*/
 
if(saveAccounts != null){
PortletPreferences prefs = actionRequest.getPreferences();
prefs.setValue("urlAccountFb", actionRequest.getParameter("inUrlAccountFacebook"));
prefs.setValue("urlAccountLIn", actionRequest.getParameter("inUrlAccountLinkedIn"));
prefs.setValue("urlAccountGoo", actionRequest.getParameter("inUrlAccountGooglePlus"));
prefs.setValue("urlAccountTw", actionRequest.getParameter("inUrlAccountTwitter"));
prefs.setValue("urlAccountGit", actionRequest.getParameter("inUrlAccountGitHub"));
 
prefs.store();
actionResponse.setPortletMode(PortletMode.VIEW);
}
 
/*You can test other getParameter of EDIT JSP*/
 
 
}
 
 
/*special method: it used to have the shorten url */
private String shorten(String longUrl){
   String shortUrl = "";
 
   try{
       URLConnection conn = new URL(googUrl).openConnection();
       conn.setDoOutput(true);
       conn.setRequestProperty("Content-Type", "application/json");
       OutputStreamWriter wr = 
                    new OutputStreamWriter(conn.getOutputStream());
       wr.write("{\"longUrl\":\"" + longUrl + "\"}");
       wr.flush();
 
       // Get the response
       BufferedReader rd = 
                    new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
       String line;
 
       while ((line = rd.readLine()) != null)
       {
           if (line.indexOf("id") > -1)
           {
               // I'm sure there's a more elegant way of parsing
               // the JSON response, but this is quick/dirty =)
               shortUrl = line.substring(8, line.length() - 2);
               break;
           }
       }
 
       wr.close();
       rd.close();
   }catch (MalformedURLException ex){
       Logger.getLogger("MalformedUrl");
   }catch (IOException ex){
       Logger.getLogger("IoException");        
   }
 
   return shortUrl;
}
 
 
}
 
You can find this project on my GitHub Repository at this repository URL: https://github.com/pastuweb/social-buttons-liferay-6-1
Average (1 Vote)
The average rating is 5.0 stars out of 5.