如何在骡子连接器中添加下拉列表项? [英] how do i add a drop down list item in a mule connector?

查看:153
本文介绍了如何在骡子连接器中添加下拉列表项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个骡子连接器,一切都很好。我有一个HTTP出站连接的4个主要属性:url,port,path和method。



我希望该方法是一个下拉列表,其值为GET, POST。



你们知道怎么做吗?需要使用什么注释才能解决问题?或者是在添加地图或枚举属性时被Mule认可?



这是我目前的简单代码

  / ** 
*此文件由Mule Development Kit自动生成
* /
package com.web.testcomponent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.mule.api.MuleEventContext;
import org.mule.api.annotations.Configurable;
import org.mule.api.annotations.Module;
import org.mule.api.annotations.Processor;
import org.mule.api.annotations.display.Placement;
import org.mule.api.annotations.expressions.Lookup;
import org.mule.api.annotations.param.Default;
import org.mule.api.annotations.param.Optional;
import org.mule.api.annotations.param.Payload;

/ **
*通用模块
*
* @author MuleSoft,Inc.
* /
@Module(name = testcomponent,schemaVersion =1.0-SNAPSHOT,friendlyName =HTTP / S Component,description =这是自定义HTTP / S组件)
public class TestComponentModule
{

@Lookup(myMuleContext)
private MuleEventContext context;

/ **
*连接URL
* /
@Configurable
@Placement(order = 1,group =Connection,tab =一般)
private String url;

/ **
*连接端口
* /
@Configurable
@Placement(order = 2,group =Connection,tab =一般)
private String port;

/ **
*连接路径
* /
@Configurable
@Placement(order = 3,group =Connection,tab =一般)
private String path;

/ **
*连接方法(GET或POST)
* /
@Configurable
@Default(value =GET)
@Optional
@Placement(order = 4,group =Connection,tab =General)
private String method;

public String getUrl(){
return url;
}

public void setUrl(String url){
this.url = url;
}

public String getPort(){
return port;
}

public void setPort(String port){
this.port = port;
}

public String getPath(){
return path;
}

public void setPath(String path){
this.path = path;
}

public String getMethod(){
return method;
}

public void setMethod(String method){
this.method = method;
}

public MuleEventContext getContext(){
return context;
}

public void setContext(MuleEventContext context){
this.context = context;
}

/ **
*自定义处理器
*
* {@sample.xml ../../../doc/TestComponent -connector.xml.sample testcomponent:test-processor}
*
* @param内容要处理的随机内容
* @param有效负载字符串有效负载本身
* @return一些string
* /
@Processor
public String testProcessor(@Payload String payload)
{
HttpURLConnection conn = null;

try {
URL url = new URL(getUrl()+/+ getPath());
conn =(HttpURLConnection)url.openConnection();
conn.setRequestMethod(getMethod());
conn.setRequestProperty(Accept,application / json);

if(conn.getResponseCode()!= HttpURLConnection.HTTP_OK){
throw new RuntimeException(Failed - > HTTP错误代码:+ conn.getResponseCode());
}

BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
字符串输出,缓冲区;
output =;
while((buffer = br.readLine())!= null){
output + = buffer;
}

payload = output;
conn.disconnect();
} catch(MalformedURLException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}

返回有效负载;
}
}


解决方案

使用枚举时没有问题:

  public enum HttpMethod 
{
GET,POST
};

@Configurable
@Default(value =GET)
@Optional
@Placement(order = 4,group =Connection,tab =General )
private HttpMethod方法;

这是不是也适合你?



作为一个附注,看看你的 @Processor 方法是什么,我想知道你是否不重新发明 @RestCall 处理器。还有为什么使用 HttpURLConnection 可以使用 MuleClient 执行HTTP调用?


I've made a mule connector, everything works great. I got 4 main properties for an HTTP outbound connection: url, port, path and method.

I would like the method to be a drop down list with values: GET, POST.

Do you guys know how to do it? what annotation needs to be used in case that's the way to solve it? Or is it recognized by Mule when adding a Map or Enum property?

This is my current simple code

/**
 * This file was automatically generated by the Mule Development Kit
 */
package com.web.testcomponent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.mule.api.MuleEventContext;
import org.mule.api.annotations.Configurable;
import org.mule.api.annotations.Module;
import org.mule.api.annotations.Processor;
import org.mule.api.annotations.display.Placement;
import org.mule.api.annotations.expressions.Lookup;
import org.mule.api.annotations.param.Default;
import org.mule.api.annotations.param.Optional;
import org.mule.api.annotations.param.Payload;

/**
 * Generic module
 *
 * @author MuleSoft, Inc.
 */
@Module(name="testcomponent", schemaVersion="1.0-SNAPSHOT", friendlyName="HTTP/S Component", description="This is custom HTTP/S Component")
public class TestComponentModule
{

    @Lookup("myMuleContext")
    private MuleEventContext context;

    /**
     * Connection URL
     */
    @Configurable
    @Placement(order=1,group="Connection",tab="General")
    private String url;

    /**
     * Connection Port
     */
    @Configurable
    @Placement(order=2,group="Connection",tab="General")
    private String port;

    /**
     * Connection Path
     */
    @Configurable
    @Placement(order=3,group="Connection",tab="General")
    private String path;    

    /**
     * Connection Method (GET or POST)
     */
    @Configurable
    @Default(value="GET")
    @Optional
    @Placement(order=4,group="Connection",tab="General")
    private String method;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public MuleEventContext getContext() {
        return context;
    }

    public void setContext(MuleEventContext context) {
        this.context = context;
    }

    /**
     * Custom processor
     *
     * {@sample.xml ../../../doc/TestComponent-connector.xml.sample testcomponent:test-processor}
     *
     * @param content Random content to be processed
     * @param payload The String payload itself
     * @return Some string
     */
    @Processor
    public String testProcessor(@Payload String payload)
    {       
        HttpURLConnection conn = null;

        try {
            URL url = new URL(getUrl() + "/" + getPath());
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod(getMethod());
            conn.setRequestProperty("Accept", "application/json");

            if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                throw new RuntimeException("Failed -> HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String output, buffer;
            output = "";
            while((buffer = br.readLine()) != null) {
                output += buffer;
            }

            payload = output;
            conn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return payload;
    }
}

解决方案

I have no problem using an enum:

public enum HttpMethod
{
    GET, POST
};

@Configurable
@Default(value = "GET")
@Optional
@Placement(order = 4, group = "Connection", tab = "General")
private HttpMethod method;

Doesn't this work for you too?

As a side note, looking at what your @Processor method does, I'm wondering if you're not re-inventing @RestCall processors. Also why using a raw HttpURLConnection when you can perform HTTP calls with the MuleClient?

这篇关于如何在骡子连接器中添加下拉列表项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆