12/30/2011

Activiti and Spring

I am currently starting to work myself into Alfresco Activiti, which is, by the way a great process engine that you can use from and in liferay.


The following problem might occur to you if you are working with Spring and Activiti:


You can use the activiti designer in eclipse to connect several steps in your activiti process to one process chain like "unzip files", "inpect unzipped files" and "copy inspected files to fileserver".


To do so, you can either use the simplest approach: Specify a Java Class (extending JavaDelegate) for your Service Task, connect all tasks by a Sequence Flow and let activiti do the calling. In this way you have access to the DelegateExecution object that lets you (for example) share variables across your process.


This will work, and every class will get called, but your spring configuration will be totally ignored.


If you decide to work with the "Expression" type, you need to define your Task in your Spring config and then you have all the spring injection magic that you like. But how do you get the DelegateExecution object in it?


I found out that "execution" is a reserved word in activiti and if you want to have an expression that just calls the standard execution(DelegateExecution execution) method, you can do it simply like this:


#{yourTask.execute(execution)}

This is how it will look like in your Eclipse Designer:

Eclipse Designer: Adding Expression


That´s all: You have your Spring injection AND you have the DelegateExecution object.


If you got interested and you are thinking about taking a look into Activiti (it´s free by the way!), you can do so here: http://activiti.org/


If you like this post it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.

12/22/2011

Xmas Break

This blog will pause for at least 2 weeks because the author is in desperate need of christmas !


I am wishing all my readers happy holidays and a happy new year.


I hope to see / read all of you next year for some intense Liferay 6.1 and ICEfaces 3.0 stuff !!!


Daniel Breitner



12/02/2011

Connecting Liferay to Facebook

Welcome to Part 2 of my series on how to connect liferay to social networks. The Last time I showed you how to do it with linkedIn, this time we will take a look into the facebook integration.


Everything that I said about linkedIn also goes for facebook: you need to know your business use cases to know how to integrate it. All I can do is to sho you how to connect to facebook. If you need asisstance, or if you need someone to help you programming in liferay you can book me by sending me an email, or adding me on linkedIn or Skype.


The first step to connect your liferay instance to facebook is to create a developer account. You can do so here: http://developers.facebook.com/. If you need any assistance during your authentication process to facebook you can either ask me or take a look here: http://developers.facebook.com/docs/authentication/.


After you did that, you will have an API key and a secret passcode. That´s like a user / password combination. Add yourself as developer to your app and everybody else who wants to take part in the development.


The next step is to choose a java library that will help you to get the data our of facebook or simply connect your liferay instance of it. I chose restFB, which you can find here: http://restfb.com/.


The general authentication flow is as follows: Redirect the user on his first visit to your app to the authentication URL of facebook and let him accept your app. Then, the flow will return to your servlet, portlet or java application. You can then use the oauth verifier to get the access token from facebook which you can store and reuse.

Redirecting the user


It doesn´t matter, if you use Servlets or portlets as long as you have access to request parameters. if you are using JSF portlets, you can look up how to access request parameters in my blog.


So, the first step will be to redirect the User to facebook to let him accept your app. You can do so by using the following snippet:
String connect = "https://www.facebook.com/dialog/oauth?client_id="
+ Facebook.api_key "&redirect_uri="
+ REDIRECT_URL + "&scope=email,read_stream,offline_access";

response.sendRedirect(URL_USER_CONNECT);
Let´s go through this snippet in detail. The first two lines are pretty straight forward: Redirect the User to the facebook oauth page and provide your api key. Then you need to specify a redirect URL. This won´t be a complete URL but instead a path that will be added to the canvas URL you have specified in your facebook developer signup. If you didn´t specify a URL back then, then please do it now. Specify it like http://your-ip and provide the path as redirect URL like /your-facebook-app/your-servlet.
The last thing you have to provide are the rights (= scope) you want to have for your facebook app. All scope types can be found here: http://developers.facebook.com/docs/reference/api/permissions/


The last thing you do is to send the redirect. The User is transported to facebook, needs to accept your app and transported back to your redirect URL.

Get the access token


If the authentication flow returns to you page, you have access to the authentication code:
String code = request.getParameter("code");
This code will be used to get the authentication token. You need to connect to the following URL:
String urlAuthToken= "https://graph.facebook.com/oauth/access_token?client_id="

+ Facebook.api_key+ "&redirect_uri="+ REDIRECT_URL+ "&client_secret="+ Facebook.secret + "&code="+code;
If you perform a GET request to this URL, you will receive the authentication token. This token you need to store and reuse every time your user wants to login  or update some data.
Here is an exemplary call to facebook to get the first- and lastName of a User:
FacebookClient facebookClient = new DefaultFacebookClient("your-authentication-token);
User user = facebookClient.fetchObject("me",User.class);
log.info("Got last name:" + user.getLastName());
That´s all. Now you must decide how to integrate facebook into your liferay portal. If you need help - just contact me by mail (daniel.breitner32@googlemail.com) or skype(daniel_breitner).




If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.