Commit c306e8da authored by Ivan Del Pino's avatar Ivan Del Pino Committed by Rafael Jesus Hernández Vasquez

feat(files): request download file and save in disk

Signed-off-by: 's avatarIvan Del Pino <idelpino@teclib.com>
parent c175f4e3
......@@ -16,6 +16,7 @@
* GNU General Public License for more details.
* --------------------------------------------------------------------
* @author Rafael Hernandez - <rhernandez@teclib.com>
* @author Ivan Del Pino - <idelpino@teclib.com>
* @copyright (C) 2017 Teclib' and contributors.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/glpi-project/java-library-glpi
......@@ -46,12 +47,16 @@ import org.glpi.api.GLPI;
import org.glpi.api.itemType;
import org.glpi.api.response.FullSessionModel;
import org.glpi.api.response.InitSession;
import org.glpi.api.utils.Helpers;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import okhttp3.ResponseBody;
public class MainActivity extends AppCompatActivity {
private List<String> resultList = new ArrayList<>();
......@@ -521,11 +526,15 @@ public class MainActivity extends AppCompatActivity {
private void btnFile() {
progressBar.setVisibility(View.VISIBLE);
resultList.clear();
glpi.downloadFileMD(new GLPI.VoidCallback() {
String url = "https://raw.githubusercontent.com/flyve-mdm/android-mdm-agent/develop/CHANGELOG.md";
glpi.downloadFile(url, new GLPI.ResponseHandle<ResponseBody, String>() {
@Override
public void onResponse(String response) {
public void onResponse(ResponseBody response) {
FlyveLog.i("Download file: %s", response);
updateAdapter("Success: Download file");
String pathname = getExternalFilesDir(null) + File.separator + "CHANGELOG.md";
boolean inDisk = Helpers.writeResponseBodyToDisk(response, pathname);
FlyveLog.i(inDisk + "");
}
@Override
......@@ -534,9 +543,9 @@ public class MainActivity extends AppCompatActivity {
updateAdapter("Error: Download file" + errorMessage);
}
});
glpi.getPluginPackage("fileId", new GLPI.VoidCallback() {
glpi.getPluginPackage("fileId", new GLPI.ResponseHandle<JsonArray, String>() {
@Override
public void onResponse(String response) {
public void onResponse(JsonArray response) {
FlyveLog.i("Package plugin: %s", response);
updateAdapter("Success: Package plugin");
}
......@@ -547,9 +556,9 @@ public class MainActivity extends AppCompatActivity {
updateAdapter("Error: Package plugin" + errorMessage);
}
});
glpi.getPluginFile("fileId", new GLPI.VoidCallback() {
glpi.getPluginFile("fileId", new GLPI.ResponseHandle<JsonArray, String>() {
@Override
public void onResponse(String response) {
public void onResponse(JsonArray response) {
FlyveLog.i("File plugin: %s", response);
updateAdapter("Success: File plugin");
progressBar.setVisibility(View.GONE);
......
......@@ -16,6 +16,7 @@
* GNU General Public License for more details.
* --------------------------------------------------------------------
* @author Rafael Hernandez - <rhernandez@teclib.com>
* @author Ivan Del Pino - <idelpino@teclib.com>
* @copyright (C) 2017 Teclib' and contributors.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/glpi-project/java-library-glpi
......@@ -47,6 +48,7 @@ import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
......@@ -179,7 +181,7 @@ public class GLPI extends ServiceGenerator {
}
@Override
public void onFailure(Call<InitSession> call, Throwable t) {
public void onFailure(@NonNull Call<InitSession> call, @NonNull Throwable t) {
callback.onFailure(t.getMessage());
}
});
......@@ -314,7 +316,7 @@ public class GLPI extends ServiceGenerator {
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
public void onFailure(@NonNull Call<JsonObject> call, @NonNull Throwable t) {
callback.onFailure(t.getMessage());
}
});
......@@ -380,7 +382,7 @@ public class GLPI extends ServiceGenerator {
private void responseJsonArray(final ResponseHandle<JsonArray, String> callback, Call<JsonArray> responseCall) {
responseCall.enqueue(new Callback<JsonArray>() {
@Override
public void onResponse(Call<JsonArray> call, Response<JsonArray> response) {
public void onResponse(@NonNull Call<JsonArray> call, @NonNull Response<JsonArray> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
......@@ -395,7 +397,7 @@ public class GLPI extends ServiceGenerator {
}
@Override
public void onFailure(Call<JsonArray> call, Throwable t) {
public void onFailure(@NonNull Call<JsonArray> call, @NonNull Throwable t) {
callback.onFailure(t.getMessage());
}
});
......@@ -461,7 +463,7 @@ public class GLPI extends ServiceGenerator {
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
callback.onFailure(t.getMessage());
}
});
......@@ -476,7 +478,7 @@ public class GLPI extends ServiceGenerator {
responseKillSession(callback, interfaces.killSession(getHeader()));
}
private void responseKillSession(final ResponseHandle<String, String> callback, Call<Void> responseCall) {
private void responseKillSession(final ResponseHandle<String, String> callback, Call<Void> responseCall) {
responseCall.enqueue(new Callback<Void>() {
@Override
public void onResponse(@NonNull Call<Void> call, @NonNull Response<Void> response) {
......@@ -495,7 +497,91 @@ public class GLPI extends ServiceGenerator {
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
callback.onFailure(t.getMessage());
}
});
}
/**
*
*
* @param callback here you are going to get the asynchronous response
*/
public void getPluginFile(String fileId, final ResponseHandle<JsonArray, String> callback) {
HashMap<String, String> header = new HashMap<>();
header.put("Session-Token", sessionToken);
responseFile(callback, interfaces.getPluginFile(header, fileId));
}
/**
* Destroy a session identified by a session token.
*
* @param callback here you are going to get the asynchronous response
*/
public void getPluginPackage(String fileId, final ResponseHandle<JsonArray, String> callback) {
HashMap<String, String> header = new HashMap<>();
header.put("Session-Token", sessionToken);
responseFile(callback, interfaces.getPluginPackage(header, fileId));
}
private void responseFile(final ResponseHandle<JsonArray, String> callback, Call<JsonArray> responseCall) {
responseCall.enqueue(new Callback<JsonArray>() {
@Override
public void onResponse(@NonNull Call<JsonArray> call, @NonNull Response<JsonArray> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(@NonNull Call<JsonArray> call, @NonNull Throwable t) {
callback.onFailure(t.getMessage());
}
});
}
/**
* Download file specifying route.
*
* @param callback here you are going to get the asynchronous response
* @param url: route to download file
*/
public void downloadFile(String url, final ResponseHandle<ResponseBody, String> callback) {
HashMap<String, String> header = new HashMap<>();
header.put("Accept","application/octet-stream");
header.put("Content-Type","application/json");
header.put("Session-Token", sessionToken);
responseFileDownload(callback, interfaces.downloadFile(url, header));
}
private void responseFileDownload(final ResponseHandle<ResponseBody, String> callback, Call<ResponseBody> responseCall) {
responseCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
callback.onFailure(t.getMessage());
}
});
......
......@@ -16,6 +16,7 @@
* GNU General Public License for more details.
* --------------------------------------------------------------------
* @author Rafael Hernandez - <rhernandez@teclib.com>
* @author Ivan Del Pino - <idelpino@teclib.com>
* @copyright (C) 2017 Teclib' and contributors.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/glpi-project/java-library-glpi
......@@ -27,6 +28,7 @@ package org.glpi.api;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.glpi.api.request.ChangeActiveEntitiesRequest;
import org.glpi.api.request.ChangeActiveProfileRequest;
......@@ -38,6 +40,7 @@ import org.json.JSONObject;
import java.util.Map;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
......@@ -145,14 +148,14 @@ public interface Routes {
/*Get File */
@Headers("Content-Type: application/json")
@GET("PluginFlyvemdmFile/{fileId}")
Call<JsonObject> getPluginFile(@HeaderMap Map<String, String> headers, @Path("fileId") String fileId);
Call<JsonArray> getPluginFile(@HeaderMap Map<String, String> headers, @Path("fileId") String fileId);
@Headers("Content-Type: application/json")
@GET("PluginFlyvemdmPackage/{fileId}")
Call<JsonObject> getPluginPackage(@HeaderMap Map<String, String> headers, @Path("fileId") String fileId);
Call<JsonArray> getPluginPackage(@HeaderMap Map<String, String> headers, @Path("fileId") String fileId);
@Headers("Content-Type: application/json")
@GET("CHANGELOG.md")
Call<JsonObject> downloadFileMD(@Url String url, @HeaderMap Map<String, String> headers);
@GET
Call<ResponseBody> downloadFile(@Url String url, @HeaderMap Map<String, String> headers);
}
......@@ -16,6 +16,7 @@
* GNU General Public License for more details.
* --------------------------------------------------------------------
* @author Rafael Hernandez - <rhernandez@teclib.com>
* @author Ivan Del Pino - <idelpino@teclib.com>
* @copyright (C) 2017 Teclib' and contributors.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/glpi-project/java-library-glpi
......@@ -42,7 +43,7 @@ public class ServiceGenerator {
/**
* Initial connection configuration
*
* @param url
* @param url: base url
*/
void start(String url) {
......
......@@ -27,8 +27,15 @@ package org.glpi.api.utils;
import android.util.Base64;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import okhttp3.ResponseBody;
public class Helpers {
/**
......@@ -72,4 +79,54 @@ public class Helpers {
}
return rtext.trim();
}
/**
* @param body: response file
* @param pathname: route to download
* example: getExternalFilesDir(null) + File.separator + "Future Studio Icon.png"
* @return if was success or no
*/
public static boolean writeResponseBodyToDisk(ResponseBody body, String pathname) {
try {
File futureStudioIconFile = new File(pathname);
InputStream inputStream = null;
OutputStream outputStream = null;
try {
byte[] fileReader = new byte[4096];
long fileSize = body.contentLength();
long fileSizeDownloaded = 0;
inputStream = body.byteStream();
outputStream = new FileOutputStream(futureStudioIconFile);
while (true) {
int read = inputStream.read(fileReader);
if (read == -1) {
break;
}
outputStream.write(fileReader, 0, read);
fileSizeDownloaded += read;
}
outputStream.flush();
return true;
} catch (IOException e) {
return false;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
} catch (IOException e) {
return false;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment