DnD file upload redirection without server side saving

Hello,

here is the deal, I have a database running on a server with a REST interface for accessing the DB, let’s call it ServerDB, and I have my Vaadin application running on another server, let’s call it ServerV. Now, I open the Vaadin application on a client machine, let’s call it just Client, and I want to upload a file with Drag’n’Drop from the Client the whole way to ServerDB.

What would be the best approach to achieve this functionality ? Skimming through Vaadin source didn’t help much, as Vaadin expects a StreamVariable to be present in the Html5File before it starts reading the request’s input stream and writing it to the output stream of that variable. However, I need to be able to redirect the input stream vie a REST-Post request to my ServerDB in a MultipartForm.

Here’s what I tried so far:

  1. I registered the application as HttpServletRequestListener and intercepted the FILE_UPLOAD request, which by the way happens after a UIDL request, then I packed the input stream into a multipart form and sent it via a post to ServerDB. The problem with this approach is that the FILE_UPLOAD request is never called if I do not set a StreamVariable in the drop handler, and if I do set it, Vaadin tries to handle it and throws an exception (“stream closed”), caused by my interception.

public void onRequestStart(HttpServletRequest request, HttpServletResponse response) {
    // check request type
    String pathInfo = request.getPathInfo();
    System.out.println("Request: " + pathInfo);
    if (pathInfo == null) return;
    if (!pathInfo.startsWith("/" + "APP/UPLOAD/")) return;

    String SERVER_URL = .. // ServerDB file upload url
    RestService service = .. // create Rest service with SERVER_URL

    MultiPartForm form = .. // build multi part form
    try {
        form.setStream(request.getInputStream());
        ClientResponse<?> restresponse = (ClientResponse<?>) service.uploadFile(form);
        restresponse.releaseConnection();
        request.getInputStream().close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  1. I made a custom StreamVariable which contains a piped output and a piped input stream, then in streamingStarted event I dispacht a new thread which sends the stream to ServerDB. Theoretically the reading end, ServerDB, should be able to receive the stream while the writing end, ServerV, writes the bytes. However, the upload call blocks till the whole stream is received on ServerV, i.e. ServerV posts the multipart form to ServerDB after streamingFinished event.

public void streamingStarted(StreamingStartEvent event) {

	mediaUploaderThread = new Thread(new Runnable() {
		public void run() {
			MultiPartForm form = .. // build multi part form
			try {
				form.setStream(pipedInputStream); // pipedInputStream is the sink of a pipedOutputStream in the StreamVariable which Vaadin writes to
				ClientResponse<?> restresponse = (ClientResponse<?>) service.uploadFile(form);
				restresponse.releaseConnection();
				pipedInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	});
	mediaUploaderThread.start();
}

Any help or suggestions are appreciated.

PS: sorry fo the long post.

Hi Iskandar Abudiab,

Were you able to solve this issue .
I have a similar requirement to read the content of file ,not on immediate drop but after some user input .