Created
April 2, 2012 08:10
-
-
Save yamanyar/2281538 to your computer and use it in GitHub Desktop.
Transformer Örneği
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yamanyar.si.transformers.DomSourceToStringNoErrorTransformer; | |
import org.apache.log4j.Logger; | |
import org.springframework.integration.Message; | |
import org.springframework.integration.support.MessageBuilder; | |
import org.springframework.integration.transformer.Transformer; | |
import javax.xml.transform.OutputKeys; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.dom.DOMSource; | |
import javax.xml.transform.stream.StreamResult; | |
import java.io.StringWriter; | |
/** | |
* This transformer changes and DomSource payload to a message with string representation. | |
* The reason it has "NoError" in it's name is; it never throws an error. If an error occurs, this only logged | |
* and toString() of original payload is used. It's assumed that the receiver/sender will handle the situation. | |
* <p/> | |
* This transformer is designed to be used with transforming DomSource messages to String Messages for LOGGING. That's why error handling is such simple. | |
* | |
* @author Kaan Yamanyar | |
*/ | |
public class DomSourceToStringNoErrorTransformer implements Transformer { | |
private static final Logger logger = Logger.getLogger(DomSourceToStringNoErrorTransformer.class); | |
public Message<?> transform(Message<?> message) { | |
Object messagePayloadObject = message.getPayload(); | |
try { | |
DOMSource payload = (DOMSource) messagePayloadObject; | |
javax.xml.transform.Transformer transformer = TransformerFactory.newInstance().newTransformer(); | |
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | |
StreamResult result = new StreamResult(new StringWriter()); | |
transformer.transform(payload, result); | |
String payloadAsString = result.getWriter().toString(); | |
message = MessageBuilder.withPayload(payloadAsString) | |
.copyHeaders(message.getHeaders()) | |
.build(); | |
} catch (Exception e) { | |
//is there payload? is it null? (with si 2.1 it's impossible) | |
//if it is an other object rather than what we are expecting; maybe toString method is badly written; cover it: | |
String payload; | |
try { | |
payload = (messagePayloadObject != null) ? messagePayloadObject.toString() : "Empty Payload (null)!"; | |
} catch (Exception toStringError) { | |
logger.error("A payload with a badly written toString()", toStringError); | |
//still ignore; this transformation is only for logging | |
payload = "Payload with bad toString()"; | |
} | |
message = MessageBuilder.withPayload(payload) | |
.copyHeaders(message.getHeaders()) | |
.build(); | |
//this should not be reached; it it is there is an design problem with the flow: | |
logger.error("An error occurred while transforming DomSource to String. Due to nature of this transformer; this error will be ignored.", e); | |
} | |
return message; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment