1
0
-1

Hi, 

I'm currently working on a Document approval process!

So I would need to access the WebDAV endpoint of Nextcloud Server and need a good way to assign the job to the chosen process tool to complete the process successful.

Folder Name and documents File Name could be used as hash variable to optimize the transfer script to match destination/target File locations.

Any good Idea would be helpful...

Thanks,

    CommentAdd your comment...

    2 answers

    1.  
      1
      0
      -1

      Hi Anders, 

      I was looking around in stackoverflow for a while and found some possible solution to trigger my file transfer.

      Most recent I found is probably  this one:

      import org.apache.commons.httpclient.HttpException;
      import org.apache.commons.httpclient.UsernamePasswordCredentials;
      import org.apache.commons.httpclient.auth.AuthScope;
      import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
      import org.apache.commons.httpclient.methods.RequestEntity;
      import org.apache.jackrabbit.webdav.client.methods.PutMethod;
      
      ...
      
      // WebDAV URL:
      final String baseUrl = ...;
      // Source file to upload:
      File f = ...;
      try{
          HttpClient client = new HttpClient();
          Credentials creds = new UsernamePasswordCredentials("username", "password");
          client.getState().setCredentials(AuthScope.ANY, creds);
      
          PutMethod method = new PutMethod(baseUrl + "/" + f.getName());
          RequestEntity requestEntity = new InputStreamRequestEntity(
              new FileInputStream(f));
          method.setRequestEntity(requestEntity);
          client.executeMethod(method);
          System.out.println(method.getStatusCode() + " " + method.getStatusText());
      }
      catch(HttpException ex){
          // Handle Exception
      }
      catch(IOException ex){
          // Handle Exception
      }

      So I decide to figure out the requirements to run this script by consolidating  the script in two parts. First part looking up for the uploadPath of the File and the second part should transfer the File to the destination Folder. I already included and updated all necessary external Libraries at /jw/WEB-INF/lib folder.

      import java.io.File;
      import java.io.FileInputStream;
      import java.io.IOException;
      import java.net.URL;
      import java.util.HashMap;
      import java.util.Map;
      import org.joget.apps.form.dao.FormDataDao;
      import org.joget.apps.form.service.FileUtil;
      import org.joget.apps.app.model.AppDefinition;
      import org.joget.apps.app.service.AppService;
      import org.joget.apps.app.service.AppUtil;
      import org.joget.workflow.model.WorkflowAssignment;
      import org.apache.commons.httpclient.Credentials;
      import org.apache.commons.httpclient.HttpClient;
      import org.apache.commons.httpclient.HttpException;
      import org.apache.commons.httpclient.UsernamePasswordCredentials;
      import org.apache.commons.httpclient.auth.AuthScope;
      import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
      import org.apache.commons.httpclient.methods.RequestEntity;
      import org.apache.jackrabbit.webdav.client.methods.PutMethod;
      
      
      //Get record Id from process:
      	AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
      	String id = appService.getOriginProcessId(workflowAssignment.getProcessId());
              String tableName = "doc_document_grid";
              String formDefId = "uploadDocumentGridForm";
              String foreignKey = "parent";
              String fileName = "fileUpload";
      	String recordId = "id";
               
              FormDataDao formDataDao = (FormDataDao) AppUtil.getApplicationContext().getBean("formDataDao");
               
                 Map exist = new HashMap();  
              formDataDao.find(formDefId, tableName, " WHERE " + FormUtil.PROPERTY_CUSTOM_PROPERTIES + "." + recordId + " = ?", new Object[]{foreignKey}, "dateCreated", false, null, null);    
              	       	 
      //Get the uploaded path of a form data record:
      	try {
          	File f = FileUtil.getUploadPath(fileName, tableName, recordId);
      	    }
              catch (IOException e) {
              System.out.println("An error occurred.");
            e.printStackTrace();  
             } 
              {  
              System.out.println("File path="+f.getName());  
            }   
        }
       
      // ---------Second Part of script covering to transfer the File-----------
        
      // WebDAV URL:
      	final String baseUrl = http://ip_address_WEBDAW_Server/remote.php/webdav/Common/;
      // Send the File to the destination:
      	try{
          	HttpClient client = new HttpClient();
          	Credentials creds = new UsernamePasswordCredentials("admin", "xxx-xxx-xxx-xxx");
          	client.getState().setCredentials(AuthScope.ANY, creds);
          	PutMethod method = new PutMethod(baseUrl + "/" + f.getName());
          	RequestEntity requestEntity = new InputStreamRequestEntity(
              new FileInputStream(f));
          	method.setRequestEntity(requestEntity);
          	client.executeMethod(method);
          	System.out.println(method.getStatusCode() + " " + method.getStatusText());
         	}
      	catch(HttpException ex){
      // Handle HTTP Exception
      	}
      	catch(IOException ex){
      // Handle IO Exception
      	}

      The problem is I need ensure that the first part of the script retrieves the respective File uploadPath to inject the result into the second part of the code. Any help would be appreciated...

      Kemal

        CommentAdd your comment...
      1.  
        1
        0
        -1

        I just try using cURL command to insert a File to NextCloud app. which actually works fine. But I still need a peace of script as part of my Document approval Process to only transfer 'approved' Documents to my external folder, location.

         

        curl -X PUT -u "username:mypassword" "http://localhost/remote.php/dav/files/share/[FOLDERNAME]/Org_v1.0.docx" --data-binary @"/home/[USER]/[FOLDERNAME]/Org_v1.0.docx"
        1. Anders

          Hi, if you need to perform a HTTP request, you can use some java code like in https://stackoverflow.com/questions/2586975/how-to-use-curl-in-java/2587000#2587000 either in beanshell or in a custom plugin.

        2. Haldun Bayrak

          Thank you for your recommendation and kind support! Please check my findings in the new Answer section. I think it would helpful to develop a plugin for file transfer purpose which enables easy integration to any Cloud storage supporting WebDav or httpClient access + UI {Enter Login information's, specify destinationURL_path && targetURL_path} together with the use of #Hash Variables#. Keep in touch,

        CommentAdd your comment...