How to call a URL if new document enters folder

cancel
Showing results for 
Search instead for 
Did you mean: 
fabermundi
Member II

How to call a URL if new document enters folder

Hello,

I have a java applications where the user can add documents using alfresco. If fact the user uploads (scans) a document that is moved to the corresponding application folder then it is display in the corresponding application.
To check if there are new documents I have a timer that checks the folder (poll).
I want to change that to do a "push" when a new document arrives. I realize this I wanted to program a "push server" that notifies the client application of new documents. What I need is that alfresco calls a URL (containing the ID of the document) when a new document arrives but unfortunately I do not know how to realize this. The easiest method would be that I create a rule on the corresponding alfresco folder to call the URL but I did not managed to write a script to do this (I did not found out how to open an external URL in a script).

Does anyone can help or does anyone knows a better solution to realize a "push" of new documents to my applications?

Thanks in advance.

2 Replies
uvukasinovic
Active Member

Re: How to call a URL if new document enters folder

Hi Andre,

Create a rule on that folder and assign Java Action that will call given URL. I have a simple example written in Java that can be useful:

package my.action;

import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.repository.NodeRef;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.List;

public class CallUrl extends ActionExecuterAbstractBase {

    public static final String NAME = "call-url";

    private final Logger logger = Logger.getLogger(this.getClass());

    private MyConnection myConnection;

    @Override
    protected void executeImpl(Action action, NodeRef nodeRef) {
        logger.debug("CallUrl action is called on nodeRef: " + nodeRef.toString());
        String nodeId = nodeRef.getId();

        try {
            String response = myConnection.getResponse(MyCustomAddress.URL, nodeId);
            logger.debug("response: " + response);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void addParameterDefinitions(List<ParameterDefinition> paramList) {

    }

    public MyConnection getMyConnection() {
        return myConnection;
    }

    public void setMyConnection(MyConnection myConnection) {
        this.myConnection = myConnection;
    }
}

nodeRef is a reference to the document that is created in your folder.

In service-context.xml file put this:

<bean id="call-url" class="my.action.CallUrl" parent="action-executer">
    <property name="myConnection" ref="myConnection" />
</bean>

In MyConnection class I created HttpURLConnection - you can find some examples of how to create Http Request in Java.

In property file, under messages folder (if you are using SDK), add following:

# Call URL action
call-url.title=Call given URL
call-url.description=This will call a given URL

You can also improve given Rule and add a field where a user can put appropriate URL.

kintu_barot
Senior Member

Re: How to call a URL if new document enters folder

Create a rule on a folder which executes java script function on document upload to call a web script. Write a code to call a required URL in a web script controller. You must manage the response you get from the URL.

Thanks

Kintu

ContCentric

Regards,
Kintu