Backendless Support
 
Works as Designed

Permission Denied - Trying to save file with Business Logic

Hi,

I am trying to implement a business logic that takes an uploaded file's URL from the data table saves back a manipulated form of the same file. However, I'm getting a permission denied error. I have gone through the forums and I even added a user login so that my code runs as authenticated user on the server. it still does not seem to solve the problem.

the error im getting is:

  1. p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff}

    span.s1 {font-variant-ligatures: no-common-ligatures}

    java.security.AccessControlException: access denied ("java.io.FilePermission" "myhelloworld-async.txt" "write")

    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)

    at java.security.AccessController.checkPermission(AccessController.java:884)

    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)

    at com.backendless.coderunner.runtime.security.CodeRunnerSecurityManager.checkPermission(CodeRunnerSecurityManager.java:58)

    at java.lang.SecurityManager.checkWrite(SecurityManager.java:979)

    at java.io.FileOutputStream.<init>(FileOutputStream.java:200)

    at java.io.FileOutputStream.<init>(FileOutputStream.java:101)

    at com.printerapplication.events.persistence_service.Prints_ReceivedTableEventHandler.uploadAsync(Unknown Source)

    at com.printerapplication.events.persistence_service.Prints_ReceivedTableEventHandler.beforeCreate(Unknown Source)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    at java.lang.reflect.Method.invoke(Method.java:498)

    at com.backendless.coderunner.runtime.task.EventInvocationTask.runImpl(EventInvocationTask.java:113)

    at com.backendless.coderunner.runtime.concurrent.ExtendedRunnable.run(ExtendedRunnable.java:26)

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff}

    span.s1 {font-variant-ligatures: no-common-ligatures}

    span.Apple-tab-span {white-space:pre}

    at java.lang.Thread.run(Thread.java:748)

the code I am trying to run is from this link from backendless.

  1. private static void uploadAsync() throws Exception
  2. {
  3. // create a file locally so there is something to upload
  4. String filename = "myhelloworld-async.txt";
  5. FileOutputStream fileOutputStream = new FileOutputStream( filename );
  6. fileOutputStream.write( "Hello mbaas!\nUploading files is easy!".getBytes() );
  7. fileOutputStream.close();
  8. final File file = new File( filename );
  9. // now upload the file
  10. Backendless.Files.upload( file, "/myfiles", new AsyncCallback<BackendlessFile>()
  11. {
  12. @Override
  13. public void handleResponse( BackendlessFile uploadedFile )
  14. {
  15. System.out.println( "File has been uploaded. File URL is - " + uploadedFile.getFileURL() );
  16. file.delete();
  17. }
  18. @Override
  19. public void handleFault( BackendlessFault backendlessFault )
  20. {
  21. System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
  22. }
  23. } );
  24. }

Any help would be highly appreciated.

Thanks in Advance!

Leave a Comment

Comments (1)

photo
1

Hi Usama,

The problem is right here:

  1. String filename = "myhelloworld-async.txt";
  2. FileOutputStream fileOutputStream = new FileOutputStream( filename );
  3. fileOutputStream.write( "Hello mbaas!\nUploading files is easy!".getBytes() );
  4. fileOutputStream.close();

You cannot write to the local file system from business logic. All File IO must be done with the API we provide.

Regards,

Mark