使用MULE接收并处理HTTP请求

一:插件安装

Mule插件地址:

mule - http://dist.muleforge.org/mule-ide/updates/3.4/

二:配置mule安装目录

Preferences->Mule->Mule Distributions

三:新建Mule工程

 

 

 

mule-config.xml配置文件

~~

 

com.test.SampleMuleRequestDto

package com.test; public class SampleMuleRequestDto { private String bodyXml; private String clientIp; public String getBodyXml() { return bodyXml; } public void setBodyXml(String bodyXml) { this.bodyXml = bodyXml; } public String getClientIp() { return clientIp; } public void setClientIp(String clientIp) { this.clientIp = clientIp; } }

com.test.SampleMuleRequestTransformer

 package com.test; import java.io.IOException; import java.io.InputStream; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.mule.api.MuleMessage; import org.mule.api.transformer.TransformerException; import org.mule.transformer.AbstractMessageAwareTransformer; import org.mule.transport.http.HttpMessageAdapter; import org.mule.util.IOUtils; public class SampleMuleRequestTransformer extends AbstractMessageAwareTransformer { private static Log log = LogFactory .getLog(SampleMuleRequestTransformer.class); @Override public Object transform(MuleMessage message, String outputEncoding) throws TransformerException { HttpMessageAdapter adapter = (HttpMessageAdapter) message.getAdapter(); String context = ""; try { context = convertRequestToString(adapter.getPayload(), outputEncoding); } catch (IOException e) { log.error("get request error", e); return "get request error"; } String ip = getIp(adapter); SampleMuleRequestDto dto = new SampleMuleRequestDto(); dto.setBodyXml(context); dto.setClientIp(ip); return dto; } private String getIp(HttpMessageAdapter adapter) { String address = (String) adapter .getProperty("MULE_REMOTE_CLIENT_ADDRESS"); String ip = address.substring(1, address.indexOf(":")); return ip; } private String convertRequestToString(Object src, String encoding) throws IOException { String srcString = null; if (src instanceof InputStream) { InputStream input = (InputStream) src; try { srcString = IOUtils.toString(input, encoding); } catch (IOException iox) { throw iox; } finally { IOUtils.closeQuietly(input); } } else { srcString = src.toString(); } return srcString; } }

com.test.SimpleProcessor

package com.test; public class SimpleProcessor { public String process(SampleMuleRequestDto context) { System.out.println(context); String responseXml="test"; return responseXml; } public String error(String message) { return message; } }

你可能感兴趣的:(JAVA)