HttpUploadConnection.java

  1package eu.siacs.conversations.http;
  2
  3import android.app.PendingIntent;
  4import android.util.Log;
  5
  6import java.io.IOException;
  7import java.io.InputStream;
  8import java.io.OutputStream;
  9import java.net.HttpURLConnection;
 10import java.net.MalformedURLException;
 11import java.net.URL;
 12
 13import javax.net.ssl.HttpsURLConnection;
 14
 15import eu.siacs.conversations.Config;
 16import eu.siacs.conversations.entities.Account;
 17import eu.siacs.conversations.entities.DownloadableFile;
 18import eu.siacs.conversations.entities.Message;
 19import eu.siacs.conversations.entities.Transferable;
 20import eu.siacs.conversations.persistance.FileBackend;
 21import eu.siacs.conversations.services.XmppConnectionService;
 22import eu.siacs.conversations.ui.UiCallback;
 23import eu.siacs.conversations.utils.CryptoHelper;
 24import eu.siacs.conversations.utils.Xmlns;
 25import eu.siacs.conversations.xml.Element;
 26import eu.siacs.conversations.xmpp.OnIqPacketReceived;
 27import eu.siacs.conversations.xmpp.jid.Jid;
 28import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 29
 30public class HttpUploadConnection implements Transferable {
 31
 32	private HttpConnectionManager mHttpConnectionManager;
 33	private XmppConnectionService mXmppConnectionService;
 34
 35	private boolean canceled = false;
 36	private boolean delayed = false;
 37	private Account account;
 38	private DownloadableFile file;
 39	private Message message;
 40	private URL mGetUrl;
 41	private URL mPutUrl;
 42
 43	private byte[] key = null;
 44
 45	private long transmitted = 0;
 46	private long expected = 1;
 47
 48	public HttpUploadConnection(HttpConnectionManager httpConnectionManager) {
 49		this.mHttpConnectionManager = httpConnectionManager;
 50		this.mXmppConnectionService = httpConnectionManager.getXmppConnectionService();
 51	}
 52
 53	@Override
 54	public boolean start() {
 55		return false;
 56	}
 57
 58	@Override
 59	public int getStatus() {
 60		return STATUS_UPLOADING;
 61	}
 62
 63	@Override
 64	public long getFileSize() {
 65		return this.file.getExpectedSize();
 66	}
 67
 68	@Override
 69	public int getProgress() {
 70		return (int) ((((double) transmitted) / expected) * 100);
 71	}
 72
 73	@Override
 74	public void cancel() {
 75		this.canceled = true;
 76	}
 77
 78	private void fail() {
 79		mHttpConnectionManager.finishUploadConnection(this);
 80		message.setTransferable(null);
 81		mXmppConnectionService.markMessage(message,Message.STATUS_SEND_FAILED);
 82	}
 83
 84	public void init(Message message, boolean delay) {
 85		this.message = message;
 86		message.setTransferable(this);
 87		mXmppConnectionService.markMessage(message,Message.STATUS_UNSEND);
 88		this.account = message.getConversation().getAccount();
 89		this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
 90		this.file.setExpectedSize(this.file.getSize());
 91		this.delayed = delay;
 92
 93		if (Config.ENCRYPT_ON_HTTP_UPLOADED
 94				|| message.getEncryption() == Message.ENCRYPTION_AXOLOTL
 95				|| message.getEncryption() == Message.ENCRYPTION_OTR) {
 96			this.key = new byte[48];
 97			mXmppConnectionService.getRNG().nextBytes(this.key);
 98			this.file.setKey(this.key);
 99		}
100
101		Jid host = account.getXmppConnection().findDiscoItemByFeature(Xmlns.HTTP_UPLOAD);
102		IqPacket request = mXmppConnectionService.getIqGenerator().requestHttpUploadSlot(host,file);
103		mXmppConnectionService.sendIqPacket(account, request, new OnIqPacketReceived() {
104			@Override
105			public void onIqPacketReceived(Account account, IqPacket packet) {
106				if (packet.getType() == IqPacket.TYPE.RESULT) {
107					Element slot = packet.findChild("slot",Xmlns.HTTP_UPLOAD);
108					if (slot != null) {
109						try {
110							mGetUrl = new URL(slot.findChildContent("get"));
111							mPutUrl = new URL(slot.findChildContent("put"));
112							if (!canceled) {
113								new Thread(new FileUploader()).start();
114							}
115						} catch (MalformedURLException e) {
116							fail();
117						}
118					} else {
119						fail();
120					}
121				} else {
122					fail();
123				}
124			}
125		});
126	}
127
128	private class FileUploader implements Runnable {
129
130		@Override
131		public void run() {
132			this.upload();
133		}
134
135		private void upload() {
136			OutputStream os = null;
137			InputStream is = null;
138			HttpURLConnection connection = null;
139			try {
140				Log.d(Config.LOGTAG, "uploading to " + mPutUrl.toString());
141				connection = (HttpURLConnection) mPutUrl.openConnection();
142				if (connection instanceof HttpsURLConnection) {
143					mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, true);
144				}
145				connection.setRequestMethod("PUT");
146				connection.setFixedLengthStreamingMode((int) file.getExpectedSize());
147				connection.setDoOutput(true);
148				connection.connect();
149				os = connection.getOutputStream();
150				is = file.createInputStream();
151				transmitted = 0;
152				expected = file.getExpectedSize();
153				int count = -1;
154				byte[] buffer = new byte[4096];
155				while (((count = is.read(buffer)) != -1) && !canceled) {
156					transmitted += count;
157					os.write(buffer, 0, count);
158					mXmppConnectionService.updateConversationUi();
159				}
160				os.flush();
161				os.close();
162				is.close();
163				int code = connection.getResponseCode();
164				if (code == 200 || code == 201) {
165					Log.d(Config.LOGTAG, "finished uploading file");
166					Message.FileParams params = message.getFileParams();
167					if (key != null) {
168						mGetUrl = new URL(mGetUrl.toString() + "#" + CryptoHelper.bytesToHex(key));
169					}
170					mXmppConnectionService.getFileBackend().updateFileParams(message, mGetUrl);
171					message.setTransferable(null);
172					message.setCounterpart(message.getConversation().getJid().toBareJid());
173					if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
174						mXmppConnectionService.getPgpEngine().encrypt(message, new UiCallback<Message>() {
175							@Override
176							public void success(Message message) {
177								mXmppConnectionService.resendMessage(message,delayed);
178							}
179
180							@Override
181							public void error(int errorCode, Message object) {
182								fail();
183							}
184
185							@Override
186							public void userInputRequried(PendingIntent pi, Message object) {
187								fail();
188							}
189						});
190					} else {
191						mXmppConnectionService.resendMessage(message,delayed);
192					}
193				} else {
194					fail();
195				}
196			} catch (IOException e) {
197				Log.d(Config.LOGTAG, e.getMessage());
198				fail();
199			} finally {
200				FileBackend.close(is);
201				FileBackend.close(os);
202				if (connection != null) {
203					connection.disconnect();
204				}
205			}
206		}
207	}
208}