package org.uberfire.server;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUploadException;
import org.uberfire.io.IOService;

public class FileUploadServlet
        extends BaseUploadServlet {

    @Inject
    @Named("ioStrategy")
    private IOService ioService;

    @Override
    protected void doPost( HttpServletRequest request,
                           HttpServletResponse response ) throws ServletException, IOException {

        try {
            if ( request.getParameter( "path" ) != null ) {
                writeFile( ioService, ioService.get( new URI( request.getParameter( "path" ) ) ), getFileItem( request ) );

                writeResponse( response, "OK" );
            } else if ( request.getParameter( "folder" ) != null ) {
                writeFile( ioService,
                           ioService.get( new URI( request.getParameter( "folder" ) + "/" + request.getParameter( "fileName" ) ) ),
                           getFileItem( request ) );

                writeResponse( response, "OK" );
            }

        } catch ( FileUploadException e ) {
            logError( e );
            writeResponse( response, "FAIL" );
        } catch ( URISyntaxException e ) {
            logError( e );
            writeResponse( response, "FAIL" );
        }
    }
}

@@ -453,24 +453,305 @@
 dServlet %7B%0A%0A
+    private static final String PARAM_PATH = %22path%22;%0A    private static final String PARAM_FOLDER = %22folder%22;%0A    private static final String PARAM_FILENAME = %22fileName%22;%0A%0A    private static final String RESPONSE_OK = %22OK%22;%0A    private static final String RESPONSE_FAIL = %22FAIL%22;%0A%0A
     @Inject%0A
@@ -1029,22 +1029,26 @@
 ameter( 
-%22path%22
+PARAM_PATH
  ) != nu
@@ -1091,16 +1091,43 @@
 Service,
+%0A                          
  ioServi
@@ -1169,21 +1169,52 @@
 er( 
-%22path%22 ) ) ),
+PARAM_PATH ) ) ),%0A                          
  get
@@ -1273,33 +1273,72 @@
 e( response,
- %22
+%0A                               RESPONSE_
 OK
-%22
  );%0A
+%0A
             
@@ -1375,30 +1375,716 @@
 er( 
-%22folder%22 ) != null ) %7B
+PARAM_FOLDER ) != null ) %7B%0A%0A                //See https://bugzilla.redhat.com/show_bug.cgi?id=1091204%0A                //If the User-provided file name has an extension use that; otherwise use the same extension as the original (OS FileSystem) extension%0A                String targetFileName;%0A                final String originalFileName = getFileItem( request ).getName();%0A                final String providedFileName = request.getParameter( PARAM_FILENAME );%0A                if ( providedFileName.contains( %22.%22 ) ) %7B%0A                    targetFileName = providedFileName;%0A                %7D else %7B%0A                    targetFileName = providedFileName + getExtension( originalFileName );%0A                %7D%0A
 %0A   
@@ -2195,61 +2195,45 @@
 er( 
-%22folder%22 ) + %22/%22 + request.getParameter( %22f
+PARAM_FOLDER ) + %22/%22 + targetF
 ileName
-%22 )
  ) )
@@ -2328,21 +2328,59 @@
 esponse,
- %22
+%0A                               RESPONSE_
 OK
-%22
  );%0A    
@@ -2493,35 +2493,70 @@
 e( response,
- %22
+%0A                           RESPONSE_
 FAIL
-%22
  );%0A
+%0A
         %7D ca
@@ -2653,33 +2653,317 @@
 nse,
- %22FAIL%22 );%0A        %7D%0A    %7D
+%0A                           RESPONSE_FAIL );%0A        %7D%0A    %7D%0A%0A    private String getExtension( final String originalFileName ) %7B%0A        if ( originalFileName.contains( %22.%22 ) ) %7B%0A            return %22.%22 + originalFileName.substring( originalFileName.lastIndexOf( %22.%22 ) + 1 );%0A        %7D%0A        return %22%22;%0A    %7D%0A
 %0A%7D%0A

Add extension to the uploaded item according to the extension of uploaded file if it was not specified