Jack Tauson

Ranch Hand
+ Follow
since Mar 27, 2014
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jack Tauson

Let's say I've a following directory structure on RHEL server. Different users related folders are there inside  Files folder.

Files
     \_User1
     \_User2
     \_User3

There is some springboot related stuff that is writing files in zip format at some point of time (depending upon when it finishes). This may not be relevant to what I'm going to ask below but I thought of sharing this.

Here's my question:

Let's say if that springboot application writes a file inside User1 then is it a good idea to have a separate Java application which could send an email to the user notifying that their files are ready and also include a link to download the file from the server. I've a separate servlet which can be used to download files from the server using a URL.  I'm wondering what that thing could be becase Server Sent events are used to send push notification to the UI. Here, no UI interaction is needed.Please advise or share your thoughts on this.
1 week ago
I'm not familiar with Gradle and hence I would like to know how can I test the following example with Maven? I'm familiar with doing Maven install, build, clean etc on my Eclipse. I was looking at the following gradle based example which I want to test using Maven:

https://www.jeejava.com/server-sent-events-spring-push-notifications/

Please advise. Thanks !
2 weeks ago
I've checked in maven projects from SCM and in the project explorer for some projects it's not showing the Project name. However, for some projects it's showing the project name.

For example:



As seen above, the Company project has different name inside the brackets starting with maven. Is there something I might be missing in pom.xml which is causing this?
2 weeks ago

Tim Moores wrote:Server-sent events are one way to accomplish this. Check the Spring documentation to see how (or if) it supports those. A framework-agnostic approach with full source code is provided in https://coderanch.com/t/685060/java/web-app-Server-Events-SSE



Thank you. Here it says SSE is not supported in IE. So is this going to be a problem if my users might use IE?

https://en.wikipedia.org/wiki/Server-sent_events
3 weeks ago
I am trying to figure out how to send push notifications back to the client in the following ActiveMQ scenario:

I have a spring boot web application #1 deployed on tomcat on RHEL server. From the user interface (this consists of Javascript,jQuery,HTML,CSS), user clicks on a request files button, which calls
a RESTful webservice (a stored procedure gets called  and a unique identifier(number) is returned). I send this identifier as a message to ActiveMQ because the overall
process could take 20-30 minutes.

With the help of this identifier I can check the status of the query from the database table.By status of the query I mean COMPLETE, ERROR, STARTED, NOT YET STARTED.I've
used this approach in my second spring boot web application as explained below.

I have another spring boot web application #2 which is deployed on the RHEL server.This acts a a consumer and keep on listnening to the ActiveMQ queue.
With the help of the identifier from ActiveMQ ,as soon as this app sees the status as COMPLETE, it grabs records from 3 tables,convert the records into 3 csv files,
convert these 3 csv files into  a zip file and put it somewhere on the same server.

The zip file generated in the last step is not available immediately as soon as user clicks on the request files button from the spring boot web app #1.

I am wondering since I've two separate spring boot web applications, is it possible to send push notifications to the user interface of spring boot web application #1
so that user is  aware that their request is not yet completed and they need to wait? Is there something that ActiveMQ does to accomplish this?

Let me know if I can answer any questions relate to this.
1 month ago

Rob Spoor wrote:Check out ZipOutputStream. In short:
* Create a ZipOutputStream wrapped around a FileOutputStream.
* For each file:
   * Create a ZipEntry.
   * Call putNextEntry.
   * Write content to the ZipOutputStream. Don't close it!
   * Call closeEntry.
* Close the ZipOutputStream.

That "Don't close it!" can be troublesome, because it means you can't use try-with-resources, or perform any other finalization that a wrapping OutputStream may need. A simple workaround is to create a wrapper that does nothing for its close method. You can extend FilterOutputStream and override close to do nothing, or you can use commons-io's CloseShieldOutputStream.



It looks like I'm inadvertently closing ZipOutputStream in the following code below? Because, my first file is getting generated without any problem inside the zip file. However, I believe for second and third file generation, it's throwing following error:



Here is my modified code where I'm trying to generate 3 csv files in a zip file. Could you point me the mistake I'm doing in the code below?



I tried removing zos.closeEntry() from each of the try block and placed it just above zos.close() but it still didn't work. Another thing I tried was the new variable name for CSVWriter for second and third entry, still didn't work.
2 months ago

Rob Spoor wrote: Files has method newOutputStream that you can use just like the newBufferedWriter method.



So, I didn't have to use FileOutputStream at all. The following worked for me:




Thanks Rob and others for your valuable inputs. Appreciated!
3 months ago

Rob Spoor wrote:

Jack Tauson wrote:


That creates the actual zip file, and it puts it in "the current working directory". Why don't you use the dir variable here? Files has method newOutputStream that you can use just like the newBufferedWriter method.



Ah,I see. Looks like it wasn't able to find where to create a zip file.

You mean something like this?

Path dir = Paths.get("/srv/custom_users", userName);
Files.createDirectories(dir);

FileOutputStream fos = new FileOutputStream(Files.newOutputStream(dir));
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);



I am wondering how would I define the name of the zip file here? I mean previously, I had "your_files.zip" and now it has been replaced with Files.newOutputStream(dir)?

Also, I defined dir variable before FileOutputStream as I am using dir variable below.


               
               
3 months ago

Rob Spoor wrote:ZipEntry takes a String (or another ZipEntry, but let's ignore that), not a Path. You need to convert your Path to a String. For instance, to include the full path:

It's also possible you only want the file name:


As for the CSVWriter, this takes a Writer, not an OutputStream. You need to wrap it:

However, I will repeat my previous warning:

Rob Spoor wrote:Check out ZipOutputStream. In short:
   * Write content to the ZipOutputStream. Don't close it!


Closing the CSVWriter will close its nested OutputStreamWriter which will close its nested ZipOutputStream. As a result you can't write your second or third file anymore.



So, I made the following changes:



And I don't see zip file getting generated over there. What could be the reason? Other two files are getting generated in CSV format since I haven't made changes related to Zip conversion over there.
3 months ago

Paul Clapham wrote:Short answer: the CSV data should go directly into the ZIP archive as ZipEntry objects. The FileOutputStream allows the ZIP archive to be written out to the file system as a file.




So, I am trying to modify the CSVWriter's constructor in sucn a way that the CSV data should go directly into the ZIP archive as ZipEntry objects.

So I made the changes in the above code (visible after this line ResultSetMetaData rsmd = rsDemo.getMetaData(); in the code below). Right now I am just trying to put one file into ZIP archive to test but I noticed following errors:

Error on Line #173 says: The constructor ZipEntry(Path) is undefined.

Error on Line #175 says: Multiple markers at this line (one out of 2 errors is The constructor CSVWriter(ZipOutputStream) is undefined) .

Screenshot from my eclipse below:



Modified Code below:

3 months ago

Paul Clapham wrote:

Jack Tauson wrote:f latter is true, then, at what point and how should I get the file content without getting it saved in the directory in RHEL server?



But I don't understand this question at all. Your question is about how to generate a ZIP archive which contains three CSV files. Okay. Then all of a sudden there's a RHEL server and you want to get some file content from somewhere?



I believe that's my wrong imagination.  Whatever you mentioned above, "the CSV data should go directly into the ZIP archive as ZipEntry objects" is what I'm looking for. I mean, the zip file should exist inside the folder of RHEL server.
3 months ago

Rob Spoor wrote:Check out ZipOutputStream. In short:
* Create a ZipOutputStream wrapped around a FileOutputStream.
* For each file:
   * Create a ZipEntry.
   * Call putNextEntry.
   * Write content to the ZipOutputStream. Don't close it!
   * Call closeEntry.
* Close the ZipOutputStream.

That "Don't close it!" can be troublesome, because it means you can't use try-with-resources, or perform any other finalization that a wrapping OutputStream may need. A simple workaround is to create a wrapper that does nothing for its close method. You can extend FilterOutputStream and override close to do nothing, or you can use commons-io's CloseShieldOutputStream.



Thanks. I'm little bit confused as to whether I should let my current code generate the csv files inside a directory as it is currently doing OR should it directly go into the zip archive (this makes more sense to me). If latter is true, then, at what point and how should I get the file content without getting it saved in the directory in RHEL server?

Also, how FileOutputStream could help here? I'm little bit confused. Could you give some pseudo code related guidance based on my code? Thanks!
3 months ago

I have this method in my springboot application which is generating 3 CSV files (related to Employee, Customer and Building) inside custom_users directory with timestamp appended to its name as shown below.






My questions:

Is it possible to zip these three files and then upload it inside custom_users folder? So, instead of 3 files I'll have a zip file , maybe with a name like this files_unixtimestamp.zip.
What changes I should be doing? Should I use servlet here to upload the files to the server in zip format or modify the existing code? Please advise. Thanks !

The reason I want to achieve this is :

1) I am using servlet from the client side to download these files in zip format. And since I could only download one file at a time using one servlet call, my call to the servlet would look something like this :

https://myservername.com/DownloadFileFromServer/DownloadFileServlet?filename=building_custom_file_112345786.csv

So, once I've zip related thing implemented , my call would look like the following:

https://myservername.com/DownloadFileFromServer/DownloadFileServlet?filename=files_112345786.zip


2) I won't have to append timestamp in front of the name of each of the 3 csv files.







3 months ago
Thanks Tim and Paul for your valuable inputs. One more quick question on which option you guys think is best to follow:

Option #1: Is it a good idea to create this servlet in my spring boot app only. And if I need the same functionality, I should create it separately for other spring boot web app?

OR

Option #2: Should I consider creating it separately, generate a WAR and deploy it with my other spring boot related WARs on Tomcat?

Option #2 sounds better to me because if I have other spring boot web apps wanting to use the same functionality, I could use this separately deployed servlet to download the file from the server. However,  different spring boot webapps might be downloading file from different location so not sure if I could tweak the servlet
to refer to different location on the server or not.
3 months ago
Thanks for the answer. I have few more follow-up questions:

1. Pretty much exactly this. Although it can be tweaked to not hard-code the filename being copied.



Right now my user interface (which consists of Javascript, HTML,CSS) calls REStful webservices(springboot) for user related operations like  getting data etc. For this download thing, user will hit Download button which behind the scenes is going to
get the files to the user from a specific user directory.

In this scenario, should I create a webservice (define an end point in my controller; define doGet method in my DAO interface and have it implemented in the implementation class of DAO interface) along with my other web services?
I am actually, trying to imagine how user (as soon as they hit Download button) would get the file(s) in zip format after they hit Download button.



2. JavaScript isn't needed, although if your client webpage wants to be more dynamic it can request the file via an AJAX URL request.



I am wondering because user is pressing the Download button, Javascript might be needed? And would I need additional libraries lke FileSaver.js that I have used above in my  javascript code?
Sorry, I am little confused here.
3 months ago