Liferay JAX-RS service

General Blogs August 16, 2017 By sejal patel

 Liferay 7 bolster for creating and deploying JAX-RS based REST services.

JAX-RS is the JEE standards based approach for creating REST web services. As far as building up the real administrations it basically utilizes an explanation based approach,where one improves strategies in Java classes with comments, for example, @GET, @POST, @Path and so forth.

In this post, we will examine about liferay JAX-RS service . Following are some prerequisite to implement liferay JAX-RS service 

Prerequisite:

1. Liferay 7 must be installed

2.Eclipse neon

3.Java 1.8 

Configure liferay JAX-RS service :

Now, lets understand the steps to generate JAX-RS Service as follow,

1.To create a liferay workspace in ide follow the following steps.

     File Menu ->New-> Select Liferay workspace

    Upon creation of the workspace, the structure should look like below 

 

2.To create rest module in ide follow the following steps

  File Menu -> New->Click on Liferay Module Project. Fill up the detail as follow,

  Project Name: enter appropriate name e.g :":Rest-Services-Demo-rest"

  Project Template Name: Select :"rest"

3. update gradle dependencies with below and run the gradle build task

dependencies {

 compileOnly group: "javax.ws.rs", name: "javax.ws.rs-api", version: "2.0.1"

 compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"

 compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"

 compileOnly group: "com.liferay.portal", name: "com.liferay.util.java", version: "2.0.0"

 compileOnly group: "com.liferay.portal", name: "com.liferay.portal.impl", version: "2.0.0"

 compile group: 'com.liferay', name: 'com.liferay.osgi.util', version: '1.0.0'

 compileOnly project(":modules:Rest-Services-Demo:Rest-Services-Demo-api")

 compileOnly project(":modules:Rest-Services-Demo:Rest-Services-Demo-service")

}

4.Configuring CXF Endpoints

Liferay supports JAX-RS via the Apache CXF implementation. CXF endpoints are context paths where the JAX web services are deployed to and accessible from.

We can configure CXFEndpoint from “/src/main/resources/configuration/com.liferay.portal.remote.cxf.common.configuration.CXFEndpointPublisherConfiguration-cxf “ file 

contextPath=/student-rest

authVerifierProperties=auth.verifier.BasicAuthHeaderAuthVerifier.urls.includes=*

5.Configuring REST Extenders

We can configure REST Extenders from /src/main/resources/configuration/com.liferay.portal.remote.rest.extender.configuration.RestExtenderConfiguration-rest file.

contextPaths=/student-rest

jaxRsServiceFilterStrings=(component.name=Rest.Services.Demo.rest.application.RestServicesDemoRestApplication)

For above configuration services will be deployed and can be accessible by following URL

http://localhost:8080/o/student-rest

6.Publishing JAX-WS RS Services

Liferay creates REST Component with sample methods, so edit the RestServicesDemoRestApplication class that returns Student objects as JSON

@ApplicationPath("/student")

@Component(immediate = true, service = Application.class,

configurationPid = "com.enprowess.rest.RestServicesDemoRestApplication",

configurationPolicy = ConfigurationPolicy.OPTIONAL,property={"jaxrs.application=true"})

public class RestServicesDemoRestApplication extends Application {

      public Set<Object> getSingletons() {

             return Collections.<Object>singleton(this);

      }

 private StudentLocalService studentLocalService;

  @Reference(unbind="-")

  public void setLeaveLocalService(StudentLocalService studentLocalService){

         this.studentLocalService=studentLocalService;

   }

   @GET

   @Path("/student-info/{studentId}")

   @Produces(MediaType.APPLICATION_JSON)

  public String getLeave(@PathParam("studentId") long studentId){

       Student student = null;

       String jsonString = null;

     try {

                student = studentLocalService.getStudent(studentId);

                jsonString = JSONFactoryUtil.serialize(student);

        } catch (Exception e) {

         e.printStackTrace();

       }

     return jsonString;

    }

}

7.Liferay REST Extender Configuration :

    Deploy the rest module in liferay server.

     Click on Control Panel -> Configuration -> System Settings 

     Search for REST ->Click on REST Extender

     Click on the configuration and you can see the deployed module specified context paths. 

 

To access rest service use following examples:

Liferay 7 begins with context /o unlike in Liferay 6 with/c

http://localhost:8090/o/student-rest/student/student-info/1

Liferay Screenlet

General Blogs August 16, 2017 By sejal patel

Liferay provides the screenlets, which are the readymade screen for the faster android app development for Liferay apps. Liferay Screens for Android contains several Screenlets that you can use in your Android apps.

Liferay offers bunch of screenlets which is called Screenlet Library. You can know more about it from following link,

(https://dev.liferay.com/develop/reference/-/knowledge_base/7-0/screenlets-in-liferay-screens-for-android) 

Prerequisite:

1. Liferay 7 must be installed.

2. Android Studio .

Configure Login screenlet:

Before we discuss steps to implement screenlets, it is important for you to understand that Android project configuration. You can learn about Android project in Liferay from following link, 

https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/preparing-android-projects-for-liferay-screens#configuring-your-project-with-gradle

Now, lets understand the steps to generate screenlet as follow,

1. Create new project in android studio

a) To create an android project in Android studio, follow the following steps and click next,

                   File->New Project ->Fill up the detail as follow,

                   Application Name: Provide appropriate name e.g." LoginScreenletDemo"

                   Company Domain: provide appropriate name e.g."com.enprowess.loginscreenletdemo”

b) Select form factor and click next,For this example, I selected phone and template and maximum SDK as API 15 Android 4.0.3

c) Select Empty Activity from this screen and click next ,

d) Provide activity name and click on finish. In this example, I provided activity name as " LoginActivity",

e) On click of finish, you will see following folder structure created,

 

2. Add Liferay libraries for screenlets

Liferay provides bunch of screenlets. In order to get access to those screenlet, you need to add dependency in gradle.build file as follow. After adding dependency sync the project to get access of Liferay screenlets.

android {
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }

  }

. . . .

. . . .

compile 'com.android.support:exifinterface:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
compile 'com.liferay.mobile:liferay-screens:+'
compile 'com.liferay.mobile:liferay-material-viewset:+'
compile 'net.the4thdimension:android-utils:2.0.4'

3. Configure login Screenlet for logging into Liferay instance.

Configure UI for logging activity by adding the following code in your activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:liferay "http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <com.liferay.mobile.screens.auth.login.LoginScreenlet
        android:id="@+id/login_screenlet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        liferay:layoutId="@layout/login_default" />
</FrameLayout>


4.Configure liferay server  

You can configure Liferay server details in server_context.xml under your res/values folder. Please see following code snipped for the reference.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="liferay_server">http://192.168.0.8:8080</string>
    <integer name="liferay_company_id">20116</integer>
    <integer name="liferay_portal_version">70</integer>
</resources>

Following is the quick summery for the attributes to configure,

liferay_server : Server address of your Liferay instance is running.

liferay_company_id : Company id for the instance

liferay_portal_version : Liferay version server is running Liferay 7 (70)

 

5.Update LoginActivity.java

Screenlet provides the interfaces to get a result of web service calls by mobile SDK. For login, you need to implement LoginListener which provides following two methods that you need to implement.

i) onLoginSuccess()

ii) onLoginFailure()

 

 

6.Run Application

Select your application -> click on run icon -> Create new Virtual Device -> Select Phone and then model of your choice then click next -> Select System image (for testing I used Nouget) -> Finally select orientation (I selected Portrait view) and click on finish

You will see your first Liferay login screenlet as follow,

 

 

Showing 2 results.