如何从ballerinalang进行此重定向?

时间:2017-11-22 13:08:58

标签: redirect ballerina

我在ballerinalang中实现了一个名为https://localhost:9090/isValidUser的REST端点。这是我的代码

import ballerina.net.http;

@http:configuration {
    basePath:"/",
    httpsPort:9090,
    keyStoreFile:"${ballerina.home}/bre/security/wso2carbon.jks",
    keyStorePassword:"wso2carbon",
    certPassword:"wso2carbon",
    trustStoreFile:"${ballerina.home}/bre/security/client-truststore.jks",
    trustStorePassword:"wso2carbon"
}
service<http> authentication {
    @http:resourceConfig {
        methods:["POST"],
        path:"/isValidUser"
    }

    resource isValidUser (http:Request req, http:Response res) {
        println(req.getHeaders());
        res.send();

    }
}

现在我需要做的是当我从浏览器调用该URL时,我需要在我的服务中进行一些验证后将用户重定向到另一个名为https://localhost:3000的URL。

那么如何从ballerinalang进行此重定向?

2 个答案:

答案 0 :(得分:1)

芭蕾舞女演员提供了顺畅的API来进行重定向。请检查以下代码,其中详细说明了Listener端点重定向。

service<http:Service> redirect1 bind {port:9090} {
    @http:ResourceConfig {
        methods:["GET"],
        path:"/"
    }
    redirect1 (endpoint client, http:Request req) {
        http:Response res = new;
        _ = client -> redirect(res, http:REDIRECT_TEMPORARY_REDIRECT_307,
            ["http://localhost:9093/redirect2"]);
    }
}

Ballerina Redirects

中提供了完整的示例

答案 1 :(得分:0)

在芭蕾舞女演员中,您需要通过设置必要的标题和状态代码来自行处理重定向。以下示例是如何在Ballerina中重定向的简单演示。 (注意:我在芭蕾舞女演员试过这个0.95.2)

import ballerina.net.http;

@http:configuration {basePath:"/hello"}
service<http> helloWorld {

    @http:resourceConfig {
        methods:["GET"],
        path:"/"
    }
    resource sayHello (http:Request request, http:Response response) {
        map qParams = request.getQueryParams();
        var name, _ = (string)qParams.name;

        if (isExistingUser(name)) {
            response.setStringPayload("Hello Ballerina!");
        } else {
            response.setHeader("Location", "http://localhost:9090/hello/newUser");
            response.setStatusCode(302);
        }

        _ = response.send();
    }

    @http:resourceConfig {path:"/newUser"}
    resource newUser (http:Request request, http:Response response) {
        string msg = "New to Ballerina? Welcome!";
        response.setStringPayload(msg);
        _ = response.send();
    }
}

function isExistingUser (string name) (boolean) {
    return name == "Ballerina";
}