wso2来自自定义代码

时间:2016-04-14 16:29:01

标签: rest wso2

我有以下用例,我必须使用属性动态创建一个rest url。为此,我创建了一个自定义Mediator,它读取属性并创建对后端服务的调用。 我遇到了如何将响应发送回用户的问题。它是一种xml格式。但我需要解析xml并发送文本。为此,我使用PayloadFactory。我在这里附上我的代码,有人可以建议我做错了吗?

        <api xmlns="http://ws.apache.org/ns/synapse" name="tririgaProxy" context="/services">
       <resource methods="GET" url-mapping="/employee">
          <inSequence>
             <sequence key="tririgaConf"/>
             <property name="triUser" expression="get-property('triUser')"/>
             <property name="triPass" expression="get-property('triPass')"/>
             <property name="triURL" expression="get-property('triURL')"/>
             <property name="triWfName" expression="get-property('triPeople.database.employee.wfName')"/>
             <class name="com.wso2.tririga.mediator.IncomingMediator"/>
             <payloadFactory media-type="text">
                <format><![CDATA[$1]</format>
                <args>
                   <arg evaluator="xml" expression="/status/text()"/>
                </args>
             </payloadFactory>
          </inSequence>
       </resource>

Java类:

package com.wso2.tririga.mediator;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
import org.apache.synapse.util.PayloadHelper;

public class IncomingMediator extends AbstractMediator {
    private static final Log log = LogFactory.getLog(IncomingMediator.class);

    @Override
    public boolean mediate(MessageContext msgContext) {
        String triUser = String.valueOf(msgContext.getProperty("triUser"));
        String triPass = String.valueOf(msgContext.getProperty("triPass"));
        String triURL = String.valueOf(msgContext.getProperty("triURL"));
        String triWfName = String.valueOf(msgContext.getProperty("triWfName"));

        try {
            URI uri = new URIBuilder(triURL)
                    .addParameter("USERNAME", triUser)
                    .addParameter("PASSWORD", triPass)
                    .addParameter("ioName", triWfName).build();
            log.info("URI: "+uri.toString());
            String response = execute(uri);         
            PayloadHelper.setTextPayload(msgContext, convertToXML(response));           
        } catch (URISyntaxException e) {
            log.error("Error while creating URI", e);           
        }
        return true;
    }

    private static String execute(URI uri) {
        String responseBody = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet get = new HttpGet();
            ((HttpRequestBase) get).setURI(uri);
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
                @Override
                public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        String responseStr = EntityUtils.toString(entity);
                        return "Successful".equalsIgnoreCase(responseStr) ? "RetCode=C;Message=Success" : "RetCode=F;Message=Failed because Itegration Exception";
                    } else {
                        throw new ClientProtocolException("Unexpected response status: " + status);
                    }
                }
            };          
            try {
                responseBody = httpclient.execute(get, responseHandler);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return responseBody;
    }

    private static String convertToXML(String response){
        return"<status>"+response+"</status>";      
    }

}

我从这里得不到任何回复。

1 个答案:

答案 0 :(得分:0)

由于您需要转换响应消息,因此需要在api的 out sequence 中执行有效负载转换。

目前您正在做的是按顺序转换中的消息。