anotherim/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java

242 lines
7.4 KiB
Java
Raw Normal View History

package eu.siacs.conversations.http;
2015-06-29 13:38:16 +00:00
import android.app.PendingIntent;
2015-07-29 21:45:37 +00:00
import android.content.Intent;
import android.net.Uri;
import android.os.PowerManager;
import android.util.Log;
import android.util.Pair;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
2015-11-28 19:11:38 +00:00
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
2015-11-28 19:11:38 +00:00
import java.net.Proxy;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message;
2015-07-20 12:26:29 +00:00
import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.AbstractConnectionManager;
import eu.siacs.conversations.services.XmppConnectionService;
2015-06-29 13:38:16 +00:00
import eu.siacs.conversations.ui.UiCallback;
import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.Xmlns;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
2015-07-10 13:11:03 +00:00
public class HttpUploadConnection implements Transferable {
private HttpConnectionManager mHttpConnectionManager;
private XmppConnectionService mXmppConnectionService;
private boolean canceled = false;
2015-07-20 16:11:33 +00:00
private boolean delayed = false;
private Account account;
private DownloadableFile file;
private Message message;
private String mime;
private URL mGetUrl;
private URL mPutUrl;
2015-11-28 19:11:38 +00:00
private boolean mUseTor = false;
private byte[] key = null;
private long transmitted = 0;
private InputStream mFileInputStream;
public HttpUploadConnection(HttpConnectionManager httpConnectionManager) {
this.mHttpConnectionManager = httpConnectionManager;
this.mXmppConnectionService = httpConnectionManager.getXmppConnectionService();
2015-11-28 19:11:38 +00:00
this.mUseTor = mXmppConnectionService.useTorToConnect();
}
@Override
public boolean start() {
return false;
}
@Override
public int getStatus() {
return STATUS_UPLOADING;
}
@Override
public long getFileSize() {
return file == null ? 0 : file.getExpectedSize();
}
@Override
public int getProgress() {
if (file == null) {
return 0;
}
return (int) ((((double) transmitted) / file.getExpectedSize()) * 100);
}
@Override
public void cancel() {
this.canceled = true;
}
private void fail() {
mHttpConnectionManager.finishUploadConnection(this);
2015-07-10 13:11:03 +00:00
message.setTransferable(null);
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
FileBackend.close(mFileInputStream);
}
2015-07-20 16:11:33 +00:00
public void init(Message message, boolean delay) {
this.message = message;
this.account = message.getConversation().getAccount();
this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
this.mime = this.file.getMimeType();
2015-07-20 16:11:33 +00:00
this.delayed = delay;
if (Config.ENCRYPT_ON_HTTP_UPLOADED
|| message.getEncryption() == Message.ENCRYPTION_AXOLOTL
|| message.getEncryption() == Message.ENCRYPTION_OTR) {
this.key = new byte[48];
mXmppConnectionService.getRNG().nextBytes(this.key);
this.file.setKeyAndIv(this.key);
}
Pair<InputStream,Integer> pair;
try {
pair = AbstractConnectionManager.createInputStream(file, true);
} catch (FileNotFoundException e) {
fail();
return;
}
this.file.setExpectedSize(pair.second);
this.mFileInputStream = pair.first;
Jid host = account.getXmppConnection().findDiscoItemByFeature(Xmlns.HTTP_UPLOAD);
IqPacket request = mXmppConnectionService.getIqGenerator().requestHttpUploadSlot(host,file,mime);
mXmppConnectionService.sendIqPacket(account, request, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE.RESULT) {
Element slot = packet.findChild("slot",Xmlns.HTTP_UPLOAD);
if (slot != null) {
try {
mGetUrl = new URL(slot.findChildContent("get"));
mPutUrl = new URL(slot.findChildContent("put"));
if (!canceled) {
new Thread(new FileUploader()).start();
}
} catch (MalformedURLException e) {
fail();
}
} else {
fail();
}
} else {
fail();
}
}
});
message.setTransferable(this);
mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
}
private class FileUploader implements Runnable {
@Override
public void run() {
this.upload();
}
private void upload() {
OutputStream os = null;
HttpURLConnection connection = null;
PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_upload_"+message.getUuid());
try {
wakeLock.acquire();
Log.d(Config.LOGTAG, "uploading to " + mPutUrl.toString());
2015-11-28 19:11:38 +00:00
if (mUseTor) {
connection = (HttpURLConnection) mPutUrl.openConnection(mHttpConnectionManager.getProxy());
} else {
connection = (HttpURLConnection) mPutUrl.openConnection();
}
if (connection instanceof HttpsURLConnection) {
mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, true);
}
connection.setRequestMethod("PUT");
connection.setFixedLengthStreamingMode((int) file.getExpectedSize());
connection.setRequestProperty("Content-Type", mime == null ? "application/octet-stream" : mime);
connection.setRequestProperty("User-Agent",mXmppConnectionService.getIqGenerator().getIdentityName());
connection.setDoOutput(true);
connection.connect();
os = connection.getOutputStream();
transmitted = 0;
int count = -1;
byte[] buffer = new byte[4096];
while (((count = mFileInputStream.read(buffer)) != -1) && !canceled) {
transmitted += count;
os.write(buffer, 0, count);
mXmppConnectionService.updateConversationUi();
}
os.flush();
os.close();
mFileInputStream.close();
int code = connection.getResponseCode();
if (code == 200 || code == 201) {
Log.d(Config.LOGTAG, "finished uploading file");
if (key != null) {
mGetUrl = new URL(mGetUrl.toString() + "#" + CryptoHelper.bytesToHex(key));
}
mXmppConnectionService.getFileBackend().updateFileParams(message, mGetUrl);
2015-07-29 21:45:37 +00:00
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
mXmppConnectionService.sendBroadcast(intent);
2015-07-10 13:11:03 +00:00
message.setTransferable(null);
message.setCounterpart(message.getConversation().getJid().toBareJid());
2015-06-29 13:38:16 +00:00
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
mXmppConnectionService.getPgpEngine().encrypt(message, new UiCallback<Message>() {
@Override
public void success(Message message) {
2015-07-20 16:11:33 +00:00
mXmppConnectionService.resendMessage(message,delayed);
2015-06-29 13:38:16 +00:00
}
@Override
public void error(int errorCode, Message object) {
fail();
}
@Override
public void userInputRequried(PendingIntent pi, Message object) {
fail();
}
});
} else {
2015-07-29 21:45:37 +00:00
mXmppConnectionService.resendMessage(message, delayed);
2015-06-29 13:38:16 +00:00
}
} else {
fail();
}
} catch (IOException e) {
2015-07-29 21:45:37 +00:00
e.printStackTrace();
Log.d(Config.LOGTAG,"http upload failed "+e.getMessage());
fail();
} finally {
FileBackend.close(mFileInputStream);
FileBackend.close(os);
if (connection != null) {
connection.disconnect();
}
wakeLock.release();
}
}
}
}