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);
}
}
}
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);
}
}
}
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);
}
}
}
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");
}
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;
}
}
Aggregations