来自alfresco javascript文件的HTTP请求

问题描述 投票:1回答:1

我是alfresco的新手,我创建了一个规则,当文件到达目录时执行javascript文件(我的javascript在数据字典脚本文件中)。但是,此javascript脚本必须向远程服务器发送http get请求,但我无法使用alfresco remote。你能建议一个解决方案吗?谢谢

我的javascript代码:

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.
javascript alfresco alfresco-share alfresco-webscripts
1个回答
1
投票

我不认为增强远程对象连接到外部资源会很有用。

最简单的解决方案是在java类中实现逻辑,然后在脚本中将该类的实例公开为根对象。

在javascript上下文中添加新对象并不困难。定义一个新bean就足够了,如下所示:

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

此示例中的类JavascriptRestClient必须扩展org.alfresco.repo.processor.BaseProcessorExtension。

使用此定义,您将能够使用变量restClient在javascript中访问类JavascriptRestClient的实例。

父bean baseJavaScriptExtension在alfresco上下文文件alfresco / script-services-context.xml中定义为:

<!-- 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>
© www.soinside.com 2019 - 2024. All rights reserved.