Sunday, February 8, 2015

Invoke Java method from Jaggery.js

Lets assume we have a Java class with login() and logout() methods. login() takes two parameters username, password and use AuthenticationAdmin adminservice internally then returns sessionCookie.

So in this can I will have following package declaration and imports within the class.

package org.wso2.carbon.session.cookie;

import org.apache.axis2.context.ServiceContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.wso2.carbon.authenticator.stub.AuthenticationAdminStub;
import org.wso2.carbon.authenticator.stub.LoginAuthenticationExceptionException;
import org.wso2.carbon.authenticator.stub.LogoutAuthenticationExceptionException;
import java.rmi.RemoteException;


Then the login and logout methods.

public String login (String username, String password) throws RemoteException, LoginAuthenticationExceptionException {

authenticationAdminStub = new AuthenticationAdminStub("https://localhost:9443/services/AuthenticationAdmin");


String sessionCookie = null;
if (authenticationAdminStub.login(username, password, "localhost")) {
System.out.println("Login Successful");
ServiceContext serviceContext = authenticationAdminStub.
_getServiceClient().getLastOperationContext().getServiceContext();
sessionCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
}
return sessionCookie;
}


public void logout () throws RemoteException, LogoutAuthenticationExceptionException {
authenticationAdminStub.logout();
System.out.println("Logout successful");
}


Inorder to manage dependencies and packaging I'm using maven in this sample.
following is my repositories and dependencies section of the project pom.xml.

<repositories>
<repository>
<id>wso2-nexus</id>
<name>WSO2 internal Repository</name>
<url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
</repository>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.authenticator.stub</artifactId>
<version>4.2.0</version>
</dependency>
</dependencies>
I'm using wso2IS-5.0 distribution to test this. Make sure to use maven packaging property as following,

<packaging>jar</packaging>

You can find the complete project at  https://github.com/udarakr/authenticator

Build org.wso2.carbon.session.cookie.gen using mvn clean install command.

Lets copy session.cookie.gen-1.0-SNAPSHOT.jar located within target directory to <IS_HOME>/repository/components/lib directory.

Then create out Jaggey application, authenticator jaggery application consists of one jag file index.jag with following content,
<%
 var SessionCookieGen =org.wso2.carbon.session.cookie.SessionCookieGen;

 var SessionCookieGen = new SessionCookieGen();
 var sessionCookie = SessionCookieGen.login('
admin', 'admin');
 print(sessionCookie);
 %>
Then copy authenticator application to the <IS_HOME>/repository/deployment/server/jaggeryapps/ directory.

NOTE :- Since this is for testing purpose only we are using the same IS node to host our application.

Then start the IS node, go to  <IS_HOME>/bin/ directory and run sh wso2server.sh within your command-line. After server start open your web browser and go to the https://localhost:9443/authenticator/

You can see,



If you don't have a complex logic within the Java class(simple method without any imports) you can follow this post published by Madhuka as a reference.

Find more information about jaggery.js from here