简体   繁体   English

无法在 class mediator 和 xml wso2 apim 3.2.0 中设置自定义响应码

[英]Unable to set the custom response code in class mediator and xml wso2 apim 3.2.0

I have used a java class mediator in the custom sequence(xml) where I process the request payload and check whether the user is valid or not.我在自定义序列 (xml) 中使用了 java class 调解器,我在其中处理请求负载并检查用户是否有效。

If the user is valid, the request will be sent to the backend.如果用户有效,请求将被发送到后端。

But, if the user is invalid, the request should not be sent to the backend and the response code needs to be set via java class mediator or the custom sequence(xml).但是,如果用户无效,则不应将请求发送到后端,并且需要通过 java class 中介或自定义序列(xml)设置响应代码。 Currently I am receiving 202 Accepted I need to override the status code to 401.目前我收到 202 Accepted 我需要将状态代码覆盖为 401。

In the java class mediator, I have used the below lines for setting the property.在 java class 调解器中,我使用以下行来设置属性。

org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) mc).getAxis2MessageContext();
msgContext.setProperty("HTTP_SC", "401"); 
//msgContext.setProperty("HTTP_SC", 401); (tried without quotes 401 too!!)
//mc.setProperty("HTTP_SC", 401);
((Axis2MessageContext) mc).setAxis2MessageContext(msgContext);

I tried via custom sequence(xml) using below lines,我尝试使用以下几行通过自定义序列(xml),

<property action="remove" name="HTTP_SC" scope="axis2"/>
<property name="SC_ACCEPTED" scope="axis2" value="false"/>
<property name="HTTP_SC" scope="axis2" value="401"/>
<send/>

In both cases the 401 is not returned, 202 is only returned..在这两种情况下,都不会返回 401,只会返回 202。

Suggest a way to to do it either in the class mediator or in the custom sequence.建议在 class 调解器或自定义序列中执行此操作的方法。

You can try the following mediation您可以尝试以下调解

<api xmlns="http://ws.apache.org/ns/synapse" name="test" context="/test">
    <resource methods="GET">
        <inSequence>
            <property name="HTTP_SC" scope="axis2" value="409"/>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>
curl -is http://localhost:8280/test

HTTP/1.1 401 Unauthorized

You can test it or play with it using the following Dockerfile您可以使用以下 Dockerfile 对其进行测试或使用

FROM wso2/wso2am:3.2.0

COPY test.xml ${WSO2_SERVER_HOME}/repository/deployment/server/synapse-configs/default/api/test.xml

EXPOSE 8280
docker build -t test .
docker run --rm --name test -it -p 8280:8280 test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM