package de.thedevstack.conversationsplus.services.filetransfer.http; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import de.thedevstack.android.logcat.Logging; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.entities.Message; import de.thedevstack.conversationsplus.http.HttpClient; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.xmpp.OnIqPacketReceived; import de.thedevstack.conversationsplus.xmpp.exceptions.XmppException; import de.thedevstack.conversationsplus.xmpp.filetransfer.http.DeleteSlotPacketParser; import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; /** * Created by steckbrief on 21.08.2016. */ public class DeleteTokenReceived implements OnIqPacketReceived { private static final String HEADER_NAME_DELETE_TOKEN = "X-FILETRANSFER-HTTP-DELETE-TOKEN"; private final DeleteRemoteFile remoteFile; public DeleteTokenReceived(DeleteRemoteFile remoteFile) { this.remoteFile = remoteFile; } @Override public void onIqPacketReceived(Account account, IqPacket packet) { try { String url = this.remoteFile.getPath(); String deleteToken = DeleteSlotPacketParser.parseDeleteToken(packet); Logging.d("filetransfer.http.delete", "Got delete token '" + deleteToken + "' for remote file '" + remoteFile.getPath() + "'"); OkHttpClient client = HttpClient.getClient(true); Request request = new Request.Builder() .url(url) .addHeader(HEADER_NAME_DELETE_TOKEN, deleteToken) .delete() .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Logging.e("filetransfer.http.delete", "Error while connecting to '" + call.request().url() + "': " + e.getMessage()); MessageUtil.markMessage(remoteFile.getMessage(), Message.STATUS_REMOTE_FILE_DELETE_FAILED); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { MessageUtil.markMessage(remoteFile.getMessage(), Message.STATUS_REMOTE_FILE_DELETED); Logging.i("filetransfer.http.delete", "Remote file successfully deleted '" + remoteFile.getPath() + "'"); } else { String detailedMessage = response.body().string(); switch (response.code()) { case 403: case 404: case 500: try { JSONObject jsonObject = new JSONObject(detailedMessage); detailedMessage = jsonObject.getString("msg"); } catch (JSONException e) { Logging.e("filetransfer.http.delete", "Failed to get error message from expected json response: " + detailedMessage); } break; } Logging.e("filetransfer.http.delete", "Could not delete remote file '" + remoteFile.getPath() + "'. Response Code: " + response.code() + ", details: " + detailedMessage); MessageUtil.markMessage(remoteFile.getMessage(), Message.STATUS_REMOTE_FILE_DELETE_FAILED); } } }); } catch (XmppException e) { Logging.e("filetransfer.http.delete", "Error while trying to get the delete token: " + e.getMessage()); MessageUtil.markMessage(remoteFile.getMessage(), Message.STATUS_REMOTE_FILE_DELETE_FAILED); } } }