initial http upload support

be careful: little error handling and no encryption
This commit is contained in:
Daniel Gultsch 2015-06-28 11:19:07 +02:00
parent 6ee11e5323
commit 9eb9a52205
14 changed files with 278 additions and 95 deletions

View File

@ -2,27 +2,25 @@ package eu.siacs.conversations.entities;
public interface Downloadable { public interface Downloadable {
public final String[] VALID_IMAGE_EXTENSIONS = {"webp", "jpeg", "jpg", "png", "jpe"}; String[] VALID_IMAGE_EXTENSIONS = {"webp", "jpeg", "jpg", "png", "jpe"};
public final String[] VALID_CRYPTO_EXTENSIONS = {"pgp", "gpg", "otr"}; String[] VALID_CRYPTO_EXTENSIONS = {"pgp", "gpg", "otr"};
public static final int STATUS_UNKNOWN = 0x200; int STATUS_UNKNOWN = 0x200;
public static final int STATUS_CHECKING = 0x201; int STATUS_CHECKING = 0x201;
public static final int STATUS_FAILED = 0x202; int STATUS_FAILED = 0x202;
public static final int STATUS_OFFER = 0x203; int STATUS_OFFER = 0x203;
public static final int STATUS_DOWNLOADING = 0x204; int STATUS_DOWNLOADING = 0x204;
public static final int STATUS_DELETED = 0x205; int STATUS_DELETED = 0x205;
public static final int STATUS_OFFER_CHECK_FILESIZE = 0x206; int STATUS_OFFER_CHECK_FILESIZE = 0x206;
public static final int STATUS_UPLOADING = 0x207; int STATUS_UPLOADING = 0x207;
public boolean start(); boolean start();
public int getStatus(); int getStatus();
public long getFileSize(); long getFileSize();
public int getProgress(); int getProgress();
public String getMimeType(); void cancel();
public void cancel();
} }

View File

@ -27,11 +27,6 @@ public class DownloadablePlaceholder implements Downloadable {
return 0; return 0;
} }
@Override
public String getMimeType() {
return "";
}
@Override @Override
public void cancel() { public void cancel() {

View File

@ -586,6 +586,14 @@ public class Message extends AbstractEntity {
return type == TYPE_FILE || type == TYPE_IMAGE; return type == TYPE_FILE || type == TYPE_IMAGE;
} }
public boolean hasFileOnRemoteHost() {
return isFileOrImage() && getImageParams().url != null;
}
public boolean needsUploading() {
return isFileOrImage() && getImageParams().url == null;
}
public class ImageParams { public class ImageParams {
public URL url; public URL url;
public long size = 0; public long size = 0;

View File

@ -6,6 +6,7 @@ import java.util.List;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.services.MessageArchiveService; import eu.siacs.conversations.services.MessageArchiveService;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.PhoneHelper; import eu.siacs.conversations.utils.PhoneHelper;
@ -194,4 +195,13 @@ public class IqGenerator extends AbstractGenerator {
item.setAttribute("role", role); item.setAttribute("role", role);
return packet; return packet;
} }
public IqPacket requestHttpUploadSlot(Jid host, DownloadableFile file) {
IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
packet.setTo(host);
Element request = packet.addChild("request",Xmlns.HTTP_UPLOAD);
request.addChild("filename").setContent(file.getName());
request.addChild("size").setContent(String.valueOf(file.getExpectedSize()));
return packet;
}
} }

View File

@ -73,7 +73,13 @@ public class MessageGenerator extends AbstractGenerator {
packet.addChild("no-copy", "urn:xmpp:hints"); packet.addChild("no-copy", "urn:xmpp:hints");
packet.addChild("no-permanent-store", "urn:xmpp:hints"); packet.addChild("no-permanent-store", "urn:xmpp:hints");
try { try {
packet.setBody(otrSession.transformSending(message.getBody())[0]); String content;
if (message.hasFileOnRemoteHost()) {
content = message.getImageParams().url.toString();
} else {
content = message.getBody();
}
packet.setBody(otrSession.transformSending(content)[0]);
return packet; return packet;
} catch (OtrException e) { } catch (OtrException e) {
return null; return null;
@ -86,7 +92,11 @@ public class MessageGenerator extends AbstractGenerator {
public MessagePacket generateChat(Message message, boolean addDelay) { public MessagePacket generateChat(Message message, boolean addDelay) {
MessagePacket packet = preparePacket(message, addDelay); MessagePacket packet = preparePacket(message, addDelay);
if (message.hasFileOnRemoteHost()) {
packet.setBody(message.getImageParams().url.toString());
} else {
packet.setBody(message.getBody()); packet.setBody(message.getBody());
}
return packet; return packet;
} }
@ -96,13 +106,11 @@ public class MessageGenerator extends AbstractGenerator {
public MessagePacket generatePgpChat(Message message, boolean addDelay) { public MessagePacket generatePgpChat(Message message, boolean addDelay) {
MessagePacket packet = preparePacket(message, addDelay); MessagePacket packet = preparePacket(message, addDelay);
packet.setBody("This is an XEP-0027 encryted message"); packet.setBody("This is an XEP-0027 encrypted message");
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
packet.addChild("x", "jabber:x:encrypted").setContent( packet.addChild("x", "jabber:x:encrypted").setContent(message.getEncryptedBody());
message.getEncryptedBody());
} else if (message.getEncryption() == Message.ENCRYPTION_PGP) { } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
packet.addChild("x", "jabber:x:encrypted").setContent( packet.addChild("x", "jabber:x:encrypted").setContent(message.getBody());
message.getBody());
} }
return packet; return packet;
} }

View File

@ -302,9 +302,4 @@ public class HttpConnection implements Downloadable {
public int getProgress() { public int getProgress() {
return this.mProgress; return this.mProgress;
} }
@Override
public String getMimeType() {
return "";
}
} }

View File

@ -13,7 +13,8 @@ public class HttpConnectionManager extends AbstractConnectionManager {
super(service); super(service);
} }
private List<HttpConnection> connections = new CopyOnWriteArrayList<HttpConnection>(); private List<HttpConnection> connections = new CopyOnWriteArrayList<>();
private List<HttpUploadConnection> uploadConnections = new CopyOnWriteArrayList<>();
public HttpConnection createNewConnection(Message message) { public HttpConnection createNewConnection(Message message) {
HttpConnection connection = new HttpConnection(this); HttpConnection connection = new HttpConnection(this);
@ -22,7 +23,18 @@ public class HttpConnectionManager extends AbstractConnectionManager {
return connection; return connection;
} }
public HttpUploadConnection createNewUploadConnection(Message message) {
HttpUploadConnection connection = new HttpUploadConnection(this);
connection.init(message);
this.uploadConnections.add(connection);
return connection;
}
public void finishConnection(HttpConnection connection) { public void finishConnection(HttpConnection connection) {
this.connections.remove(connection); this.connections.remove(connection);
} }
public void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
this.uploadConnections.remove(httpUploadConnection);
}
} }

View File

@ -0,0 +1,164 @@
package eu.siacs.conversations.http;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.XmppConnectionService;
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;
public class HttpUploadConnection implements Downloadable {
private HttpConnectionManager mHttpConnectionManager;
private XmppConnectionService mXmppConnectionService;
private boolean canceled = false;
private Account account;
private DownloadableFile file;
private Message message;
private URL mGetUrl;
private URL mPutUrl;
private long transmitted = 0;
private long expected = 1;
public HttpUploadConnection(HttpConnectionManager httpConnectionManager) {
this.mHttpConnectionManager = httpConnectionManager;
this.mXmppConnectionService = httpConnectionManager.getXmppConnectionService();
}
@Override
public boolean start() {
return false;
}
@Override
public int getStatus() {
return STATUS_UPLOADING;
}
@Override
public long getFileSize() {
return this.file.getExpectedSize();
}
@Override
public int getProgress() {
return (int) ((((double) transmitted) / expected) * 100);
}
@Override
public void cancel() {
this.canceled = true;
}
private void fail() {
mHttpConnectionManager.finishUploadConnection(this);
message.setDownloadable(null);
mXmppConnectionService.markMessage(message,Message.STATUS_SEND_FAILED);
}
public void init(Message message) {
this.message = message;
message.setDownloadable(this);
mXmppConnectionService.markMessage(message,Message.STATUS_UNSEND);
this.account = message.getConversation().getAccount();
this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
this.file.setExpectedSize(this.file.getSize());
Jid host = account.getXmppConnection().findDiscoItemByFeature(Xmlns.HTTP_UPLOAD);
IqPacket request = mXmppConnectionService.getIqGenerator().requestHttpUploadSlot(host,file);
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();
}
}
});
}
private class FileUploader implements Runnable {
@Override
public void run() {
this.upload();
}
private void upload() {
OutputStream os = null;
InputStream is = null;
HttpURLConnection connection = null;
try {
Log.d(Config.LOGTAG, "uploading to " + mPutUrl.toString());
connection = (HttpURLConnection) mPutUrl.openConnection();
connection.setRequestMethod("PUT");
connection.setFixedLengthStreamingMode((int) file.getExpectedSize());
connection.setDoOutput(true);
connection.connect();
os = connection.getOutputStream();
is = file.createInputStream();
transmitted = 0;
expected = file.getExpectedSize();
int count = -1;
byte[] buffer = new byte[4096];
while (((count = is.read(buffer)) != -1) && !canceled) {
transmitted += count;
os.write(buffer, 0, count);
mXmppConnectionService.updateConversationUi();
}
os.flush();
os.close();
is.close();
int code = connection.getResponseCode();
if (code == 200) {
Log.d(Config.LOGTAG, "finished uploading file");
Message.ImageParams params = message.getImageParams();
message.setBody(mGetUrl.toString()+"|"+String.valueOf(params.size)+"|"+String.valueOf(params.width)+"|"+String.valueOf(params.height));
message.setDownloadable(null);
mXmppConnectionService.resendMessage(message);
} else {
fail();
}
} catch (IOException e) {
Log.d(Config.LOGTAG, e.getMessage());
fail();
} finally {
FileBackend.close(is);
FileBackend.close(os);
if (connection != null) {
connection.disconnect();
}
}
}
}
}

View File

@ -217,7 +217,7 @@ public class FileBackend {
long size = file.getSize(); long size = file.getSize();
int width = scaledBitmap.getWidth(); int width = scaledBitmap.getWidth();
int height = scaledBitmap.getHeight(); int height = scaledBitmap.getHeight();
message.setBody(Long.toString(size) + ',' + width + ',' + height); message.setBody(Long.toString(size) + '|' + width + '|' + height);
return file; return file;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new FileCopyException(R.string.error_file_not_found); throw new FileCopyException(R.string.error_file_not_found);

View File

@ -671,6 +671,17 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
} }
private void sendFileMessage(final Message message) {
Log.d(Config.LOGTAG, "send file message");
final Account account = message.getConversation().getAccount();
final XmppConnection connection = account.getXmppConnection();
if (connection != null && connection.getFeatures().httpUpload()) {
mHttpConnectionManager.createNewUploadConnection(message);
} else {
mJingleConnectionManager.createNewConnection(message);
}
}
public void sendMessage(final Message message) { public void sendMessage(final Message message) {
final Account account = message.getConversation().getAccount(); final Account account = message.getConversation().getAccount();
account.deactivateGracePeriod(); account.deactivateGracePeriod();
@ -680,7 +691,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
boolean send = false; boolean send = false;
if (account.getStatus() == Account.State.ONLINE if (account.getStatus() == Account.State.ONLINE
&& account.getXmppConnection() != null) { && account.getXmppConnection() != null) {
if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) { if (message.needsUploading()) {
if (message.getCounterpart() != null) { if (message.getCounterpart() != null) {
if (message.getEncryption() == Message.ENCRYPTION_OTR) { if (message.getEncryption() == Message.ENCRYPTION_OTR) {
if (!conv.hasValidOtrSession()) { if (!conv.hasValidOtrSession()) {
@ -688,11 +699,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
message.setStatus(Message.STATUS_WAITING); message.setStatus(Message.STATUS_WAITING);
} else if (conv.hasValidOtrSession() } else if (conv.hasValidOtrSession()
&& conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) { && conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
mJingleConnectionManager mJingleConnectionManager.createNewConnection(message);
.createNewConnection(message);
} }
} else { } else {
mJingleConnectionManager.createNewConnection(message); this.sendFileMessage(message);
} }
} else { } else {
if (message.getEncryption() == Message.ENCRYPTION_OTR) { if (message.getEncryption() == Message.ENCRYPTION_OTR) {
@ -791,12 +802,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}); });
} }
private void resendMessage(final Message message) { public void resendMessage(final Message message) {
Account account = message.getConversation().getAccount(); Account account = message.getConversation().getAccount();
MessagePacket packet = null; MessagePacket packet = null;
if (message.getEncryption() == Message.ENCRYPTION_OTR) { if (message.getEncryption() == Message.ENCRYPTION_OTR) {
Presences presences = message.getConversation().getContact() Presences presences = message.getConversation().getContact().getPresences();
.getPresences();
if (!message.getConversation().hasValidOtrSession()) { if (!message.getConversation().hasValidOtrSession()) {
if ((message.getCounterpart() != null) if ((message.getCounterpart() != null)
&& (presences.has(message.getCounterpart().getResourcepart()))) { && (presences.has(message.getCounterpart().getResourcepart()))) {
@ -808,34 +818,25 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
} }
} else { } else {
if (message.getConversation().getOtrSession() if (message.getConversation().getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
.getSessionStatus() == SessionStatus.ENCRYPTED) {
try { try {
message.setCounterpart(Jid.fromSessionID(message.getConversation().getOtrSession().getSessionID())); message.setCounterpart(Jid.fromSessionID(message.getConversation().getOtrSession().getSessionID()));
if (message.getType() == Message.TYPE_TEXT) { if (message.needsUploading()) {
packet = mMessageGenerator.generateOtrChat(message,
true);
} else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
mJingleConnectionManager.createNewConnection(message); mJingleConnectionManager.createNewConnection(message);
} else {
packet = mMessageGenerator.generateOtrChat(message, true);
} }
} catch (final InvalidJidException ignored) { } catch (final InvalidJidException ignored) {
} }
} }
} }
} else if (message.getType() == Message.TYPE_TEXT) { } else if (message.needsUploading()) {
if (message.getEncryption() == Message.ENCRYPTION_NONE) {
packet = mMessageGenerator.generateChat(message, true);
} else if ((message.getEncryption() == Message.ENCRYPTION_DECRYPTED)
|| (message.getEncryption() == Message.ENCRYPTION_PGP)) {
packet = mMessageGenerator.generatePgpChat(message, true);
}
} else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
Contact contact = message.getConversation().getContact(); Contact contact = message.getConversation().getContact();
Presences presences = contact.getPresences(); Presences presences = contact.getPresences();
if ((message.getCounterpart() != null) if ((message.getCounterpart() != null)
&& (presences.has(message.getCounterpart().getResourcepart()))) { && (presences.has(message.getCounterpart().getResourcepart()))) {
mJingleConnectionManager.createNewConnection(message); this.sendFileMessage(message);
} else { } else {
if (presences.size() == 1) { if (presences.size() == 1) {
String presence = presences.asStringArray()[0]; String presence = presences.asStringArray()[0];
@ -844,9 +845,16 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} catch (InvalidJidException e) { } catch (InvalidJidException e) {
return; return;
} }
mJingleConnectionManager.createNewConnection(message); this.sendFileMessage(message);
} }
} }
} else {
if (message.getEncryption() == Message.ENCRYPTION_NONE) {
packet = mMessageGenerator.generateChat(message, true);
} else if ((message.getEncryption() == Message.ENCRYPTION_DECRYPTED)
|| (message.getEncryption() == Message.ENCRYPTION_PGP)) {
packet = mMessageGenerator.generatePgpChat(message, true);
}
} }
if (packet != null) { if (packet != null) {
if (!account.getXmppConnection().getFeatures().sm() if (!account.getXmppConnection().getFeatures().sm()
@ -1809,15 +1817,15 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} catch (InvalidJidException e) { } catch (InvalidJidException e) {
return; return;
} }
if (message.getType() == Message.TYPE_TEXT) { if (message.needsUploading()) {
mJingleConnectionManager.createNewConnection(message);
} else {
MessagePacket outPacket = mMessageGenerator.generateOtrChat(message, true); MessagePacket outPacket = mMessageGenerator.generateOtrChat(message, true);
if (outPacket != null) { if (outPacket != null) {
message.setStatus(Message.STATUS_SEND); message.setStatus(Message.STATUS_SEND);
databaseBackend.updateMessage(message); databaseBackend.updateMessage(message);
sendMessagePacket(account, outPacket); sendMessagePacket(account, outPacket);
} }
} else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
mJingleConnectionManager.createNewConnection(message);
} }
updateConversationUi(); updateConversationUi();
} }

View File

@ -5,4 +5,5 @@ public final class Xmlns {
public static final String ROSTER = "jabber:iq:roster"; public static final String ROSTER = "jabber:iq:roster";
public static final String REGISTER = "jabber:iq:register"; public static final String REGISTER = "jabber:iq:register";
public static final String BYTE_STREAMS = "http://jabber.org/protocol/bytestreams"; public static final String BYTE_STREAMS = "http://jabber.org/protocol/bytestreams";
public static final String HTTP_UPLOAD = "urn:xmpp:http:upload";
} }

View File

@ -1025,18 +1025,18 @@ public class XmppConnection implements Runnable {
this.streamId = null; this.streamId = null;
} }
public List<String> findDiscoItemsByFeature(final String feature) { public List<Jid> findDiscoItemsByFeature(final String feature) {
final List<String> items = new ArrayList<>(); final List<Jid> items = new ArrayList<>();
for (final Entry<Jid, Info> cursor : disco.entrySet()) { for (final Entry<Jid, Info> cursor : disco.entrySet()) {
if (cursor.getValue().features.contains(feature)) { if (cursor.getValue().features.contains(feature)) {
items.add(cursor.getKey().toString()); items.add(cursor.getKey());
} }
} }
return items; return items;
} }
public String findDiscoItemByFeature(final String feature) { public Jid findDiscoItemByFeature(final String feature) {
final List<String> items = findDiscoItemsByFeature(feature); final List<Jid> items = findDiscoItemsByFeature(feature);
if (items.size() >= 1) { if (items.size() >= 1) {
return items.get(0); return items.get(0);
} }
@ -1191,6 +1191,10 @@ public class XmppConnection implements Runnable {
public void setBlockListRequested(boolean value) { public void setBlockListRequested(boolean value) {
this.blockListRequested = value; this.blockListRequested = value;
} }
public boolean httpUpload() {
return findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD).size() > 0;
}
} }
private IqGenerator getIqGenerator() { private IqGenerator getIqGenerator() {

View File

@ -954,24 +954,4 @@ public class JingleConnection implements Downloadable {
public int getProgress() { public int getProgress() {
return this.mProgress; return this.mProgress;
} }
@Override
public String getMimeType() {
if (this.message.getType() == Message.TYPE_FILE) {
String mime = null;
String path = this.message.getRelativeFilePath();
if (path != null && !this.message.getRelativeFilePath().isEmpty()) {
mime = URLConnection.guessContentTypeFromName(this.message.getRelativeFilePath());
if (mime!=null) {
return mime;
} else {
return "";
}
} else {
return "";
}
} else {
return "image/webp";
}
}
} }

View File

@ -87,10 +87,10 @@ public class JingleConnectionManager extends AbstractConnectionManager {
return; return;
} }
if (!this.primaryCandidates.containsKey(account.getJid().toBareJid())) { if (!this.primaryCandidates.containsKey(account.getJid().toBareJid())) {
final String proxy = account.getXmppConnection().findDiscoItemByFeature(Xmlns.BYTE_STREAMS); final Jid proxy = account.getXmppConnection().findDiscoItemByFeature(Xmlns.BYTE_STREAMS);
if (proxy != null) { if (proxy != null) {
IqPacket iq = new IqPacket(IqPacket.TYPE.GET); IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setAttribute("to", proxy); iq.setTo(proxy);
iq.query(Xmlns.BYTE_STREAMS); iq.query(Xmlns.BYTE_STREAMS);
account.getXmppConnection().sendIqPacket(iq,new OnIqPacketReceived() { account.getXmppConnection().sendIqPacket(iq,new OnIqPacketReceived() {
@ -105,11 +105,11 @@ public class JingleConnectionManager extends AbstractConnectionManager {
candidate.setHost(host); candidate.setHost(host);
candidate.setPort(Integer.parseInt(port)); candidate.setPort(Integer.parseInt(port));
candidate.setType(JingleCandidate.TYPE_PROXY); candidate.setType(JingleCandidate.TYPE_PROXY);
candidate.setJid(Jid.fromString(proxy)); candidate.setJid(proxy);
candidate.setPriority(655360 + 65535); candidate.setPriority(655360 + 65535);
primaryCandidates.put(account.getJid().toBareJid(),candidate); primaryCandidates.put(account.getJid().toBareJid(),candidate);
listener.onPrimaryCandidateFound(true,candidate); listener.onPrimaryCandidateFound(true,candidate);
} catch (final NumberFormatException | InvalidJidException e) { } catch (final NumberFormatException e) {
listener.onPrimaryCandidateFound(false,null); listener.onPrimaryCandidateFound(false,null);
return; return;
} }