1

I am new in alfresco and I create a rule that execute a javascript file when a file arrives in a directory (My javascript it inside data dictionnary script file). However, this javascript script must send a http get request to a remote server but I can not use the alfresco remote. Can you suggest a solution? Thank you

My javascript code:

function main() {
    var name = document.name;
    var username = "";
    var msgContent = "";
    var siteName = document.siteShortName;
    var firstName = person.properties.firstName;
    var lastName = person.properties.lastName;

    if (siteName == null) {
        if (logger.isLoggingEnabled())
            logger.log("Erreur");

        return;
    }
    if (firstName != null) {
        username = username + firstName + " ";
    }
    if (lastName != null) {
        username = username + lastName;
    }
    msgContent = username + " a ajouté un nouvel élément de contenu " + name + " au site " + siteName;

    if (logger.isLoggingEnabled()) {
        logger.log(encodeURIComponent(msgContent));
    }
    var connector = remote.connect("gedrepportremote");
    var result = connector.get("/api/sendSMS?siteName=" + encodeURIComponent(siteName) + "&msgContent="+encodeURIComponent(msgContent));
    if (result.status == 200) {
        return JSON.parse(result);
    } else if (result.status == 403) {
        
    } else {
        return null;
    }
}

main();
I have this erreor : "remote is not defined". I tried this HTTP Request from WebScript in Alfresco but it doesn't work for me.
kboul
  • 13,836
  • 5
  • 42
  • 53

1 Answers1

1

I do not think that it would be useful to enhance the remote object to connect to your external resource.

The easiest solution is to implement the logic in a java class and then expose an instance of that class as a root object in the script.

It is not difficult to add a new object in the javascript context. It is enough to define a new bean like the following:

<bean id="javascriptRestClient" parent="baseJavaScriptExtension"
    class="com.example.javascript.JavascriptRestClient">
    <property name="extensionName" value="restClient" />
</bean>

The class JavascriptRestClient in this example must extends org.alfresco.repo.processor.BaseProcessorExtension.

With this definition you will be able to access an instance of the class JavascriptRestClient in javascript using the variable restClient.

The parent bean baseJavaScriptExtension is defined in the alfresco context file alfresco/script-services-context.xml as:

<!-- base config implementation that script extension beans extend from - for auto registration
     as a global script with the ScriptService -->
<bean id="baseJavaScriptExtension" abstract="true" init-method="register">
    <property name="processor">
        <ref bean="javaScriptProcessor"/>
    </property>
</bean>
Marco Altieri
  • 3,726
  • 2
  • 33
  • 47