Search in sources :

Example 6 with Outbound

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

the class GetPayloadCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport ar = context.getActionReport();
    Job job = registry.get(jobID);
    if (job == null) {
        ar.setActionExitCode(ActionReport.ExitCode.FAILURE);
        ar.setMessage(strings.getLocalString("getPayload.wrong.commandinstance.id", "Command instance {0} does not exist.", jobID));
        return;
    }
    Outbound jobPayload = job.getPayload();
    if (jobPayload == null) {
        ar.setMessage(strings.getLocalString("getPayload.nopayload", "Outbound payload does not exist."));
        // Just return. This is OK.
        return;
    }
    Outbound paylaod = context.getOutboundPayload();
    if ((paylaod instanceof PayloadImpl.Outbound) && (jobPayload instanceof PayloadImpl.Outbound)) {
        PayloadImpl.Outbound destination = (PayloadImpl.Outbound) paylaod;
        PayloadImpl.Outbound source = (PayloadImpl.Outbound) jobPayload;
        destination.getParts().addAll(source.getParts());
    } else {
        ar.setActionExitCode(ActionReport.ExitCode.FAILURE);
        ar.setMessage(strings.getLocalString("getPayload.unsupported", "Payload type is not supported. Can not download data."));
    }
}
Also used : Outbound(org.glassfish.api.admin.Payload.Outbound) ActionReport(org.glassfish.api.ActionReport) PayloadImpl(org.glassfish.admin.payload.PayloadImpl)

Example 7 with Outbound

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

the class PayloadFilesManagerTest method simplePermanentTransferDirTest.

@Test
public void simplePermanentTransferDirTest() throws Exception {
    final String DIR = "x/y/z/";
    final String FILE_PREFIX = "x/y/z/";
    final String FILE_NAME = "fileB.txt";
    final Set<File> desiredResults = 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();
        /*
             * Add the directory first, then add a file in the directory.  That
             * will let us check to make sure the PayloadFileManager set the
             * lastModified time on the directory correctly.
             */
        final URI dirURI = URI.create(DIR);
        final File dir = new File(origDir, DIR);
        dir.mkdirs();
        desiredResults.add(dir);
        final File file = new File(dir, FILE_NAME);
        desiredResults.add(new File(targetDir.toURI().resolve(FILE_PREFIX + FILE_NAME)));
        writeFile(file, "Here is the File", "which has an", "additional line");
        final long dirCreationTime = dir.lastModified();
        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);
                ob.attachFile("text/plain", URI.create(FILE_PREFIX + file.getName()), "test-xfer", file);
            }

            @Override
            protected void checkResults(Inbound ib, PayloadFilesManager instance) throws Exception {
                /*
                     * Extract files to where we want them.
                     */
                instance.processParts(ib);
                final URI extractedDirURI = myTargetDir.toURI().resolve(dirURI);
                final File extractedDir = new File(extractedDirURI);
                final long extractedLastModified = extractedDir.lastModified();
                assertEquals("Directory lastModified mismatch after extraction", dirCreationTime, extractedLastModified);
            }

            @Override
            protected void cleanup() {
                for (File f : desiredResults) {
                    f.delete();
                }
                PayloadFilesManagerTest.cleanup(origDir);
            }
        }.init(targetDir).run("simplePermanentTransferDirTest");
    } 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) URI(java.net.URI) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with Outbound

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

the class PayloadFilesManagerTest method simplePermanentDirWithNoSlashRemovalTest.

@Test
public void simplePermanentDirWithNoSlashRemovalTest() throws Exception {
    final String DIR = "x/";
    final String DIR_WITH_NO_SLASH = "x";
    final String FILE_A_PREFIX = DIR;
    final String FILE_A_NAME = "fileA.txt";
    final Set<File> desiredAbsent = 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 fileA = new File(dir, FILE_A_NAME);
    writeFile(fileA, "This is FileA", "with two lines of content");
    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();
        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.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);
                // listDir("After creation, before deletion", myTargetDir);
                /*
                     * Now ask another PayloadFilesManager to remove a directory
                     * recursively.
                     */
                Payload.Outbound ob = PayloadImpl.Outbound.newInstance();
                ob.requestFileRemoval(URI.create(DIR_WITH_NO_SLASH), "removeTest", /* dataRequestName */
                null, /* props */
                true);
                ob.requestFileRemoval(URI.create("notThere"), "removeTest", null, true);
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ob.writeTo(baos);
                baos.close();
                final PayloadFilesManager remover = new PayloadFilesManager.Perm(instance.getTargetDir(), null, debugLogger());
                final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
                Payload.Inbound removerIB = PayloadImpl.Inbound.newInstance("application/zip", bais);
                remover.processParts(removerIB);
                // 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 removal request", Collections.EMPTY_SET, unexpectedlyPresent);
            }

            @Override
            protected void cleanup() {
                for (File f : desiredAbsent) {
                    if (f.exists()) {
                        f.delete();
                    }
                }
                PayloadFilesManagerTest.cleanup(origDir);
            }
        }.init(targetDir).run("simplePermanentDirWithNoSlashRemovalTest");
    } 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 9 with Outbound

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

the class PayloadFilesManagerTest method simpleTempRecursiveTransferDirOnlyTest.

@Test
public void simpleTempRecursiveTransferDirOnlyTest() throws Exception {
    final String DIR = "x/";
    final String Y_SUBDIR = "y/";
    final String Z_SUBDIR = "z/";
    final List<String> desiredResultsNamePrefixes = new ArrayList<String>();
    /*
         * 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(ySubdir, 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();
        desiredResultsNamePrefixes.add("/x");
        desiredResultsNamePrefixes.add("/x/y");
        desiredResultsNamePrefixes.add("/x/y/z");
        /*
             * Add the original directory recursively.
             */
        new CommonTempTest() {

            @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.
                     */
                final List<File> files = instance.processParts(ib);
                checkNextFile: for (File f : files) {
                    for (ListIterator<String> it = desiredResultsNamePrefixes.listIterator(); it.hasNext(); ) {
                        final String desiredPrefix = it.next().replace("/", File.separator);
                        if (f.getPath().contains(desiredPrefix)) {
                            it.remove();
                            continue checkNextFile;
                        }
                    }
                }
                assertEquals("Unexpected missing files after extraction", Collections.EMPTY_LIST, desiredResultsNamePrefixes);
            }

            @Override
            protected void doCleanup() {
                PayloadFilesManagerTest.cleanup(origDir);
            }
        }.run("simpleTempRecursiveTransferDirOnlyTest");
    } finally {
        if (targetDir != null) {
            FileUtils.whack(targetDir);
        }
    }
}
Also used : Outbound(org.glassfish.api.admin.Payload.Outbound) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Inbound(org.glassfish.api.admin.Payload.Inbound) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 10 with Outbound

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

the class PayloadFilesManagerTest method simplePermanentTransferTest.

// @Test
// public void testWindowsPath() throws Exception {
// //System.out.println("testWindowsPath");
// testForBadChars("C:\\Program Files\\someDir");
// }
// 
// @Test
// public void testNonWindowsPath() throws Exception {
// //System.out.println("testNonWindowsPath");
// testForBadChars("/Users/whoever/someDir");
// 
// }
@Test
public void simplePermanentTransferTest() 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 Set<File> desiredResults = 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");
        desiredResults.add(new File(targetDir.toURI().resolve(FILE_A_PREFIX + FILE_A_NAME)));
        final File fileB = new File(origDir, FILE_B_NAME);
        desiredResults.add(new File(targetDir.toURI().resolve(FILE_B_PREFIX + FILE_B_NAME)));
        writeFile(fileB, "Here is File B", "which has an", "additional line");
        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);
            }

            @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("simplePermanentTransferTest");
    } 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)

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