Hi John
The problem is that the messagecontext only seems to want xml.
This is because our canonical message format is a SOAP "infoset" (note:
this is not an XML serialization)
My intent is to return non-XML for the HTTP GET. setPayLoadXML for the
script mediators requires XML doesn't it?
Yes, the script mediator expects XML, but the way we carry binary and
text payloads within the SOAP infoset is by using special wrapper
elements ns:binary and ns:text where ns refers to the
http://ws.apache.org/commons/ns/payload namespace. Thus for your
example, you could do the following:
<definitions xmlns="http://ws.apache.org/ns/synapse">
<sequence name="textout">
<drop/>
</sequence>
<sequence name="textin">
<log level="custom">
<property name="request name" expression="//ns:name"
xmlns:ns="http://org.apache.axis2/xsd"/>
</log>
<script language="groovy"><![CDATA[
println mc.getReplyTo();
mc.setTo("http://www.w3.org/2005/08/addressing/anonymous");
mc.setProperty ("RESPONSE", "true");
mc.setPayloadXML('*<ns:text
xmlns:ns="http://ws.apache.org/commons/ns/payload">hello=world</ns:text>*');
]]></script>
<property name="messageType" value="text/plain" scope="axis2-client"/>
<send/>
</sequence>
<proxy name="StockQuoteProxy" transports="http">
<target inSequence="textin" outSequence="textout" />
</proxy>
</definitions>
Since I assume you want the return content type to be "text/plain", I am
setting the "messageType" property to that, and this also triggers the
underlying Axis2 MessageFormatter (that will know how to write
"text/plain" messages) for this content type. However, Axis2 nor Synapse
dis not ship a message formatter for "text/plain" as Axis2 was mainly
dealing with only SOAP, PoX/REST messages. But its easy to write one,
and the code for this is attached to this email. Then I will edit the
repository/conf/axis2.xml from my Synapse installation, and add this
definition as:
<messageFormatter contentType="text/plain"
class="org.apache.synapse.transport.common.PlainTextFormatter"/>
For your convenience, I am attaching a binary patch that includes the
compiled code for the above message formatter, and you can add this to
your lib directory if you do not want to compile the source again.
When I now do a GET request as
"http://localhost:8080/soap/StockQuoteProxy/mediate?name=valuex", I get
the following reply back
[EMAIL PROTECTED]:~/java/synapse-1.1.1/bin$ telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /soap/StockQuoteProxy/mediate?name=valuex HTTP/1.0
HTTP/1.0 200 OK
Content-Type: text/plain; charset=UTF-8
Date: Thu, 28 Feb 2008 08:06:35 GMT
Server: Synapse-HttpComponents-NIO
Connection: Close
hello=world
Let me know if you have any issues getting this working
asankha
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.synapse.transport.common;
import org.apache.axis2.transport.MessageFormatter;
import org.apache.axis2.transport.http.util.URLTemplatingUtil;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.AxisFault;
import org.apache.axiom.om.OMOutputFormat;
import org.apache.axiom.om.OMElement;
import org.apache.synapse.transport.base.BaseConstants;
import java.io.OutputStream;
import java.io.IOException;
import java.net.URL;
public class PlainTextFormatter implements MessageFormatter {
public byte[] getBytes(MessageContext messageContext, OMOutputFormat format) throws AxisFault {
OMElement textElt = messageContext.getEnvelope().getBody().getFirstElement();
if (BaseConstants.DEFAULT_TEXT_WRAPPER.equals(textElt.getQName())) {
return textElt.getText().getBytes();
}
return new byte[0];
}
public void writeTo(MessageContext messageContext, OMOutputFormat format, OutputStream outputStream, boolean preserve) throws AxisFault {
try {
outputStream.write(getBytes(messageContext, format));
} catch (IOException e) {
throw new AxisFault("Error writing text message to stream", e);
}
}
public String getContentType(MessageContext messageContext, OMOutputFormat format, String soapAction) {
String encoding = format.getCharSetEncoding();
String contentType = "text/plain";
if (encoding != null) {
contentType += "; charset=" + encoding;
}
// if soap action is there (can be there is soap response MEP is used) add it.
if ((soapAction != null)
&& !"".equals(soapAction.trim())
&& !"\"\"".equals(soapAction.trim())) {
contentType = contentType + ";action=\"" + soapAction + "\";";
}
return contentType;
}
public URL getTargetAddress(MessageContext msgCtxt, OMOutputFormat format, URL targetURL) throws AxisFault {
// Check whether there is a template in the URL, if so we have to replace then with data
// values and create a new target URL.
targetURL = URLTemplatingUtil.getTemplatedURL(targetURL, msgCtxt, false);
return targetURL;
}
public String formatSOAPAction(MessageContext messageContext, OMOutputFormat format, String soapAction) {
return null;
}
}