Search in sources :

Example 1 with Inbound

use of org.glassfish.api.admin.Payload.Inbound 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 Inbound

use of org.glassfish.api.admin.Payload.Inbound 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 Inbound

use of org.glassfish.api.admin.Payload.Inbound 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 Inbound

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

the class CheckpointHelper method loadInbound.

private Inbound loadInbound(File inboundFile) throws IOException {
    if (inboundFile == null || !inboundFile.exists()) {
        return null;
    }
    FileInputStream is = new FileInputStream(inboundFile);
    Inbound inboundSource = PayloadImpl.Inbound.newInstance("application/zip", is);
    return inboundSource;
}
Also used : Inbound(org.glassfish.api.admin.Payload.Inbound) FileInputStream(java.io.FileInputStream)

Example 5 with Inbound

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

the class CheckpointHelper method loadOutbound.

private void loadOutbound(Outbound outbound, File outboundFile) throws IOException {
    if (outbound == null || !outboundFile.exists()) {
        return;
    }
    Inbound outboundSource = loadInbound(outboundFile);
    Iterator<Part> parts = outboundSource.parts();
    File topDir = createTempDir("checkpoint", "");
    topDir.deleteOnExit();
    while (parts.hasNext()) {
        Part part = parts.next();
        File sourceFile = File.createTempFile("source", "", topDir);
        FileUtils.copy(part.getInputStream(), new FileOutputStream(sourceFile), Long.MAX_VALUE);
        outbound.addPart(part.getContentType(), part.getName(), part.getProperties(), new FileInputStream(sourceFile));
    }
    outbound.resetDirty();
}
Also used : Part(org.glassfish.api.admin.Payload.Part) FileOutputStream(java.io.FileOutputStream) Inbound(org.glassfish.api.admin.Payload.Inbound) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

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