Search in sources :

Example 56 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project hudson-2.x by hudson.

the class AnnotatedLargeText method writeHtmlTo.

public long writeHtmlTo(long start, Writer w) throws IOException {
    ConsoleAnnotationOutputStream caw = new ConsoleAnnotationOutputStream(w, createAnnotator(Stapler.getCurrentRequest()), context, charset);
    long r = super.writeLogTo(start, caw);
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Cipher sym = Secret.getCipher("AES");
        sym.init(Cipher.ENCRYPT_MODE, Hudson.getInstance().getSecretKeyAsAES128());
        ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(new CipherOutputStream(baos, sym)));
        // send timestamp to prevent a replay attack
        oos.writeLong(System.currentTimeMillis());
        oos.writeObject(caw.getConsoleAnnotator());
        oos.close();
        StaplerResponse rsp = Stapler.getCurrentResponse();
        if (rsp != null)
            rsp.setHeader("X-ConsoleAnnotator", new String(Base64.encode(baos.toByteArray())));
    } catch (GeneralSecurityException e) {
        throw new IOException2(e);
    }
    return r;
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) GeneralSecurityException(java.security.GeneralSecurityException) StaplerResponse(org.kohsuke.stapler.StaplerResponse) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Cipher(javax.crypto.Cipher) ObjectOutputStream(java.io.ObjectOutputStream) IOException2(hudson.util.IOException2)

Example 57 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project hudson-2.x by hudson.

the class WindowsServiceLifecycle method restart.

@Override
public void restart() throws IOException, InterruptedException {
    File me = getHudsonWar();
    File home = me.getParentFile();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StreamTaskListener task = new StreamTaskListener(baos);
    task.getLogger().println("Restarting a service");
    int r = new LocalLauncher(task).launch().cmds(new File(home, "hudson.exe"), "restart").stdout(task).pwd(home).join();
    if (r != 0)
        throw new IOException(baos.toString());
}
Also used : LocalLauncher(hudson.Launcher.LocalLauncher) StreamTaskListener(hudson.util.StreamTaskListener) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 58 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project hudson-2.x by hudson.

the class WindowsSlaveInstaller method actionPerformed.

/**
     * Called when the install menu is selected
     */
public void actionPerformed(ActionEvent e) {
    try {
        int r = JOptionPane.showConfirmDialog(dialog, Messages.WindowsSlaveInstaller_ConfirmInstallation(), Messages.WindowsInstallerLink_DisplayName(), OK_CANCEL_OPTION);
        if (r != JOptionPane.OK_OPTION)
            return;
        if (!DotNet.isInstalled(2, 0)) {
            JOptionPane.showMessageDialog(dialog, Messages.WindowsSlaveInstaller_DotNetRequired(), Messages.WindowsInstallerLink_DisplayName(), ERROR_MESSAGE);
            return;
        }
        final File dir = new File(rootDir);
        if (!dir.exists()) {
            JOptionPane.showMessageDialog(dialog, Messages.WindowsSlaveInstaller_RootFsDoesntExist(rootDir), Messages.WindowsInstallerLink_DisplayName(), ERROR_MESSAGE);
            return;
        }
        final File slaveExe = new File(dir, "hudson-slave.exe");
        FileUtils.copyURLToFile(getClass().getResource("/windows-service/hudson.exe"), slaveExe);
        // write out the descriptor
        URL jnlp = new URL(engine.getHudsonUrl(), "computer/" + Util.rawEncode(engine.slaveName) + "/slave-agent.jnlp");
        String xml = generateSlaveXml(generateServiceId(rootDir), System.getProperty("java.home") + "\\bin\\java.exe", "-jnlpUrl " + jnlp.toExternalForm());
        FileUtils.writeStringToFile(new File(dir, "hudson-slave.xml"), xml, "UTF-8");
        // copy slave.jar
        URL slaveJar = new URL(engine.getHudsonUrl(), "jnlpJars/remoting.jar");
        File dstSlaveJar = new File(dir, "slave.jar").getCanonicalFile();
        if (// perhaps slave.jar is already there?
        !dstSlaveJar.exists())
            FileUtils.copyURLToFile(slaveJar, dstSlaveJar);
        // install as a service
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        StreamTaskListener task = new StreamTaskListener(baos);
        r = runElevated(slaveExe, "install", task, dir);
        if (r != 0) {
            JOptionPane.showMessageDialog(dialog, baos.toString(), "Error", ERROR_MESSAGE);
            return;
        }
        r = JOptionPane.showConfirmDialog(dialog, Messages.WindowsSlaveInstaller_InstallationSuccessful(), Messages.WindowsInstallerLink_DisplayName(), OK_CANCEL_OPTION);
        if (r != JOptionPane.OK_OPTION)
            return;
        // let the service start after we close our connection, to avoid conflicts
        Runtime.getRuntime().addShutdownHook(new Thread("service starter") {

            public void run() {
                try {
                    StreamTaskListener task = StreamTaskListener.fromStdout();
                    int r = runElevated(slaveExe, "start", task, dir);
                    task.getLogger().println(r == 0 ? "Successfully started" : "start service failed. Exit code=" + r);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        System.exit(0);
    } catch (Exception t) {
        // this runs as a JNLP app, so if we let an exeption go, we'll never find out why it failed 
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw));
        JOptionPane.showMessageDialog(dialog, sw.toString(), "Error", ERROR_MESSAGE);
    }
}
Also used : StringWriter(java.io.StringWriter) StreamTaskListener(hudson.util.StreamTaskListener) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 59 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project flink by apache.

the class NFATest method testNFASerialization.

@Test
public void testNFASerialization() throws IOException, ClassNotFoundException {
    NFA<Event> nfa = new NFA<>(Event.createTypeSerializer(), 0, false);
    State<Event> startingState = new State<>("", State.StateType.Start);
    State<Event> startState = new State<>("start", State.StateType.Normal);
    State<Event> endState = new State<>("end", State.StateType.Final);
    StateTransition<Event> starting2Start = new StateTransition<>(StateTransitionAction.TAKE, startState, new NameFilter("start"));
    StateTransition<Event> start2End = new StateTransition<>(StateTransitionAction.TAKE, endState, new NameFilter("end"));
    StateTransition<Event> start2Start = new StateTransition<>(StateTransitionAction.IGNORE, startState, null);
    startingState.addStateTransition(starting2Start);
    startState.addStateTransition(start2End);
    startState.addStateTransition(start2Start);
    nfa.addState(startingState);
    nfa.addState(startState);
    nfa.addState(endState);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(nfa);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    @SuppressWarnings("unchecked") NFA<Event> copy = (NFA<Event>) ois.readObject();
    assertEquals(nfa, copy);
}
Also used : ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Event(org.apache.flink.cep.Event) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 60 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project cassandra by apache.

the class MultiResultLoggerTest method delegatesPrintfToAdditionalPrintStreams.

@Test
public void delegatesPrintfToAdditionalPrintStreams() throws Exception {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    PrintStream additionalPrintStream = new PrintStream(output, true);
    MultiResultLogger underTest = new MultiResultLogger(new PrintStream(NOOP));
    underTest.addStream(additionalPrintStream);
    underTest.printf("%s %s %s", "one", "two", "three");
    assertEquals("one two three", output.toString());
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)92 Test (org.junit.Test)36 DataOutputStream (java.io.DataOutputStream)15 IOException (java.io.IOException)14 HashSet (java.util.HashSet)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ArrayList (java.util.ArrayList)12 Configuration (org.apache.hadoop.conf.Configuration)12 PrintStream (java.io.PrintStream)10 SparkConf (org.apache.spark.SparkConf)10 Edge (uk.gov.gchq.gaffer.data.element.Edge)10 Element (uk.gov.gchq.gaffer.data.element.Element)10 Entity (uk.gov.gchq.gaffer.data.element.Entity)10 Graph (uk.gov.gchq.gaffer.graph.Graph)10 User (uk.gov.gchq.gaffer.user.User)10 File (java.io.File)9 HashMap (java.util.HashMap)8 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)6 JavaSparkContext (org.apache.spark.api.java.JavaSparkContext)6