use of java.io.PrintWriter in project camel by apache.
the class PGPDataFormatTest method getStrackTrace.
public static String getStrackTrace(Exception e) throws UnsupportedEncodingException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintWriter w = new PrintWriter(os);
e.printStackTrace(w);
w.close();
String stackTrace = new String(os.toByteArray(), "UTF-8");
return stackTrace;
}
use of java.io.PrintWriter in project camel by apache.
the class ExpressionBuilder method exchangeExceptionStackTraceExpression.
/**
* Returns an expression for an exception stacktrace set on the exchange
*
* @return an expression object which will return the exception stacktrace set on the exchange
*/
public static Expression exchangeExceptionStackTraceExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Exception exception = exchange.getException();
if (exception == null) {
exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
}
if (exception != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
IOHelper.close(pw, sw);
return sw.toString();
} else {
return null;
}
}
@Override
public String toString() {
return "exchangeExceptionStackTrace";
}
};
}
use of java.io.PrintWriter in project camel by apache.
the class DefaultExchangeFormatter method format.
public String format(Exchange exchange) {
Message in = exchange.getIn();
StringBuilder sb = new StringBuilder();
if (showAll || showExchangeId) {
if (multiline) {
sb.append(SEPARATOR);
}
sb.append(style("Id")).append(exchange.getExchangeId());
}
if (showAll || showExchangePattern) {
if (multiline) {
sb.append(SEPARATOR);
}
sb.append(style("ExchangePattern")).append(exchange.getPattern());
}
if (showAll || showProperties) {
if (multiline) {
sb.append(SEPARATOR);
}
sb.append(style("Properties")).append(sortMap(exchange.getProperties()));
}
if (showAll || showHeaders) {
if (multiline) {
sb.append(SEPARATOR);
}
sb.append(style("Headers")).append(sortMap(in.getHeaders()));
}
if (showAll || showBodyType) {
if (multiline) {
sb.append(SEPARATOR);
}
sb.append(style("BodyType")).append(getBodyTypeAsString(in));
}
if (showAll || showBody) {
if (multiline) {
sb.append(SEPARATOR);
}
String body = getBodyAsString(in);
if (skipBodyLineSeparator) {
body = StringHelper.replaceAll(body, LS, "");
}
sb.append(style("Body")).append(body);
}
if (showAll || showException || showCaughtException) {
// try exception on exchange first
Exception exception = exchange.getException();
boolean caught = false;
if ((showAll || showCaughtException) && exception == null) {
// fallback to caught exception
exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
caught = true;
}
if (exception != null) {
if (multiline) {
sb.append(SEPARATOR);
}
if (caught) {
sb.append(style("CaughtExceptionType")).append(exception.getClass().getCanonicalName());
sb.append(style("CaughtExceptionMessage")).append(exception.getMessage());
} else {
sb.append(style("ExceptionType")).append(exception.getClass().getCanonicalName());
sb.append(style("ExceptionMessage")).append(exception.getMessage());
}
if (showAll || showStackTrace) {
StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));
sb.append(style("StackTrace")).append(sw.toString());
}
}
}
if (showAll || showOut) {
if (exchange.hasOut()) {
Message out = exchange.getOut();
if (showAll || showHeaders) {
if (multiline) {
sb.append(SEPARATOR);
}
sb.append(style("OutHeaders")).append(sortMap(out.getHeaders()));
}
if (showAll || showBodyType) {
if (multiline) {
sb.append(SEPARATOR);
}
sb.append(style("OutBodyType")).append(getBodyTypeAsString(out));
}
if (showAll || showBody) {
if (multiline) {
sb.append(SEPARATOR);
}
String body = getBodyAsString(out);
if (skipBodyLineSeparator) {
body = StringHelper.replaceAll(body, LS, "");
}
sb.append(style("OutBody")).append(body);
}
} else {
if (multiline) {
sb.append(SEPARATOR);
}
sb.append(style("Out: null"));
}
}
if (maxChars > 0) {
StringBuilder answer = new StringBuilder();
for (String s : sb.toString().split(SEPARATOR)) {
if (s != null) {
if (s.length() > maxChars) {
s = s.substring(0, maxChars);
answer.append(s).append("...");
} else {
answer.append(s);
}
if (multiline) {
answer.append(LS);
}
}
}
// switch string buffer
sb = answer;
}
if (multiline) {
sb.insert(0, "Exchange[");
sb.append("]");
return sb.toString();
} else {
// get rid of the leading space comma if needed
if (sb.length() > 0 && sb.charAt(0) == ',' && sb.charAt(1) == ' ') {
sb.replace(0, 2, "");
}
sb.insert(0, "Exchange[");
sb.append("]");
return sb.toString();
}
}
use of java.io.PrintWriter in project camel by apache.
the class IOHelperTest method write.
private void write(File file, String text) throws Exception {
PrintWriter out = new PrintWriter(file);
out.print(text);
out.close();
}
use of java.io.PrintWriter in project camel by apache.
the class XmlParserDataFormat method newPrinter.
private XmlNodePrinter newPrinter(OutputStream stream) {
XmlNodePrinter xmlNodePrinter = new XmlNodePrinter(new PrintWriter(stream));
xmlNodePrinter.setNamespaceAware(isNamespaceAware());
return xmlNodePrinter;
}
Aggregations