Thursday 24 October 2013

Sending bytearray and other parameters to the webscript at the same time

Hey everyone !

There may be the scenarios where you want to upload a file to the alfresco repository through the webscript and also want to pass some parameters to the webscript.
That means you want to send the uploaded file (bytearray) alongwith some parameters to the webscript.

In this scenario, You definitely will think of writing a POST webscript. Absolutely fine.

However, It is not possible to send both the content (other parameters) and uploaded file to the webscript at the same time. You may try doing it from POSTER plugin, it will not allow.
Note : Poster is a firebug plugin I generally use for testing POST webscripts.

In order to send bytearray of the content and also pass other parameters to the webscript. Here is the trick.

You will need to do below -
1. Open poster
2. Provide your webscript URL.
    e.g. http://localhost:8080/alfresco/service/test/upload
3. Provide the parameters you want to send as the querystring. Hence, modify the URL as below.
    e.g. http://localhost:8080/alfresco/service/test/upload?nodeid=parentid&type=thumb
4. In poster, click on Content to send tab. Open the file you want to upload and Click on POST to hit the webscript.

In your controller class of the webscript -
You can retrieve the uploaded content as mentioned below.
1. For parameters, use request.getParameter("nodeid");  This we all have used.
2. To retrieve uploaded content, use request.getContent() and this way you will get the uploaded content. You can get the inputstream of the content and use it for further processing.

This way you can pass bytearray and other parameters to the webscript at the same time.

Hope this will help you at some point of your coding experience in alfresco.

Thanks,

Wednesday 23 October 2013

Authenticate user without password in alfresco

Hello everyone !

This one is really interesting.

You may come across a scenario where you do not want to use alfresco's username and password based authentication. Instead you are having some custom property set on user for example PIN number and want to do user's authentication against this PIN OR you do not want to use any authentication subsystem and want to do the user authentication against a database table. Authentication based on custom property is not available out-of-the-box in alfresco.

Here I am providing the details about the implementation approach in case you ever come across such scenario.

Okay ! here it goes !

Consider a scenario, wherein you want to do the authentication based on username and PIN (user's custom property) instead of password and retrive the ticket for further webscript calls.
This is not possible out-of-the-box in alfresco.

In out-of-the-box, you can use login webscript passing username and password to get the ticket.
Also, unless user is authenticated, you can not get user's metadata.

Following is the implementation detail to achieve user authentication without password.
1. Create a custom login webscript, which will take username and PIN as the input parameter.

2. Make sure you have set the <authentication>none</authentication> in the description file of this webcript.

3. Now, in your webscript controller (either java/javascript), first set the authentication to system user and search for the user with the given name. Once user is retrieved, get the PIN number of the user. Clear the system user authentication.

4. Match the retrived PIN with the webscript input parameter PIN. If both matches then here comes the main step. Using AuthenticationUtil.setRunAsUser(username), set the authentication for current user.

5. In order to retrieve the ticket, use following - authenticationService.getCurrentTicket() and populate the model object to return ticket as webscript response.

6. Then next step is to clear the security context using below - AuthenticationUtil.clearCurrentSecurityContext();

By implementing above, you should retrieve the ticket for the given user.

You can also validate the ticket by calling out-of-the-box loginticket webscript to determine that if the ticket belongs to the given user only or not.

Hope this one will be helpful to you.

Thanks,

Wednesday 18 September 2013

Returning output values from action in Alfresco

Welcome everyone ! First of all thank you for your time visiting this post. I hope this post will help you get the solution you are looking for.

Okay, Let me start now.

Generally in alfresco, we invoke an action to process certain task and we do not return any output from the action. This is a general implementation you might have done.

Consider a scenario wherein you need to return output from your action for further processing (say for example, you have to invoke another action which should have input as the output NodeRef from your first action, you might need to use the output from your action to invoke another webscript).

Here is the implementation detail about how to achieve this.

Implementation -
1. Firstly, set up output value in your Action class -

   In your ActionExecuter class - in the executeImpl method - Once you are done with your processing and you got the required output, then as shown below set up a result parameter.

   ruleAction.setParameterValue("output-value", resultNodeRef.getId());

2. Secondly, Retrieving output value of the action from where you have invoked the action.

   Scenario-1 - If you have invoked your action from javascript based webscript then below is the code snippet to retrieve the output value from the action.

     var actionOne = actions.create("your-custom-action");

     actionOne.execute(inputNode);

     logger.log("Output from the action : " + actionOne.parameters["output-value"]);
  
   Scenario-2 - If you have invoked action from java code then below is the code snippet to retrieve the output value from the action.
    
    Action action = actionService.createAction("your-custom-action");

    actionService.executeAction(action, node.getNodeRef());
    
    System.out.println(action.getParameterValue("output-value"));

Yay ! It's done. Coding done ! Time to test now ! please test it. It should work fine.

Hope this help you.

Thanks,
Ramesh C