My response generator class is :
package com.in2m.servicemix.common.bean;
import java.io.IOException;
import java.io.StringWriter;
import javax.annotation.Resource;
import javax.jbi.messaging.DeliveryChannel;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.NormalizedMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.servicemix.MessageExchangeListener;
import org.apache.servicemix.jbi.jaxp.SourceTransformer;
import org.apache.servicemix.jbi.jaxp.StringSource;
import org.w3c.dom.Document;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import com.thoughtworks.xstream.XStream;
import com.in2m.servicemix.common.errorhandling.ErrorConstants;
/**
* @author pghogale
*
*/
public class ResponseGenerator implements MessageExchangeListener {
@Resource
private DeliveryChannel channel;
private static final Log logger =
LogFactory.getLog(ResponseGenerator.class);
private final static String RESPONSE_MESSAGE_ROOT = "response";
private static final String REQUEST_MESSAGE_ROOT = "request";
public void onMessageExchange(MessageExchange exchange) throws
MessagingException {
try{
if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
NormalizedMessage normalizedMessageIn =
exchange.getMessage("in");
String outMessage =
getResponseToClientFromInMessage(normalizedMessageIn);
NormalizedMessage normalizedMessageOut=
exchange.createMessage();
normalizedMessageOut.setContent(new
StringSource(outMessage));
exchange.setMessage(normalizedMessageOut,"out");
channel.send(exchange);
}
} catch(Exception e){
logger.error(e);
}
}
private String getResponseToClientFromInMessage(NormalizedMessage
normalizedMessage) throws Exception {
// if nothing comes to response generator send system error
if(normalizedMessage.getContent() == null){
return
createResponseMessage(ErrorConstants.SYSTEM_ERROR,"System Error");
}
SourceTransformer sourceTransformer = new SourceTransformer();
Document inDocument =
sourceTransformer.toDOMDocument(normalizedMessage);
inDocument.normalize();
String response=getResponseMessageFromInMessage(inDocument);
return response;
}
private String getResponseMessageFromInMessage(Document inDoc) throws
Exception{
String documentElement=inDoc.getDocumentElement().getNodeName();
//send same message as response
if(documentElement.equals(RESPONSE_MESSAGE_ROOT)){
return createXMLMessageFromDocument(inDoc);
}
//if message goes to jms provider in case of update profile
send succsess
if(documentElement.equals(REQUEST_MESSAGE_ROOT)){
return
createResponseMessage(ErrorConstants.SUCCESS,"success");
}
return null;
}
private static String createResponseMessage(String status, String
message)
{
XStream xstream = new XStream();
xstream.alias(RESPONSE_MESSAGE_ROOT, ResponseMessage.class);
ResponseMessage response = new ResponseMessage();
response.setStatus(status);
response.setMessage(message);
return xstream.toXML(response);
}
private String createXMLMessageFromDocument(Document document) throws
IOException{
OutputFormat format = new OutputFormat (document);
StringWriter stringOut = new StringWriter ();
XMLSerializer serial = new XMLSerializer (stringOut, format);
serial.serialize(document);
String message= stringOut.toString().replace("<?xml
version=\"1.0\"
encoding=\"UTF-8\"?>", "");
return message;
}
}
--
View this message in context:
http://old.nabble.com/An-Operation-working-with-3.2.2-now-does-not-work-with-3.3.1-tp27873544p27873632.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.