Tuesday, March 10, 2015

Liferay JSON Web services Authenticated Access required

Whenever we are calling Liferay JSON web services we can find Authenticated Access required.
Even we send user name password still we can see the above exception. Generally this kind of exception we can when use apache http client to call Liferay JSON web services.

Generally this exception will encounter due to following reasons
  1. If the Liferay Web services not enable to Guest user Access
  2. When permission checker object is Null
  3. When users not sign in the permission checker  

The following is Liferay portal java class you can find the code



public class AuthenticatedAccessControlPolicy extends BaseAccessControlPolicy {

            @Override
            public void onServiceRemoteAccess(
                                    Method method, Object[] arguments,
                                    AccessControlled accessControlled)
                        throws SecurityException {

                        PermissionChecker permissionChecker =
                                    PermissionThreadLocal.getPermissionChecker();

                        if (!accessControlled.guestAccessEnabled() &&
                                    ((permissionChecker == null) || !permissionChecker.isSignedIn())) {

                                    throw new SecurityException("Authenticated access required");
                        }
            }

}

Even we use Admin User Name and Credentials to access these web services we can see Authenticated Access required.

Solution:

We need to use Basic Authorization heard in the URL request so that we can successfully call the Liferay JSON web services.

We need to set header to httpPost or HttpGet Request so that we can call these web services. When we set Authorization header basic then we need to encode credentials with Base64 encoding format

Example:


Base64 b = new Base64();
String encoding = b.encodeAsString(new String("test@liferay.com:test").getBytes());


Set Basic Authorization header to Http Post/Http Get request as follows


HttpPost post = new HttpPost("/LiferayJSONWebservices-portlet/api/jsonws/employee/get-employee");
Base64 b = new Base64();
String encoding = b.encodeAsString(new String("test@liferay.com:test").getBytes());
post.setHeader("Authorization", "Basic " + encoding);


Consume Liferay Plugin Portlet JSON web services Http Client


package com.meera.liferay;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
public class LiferayWebserviceClient {
     public static void main(String[] args) throws ClientProtocolException,
     IOException {
     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
     HttpHost targetHost = new HttpHost("localhost", 8080, "http");
     DefaultHttpClient httpclient = new DefaultHttpClient();
     BasicHttpContext ctx = new BasicHttpContext();
     // Plugin Context Use for Liferay 6.1
     HttpPost post = new HttpPost("/LiferayJSONWebservices-portlet/api/jsonws/employee/get-employee");
     Base64 b = new Base64();
     String encoding = b.encodeAsString(new String("test@liferay.com:test").getBytes());
     post.setHeader("Authorization", "Basic " + encoding);
     List<NameValuePair> params = new ArrayList<NameValuePair>();
     params.add(new BasicNameValuePair("emplyeeId", "30722"));
     UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
     post.setEntity(entity);
     HttpResponse resp = httpclient.execute(targetHost, post, ctx);
     resp.getEntity().writeTo(System.out);
     httpclient.getConnectionManager().shutdown();
     }

}

Consume Liferay Portal Services Using Http Client

package com.meera.liferay;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
public class LiferayWebserviceClient {
     public static void main(String[] args) throws ClientProtocolException, IOException {
           HttpHost targetHost = new HttpHost("localhost", 8080, "http");
           DefaultHttpClient httpclient = new DefaultHttpClient();
           BasicHttpContext ctx = new BasicHttpContext();
           // Plugin Context Use for Liferay 6.1
           HttpPost post = new HttpPost("/api/jsonws/country/get-countries");
           Base64 b = new Base64();
        String encoding = b.encodeAsString(new String("test@liferay.com:test").getBytes());
        post.setHeader("Authorization", "Basic " + encoding);
           List<NameValuePair> params = new ArrayList<NameValuePair>();
           //params.add(new BasicNameValuePair("emplyeeId", "30722"));
           UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
           post.setEntity(entity);
           HttpResponse resp = httpclient.execute(targetHost, post, ctx);
           resp.getEntity().writeTo(System.out);
           httpclient.getConnectionManager().shutdown();

     }

}

Required Jar Files


commons-codec.jar
httpclient.jar
httpcore.jar
commons-logging.jar


Download Example and Required Jar files


Tuesday, March 3, 2015

Liferay 7 Installation

Liferay have announced Liferay 7 Community Edition Milestone 4.They have conducted program called Liferay 7 Community Expedition Program so that interested people can join in the program they can test and experience the Liferay 7 portal.

Liferay released liferay-portal-7.0-ce-m4 and we can download portal server bundle and we can explore new featured and we can give feedback to Liferay community so that it will improve the next official release of Liferay 7


The following is download URL for Liferay 7(liferay-portal-7.0-ce-m4)

The following link for Liferay 7 Tomcat Bundle


Liferay 7 Bundle consist following things

Tomcat server with deployed Liferay Portal

Data Directory this folder contains document library and hsql database this is default database for Liferay.

The following screen shows what Liferay Portal Server Bundle contains.



Liferay 7 Unsupported Class Version Error with Java 1.6

When we run this bundle with java 1.6 versions (jdk/jre 1.6) then we will get Unsupported Class Version Error.

Liferay 7 Portal (ROOT) was compiled and packaged with JDK/JRE 1.7 so when we use JAVA 1.6 then we can face Unsupported Class Version Error

Generally when we install Java in our machined we set the JAVA_HOME environment variable so when we start Tomcat Server it will look for JAVA_HOME variable and this will point to our JRE/JDK home directory so that tomcat will use this and start the server successfully.

When we run Liferay Portal 7 and our system have java 1.6 then portal won’t be started and it will give above error.

Solution:

Install JDK 1.7

We need to download JDK1.7 and install into local machine then we need to set JAVA_HOME system environment variable so that we can run Liferay 7 portal.

Download JDK1.7 from following URL


Install JDK then it will be available in C:\Program Files\Java

Set JAVA_HOME environment variables as follows

Variable Name
JAVA_HOME
Value
C:\Program Files\Java\jdk1.7.0_75


Scenario:

Assume we are running some applications and servers in JAVA 1.6 then we don’t want to change JAVA_HOME variable to java 1.7. But we wanted run Liferay 7 portal

This scenario we will use setenv.bat/setenv.sh files which is in Tomcat Server “bin” directory there we will explicitly configure Java 1.7 only for this server so that whenever we start Liferay 7 bundle tomcat server then it will use java 1.7.

Easy Steps:
  1. Download JDK 1.7 and install  
  2. Copy JRE 1.7 and place in Liferay 7 Portal Tomcat server
  3. Open setenv.bat/setenv.sh then set java home

Download JDK 1.7 and install 

Download JDK1.7 from following URL


Install JDK then it will be available in C:\Program Files\Java


Copy JRE 1.7 and place in Liferay 7 Portal Tomcat server

Now copy just jre7 folder from C:\Program Files\Java place this in tomcat server
Create one directory in liferay-portal-7.0-ce-m4\tomcat-7.0.42 say “win” then place jre7 in this newly created directory.




 The following screen shows that JRE7 available in “win” directory


Open setenv.bat/setenv.sh then set java home

Now go to “tomcat-7.0.42\bin” directory open setenv.bat in editor if you are using windows OS otherwise use setenv.sh

Replace the existed content with following content


set "JAVA_HOME=%CATALINA_HOME%/win/jre7"

set "CATALINA_OPTS=%CATALINA_OPTS% -Dfile.encoding=UTF8 -Djava.net.preferIPv4Stack=true  -Dorg.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES=false -Duser.timezone=GMT -Xmx1024m -XX:MaxPermSize=256m"


Finally save the file and start tomcat serve then Liferay portal will start successfully by using jre7.

Launch portal with www.localhost:8080

Complete basic configuration steps and you explore Liferay 7 portal.

Liferay default use there hsql database if you want to change to other database engine create portal-ext.properties file Liferay Home directory then configure database connection details.

Follow the post which is for Liferay 6.2 Installation same you can replicate to Liferay 7.


Liferay 7 Portal Screen


Note:

When we install JDK 1.7 then default java will be latest installed one even we explicitly create JAVA_HOME that point to JAVA1.6.

To make java 1.6 as your default JAVA for your system then go to “C:\Windows\System32” look for java.exe, javaw.exe and javaws.exe files and delete it so that your default java in your machine will be java 1.6 that we set in the JAVA_HOME.

Monday, March 2, 2015

Liferay 7 Community Expedition Program

Liferay recently announced Liferay 7 Community Edition Milestone 4(liferay-portal-7.0-ce-m4). Liferay have given this for testing and add some new ideas to Liferay 7. People who are interested can join in the explorer group so that they can share experiences as feedback and they can post some queries in the discussion forums.

The following is download URL for Liferay 7(liferay-portal-7.0-ce-m4)

The following link for Liferay 7 Tomcat Bundle


Join in Liferay 7 Explorer Group please follow the link


Liferay 7 has added many new features so that we can download bundle, we can explore and we can share our experience with Liferay community so that it will help to improve the Liferay 7.

Many of them already joined in the explorer group people who are interested please can participate and share experience. To join as member there some instruction please follows and become member.

Liferay Feature areas are categorized as follows

Web Experience Management


Collaboration & Productivity


Dev Tooling


Frontend Infrastructure


Interested people can join in this program and then can explore in any of above category and they can share experience and feedback

The following is place to share experience and feedback.




Liferay 7 Portal Screen


Thursday, February 26, 2015

Liferay Themes Introduction

Liferay themes are the one of Liferay plugin to change look and feel of the Liferay portal.

Liferay have provided theme mechanism so that we can apply different look and feel to portal and it’s very easy to develop Liferay themes and we can change look and feel of portals. 

Liferay themes are working like independent application that is called plugin in Liferay terminology.
Liferay Portal already implemented some of default themes it will part of Liferay portal application and these themes have basic look and feel.

When we start to develop Liferay themes instead of starting from scratch we will inherited these basic theme into our newly developed themes so that we will change some CSS and some of templates files as for our requirement so that finally we can give new look and feel to the portals.

The following theme Hierarchy of Thames in Liferay Portal


The above themes are reside in in Liferay portal so that when we come from top to bottom in each theme Liferay added new styles so that finally it made good look and feel to the portal.

Liferay Portal use Classic theme is default theme so that when we install Liferay portal the portal look and feel is from classic theme.

The above specified themes are resided in the Liferay portal application which is in themes directory of porta (\tomcat-7.0.42\webapps\ROOT\html\themes)



The following is General Overview of Liferay Themes

Generally when we change the look and feel of application we need to use css, java scripts and templates. Same way Liferay themes consist of all files that are css, java script and templates.
CSS files used to apply look and feel related things like colors, font size, padding, margin and backgrounds to the pages.

Java Script is used to do some actions and some dynamically changes elements in the pages like when we click on button we want open popup, when we click on some text then some animated images need to show such a we will use java script.

Templates are used to design page structure like body section, header section, left menu and right menu such way we will use templates to design structures to the page.

Images are used to make user friendly understand in the pages like some of slide shows, some background for pages.



Generally when we start new theme we will use existed theme files then we will override as for our needs. To make original files safe Liferay have created new directory called _diff there we will maintain same files and folder structure that we can see in original theme.

So all modifications is take place in the _diff directory files so that at the time of deployment original files will be overridden by the _diff directory files so that new changes will be applied.

From source side it’s separated as original files, directory and also we have _diff but after packaging or deployment the original files reflected by new changes that we already made in the _diff. finally all together become as theme with original directory structure.

Liferay Portal Theme CSS files

The following is CSS files we will use to change the styles


Instead of writing CSS styles in each file Liferay have given one file called custom.css there we can write all new CSS styles so that it will be applied.

Liferay had made all portal with predefined CSS class names and ID for html elements. Whenever we are going to change the CSS properties we need identify the appropriate CSS class name and ID then we needs to write new CSS styles in custom.css file.

To know CSS class name for portal we can use browser inspect element feature so that we can identify the CSS class names and its CSS properties.

Liferay Portal Theme Template files

Liferay have used template implementation to aggregate dynamic content and to made portal structure as one page for this Liferay used Velocity Templates and Free Marker Templates.

When we develop custom theme we can use any one and that is based on our convenient. Before Liferay 6.2 we have only velocity templates and from Liferay 6.2 version we have another choice is free market templates while develop our custom themes.

portal_noraml.vm/portal_noraml.ftl:

We have many template files all files are included in portal_noraml.vm/  portal_noraml.ftl  and this will aggregate all content and make it as page.

navigation.vm/ navigation.ftl:

This file Liferay pages navigation is implemented.

portlet.vm/ portlet.ftl:

This is for aggregate Portlet content and added to main page.

init.vm/ init.ftl:

 This file consist all requited velocity variables and free marker variable that we will use in them get dynamic content like sign in user name and id like that.

The following is Templates files we will use to change page structure


main.js:

Theme related java script we will write in the main.js file this will be available in /js/main.js location.main.js java script is loaded at the time of page loading so we can write any java script that will be loaded at the time of page load.

Images:

We have images directory in theme to store all images related to theme and Liferay already used many images so this will be inherited from parent theme to our custom theme. Whenever we need new images we simply place in the images directory.

The following screen shows image directories which contain many sub directories inside they have images.


Popular Posts

Recent Posts

Recent Posts Widget