Skip to content

Commit 720546b

Browse files
JoeSilentJoeVladimir Yalovy
andauthored
[BKNDLSS-24972]: Add upload file method for Java sdk (#504)
* [BKNDLSS-24972]: Add upload file method for Java sdk * [BKNDLSS-24972]: Improve async method Co-authored-by: Vladimir Yalovy <[email protected]>
1 parent 6cf71af commit 720546b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/com/backendless/Files.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,61 @@ static Files getInstance()
7272
return instance;
7373
}
7474

75+
public BackendlessFile upload( String urlToFile, String backendlessPath )
76+
{
77+
return upload( urlToFile, backendlessPath, false );
78+
}
79+
80+
public BackendlessFile upload( String urlToFile, String backendlessPath, boolean overwrite )
81+
{
82+
if( urlToFile == null || urlToFile.isEmpty() )
83+
throw new NullPointerException( ExceptionMessage.NULL_URL_TO_FILE );
84+
85+
if( backendlessPath == null )
86+
throw new NullPointerException( ExceptionMessage.NULL_PATH );
87+
88+
final String resultURL = Invoker.invokeSync( FILE_MANAGER_SERVER_ALIAS, "upload", new Object[] { urlToFile, backendlessPath, overwrite } );
89+
return new BackendlessFile( resultURL );
90+
}
91+
92+
public void upload( String urlToFile, String backendlessPath, AsyncCallback<BackendlessFile> responder )
93+
{
94+
upload( urlToFile, backendlessPath, false, responder );
95+
}
96+
97+
public void upload( String urlToFile, String backendlessPath, boolean overwrite, final AsyncCallback<BackendlessFile> responder )
98+
{
99+
try
100+
{
101+
if( urlToFile == null || urlToFile.isEmpty() )
102+
throw new NullPointerException( ExceptionMessage.NULL_URL_TO_FILE );
103+
104+
if( backendlessPath == null )
105+
throw new NullPointerException( ExceptionMessage.NULL_PATH );
106+
107+
Invoker.invokeAsync( FILE_MANAGER_SERVER_ALIAS, "upload", new Object[] { urlToFile, backendlessPath, overwrite }, new AsyncCallback<String>()
108+
{
109+
@Override
110+
public void handleResponse( String response )
111+
{
112+
responder.handleResponse( new BackendlessFile( response ) );
113+
}
114+
115+
@Override
116+
public void handleFault( BackendlessFault fault )
117+
{
118+
if( responder != null )
119+
responder.handleFault( fault );
120+
}
121+
} );
122+
}
123+
catch( Throwable e )
124+
{
125+
if( responder != null )
126+
responder.handleFault( new BackendlessFault( e ) );
127+
}
128+
}
129+
75130
public BackendlessFile upload( File file, String path ) throws Exception
76131
{
77132
return upload( file, path, false );

src/com/backendless/exceptions/ExceptionMessage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class ExceptionMessage
5252
public final static String NULL_EMPTY_TEMPLATE_NAME = "Template name can not be null or empty.";
5353
public final static String NULL_EMAIL_ENVELOPE = "EmailEnvelope can not be null.";
5454

55+
public final static String NULL_URL_TO_FILE = "URL to file cannot be null.";
5556
public final static String NULL_FILE = "File cannot be null.";
5657
public final static String NULL_PATH = "File path cannot be null or empty.";
5758
public final static String NULL_NAME = "File name cannot be null or empty.";

0 commit comments

Comments
 (0)