实战mule(2)

3、http provider

首先把mule源码中的一个transform文件复制过来并编译到classes下面
HttpRequestToString.java  :

package org.mule.samples.hello;

import org.mule.config.i18n.Message;
import org.mule.transformers.AbstractTransformer;
import org.mule.umo.transformer.TransformerException;

import java.io.UnsupportedEncodingException;

/**
 * <code>NameStringToChatString</code> This is test class only for use with the Hello world
 * test application.
 *
 * @author <a href="[email protected]">Ross Mason</a>
 * @version $Revision: 1.6 $
 */
public class HttpRequestToString extends AbstractTransformer
{
    /**
     *
     */
    public HttpRequestToString()
    {
        super();
        this.registerSourceType(String.class);
        this.registerSourceType(byte[].class);
    }

    /* (non-Javadoc)
     * @see org.mule.transformers.AbstractTransformer#doTransform(java.lang.Object)
     */
    public Object doTransform(Object src, String encoding) throws TransformerException
    {
        String param = null;
        if (src instanceof byte[]) {
          if (encoding != null) {
            try {  
              param = new String((byte[]) src, encoding);
            } catch (UnsupportedEncodingException ex){
                param = new String((byte[]) src);
            }
           } else {
              param = new String((byte[]) src);
           }
        } else {
            param = src.toString();
        }
        int equals = param.indexOf("=");
        if(equals > -1)
        {
            return param.substring(equals + 1);
        } else {
            throw new TransformerException(Message.createStaticMessage("Failed to parse param string: " + param), this);
        }
    }
}

在conf下编写相应的配置文件 test-http-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mule-configuration PUBLIC "-//SymphonySoft //DTD mule-configuration XML V1.0//EN"
                                "http://www.symphonysoft.com/dtds/mule/mule-configuration.dtd">

<mule-configuration id="file" version="1.0">
    <description>
        本测试由刘玉军完整测试,如果有问题请访问http://blog.csdn.net/lyj_china
    </description>
    <mule-environment-properties synchronous="true" serverUrl=""/>
    <transformers>
        <transformer name="HttpRequestToString" className="org.mule.samples.hello.HttpRequestToString"/>
    </transformers>
    <model name="test http">
        <mule-descriptor name="Service2" implementation="org.lyj.mule.Service">
            <inbound-router>
                <endpoint address="http://localhost:4567" transformers="HttpRequestToString">
                    <properties>
                        <property name="Content-Type" value="text/plain"/>
                    </properties>
                </endpoint>
            </inbound-router>
<!--
            <outbound-router>
                <router className="org.mule.routing.outbound.FilteringOutboundRouter">
                    <endpoint address="stream://System.out">
                    </endpoint>
                </router>
            </outbound-router>
-->
        </mule-descriptor>
    </model>
</mule-configuration>

在bin目录下创建testhttp.bat文件,内容如下
@echo off
REM There is no need to call this if you set the MULE_HOME in your environment properties
if "%MULE_HOME%" == "" SET MULE_HOME=..\..

REM Set your application specific classpath like this
SET CLASSPATH=%MULE_HOME%\testmule\conf;%MULE_HOME%\testmule\classes;

call %MULE_HOME%\bin\mule.bat -config ../conf/test-http-config.xml

SET MULE_MAIN=
SET CLASSPATH=

然后执行testhttp.bat,正常启动mule后,就可以看到效果了
通过 http://localhost:4567/name=success就可以看到页面上打印出success,mule后台打印出success;
如果把<mule-environment-properties synchronous="true" serverUrl=""/>去掉,就会发现页面是空白的,仅在后台打印出信息.

<!--EndFragment-->

你可能感兴趣的:(xml,.net,Blog)