Java code OK in AIX 3.5, fails in RHEL 7.5: It makes corrupt DOC, DOCX, XLS, XLSX files but good HTML files - rhel

We have a Java process (see below) to generate DOC, DOCX, XLS, XLSX, and HTML and save it to our Linux PCs. It ran well in our old AIX 3.5 machine; but in our new RHEL 7.5 machine, Microsoft proprietary document formats (DOCX, DOC, XLSX, and XLS) it outputs are corrupt. HTML files are not corrupt.
I have downloaded the output files into a Windows PC and try to open them; Exception errors say the DOC/DOCX/XLS/XSLX files are corrupt and cannot be opened.
The Java code works well in the old Linux PC. I am assuming there might some libraries or software we need to install in the new PC.
Below is my java code (Process to generate the file)
int BUFFER_SIZE=1024*256;
int bytesRead=0;
Resultset rs=obj.resultSet();
DocumentClass = new DocumentClass();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
BufferedInputStream bis = new BufferedInputStream(rs.getBinaryStream(1));
try
{
bytesRead = bis.read(buffer,0,BUFFER_SIZE);
while (bytesRead > 0)
{
baos.write(buffer, 0, bytesRead);
buffer = new byte[BUFFER_SIZE];
bytesRead = bis.read(buffer,0,BUFFER_SIZE);
}
}
catch (IOException io )
{
System.out.println(io.getMessage());
}
DocumentClass.setFileBody(baos);
BufferedWriter CreateDoc = new BufferedWriter(new FileWriter("/usr/Test.docx"));
CreateDoc.write(DocumentClass.getFileBody().toString());
CreateDoc.close();

Related

download.default_directory is working on windows but not working on Linux

I am using Selenium, Java, and chrome to automate the website. It has one test case wherein it automatically downloads one file. I have tried the below code on windows, it worked fine but when try to execute it through Jenkins job which is having Linux operating system, it has failed to download the file in a given path.
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.prompt_for_download", false);
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", 1 );
String downloadFilepath = System.getProperty("user.dir") + "\\src\\test\\resources\\downloads";
if (System.getProperty("user.dir").startsWith("/home")) {
downloadFilepath = downloadFilepath.replace("\\", "/");
}
prefs.put("download.default_directory", downloadFilepath);
options.setExperimentalOption("prefs", prefs);
options.addArguments("start-maximized");
options.addArguments("--safebrowsing-disable-download-protection");
options.addArguments("safebrowsing-disable-extension-blacklist");
options.addArguments("--test-type");
options.addArguments("--disable-extentions");
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver(options);

Unable to create sheet when using SXSSFWorkbook in docker with apache poi

When executing following code in docker context, the page just hangs and I do not get any exceptions either:
File file = fileService.createTempFile('some-file.xlsx')
log.info("CREATED EXCEL FILE : ${file.path}")
Workbook workbook = new SXSSFWorkbook(SXSSFWorkbook.DEFAULT_WINDOW_SIZE)
log.info("WORKBOOK CREATED")
workbook.createSheet(WorkbookUtil.createSafeSheetName('TEST'))
log.info("SHEET CREATED")
FileOutputStream fos = new FileOutputStream(file)
workbook.write(fos)
//workbook.dispose()
fos.close()
I can see the file is craeted and also see WORKBOOK CREATED log generated but nothing after that.
This happens only when running the code in the docker container and not when I run the app outside of docker context.
I am using poi 4.1.2 and jdk 11.0.16.
UPDATE:
I have read on other posts about the temp directory not existing or not being writable. Just to test this out, I went to the node and created pre-defined folder with all read write permissions as well and updated the code to point SXXF to write to that temp folder as well :
try {
//THIS FOLDER ALREADY EXISTS IN NODE AND HAS ALL PERMS
String path = '/usr/local/tomcat/temp/reporting-service-16769203353988068118/'
File tmpdir = new File(path)
log.info("TMP DIR IS ${tmpdir.path}")
File file = new File("${tmpdir.path}/surveyQuestionExport.xlsx")
log.info("CREATED EXCEL FILE : ${file.path}")
TempFile.setTempFileCreationStrategy(new DefaultTempFileCreationStrategy(tmpdir))
Workbook workbook = new SXSSFWorkbook(new XSSFWorkbook(), SXSSFWorkbook.DEFAULT_WINDOW_SIZE)
log.info("WORKBOOK CREATED")
workbook.createSheet(WorkbookUtil.createSafeSheetName('TEST'))
log.info("SHEET CREATED")
FileOutputStream fos = new FileOutputStream(file)
workbook.write(fos)
workbook.dispose()
fos.flush()
fos.close()
return file
} catch (Exception ex) {
log.error("EXPORT FAILED", ex)
}
But still same issue.

How to handle windows pop up to upload a file in Linux machine using selenium script

I am trying to automate a windows popup for file upload in selenium and want to trigger the script on LINUX.
There are tools like autoIT, JACOB which can handle these kinda pop-ups, but these doesn't work on LINUX directly.
I need help in any tool which does the job.
I have tried AutoIT, but due to autoIt exe not able to run the code on linux.
Then I tried JACOB but to use jacob needs to save two .dll files and did some exploration and found out taht Linux doen't support .dll it supports .so and as i am very new to linux don't have ides how to perform this.
private void fileUploadData(String fileName) {
String workingDir = "";
AutoItX uploadWindow = new AutoItX();;
try {
workingDir = System.getProperty("user.dir");
final String jacobdllarch =
System.getProperty("sun.arch.data.model")
.contains("32") ? "jacob-1.18-x86.dll" : "jacob-1.18-x64.dll";
String jacobdllpath = workingDir + "/" + jacobdllarch;
File filejacob = new File(jacobdllpath);
System.setProperty(LibraryLoader.JACOB_DLL_PATH,
filejacob+System.getProperty("user.dir"));
}
catch(Exception e) {
e.printStackTrace();
}
String filepath = workingDir + "\\src\\test\\resources\\testdata\\" +
fileName ;
File file=new File(filepath);
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
browseButton.click();
uploadWindow.winActive( "File Upload", "" );
uploadWindow.sleep(2000);
uploadWindow.ControlSetText( "Open", "", "Edit1", filepath);
uploadWindow.controlClick( "", "", "&Open" );
}
Getting below error on LINUX code is working fine on Windows -
Unsatisfied link error: no jacob-1.18-x64 in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib]

Image.FromStream mono memory leak

I believe I have stumbled across a memory leak in Mono 4.6 and 4.8 using the Image.FromStream call. Below is a code snippet that can be used to reproduce the problem. I have tested this in Ubuntu 16.04 LTS with Mono 4.6 and 4.8.
I have posted a Visual Studio/MonoDevelop solution and project (in a tar.gz file) here: BitmapTest.tar.gz. After extracting BitmapTest.gz, rename BitmapTest to BitmapTest.tar.gz so you can extract the code.
This project is a simple Winforms app that runs the code below.
Is there a work around, or something I am not considering?
EDIT:
If I use
using (Bitmap srcBitmap = (Bitmap)Image.FromFile(_file_name))
instead of
using (Bitmap srcBitmap = (Bitmap)Image.FromStream(ms))
It works. The problem seems to be in Image.FromStream.
// populate our byte[] array with a .BMP file
ImageConverter convert = new ImageConverter();
private byte[] _sourceArray = (byte[])convert.ConvertTo(Image.FromFile(filename_string), typeof(byte[]));
// put the following code in a loop
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(_sourceArray))
{
System.Diagnostics.Trace.WriteLine("First memory stream created OK size: " + ms.Capacity.ToString());
using (Bitmap srcBitmap = (Bitmap)Image.FromStream(ms))
{
System.Diagnostics.Trace.WriteLine("First Bitmap created OK size: " + srcBitmap.Size.ToString());
}
}
// If GC is called then the memory climbs slower
if (_forceGC) {
GC.WaitForPendingFinalizers();
GC.Collect();
}

Decompressing using GZipStream

I have a zipped up file within a resource and am attempting to decompress this zip file. However I keep getting this exception...?
The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.
byte[] zipFile = HTMLEditor.ZipTest.HTMLEditor;
string output = AppDomain.CurrentDomain.BaseDirectory;
var bigStreamOut = new System.IO.MemoryStream();
//Decompress
using (var bigStream = new GZipStream(new MemoryStream(zipFile), CompressionMode.Decompress))
{
bigStream.CopyTo(bigStreamOut);
}
Any ideas anyone? Currently using .Net 4.0 and don't really want to use external libraries?
I suspect the problem with bigStream.CopyTo(bigStreamOut);
byte[] step = new byte[16]; //Instead of 16 can put any 2^x
int readCount;
do
{
readCount = bigStream.Read(step, 0, step.Length);
bigStreamOut.Write(step, 0, readCount);
} while (readCount > 0);
bigStream.Close();

Resources