Accessing Directory Server from Flex using Java

May 16, 2008

Accessing Directory Server from Flex application using Java and LDAP is pretty straight forward. All you need to do is to create a Java class which will access your Directory Server using LDAP and return the results. In this sample I used the following to access Directory Server from my Flex application.

Netscape Directory SDK for Java: I used this to access Directory Server from my Java class.

Directory SDK Dev guide

BlazeDS Remoting: used this for accessing Java class from Flex application.

We will create one Java class which will search Directory Server and return the values. MXML file to access the Java class and display the results.

Pre-requisite: Knowledge on how to invoke Java methods from Flex applications and also have BlazeDS set up on your server. If you have not done this, please visit this URL for details on how to access Java methods from Flex applications. Invoking Java methods from Flex applications

LDAPHelper.java

Download this file at this URL: LDAPHelper.java

We have a searchEmployees() method which will search the Directory Server and returns ArrayList. You have to edit the attached file to add your server name and credentials to access your Directory Server. searchEmployees() method is not doing much, check out the dev guide to learn more about how to play around with Directory Server.

You should add a destination to your remoting-config.xml. I have added the following under the service tag in my remoting-config.xml.

<destination id=” LDAPHelper” channels=”my-amf”>

<properties>

<source> com.adobe.ldap.LDAPHelper</source>

</properties>

<adapter ref=”java-object”/>

</destination>

You should add the Netscape Directory SDK for Java jar file into the WEB-INF/lib folder of the web application and also to your project in the Eclipse for compiling the class.

LDAPSearch.mxml

Download this file at this URL: LDAPSearch.mxml

In this application we are invoking the searchEmployees() method on the Java class using BlazeDS Remoting and displaying the results returned.

That’s it 🙂


Session data management in Flex Remoting

May 16, 2008

If you are using Flex Remoting to communicate with Java classes on the server, you might want to maintain instance of an object across a user session. For example you might want to store the User object after successful authentication of a user. BlazeDS provides class named FlexContext.

Please visit the BlazeDS Developer guide at this URL for more details on FlexContext class and session data management. BlazeDS Developer Guide

Updated: Free AIR based Tool to generate Flex code for consuming/exposing Java classes as BlazeDS Remoting services. Visit this URL for more details https://sujitreddyg.wordpress.com/2009/05/07/blazemonster/

I created a simple Java class which will add objects to session. It will also retrieve objects from session and modify them. I access this Java class from a Flex application to get the data which is stored in user session. In this sample we will be creating one Java class and one .mxml file.

Pre-requisite: Knowledge on how to invoke Java methods from Flex applications and also have BlazeDS set up on your server. If you have not done this, please visit this URL for details on how to access Java methods from Flex applications. Invoking Java methods from Flex applications

Let’s move into details. First create a Java class as shown below.

com.adobe.services.MySessionHandler.java

Download this file from this URL: MySessionHandler.java

In the constructor I am checking if object already exists in the session and add objects if necessary. We have two objects in the session. One is userName which is object of type java.lang.String and myCounter which is object of type java.lang.Integer and is used to store a counter. We will provide method to increment this counter.

public MySessionHandler()

{

mySession= FlexContext.getFlexSession();

if(mySession.getAttribute(“myCounter”) == null)

{

mySession.setAttribute(“myCounter”, new Integer(1));

}

if(mySession.getAttribute(“userName”) == null)

{

mySession.setAttribute(“userName”, “Sujit Reddy G”);

}

}

In this method we are incrementing the value of the object stored in the session.

public void increaseCounter()

{

Integer i = (Integer) mySession.getAttribute(“myCounter”);

i = i + 1;

mySession.setAttribute(“myCounter”, i);

}

You can find more methods in Java file attached. Download the file and place the compiled file into WEB-INF/classes folder of the web application where your BlazeDS is present. You should add flex-messaging-core.jar to your project build path in Eclipse to compile this class.

You should add a destination to your remoting-config.xml. I have added the following under the service tag in my remoting-config.xml.

<destination id=”MySessionHandler” channels=”my-amf”>

<properties>

<source>com.adobe.services.MySessionHandler</source>

</properties>

<adapter ref=”java-object”/>

</destination>

SessionSample.mxml

Download this file from this URL: SessionSample.mxml

In this Flex application we have three buttons which will invoke methods on the Java class created above to get the results. There are three methods which we will invoke.

getUserName() – this will return the user name stored in the session

getCounter() – this will return the value of the counter

increaseCounter() – this will increment the counter value

You have to map your Flex application to the web application with BlazeDS as described in the pre-requisite for this blog.

That is all you need to do. It’s that easy to manage session data while using Flex Remoting. 🙂