Search in sources :

Example 1 with Outbound

use of org.glassfish.api.admin.Payload.Outbound in project Payara by payara.

the class PayloadFilesManagerTest method recursiveReplacementTest.

@Test
public void recursiveReplacementTest() throws Exception {
    /*
         * Populate the target directory with a subdirectory containing a file,
         * then replace the subdirectory via a replacement request in a Payload.
         */
    final String DIR = "x/";
    final String FILE_A_PREFIX = DIR;
    final String FILE_A_NAME = "fileA.txt";
    final String FILE_B_PREFIX = DIR;
    final String FILE_B_NAME = "fileB.txt";
    final Set<File> desiredAbsent = new HashSet<File>();
    final Set<File> desiredPresent = new HashSet<File>();
    final File targetDir = File.createTempFile("tgt", "");
    targetDir.delete();
    targetDir.mkdir();
    final File dir = new File(targetDir, DIR);
    dir.mkdir();
    final File fileA = new File(dir, FILE_A_NAME);
    writeFile(fileA, "This is FileA", "with two lines of content");
    final File origDir = File.createTempFile("pfm", "");
    origDir.delete();
    origDir.mkdir();
    final File origSubDir = new File(origDir, DIR);
    origSubDir.mkdirs();
    final File fileB = new File(origSubDir, FILE_B_NAME);
    writeFile(fileB, "This is FileB", "with yet another", "line of content");
    try {
        desiredPresent.add(new File(targetDir.toURI().resolve(FILE_B_PREFIX + FILE_B_NAME)));
        desiredAbsent.add(new File(targetDir.toURI().resolve(FILE_A_PREFIX + FILE_A_NAME)));
        new CommonPermTest() {

            @Override
            protected CommonPermTest init(File targetDir) {
                super.init(targetDir);
                return this;
            }

            @Override
            protected void addParts(Outbound ob, PayloadFilesManager instance) throws Exception {
                ob.requestFileReplacement("application/octet-stream", URI.create(DIR), "test-xfer", null, /* props */
                origSubDir, true);
            }

            @Override
            protected void checkResults(Inbound ib, PayloadFilesManager instance) throws Exception {
                listDir("After creation, before deletion", myTargetDir);
                /*
                     * Process files.
                     */
                instance.processParts(ib);
                listDir("After deletion", myTargetDir);
                final Set<File> unexpectedlyPresent = new HashSet<File>();
                for (File f : desiredAbsent) {
                    if (f.exists()) {
                        unexpectedlyPresent.add(f);
                    }
                }
                assertEquals("Unexpected files remain after replacement request", Collections.EMPTY_SET, unexpectedlyPresent);
                final Set<File> unexpectedlyAbsent = new HashSet<File>();
                for (File f : desiredPresent) {
                    if (!f.exists()) {
                        unexpectedlyAbsent.add(f);
                    }
                }
                assertEquals("Unexpected files absent after replacement request", Collections.EMPTY_SET, unexpectedlyAbsent);
            }

            @Override
            protected void cleanup() {
                for (File f : desiredAbsent) {
                    if (f.exists()) {
                        f.delete();
                    }
                }
                PayloadFilesManagerTest.cleanup(origDir);
            }
        }.init(targetDir).run("replacementTest");
    } finally {
        if (targetDir != null) {
            FileUtils.whack(targetDir);
        }
    }
}
Also used : Outbound(org.glassfish.api.admin.Payload.Outbound) Inbound(org.glassfish.api.admin.Payload.Inbound) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with Outbound

use of org.glassfish.api.admin.Payload.Outbound in project Payara by payara.

the class PayloadFilesManagerTest method simplePermanentTransferAndRemovalTest.

@Test
public void simplePermanentTransferAndRemovalTest() throws Exception {
    final String FILE_A_PREFIX = "";
    final String FILE_A_NAME = "fileA.txt";
    final String FILE_B_PREFIX = "x/y/z/";
    final String FILE_B_NAME = "fileB.txt";
    final String FILE_C_PREFIX = FILE_B_PREFIX;
    final String FILE_C_NAME = "fileC.txt";
    final Set<File> desiredPresent = new HashSet<File>();
    final Set<File> desiredAbsent = new HashSet<File>();
    /*
         * Create a directory into which we'll transfer some small files.
         */
    final File origDir = File.createTempFile("pfm", "");
    origDir.delete();
    File targetDir = null;
    try {
        /*
             * Choose the directory into which we want the PayloadFilesManager to
             * deliver the files.
             */
        targetDir = File.createTempFile("tgt", "");
        targetDir.delete();
        targetDir.mkdir();
        origDir.mkdir();
        final File fileA = new File(origDir, FILE_A_NAME);
        writeFile(fileA, "This is File A", "and another line");
        desiredPresent.add(new File(targetDir.toURI().resolve(FILE_A_PREFIX + FILE_A_NAME)));
        /*
             * In this test result, we want file B to be absent after we transfer
             * it (with files A and C) and then use a second PayloadFilesManager
             * to request B's removal.
             */
        final File fileB = new File(origDir, FILE_B_NAME);
        desiredAbsent.add(new File(targetDir.toURI().resolve(FILE_B_PREFIX + FILE_B_NAME)));
        writeFile(fileB, "Here is File B", "which has an", "additional line");
        final File fileC = new File(origDir, FILE_C_NAME);
        desiredPresent.add(new File(targetDir.toURI().resolve(FILE_C_PREFIX + FILE_C_NAME)));
        writeFile(fileC, "Here is File C", "which has an", "additional line", "even beyond what fileB has");
        new CommonPermTest() {

            @Override
            protected CommonPermTest init(File targetDir) {
                super.init(targetDir);
                return this;
            }

            @Override
            protected void addParts(Outbound ob, PayloadFilesManager instance) throws Exception {
                ob.attachFile("application/octet-stream", URI.create(FILE_A_PREFIX + fileA.getName()), "test-xfer", fileA);
                ob.attachFile("text/plain", URI.create(FILE_B_PREFIX + fileB.getName()), "test-xfer", fileB);
                ob.attachFile("text/plain", URI.create(FILE_C_PREFIX + fileC.getName()), "test-xfer", fileC);
            }

            @Override
            protected void checkResults(Inbound ib, PayloadFilesManager instance) throws Exception {
                /*
                     * Extract files to where we want them.
                     */
                instance.processParts(ib);
                /*
                     * Now ask another PayloadFilesManager to remove one of the
                     * just-extracted files.
                     */
                Payload.Outbound ob = PayloadImpl.Outbound.newInstance();
                ob.requestFileRemoval(URI.create(FILE_B_PREFIX + FILE_B_NAME), "removeTest", /* dataRequestName */
                null);
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ob.writeTo(baos);
                baos.close();
                final PayloadFilesManager remover = new PayloadFilesManager.Perm(instance.getTargetDir(), null, Logger.getAnonymousLogger());
                final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
                Payload.Inbound removerIB = PayloadImpl.Inbound.newInstance("application/zip", bais);
                remover.processParts(removerIB);
                final Set<File> missing = new HashSet<File>();
                for (File f : desiredPresent) {
                    if (!f.exists()) {
                        missing.add(f);
                    }
                }
                assertEquals("Unexpected missing files after extraction", Collections.EMPTY_SET, missing);
                final Set<File> unexpectedlyPresent = new HashSet<File>();
                for (File f : desiredAbsent) {
                    if (f.exists()) {
                        unexpectedlyPresent.add(f);
                    }
                }
                assertEquals("Unexpected files remain after removal request", Collections.EMPTY_SET, unexpectedlyPresent);
            }

            @Override
            protected void cleanup() {
                for (File f : desiredPresent) {
                    f.delete();
                }
                PayloadFilesManagerTest.cleanup(origDir);
            }
        }.init(targetDir).run("simplePermanentTransferAndRemovalTest");
    } finally {
        if (targetDir != null) {
            FileUtils.whack(targetDir);
        }
    }
}
Also used : Inbound(org.glassfish.api.admin.Payload.Inbound) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Inbound(org.glassfish.api.admin.Payload.Inbound) Outbound(org.glassfish.api.admin.Payload.Outbound) Outbound(org.glassfish.api.admin.Payload.Outbound) ByteArrayInputStream(java.io.ByteArrayInputStream) Payload(org.glassfish.api.admin.Payload) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Outbound

use of org.glassfish.api.admin.Payload.Outbound in project Payara by payara.

the class PayloadFilesManagerTest method simplePermanentRecursiveTransferDirOnlyTest.

@Test
public void simplePermanentRecursiveTransferDirOnlyTest() throws Exception {
    final String DIR = "x/";
    final String Y_SUBDIR = "y/";
    final String Z_SUBDIR = "z/";
    final Set<File> desiredResults = new HashSet<File>();
    /*
         * Create a directory into which we'll copy some small files.
         */
    final File origDir = File.createTempFile("pfm", "");
    origDir.delete();
    origDir.mkdir();
    final File dir = new File(origDir, DIR);
    dir.mkdir();
    final File ySubdir = new File(dir, Y_SUBDIR);
    ySubdir.mkdir();
    final File zSubdir = new File(dir, Y_SUBDIR + Z_SUBDIR);
    zSubdir.mkdir();
    File targetDir = null;
    try {
        /*
             * Choose the directory into which we want the PayloadFilesManager to
             * deliver the files.
             */
        targetDir = File.createTempFile("tgt", "");
        targetDir.delete();
        targetDir.mkdir();
        desiredResults.add(new File(targetDir.toURI().resolve(DIR)));
        desiredResults.add(new File(targetDir.toURI().resolve(DIR + Y_SUBDIR)));
        desiredResults.add(new File(targetDir.toURI().resolve(DIR + Y_SUBDIR + Z_SUBDIR)));
        /*
             * Add the original directory recursively.
             */
        new CommonPermTest() {

            @Override
            protected CommonPermTest init(File targetDir) {
                super.init(targetDir);
                return this;
            }

            @Override
            protected void addParts(Outbound ob, PayloadFilesManager instance) throws Exception {
                ob.attachFile("application/octet-stream", URI.create(DIR), "test-xfer", dir, true);
            }

            @Override
            protected void checkResults(Inbound ib, PayloadFilesManager instance) throws Exception {
                /*
                     * Extract files to where we want them.
                     */
                instance.processParts(ib);
                final Set<File> missing = new HashSet<File>();
                for (File f : desiredResults) {
                    if (!f.exists()) {
                        missing.add(f);
                    }
                }
                assertEquals("Unexpected missing files after extraction", Collections.EMPTY_SET, missing);
            }

            @Override
            protected void cleanup() {
                for (File f : desiredResults) {
                    f.delete();
                }
                PayloadFilesManagerTest.cleanup(origDir);
            }
        }.init(targetDir).run("simplePermanentRecursiveTransferDirOnlyTest");
    } finally {
        if (targetDir != null) {
            FileUtils.whack(targetDir);
        }
    }
}
Also used : Outbound(org.glassfish.api.admin.Payload.Outbound) Inbound(org.glassfish.api.admin.Payload.Inbound) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with Outbound

use of org.glassfish.api.admin.Payload.Outbound in project Payara by payara.

the class ProgressPayloadCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    ProgressStatus ps = context.getProgressStatus();
    for (int i = 0; i < 4; i++) {
        doSomeLogic();
        ps.progress(1);
    }
    // Prepare payload
    Outbound out = context.getOutboundPayload();
    StringBuilder msg = new StringBuilder();
    if (down == null || down.isEmpty()) {
        msg.append("No file requested.");
    } else {
        msg.append("You are requesting for ").append(down).append('.').append(StringUtils.EOL);
        File f = new File(down);
        if (!f.exists()) {
            msg.append("But it does not exist!");
        } else {
            try {
                String canonicalPath = f.getCanonicalPath();
                canonicalPath = canonicalPath.replace('\\', '/');
                if (canonicalPath.charAt(1) == ':') {
                    canonicalPath = canonicalPath.substring(2);
                }
                if (f.isDirectory()) {
                    msg.append("It is directory - recursive download");
                    out.attachFile("application/octet-stream", URI.create(canonicalPath), f.getName(), f, true);
                } else {
                    out.attachFile("application/octet-stream", URI.create(canonicalPath), f.getName(), f);
                }
            } catch (IOException ex) {
                report.failure(logger, "Can not append " + f.getAbsolutePath());
            }
        }
    }
    if (report.getActionExitCode() == ActionReport.ExitCode.SUCCESS) {
        report.setMessage(msg.toString());
    }
    // Return
    ps.progress(1);
    ps.complete("Finished");
}
Also used : Outbound(org.glassfish.api.admin.Payload.Outbound) ProgressStatus(org.glassfish.api.admin.ProgressStatus) IOException(java.io.IOException) ActionReport(org.glassfish.api.ActionReport) File(java.io.File)

Example 5 with Outbound

use of org.glassfish.api.admin.Payload.Outbound in project Payara by payara.

the class CheckpointHelper method save.

public void save(JobManager.Checkpoint checkpoint) throws IOException {
    CheckpointFilename cf = CheckpointFilename.createBasic(checkpoint.getJob());
    ObjectOutputStream oos = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(cf.getFile());
        oos = factory.createObjectOutputStream(fos);
        oos.writeObject(checkpoint);
        oos.close();
        Outbound outboundPayload = checkpoint.getContext().getOutboundPayload();
        if (outboundPayload != null && outboundPayload.isDirty()) {
            saveOutbound(outboundPayload, cf.getForPayload(false).getFile());
        }
        Inbound inboundPayload = checkpoint.getContext().getInboundPayload();
        if (inboundPayload != null) {
            saveInbound(inboundPayload, cf.getForPayload(true).getFile());
        }
    } catch (IOException e) {
        try {
            oos.close();
        } catch (Exception ex) {
        }
        try {
            fos.close();
        } catch (Exception ex) {
        }
        File file = cf.getFile();
        if (file.exists()) {
            file.delete();
            ;
        }
        file = cf.getForPayload(true).getFile();
        if (file.exists()) {
            file.delete();
        }
        file = cf.getForPayload(false).getFile();
        if (file.exists()) {
            file.delete();
        }
        throw e;
    }
}
Also used : Outbound(org.glassfish.api.admin.Payload.Outbound) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Inbound(org.glassfish.api.admin.Payload.Inbound) File(java.io.File) LoginException(javax.security.auth.login.LoginException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

Outbound (org.glassfish.api.admin.Payload.Outbound)11 File (java.io.File)10 Inbound (org.glassfish.api.admin.Payload.Inbound)9 Test (org.junit.Test)8 HashSet (java.util.HashSet)7 IOException (java.io.IOException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ActionReport (org.glassfish.api.ActionReport)2 Payload (org.glassfish.api.admin.Payload)2 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 LoginException (javax.security.auth.login.LoginException)1 PayloadImpl (org.glassfish.admin.payload.PayloadImpl)1