Search in sources :

Example 1 with PrintWriter

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;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) PrintWriter(java.io.PrintWriter)

Example 2 with PrintWriter

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";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) StringWriter(java.io.StringWriter) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) NoSuchLanguageException(org.apache.camel.NoSuchLanguageException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter) PrintWriter(java.io.PrintWriter)

Example 3 with PrintWriter

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();
    }
}
Also used : Message(org.apache.camel.Message) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 4 with PrintWriter

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();
}
Also used : PrintWriter(java.io.PrintWriter)

Example 5 with PrintWriter

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;
}
Also used : XmlNodePrinter(groovy.util.XmlNodePrinter) PrintWriter(java.io.PrintWriter)

Aggregations

PrintWriter (java.io.PrintWriter)3529 StringWriter (java.io.StringWriter)1062 IOException (java.io.IOException)653 File (java.io.File)532 Test (org.junit.Test)432 FileOutputStream (java.io.FileOutputStream)293 FileWriter (java.io.FileWriter)274 OutputStreamWriter (java.io.OutputStreamWriter)255 BufferedReader (java.io.BufferedReader)180 ArrayList (java.util.ArrayList)171 HttpServletResponse (javax.servlet.http.HttpServletResponse)141 ByteArrayOutputStream (java.io.ByteArrayOutputStream)139 FastPrintWriter (com.android.internal.util.FastPrintWriter)124 InputStreamReader (java.io.InputStreamReader)123 HttpServletRequest (javax.servlet.http.HttpServletRequest)121 Date (java.util.Date)120 HashMap (java.util.HashMap)113 Map (java.util.Map)106 BufferedWriter (java.io.BufferedWriter)105 Writer (java.io.Writer)87