Search in sources :

Example 6 with PrintWriter

use of java.io.PrintWriter in project camel by apache.

the class BasicValidationHandler method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    baseRequest.setHandled(true);
    if (expectedMethod != null && !expectedMethod.equals(request.getMethod())) {
        response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }
    if (!validateQuery(request)) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    if (expectedContent != null) {
        StringBuilder content = new StringBuilder();
        String line = null;
        while ((line = request.getReader().readLine()) != null) {
            content.append(line);
        }
        if (!expectedContent.equals(content.toString())) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
    }
    response.setStatus(HttpServletResponse.SC_OK);
    if (responseContent != null) {
        response.setContentType("text/plain; charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(responseContent);
    }
}
Also used : PrintWriter(java.io.PrintWriter)

Example 7 with PrintWriter

use of java.io.PrintWriter in project camel by apache.

the class NoClassDefFoundErrorWrapExceptionTest method testNoClassDef.

@Test
public void testNoClassDef() throws Exception {
    try {
        template.requestBody("activemq:start?transferException=true", "Hello World");
        fail("Should throw exception");
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        String s = sw.toString();
        assertTrue(s.contains("java.lang.LinkageError"));
        assertTrue(s.contains("Cannot do this"));
        assertTrue(s.contains("org.apache.camel.component.jms.issues.ProcessorFail.process"));
    }
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 8 with PrintWriter

use of java.io.PrintWriter in project groovy by apache.

the class XmlTemplateEngine method createTemplate.

public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException {
    Node root;
    try {
        root = xmlParser.parse(reader);
    } catch (SAXException e) {
        throw new RuntimeException("Parsing XML source failed.", e);
    }
    if (root == null) {
        throw new IOException("Parsing XML source failed: root node is null.");
    }
    StringWriter writer = new StringWriter(1024);
    writer.write("/* Generated by XmlTemplateEngine */\n");
    new GspPrinter(new PrintWriter(writer), indentation).print(root);
    Script script;
    try {
        script = groovyShell.parse(writer.toString(), "XmlTemplateScript" + counter++ + ".groovy");
    } catch (Exception e) {
        throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
    }
    return new XmlTemplate(script);
}
Also used : Script(groovy.lang.Script) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) StringWriter(java.io.StringWriter) Node(groovy.util.Node) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SAXException(org.xml.sax.SAXException) PrintWriter(java.io.PrintWriter)

Example 9 with PrintWriter

use of java.io.PrintWriter in project groovy by apache.

the class DomToGroovy method main.

public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: DomToGroovy infilename [outfilename]");
        System.exit(1);
    }
    Document document = null;
    try {
        document = parse(args[0]);
    } catch (Exception e) {
        System.out.println("Unable to parse input file '" + args[0] + "': " + e.getMessage());
        System.exit(1);
    }
    PrintWriter writer = null;
    if (args.length < 2) {
        writer = new PrintWriter(System.out);
    } else {
        try {
            writer = new PrintWriter(new FileWriter(new File(args[1])));
        } catch (IOException e) {
            System.out.println("Unable to create output file '" + args[1] + "': " + e.getMessage());
            System.exit(1);
        }
    }
    DomToGroovy converter = new DomToGroovy(writer);
    converter.out.incrementIndent();
    writer.println("#!/bin/groovy");
    writer.println();
    writer.println("// generated from " + args[0]);
    writer.println("System.out << new groovy.xml.StreamingMarkupBuilder().bind {");
    converter.print(document);
    writer.println("}");
    writer.close();
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 10 with PrintWriter

use of java.io.PrintWriter in project flink by apache.

the class BootstrapTools method writeConfiguration.

/**
	 * Writes a Flink YAML config file from a Flink Configuration object.
	 * @param cfg The Flink config
	 * @param file The File to write to
	 * @throws IOException
	 */
public static void writeConfiguration(Configuration cfg, File file) throws IOException {
    try (FileWriter fwrt = new FileWriter(file);
        PrintWriter out = new PrintWriter(fwrt)) {
        for (String key : cfg.keySet()) {
            String value = cfg.getString(key, null);
            out.print(key);
            out.print(": ");
            out.println(value);
        }
    }
}
Also used : FileWriter(java.io.FileWriter) 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