Temporary File Usage

To prevent WVE-2023-0509 from being triggered when building the extension, we will have to change the use of File.createTempFile which uses the default Java environment variable.

The new API is allocated in wt-framework-commons.

Getting Windchill Temp directory we will have to call the following method:

File WindchillTempDirectory = TempAccess.instance.getFileWindchillTemp();

Getting specific Extension Temp directory:

File  ExtensionTempDirectory = TempAccess.instance.getExtensionTempDir();

API Create File or Directory Methods:

Creates a new temp directory inside extensions tmp dir and then creates a file with given name inside this directory.

File FileWithName = TempAccess.instance.createTempNamedFile(String name);

Creates temporal file in Extensions temporal directory.

File createFile = TempAccess.instance.createWexTempFile();

Creates temporal file in Windchill temporal directory.

File createFileWT = TempAccess.instance.createWtTempFile();

Creates temporal file in Extensions temporal directory.

File createFileNewTempDir = TempAccess.instance.createWexTempDirectory();

Creates temporal file in Windchill temporal directory.

File createFileNewTempDir = TempAccess.instance.createWtTempDirectory();

Get stream for file and deletes file when stream is closed. If file is outside of {@link #ROOT_DIR} IOException is thrown.

InputStream is = TempAccess.instance.getStreamOnCloseDelete(File f);

Removes all file separators and 2dots ['/', '\', '..'].

TempAccess.instance.clean(String toClean);

API Files or Directories singleUse

The provided function will be fed a temporal directory which will later be deleted.

TempAccess.instance.singleUseTempDirectory(TempDirectoryFunction<R> func);
TempAccess.instance.singleUseTempDirectory(TempDirectoryConsumer cons);

The provided consumer will be fed a temporal directory which will later be deleted. Returned object should not reference the file passed to the consumer since this could lead to file not being deleted properly.

TempAccess.instance.singleUseTempFile(TempFileFunction<R> func);
TempAccess.instance.singleUseTempFile(TempFileConsumer func);
TempAccess.instance.singleUseTempFile(TempFileFunction<R> func, String ext);
TempAccess.instance.singleUseTempFile(TempFileConsumer func, String ext);

This function create file by calling TempAccess#createTempNamedFile(java.lang.String), File and parent directory are deleted after consumer returns.

TempAccess.instance.singleUseNamedTempFile(String filename, TempFileFunction<R> func);
TempAccess.instance.singleUseNamedTempFile(String filename, TempFileConsumer cons);

If destination is outside of wt.temp method throws IOException. If source is file then content of source is copied to destination. If source is directory, copies source directory recursively into destination.

TempAccess.instance.copy(File source, File dest);
File destination = TempAccess.instance.copy(File source);

If source is file it is copied to destination directory with source name. If source is directory, directory and its contents is copied into destination.

TempAccess.instance.copyKeepName(File source, File dest);
File destination = TempAccess.instance.copyKeepName(File source);

API Temp Directory Cleaner

TempAccess.instance.cleanWexTempArea();

EXAMPLES

Using Java Temp

File newTemplate = File.createTempFile("wexcs", "", TempAccess.getFileWindchillTemp());
newTemplate.deleteOnExit()

Using Windchill Temp

File newTemplate = TempAccess.instance.createWexTempFile("wexreport", "");
newTemplate.deleteOnExit();

Using Java Temp

File tmpdir = Files.createTempDirectory("wex-report-template").toFile();

Using Windchill Temp

File tmpdir = TempAccess.instance.createWexTempDirectory();

Using Java Temp

File pdf = FileUtils.createTempFile(".pdf");

Using Windchill Temp

File pdf = TempAccess.instance.createWexTempFile(".pdf");

Using Java Temp

Path tmpFile = Files.createTempFile(TempAccess.instance.getFileWindchillTemp().toPath(), "wex_wm", "");
tmpFile.toFile().deleteOnExit();
OutputStream os = new FileOutputStream(tmpFile.toFile());
logger.debug("Watermark effective template name: {0}", template);
escapeKeyValues(data);
if (coversheet) {
    watermarker.watermarkCoversheet(watermarkableContent, os, data, getDimensions());
} else {
    watermarkProfiled(watermarker, watermarkableContent, os, data);
}
os.close();
InputStream is = Files.newInputStream(tmpFile, DELETE_ON_CLOSE);
return is;

Using Windchill Temp

return TempAccess.instance.singleUseInputStream((tf) -> {
    try (OutputStream os = new FileOutputStream(tf)) {
        logger.debug("Watermark effective template name: {0}", template);
        escapeKeyValues(data);
        if (coversheet) {
            watermarker.watermarkCoversheet(watermarkableContent, os, data, getDimensions());
        } else {
            watermarkProfiled(watermarker, watermarkableContent, os, data);
        }
    }
});