What is the secure and efficient way of storing files uploaded by user in Spring Project? - spring

I have a spring project where the user will upload their files. Other users are going to pay to download that file. What is the best way to achieve this? I'm thinking of converting it to bytes and store it to database. I don't want to directly store the file in some folder in the project. Is there any other way?

You should take a look at the community/companion project Spring Content.
If you are familiar with Spring Data then this project is for unstructured data (i.e. documents, images, videos, etc) what Spring Data is for structured data.
Like Spring Data, it provides a simple, labor-saving programming interface that allows you very quickly and easily create REST APIs for uploading/downloading content, associating that content with Spring Data entities should you want to, and managing it all on the filesystem, or as BLOBs in the database, or in the cloud in something like S3.
Spring Content will provide a programming/modeling framework that should enable you to easily create a content management site with its content behind a paywall.

Related

Spring Cloud Config, how to fetch binary files from git repository

Spring cloud-config-server has a built in mechanism to communicate with git repository and read files which are stored there. Then cloud-config-server also has built in mechanism to expose endpoints to clients (normally spring boot apps) which can read those files and use them as configurations.
This is well documented in spring documentation as can be seen from bellow.
According to doc
Spring Cloud Config Server
Spring Cloud Config Server provides an HTTP
resource-based API for external configuration (name-value pairs or
equivalent YAML content).
Also as documented about the serving format
Serving Alternative Formats
The default JSON format from the
environment endpoints is perfect for consumption by Spring
applications, because it maps directly onto the Environment
abstraction. If you prefer, you can consume the same data as YAML or
Java properties by adding a suffix (".yml", ".yaml" or ".properties")
to the resource path. This can be useful for consumption by
applications that do not care about the structure of the JSON
endpoints or the extra metadata they provide (for example, an
application that is not using Spring might benefit from the simplicity
of this approach).
It can also support txt format
Serving Plain Text
Instead of using the Environment abstraction (or
one of the alternative representations of it in YAML or properties
format), your applications might need generic plain-text configuration
files that are tailored to their environment.
But considering that spring cloud config server has the built in mechanism to communicate with a git repository and also exposes endpoints to the clients to consume the delivered files, it would make sense for other type of files to be able to be served from those endpoints as well.
It could be for example .pdf , .xslx , or even .zip
For example let's assume that the configured git repository contains the file myFile.zip in featureA branch. Then the call under the exposed path of type /{application}/{profile}[/{label}] for example as
serverUrl:serverPort/myApp/default/featureA/myFile.zip is able to deliver the file but is always delivered as raw .txt file which then corrupts the content of the original file existing in git.
I have already found the solution, but invested many hours on it and it was strange that it was not documented in spring documentation. So it is probably good to exist here as well to spare some time from others having the same issue.
As discussed under this issue, spring-cloud-config-server runs under the hood with the help of a normal spring-boot app. Considering that spring-boot has built in content negotiation mechanism it is able to consume and produce different content as well.
As for spring-cloud-config-server it is possible to fetch binary files from git as well as other files (ex zip, pdf, word, xlsx ...) if the call is made with the header Accept: application/octet-stream . This way the call to serverUrl:serverPort/myApp/default/featureA/myFile.zip is able to deliver a copy of the original file myFile.zip without any corruption.

Persisting data within the Spring Boot app

I am creating a small application using Spring Boot, this application allows users to store XML templates which can be reused in various scenarios.
The XML templates are small and will be less that 100 lines each, also there will not be more than 20 templates. I don't want to use a separate database to store this small information.
Also I don't want to store this information in memory as I want to retain the data when the app is restarted.
What is the suggested option to store this kind of data within the Spring Boot app itself without using an external database ?
Use H2 database with in memory db or save it on local disk. Or you can go for plain old write file to disk and just write a file to the disk and have another file to keep track of meta information for your writes. But this will help you only if you can ensure you have access to servers and they in turn are not volatile. Spring boot as such has not out of box solution for this other than embedded database

How to import a CVS file into a MongoDB using reactive way?

As the title says, I am trying to read a cvs file that contains thousands of ip addresses to their respective country. I want to import the cvs file into a MongoDB using WebFlux. I haven't been able to find any resources on how to do this. I have come across Spring Batch but I don't believe it supports WebFlux.
One way I thought of achieving this is just read the CVS file, parse the file, create DTO with values then save it into the database, however, I worry about performance.
Spring WebFlux is the alternative to Spring MVC module. It is not suitable for data processing. So if you want to solve your problem, use this way:
One way I thought of achieving this is just read the CVS file, parse the file, create DTO with values then save it into the database, however, I worry about performance.
And "reactive way" won't be faster than batch processing just because it's "reactive".

Spring MongoDB REST API without HAL?

This Guide shows how to easily create a RESTful interface to a Mongo Database. It produces Json-data in HAL format(Hypertext Application Language), but unfortunately I am unable to find a working Android Client that supports HAL.
Is there any way to disable this HAL format an just GET the documents from the DB without any extra? So that it can be directly parsed into my datatype classes?
It would be really nice to use this approach to somehow automatically generate the REST interface, I cannot go back to manually writing all the methods in controllers after seeing this very short code.
This post seems to deal with the same topic, but I do not understand how to do this configuration.
The guide you are linking to is specifically aimed at using Spring Data REST + Spring Data MongoDB, so to disable the hypermedia for a project designed to generate hypermedia, i.e. a RESTful interface, sounds very strange.
On a mobile platform like Android, the question is what are you trying to do? Are you trying to query for a single, small piece of JSON from MongoDB? The risk of not having any type of hypermedia layer in the middle is that you could query for a giant (i.e. humongous data set) and cripple both the server and mobile device.
For more details about hypermedia and Spring Data REST, check out Oliver Gierke's answer at Disable Hypertext Application Language (HAL) in JSON?.
Regarding the ability to communicate between Android and a HAL backend, of course it's possible. You may wish to look at Roy Clarkson's sample Android app used to talk to a HAL backend that was used at SpringOne 2014 at https://github.com/SpringOne2GX-2014/spring-a-gram-android.
The slides from that presentations are at https://speakerdeck.com/gregturn/springone2gx-2014-spring-data-rest-data-meets-hypermedia.

Document Management System

At the company I work for, we are developing a billing web application with Spring and Vaadin. The trouble is that the number of files to manage is becoming too large; bill,offers contract, etc. We currently store each document as a file on the server, but in this way it is too hard to manage them. This is tedious and error-prone, and it also means we lack any sort of security for accessing these documents.
Now, I'm looking a Document Management System to manage this document. I saw Alfresco Document Management, but I don't know how to integrate it with my application.
Any suggestions?
Alfresco has REST API, so you can use it in your Spring + Vaadin application. Spring has RestTemplate based on Jackson who will help you with REST client implementation.
There are several ways to integrate with alfresco. My two favorite ways to integrate with Alfresco are:
Using the CMIS api
http://wiki.alfresco.com/wiki/CMIS
Using your own custom webscripts or java back end webscripts. These allow you to quickly develop your own rest api with alfresco.
http://docs.alfresco.com/4.0/index.jsp?topic=%2Fcom.alfresco.enterprise.doc%2Fconcepts%2Fws-architecture.html
There are many different ways to integrate. They have webdav, cifs, ftp, and several other ways to integrate. Here is some documentation from alfresco about it
http://docs.alfresco.com/4.2/index.jsp?topic=%2Fcom.alfresco.enterprise.doc%2Fconcepts%2Fintegration-options.html

Resources