Vaadin 8: Where to store file created at server start in ServletContextListener - vaadin8

I want to create a file with data from the database only once at server start and store the file.
I found the helpful examples:ServletContextListener Example 1 and ServletContextListener Example 2.
I wanted to store my file created in the contextInitialized method in the following directory:
String fileName = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath()
+ "/WEB-INF/fileName.xlsx";
But I get a Nullpointer exception for VaadinService.getCurrent().getBaseDirectory().getAbsolutePath()
It seems not be available at start.
Where can I store my file so that a user can download it later?
Thank you for your help!

Related

Spring batch reader changes to read file content from GCP cloud storage bucket

I have a pre existing springboot application like this one which uses spring batch to read a huge CSV file(size=1GB) from a local location and inserts the content after some changes to a database. Now, the file will be coming from a bucket in Google Cloud Platform(GCP). I am planning to use something like Inbound streaming channel adapter mentioned here. Is this feasible?If yes, what changes will be needed in the ItemReader of the batch? Or is there any other option I need to consider? Any suggestion is welcome.
The FlatFileItemReader works with any Resource implementation. So you could use an URLResource and point it to your file on GCP without having to download it.
Here’s a specific example for a CSV file using the URLResource:
URLResource ur = new URLResource("http://www.dukelearntoprogram.com/course2/java/food.csv");
for (CSVRecord record : ur.getCSVParser()) {
// print or process fields in record
String name = record.get("Name");
// other processing
}
This was extracted from non-official documentation.
by adding relevant dependencies and changing the local file path
->"flatFileItemReader.setResource(new FileSystemResource("src/main/resources/message.csv"));"
to this
->"flatFileItemReader.setResource(new UrlResource("storage.googleapis.com/test-mybucket/mycsv.csv"));"
will it work ??

Couchbase lite data storing path

I am using couchbase lite for my Windows application, I want to know the place where Document created by code are going to be stored.
As of 2018 you can specify the location of a database when you connect to it.
According to the docs:
The DatabaseConfiguration.Database property Gets or sets the directory to use when creating or opening the data.
So when you create the Database object, the constructor
"Creates a database with a given name and database configuration. If the configuration is null then the default configuration will be used. If the database does not yet exist, it will be created."
var database = new Database("mydb", new DatabaseConfiguration(){Database="mylocation/dbname"});
This is rough code but you get the idea.
If you do not specify a directory path when you create a db then it goes in a default directory in your local user space, for Windows this is explained in this SO page
The location is determined by the JavaContext you pass when you create a new Manager object.
The default is a subdirectory named "cblite", or you can pass a String arg when you create the JavaContext instance.
If the path you supply is not absolute, the location is relative to the working directory of the application.

Once Spring controller method is invoked can I confirm that the file is completely uploaded?

I am trying to upload an excel file in the OpenShift server data directory (OPENSHIFT_DATA_DIR) and store the data in the MySQL database provided by OpenShift. I am following the below steps:
Upload the excel file to the OPENSHIFT_DATA_DIR.
Read the excel file from OPENSHIFT_DATA_DIR and parse it.
Update/insert the parse data into the database
I am able to upload excel file into the OPENSHIFT_DATA_DIR with the below code. I use Spring's multipart resolver
private void uploadExcelFile(MultipartFile file) throws IOException{
String fileNameWithPath = System.getenv("OPENSHIFT_DATA_DIR")+"Test.xls";
file.transferTo(new File(fileNameWithPath));
//Start reading the excel file from OPENSHIFT_DATA_DIR
.......
.......
}
I will be able to read the excel file from OPENSHIFT_DATA_DIR, but my concern is I am not sure whether the file is completely uploaded or not at the time of reading the excel.
Once "transferTo()" method is executed can I confirm that the file is completely uploaded?
Or, How will I handle it if the file is not completely uploaded?
After the transferTo() mothod call, you can do anything you want, with the transferred file.
If you read the doc of the transferTo() method, you will see, it is not asynchron.
Documentation says:
void transferTo(File dest)
throws IOException,
IllegalStateException
Transfer the received file to the given destination file.
This may either move the file in the filesystem, copy the file in the filesystem, or save memory-held contents to the destination file. If the destination file already exists, it will be deleted first.
If the file has been moved in the filesystem, this operation cannot be invoked again. Therefore, call this method just once to be able to work with any storage mechanism.
Note: when using Servlet 3.0 multipart support you need to configure the location relative to which files will be copied as explained in Part.write(java.lang.String).
Parameters:
dest - the destination file
Throws:
IOException - in case of reading or writing errors
IllegalStateException - if the file has already been moved in the filesystem and is not available anymore for another transfer

Populate backend datastore with data using upload script

I am following this tutorial an have a lot of trouble with it as many parts are not explained to beginners: https://cloud.google.com/developers/articles/how-to-build-mobile-app-with-app-engine-backend-tutorial
I came to the part where we need to upload some data to datastore. They are using a .csv file to accomplish that.
This is part of the tutorial where I've stuck:
Create Upload Script
This is the script that actually uploads the simulated information into the backend datastore.
For information about the bulkloader.yaml and more, refer to the App Engine documentation in: Uploading and Downloading Data. Also, you can find the bulkloader.yaml file and the test data here MobileAssistant-Data.
Create a new directory and make it your current directory.
In your editor, create a script file and call it upload_data.sh.
Copy and paste the following code into the file.
#!/bin/sh
appcfg.py upload_data
--config_file bulkloader.yaml --url=http://localhost:8888/remote_api --filename $1 --kind=$2 -e
nobody#nowhere.com
The previous script accepts two arguments separated by a blank space:
./upload_data.sh<data.csv> <class entity name>
The first argument is the name of the csv file that contains the data you want to upload, The second is the name of the entity class to handle the data.
Close and save the file. Notice that the script at this time is intended to be used on local server. It must be modified when the backend application is deployed to the cloud. For example, e-mail “nobody#nowhere.com ” will be changed to an actual e-mail address of the App Engine administrator before deployment.
Places Simulated Data
The simulated data is contained in the file places.csv that you downloaded earlier from the MobileAssistant-Data directory in Mobile Shopping Assistant.
The file contains the following comma separated fields:
name: <place’s name>
placeId: <number that identifies the place>
location: <place’s latitude and longitude coordinates (two values separated by a comma) >
key: <unique key associated with the place>
address: <the place’s address >
Modify Web.xml
To upload data to the datastore, reference the “remote API servlet” and associate it with a URL. You’ll define these mappings in the Web.xml file of the MobileAssistant-Appengine application, as shown next. The web server uses this configuration to identify the servlet to handle a given request.
Note. Notice, you might need to restart Eclipse. if you skip th e Web.xml modification you will get Error 404 Not Found.
<servlet>
<display-name>Remote API Servlet</display-name>
<servlet-name>RemoteApiServlet</servlet-name>
<servlet-class>com.google.apphosting.utils.remoteapi.RemoteApiServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RemoteApiServlet</servlet-name>
<url-pattern>/remote_api</url-pattern>
</servlet-mapping>
Upload Places Simulated Data
To upload the test data to the data store, run the script that uploads the data from your script directory.
Before you upload your data, make sure to start MobileAssistant-AppEngine as a Web Application (in Debug mode) in Eclipse.
./upload_data.sh <places.csv> Place
At the password request, just press enter.
I don't understand that part with the script. I created a folder age on Desktop, placed inside their files bulkloader.yaml and places.csv. Inside that folder is that upload script.
After I run ./upload_data.sh <places.csv> Place, in my terminal I receive the following error:
upload_data.sh: line 2: appcfg.py: command not found
upload_data.sh: line 3: --config_file: command not found
upload_data.sh: line 4: nobody#nowhere.com: command not found
What should I do to upload that data to Google App Engine datastore? Thank you.
Is appcfg.py in your path, if not then set the path explicitly in your script or add the path the appcfg.py to your PATH

How can we provide the path for the image file in order to create the multimedia component in server?

I am trying to Create a multimedia Component where
1) I am executing my code in client machine
2)Target where it should create a component is one of the test server.
3)Here i am using the http Coreservices added as the Service Reference.
but the part in which i am getting error is the path for the image file for the multimedia component.
Case:1) I am selecting the Image file from the client machine -
In this case i am getting error in the component creation stage stated:
Unable to get file size of 'file:///C:foldername/image.jpg' Could
not find a part of the path 'C:\foldername\image.jpg'.
Case:2) I mapped the test Server in client machine so it created the Z drive and i am selecting the image file
In this case i am getting same error at component creation stage:
Unable to get file size of 'file:///Z:/foldername/image.jpg' Could
not find a part of the path 'Z:\foldername\image.jpg'.
My question is , How can we provide the path for the image file in order to create the multimedia component?
I got my answer and able to create the multimedia component to the target server:
have used
StreamUpload2010Client streamClient = new StreamUpload2010Client();
and stored the file at templocation and uploaded it from there.
thanks

Resources