Get User confirmation to replace a file or not?

Hi,
i am using Upload component to upload files to the server.I have a scenario that when the user uploads the same file already exists in the server.It should pop up a window and get confirmation for user and proceed for replacement.I am able to get the popup,but when the i click yes button for replace,the upload doesn’t happen.I am in urgent need of this.Please kindly help me in acheving this.

Below is the eror i am getting

Oct 31, 2012 11:42:58 AM com.vaadin.Application terminalError
SEVERE: Terminal error:
com.vaadin.terminal.gwt.server.UploadException: Upload failed
        at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.streamToReceiver(AbstractCommunicationManager.java:616)
        at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleSimpleMultipartFileUpload(AbstractCommunicationManager.java:469)
        at com.vaadin.terminal.gwt.server.CommunicationManager.handleFileUpload(CommunicationManager.java:257)
        at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:495)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:662)
Caused by: com.vaadin.terminal.gwt.server.NoOutputStreamException
        at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.streamToReceiver(AbstractCommunicationManager.java:561)
        ... 16 more

I am generating the popup inside
receiveUpload
method.Please find by code snippet

public OutputStream receiveUpload(String filename, String mimeType) {
	  
   
    StringBuffer fileLocation = new StringBuffer();
    
    fileLocation.append(FILE_DIR);
    fileLocation.append(File.separatorChar);
    fileLocation.append("serverfiles");
    fileLocation.append(File.separatorChar);
    fileLocation.append(filename);
 
    filePath = fileLocation.toString();
    file = new File(filePath);
    if(file.exists()){
    	System.out.println("File exists");
    	// Add listener to confirmation window. If confirmed, file replaced
	    ConfirmationDialogPopupWindow confirmationPopup = 
	      new ConfirmationDialogPopupWindow("File already exists.Do you want to replace?");
	    confirmationPopup.addListener(new ConfirmationEventListener() {
	      protected void rejected(ConfirmationEvent event) {
	      }
	      protected void confirmed(ConfirmationEvent event) {
	       System.out.println("Entering"+filePath);
	       try {
	            // Open the file for writing.
	        	outputStream = new FileOutputStream(filePath,false);
	        	
	        } catch (final Exception e) {
	            // Error while opening the file. Not reported here.
	            e.printStackTrace();
	            outputStream = null;
	        }
	      }
	    });
	    
	    // Show popup
	    viewManager.showPopupWindow(confirmationPopup);
    	
        
    }else{
    	System.out.println("File doesn't exists");
    	try {
            // Open the file for writing.
        	this.outputStream = new FileOutputStream(fileLocation.toString());
        } catch (final java.io.FileNotFoundException e) {
            // Error while opening the file. Not reported here.
            e.printStackTrace();
            return null;
        }
        
    }
    return outputStream;
  }

Thanks in advance
Dinesh

Well, your problem is that you don’t give the upload component an outputStream, which cancels the upload. You need to initialize the upload again in the confirm popup by using Upload.submitUpload(). This will naturally call your receiveupload again, so you need to have a marker somewhere which bypasses the duplicate file check if the user already clicked ‘confirm’ last time…

Hi Mattson,

Thanks for your reply.I tried doing a submitUpload(). But still getting the same error.One thing i noticed that,Since i have written the code inside the receiveUpload method.It doesn’t wait for user intervention,it executes as usual.Please help to find out where i am going wrong.Below is the modified code snippet as per suggestion.

  public OutputStream receiveUpload(String filename, String mimeType) {
	  
    this.fileName = filename;
    
    StringBuffer fileLocation = new StringBuffer();
    
    fileLocation.append(FILE_DIR);
    fileLocation.append(File.separatorChar);
    fileLocation.append("serverfiles");
    fileLocation.append(File.separatorChar);
    fileLocation.append(filename);
    filePath = fileLocation.toString();
    file = new File(filePath);
    if(file.exists()){
    
    	// Add listener to confirmation window. If confirmed, file replaced
	    ConfirmationDialogPopupWindow confirmationPopup = 
	      new ConfirmationDialogPopupWindow("File already exists.Do you want to replace?");
	    confirmationPopup.addListener(new ConfirmationEventListener() {
	      protected void rejected(ConfirmationEvent event) {
	      }
	      protected void confirmed(ConfirmationEvent event) {
	       System.out.println("Entering"+filePath);
	       try {
	    	   Upload upload = new Upload();
	            // Open the file for writing.
	        	outputStream = new FileOutputStream(filePath,false);
	        	
	        	upload.submitUpload();
	        } catch (final Exception e) {
	            // Error while opening the file. Not reported here.
	            e.printStackTrace();
	            outputStream = null;
	        }
	      }
	    });
	    
	    // Show popup
	    viewManager.showPopupWindow(confirmationPopup);
    	
        
    }else{
    	
    	try {
            // Open the file for writing.
        	this.outputStream = new FileOutputStream(fileLocation.toString());
        } catch (final java.io.FileNotFoundException e) {
            // Error while opening the file. Not reported here.
            e.printStackTrace();
            return null;
        }
        
    }
    return outputStream;
  }

Thanks
Dinesh

Yes, as I explained; the confirmation code is executed in a completely different request than the rest of the receiveUpload-method, this is normal Java.

Just adding the one call that you did will just execute the exact same code again. As I said, you need to detect that the user already pressed ‘ok’, and if this is the case then you bypass the confirmation.