Search in sources :

Example 6 with Inbound

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

Example 7 with Inbound

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

the class CheckpointHelper method load.

public JobManager.Checkpoint load(CheckpointFilename cf, Outbound outbound) throws IOException, ClassNotFoundException {
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    JobManager.Checkpoint checkpoint;
    try {
        fis = new FileInputStream(cf.getFile());
        ois = factory.createObjectInputStream(fis);
        checkpoint = (JobManager.Checkpoint) ois.readObject();
    } finally {
        try {
            ois.close();
        } catch (Exception ex) {
        }
        try {
            fis.close();
        } catch (Exception ex) {
        }
    }
    if (outbound != null) {
        loadOutbound(outbound, cf.getForPayload(false).getFile());
        checkpoint.getContext().setOutboundPayload(outbound);
    }
    Inbound inbound = loadInbound(cf.getForPayload(true).getFile());
    checkpoint.getContext().setInboundPayload(inbound);
    try {
        String username = checkpoint.getJob().getSubjectUsernames().get(0);
        Subject subject = authenticationService.impersonate(username, /* groups */
        null, /* subject */
        null, /* virtual */
        false);
        checkpoint.getContext().setSubject(subject);
    } catch (LoginException e) {
        throw new RuntimeException(e);
    }
    return checkpoint;
}
Also used : LoginException(javax.security.auth.login.LoginException) JobManager(org.glassfish.api.admin.JobManager) Inbound(org.glassfish.api.admin.Payload.Inbound) FileInputStream(java.io.FileInputStream) LoginException(javax.security.auth.login.LoginException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Subject(javax.security.auth.Subject) ObjectInputStream(java.io.ObjectInputStream)

Example 8 with Inbound

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

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

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

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