2013-07-04 71 views
0

我做了一個騾子連接器,一切都很好。我有4個HTTP出站連接的主要屬性:url,端口,路徑和方法。如何在mule連接器中添加下拉列表項?

我想方法是一個下拉列表值:GET,POST。

你們知道該怎麼做嗎?如果要解決這個問題,需要使用哪些註釋?或者在添加Map或Enum屬性時由Mule識別?

這是我目前簡單的代碼

/** 
* 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; 
    } 
} 
+0

一個枚舉應該這樣做。 –

+0

嘗試創建一個枚舉,但像Configurable,Placement,Default和Optional這樣的註釋表示它無法添加到枚舉中。我真的需要這些。我該如何解決這個問題? – msqar

+0

而且,我將如何從JAVA中的處理器方法中獲得「選定的值」? – msqar

回答

3

我用枚舉沒有問題:

public enum HttpMethod 
{ 
    GET, POST 
}; 

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

難道這不是對你的工作嗎?

作爲一個便箋,看看你的@Processor方法做什麼,我想知道你是不是在重新發明@RestCall處理器。此外,爲什麼在使用MuleClient執行HTTP調用時使用原始的HttpURLConnection

+0

實際上有意義的LOL。我做錯了。我只是爲了實踐目的而這樣做。 :)這是我的第一個組件,並且想要製作一個簡單的HTTP/HTTPS組件,這僅僅是開始^ _ ^感謝您的幫助!這絕對是正確的答案! – msqar

+0

絕對! :) 再次感謝! – msqar

+0

嘿大衛另一個問題,我如何獲得處理器方法中選定的屬性?嘗試執行getProtocol()。toString()。toLowerCase()(該協議是另一個帶有值的枚舉:HTTP和HTTPS)想要接收「http」值。但它不工作:(我怎麼得到它? – msqar

相關問題