use of com.backendless.exceptions.BackendlessException in project Android-SDK by Backendless.
the class Files method uploadFromStream.
public BackendlessFile uploadFromStream(OutputStreamRouter outputStreamRouter, String name, String path, boolean overwrite) throws Exception {
HttpURLConnection connection = null;
String CRLF = "\r\n";
String boundary = (new GUID()).toString();
try {
String urlStr = Backendless.getUrl() + '/' + Backendless.getApplicationId() + '/' + Backendless.getSecretKey() + "/files/" + encodeURL(path) + "/" + encodeURL(name);
if (overwrite)
urlStr = urlStr + "?" + OVERWRITE_PARAMETER_NAME + "=" + overwrite;
java.net.URL url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
connection.setChunkedStreamingMode(DEFAULT_CHUNK_SIZE);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
for (String key : HeadersManager.getInstance().getHeaders().keySet()) connection.addRequestProperty(key, HeadersManager.getInstance().getHeaders().get(key));
try (OutputStream outputStream = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true)) {
writer.append("--").append(boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(name).append("\"").append(CRLF);
writer.append("Content-Type: application/octet-stream").append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
outputStreamRouter.writeStream(outputStream);
outputStream.flush();
writer.append(CRLF).flush();
writer.append("--").append(boundary).append("--").append(CRLF);
}
if (connection.getResponseCode() != 200) {
Scanner scanner = new Scanner(connection.getErrorStream());
scanner.useDelimiter("\\Z");
String response = scanner.next();
scanner.close();
Matcher matcher = SERVER_ERROR_PATTERN.matcher(response);
String message = null;
String code = null;
while (matcher.find()) {
message = matcher.group(MESSAGE_POSITION);
code = matcher.group(CODE_POSITION);
}
throw new BackendlessException(code == null ? String.valueOf(connection.getResponseCode()) : code, message);
} else {
Scanner scanner = new Scanner(connection.getInputStream());
scanner.useDelimiter("\\Z");
String response = scanner.next();
scanner.close();
Matcher matcher = SERVER_RESULT_PATTERN.matcher(response);
String fileUrl = null;
while (matcher.find()) fileUrl = matcher.group(MESSAGE_POSITION);
return new BackendlessFile(fileUrl);
}
} catch (MalformedURLException e) {
throw new IllegalArgumentException(ExceptionMessage.FILE_UPLOAD_ERROR, e);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(ExceptionMessage.FILE_UPLOAD_ERROR, e);
} catch (IOException e) {
throw new BackendlessException(ExceptionMessage.FILE_UPLOAD_ERROR, e.getMessage());
} finally {
if (connection != null)
connection.disconnect();
}
}
use of com.backendless.exceptions.BackendlessException in project Android-SDK by Backendless.
the class Invoker method invokeSync.
public static <T> T invokeSync(String className, String methodName, Object[] args, IChainedResponder chainedResponder) throws BackendlessException {
SyncResponder invokeResponder = new SyncResponder();
if (chainedResponder == null)
chainedResponder = new AdaptingResponder<T>();
chainedResponder.setNextResponder(invokeResponder);
try {
ThreadContext.cleanup();
getWebOrbClient().invoke(className, methodName, args, null, null, HeadersManager.getInstance().getHeaders(), chainedResponder);
} catch (Exception e) {
throw new BackendlessException(ExceptionMessage.SERVER_ERROR, e.getMessage());
}
return (T) invokeResponder.getResult();
}
use of com.backendless.exceptions.BackendlessException in project Android-SDK by Backendless.
the class Persistence method getEntityId.
public static String getEntityId(Object entity) throws BackendlessException {
String id = null;
if (entity instanceof Map)
return (String) ((Map) entity).get(Persistence.DEFAULT_OBJECT_ID_FIELD);
if (ReflectionUtil.hasField(entity.getClass(), Persistence.DEFAULT_OBJECT_ID_FIELD)) {
try {
Field field = ReflectionUtil.getField(entity.getClass(), Persistence.DEFAULT_OBJECT_ID_FIELD);
field.setAccessible(true);
id = (String) field.get(entity);
} catch (NoSuchFieldException | IllegalAccessException e) {
}
} else {
try {
Method declaredMethod = entity.getClass().getMethod(DEFAULT_OBJECT_ID_GETTER);
if (!declaredMethod.isAccessible())
declaredMethod.setAccessible(true);
id = (String) declaredMethod.invoke(entity);
} catch (Exception e) {
id = null;
}
}
if (id == null)
id = FootprintsManager.getInstance().getObjectId(entity);
return id;
}
Aggregations