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 final String[] VALID_IMAGE_EXTENSIONS = {"webp", "jpeg", "jpg", "png", "jpe"};
public final String[] VALID_CRYPTO_EXTENSIONS = {"pgp", "gpg", "otr"};
String[] VALID_IMAGE_EXTENSIONS = {"webp", "jpeg", "jpg", "png", "jpe"};
String[] VALID_CRYPTO_EXTENSIONS = {"pgp", "gpg", "otr"};
public static final int STATUS_UNKNOWN = 0x200;
public static final int STATUS_CHECKING = 0x201;
public static final int STATUS_FAILED = 0x202;
public static final int STATUS_OFFER = 0x203;
public static final int STATUS_DOWNLOADING = 0x204;
public static final int STATUS_DELETED = 0x205;
public static final int STATUS_OFFER_CHECK_FILESIZE = 0x206;
public static final int STATUS_UPLOADING = 0x207;
int STATUS_UNKNOWN = 0x200;
int STATUS_CHECKING = 0x201;
int STATUS_FAILED = 0x202;
int STATUS_OFFER = 0x203;
int STATUS_DOWNLOADING = 0x204;
int STATUS_DELETED = 0x205;
int STATUS_OFFER_CHECK_FILESIZE = 0x206;
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();
public void cancel();
void cancel();
}

View file

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

View file

@ -586,6 +586,14 @@ public class Message extends AbstractEntity {
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 URL url;
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.Conversation;
import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.services.MessageArchiveService;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.PhoneHelper;
@ -102,7 +103,7 @@ public class IqGenerator extends AbstractGenerator {
public IqPacket retrieveVcardAvatar(final Avatar avatar) {
final IqPacket packet = new IqPacket(IqPacket.TYPE.GET);
packet.setTo(avatar.owner);
packet.addChild("vCard","vcard-temp");
packet.addChild("vCard", "vcard-temp");
return packet;
}
@ -194,4 +195,13 @@ public class IqGenerator extends AbstractGenerator {
item.setAttribute("role", role);
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-permanent-store", "urn:xmpp:hints");
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;
} catch (OtrException e) {
return null;
@ -86,7 +92,11 @@ public class MessageGenerator extends AbstractGenerator {
public MessagePacket generateChat(Message message, boolean addDelay) {
MessagePacket packet = preparePacket(message, addDelay);
packet.setBody(message.getBody());
if (message.hasFileOnRemoteHost()) {
packet.setBody(message.getImageParams().url.toString());
} else {
packet.setBody(message.getBody());
}
return packet;
}
@ -96,13 +106,11 @@ public class MessageGenerator extends AbstractGenerator {
public MessagePacket generatePgpChat(Message message, boolean 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) {
packet.addChild("x", "jabber:x:encrypted").setContent(
message.getEncryptedBody());
packet.addChild("x", "jabber:x:encrypted").setContent(message.getEncryptedBody());
} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
packet.addChild("x", "jabber:x:encrypted").setContent(
message.getBody());
packet.addChild("x", "jabber:x:encrypted").setContent(message.getBody());
}
return packet;
}

View file

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

View file

@ -13,7 +13,8 @@ public class HttpConnectionManager extends AbstractConnectionManager {
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) {
HttpConnection connection = new HttpConnection(this);
@ -22,7 +23,18 @@ public class HttpConnectionManager extends AbstractConnectionManager {
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) {
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();
int width = scaledBitmap.getWidth();
int height = scaledBitmap.getHeight();
message.setBody(Long.toString(size) + ',' + width + ',' + height);
message.setBody(Long.toString(size) + '|' + width + '|' + height);
return file;
} catch (FileNotFoundException e) {
throw new FileCopyException(R.string.error_file_not_found);

View file

@ -390,7 +390,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
callback.success(message);
}
} catch (FileBackend.FileCopyException e) {
callback.error(e.getResId(),message);
callback.error(e.getResId(), message);
}
}
});
@ -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) {
final Account account = message.getConversation().getAccount();
account.deactivateGracePeriod();
@ -680,7 +691,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
boolean send = false;
if (account.getStatus() == Account.State.ONLINE
&& account.getXmppConnection() != null) {
if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
if (message.needsUploading()) {
if (message.getCounterpart() != null) {
if (message.getEncryption() == Message.ENCRYPTION_OTR) {
if (!conv.hasValidOtrSession()) {
@ -688,11 +699,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
message.setStatus(Message.STATUS_WAITING);
} else if (conv.hasValidOtrSession()
&& conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
mJingleConnectionManager
.createNewConnection(message);
}
mJingleConnectionManager.createNewConnection(message);
}
} else {
mJingleConnectionManager.createNewConnection(message);
this.sendFileMessage(message);
}
} else {
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();
MessagePacket packet = null;
if (message.getEncryption() == Message.ENCRYPTION_OTR) {
Presences presences = message.getConversation().getContact()
.getPresences();
Presences presences = message.getConversation().getContact().getPresences();
if (!message.getConversation().hasValidOtrSession()) {
if ((message.getCounterpart() != null)
&& (presences.has(message.getCounterpart().getResourcepart()))) {
@ -808,34 +818,25 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}
}
} else {
if (message.getConversation().getOtrSession()
.getSessionStatus() == SessionStatus.ENCRYPTED) {
if (message.getConversation().getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
try {
message.setCounterpart(Jid.fromSessionID(message.getConversation().getOtrSession().getSessionID()));
if (message.getType() == Message.TYPE_TEXT) {
packet = mMessageGenerator.generateOtrChat(message,
true);
} else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
if (message.needsUploading()) {
mJingleConnectionManager.createNewConnection(message);
} else {
packet = mMessageGenerator.generateOtrChat(message, true);
}
} catch (final InvalidJidException ignored) {
}
}
}
}
} else if (message.getType() == Message.TYPE_TEXT) {
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) {
} else if (message.needsUploading()) {
Contact contact = message.getConversation().getContact();
Presences presences = contact.getPresences();
if ((message.getCounterpart() != null)
&& (presences.has(message.getCounterpart().getResourcepart()))) {
mJingleConnectionManager.createNewConnection(message);
this.sendFileMessage(message);
} else {
if (presences.size() == 1) {
String presence = presences.asStringArray()[0];
@ -844,9 +845,16 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} catch (InvalidJidException e) {
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 (!account.getXmppConnection().getFeatures().sm()
@ -1809,15 +1817,15 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} catch (InvalidJidException e) {
return;
}
if (message.getType() == Message.TYPE_TEXT) {
if (message.needsUploading()) {
mJingleConnectionManager.createNewConnection(message);
} else {
MessagePacket outPacket = mMessageGenerator.generateOtrChat(message, true);
if (outPacket != null) {
message.setStatus(Message.STATUS_SEND);
databaseBackend.updateMessage(message);
sendMessagePacket(account, outPacket);
}
} else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
mJingleConnectionManager.createNewConnection(message);
}
updateConversationUi();
}

View file

@ -5,4 +5,5 @@ public final class Xmlns {
public static final String ROSTER = "jabber:iq:roster";
public static final String REGISTER = "jabber:iq:register";
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;
}
public List<String> findDiscoItemsByFeature(final String feature) {
final List<String> items = new ArrayList<>();
public List<Jid> findDiscoItemsByFeature(final String feature) {
final List<Jid> items = new ArrayList<>();
for (final Entry<Jid, Info> cursor : disco.entrySet()) {
if (cursor.getValue().features.contains(feature)) {
items.add(cursor.getKey().toString());
items.add(cursor.getKey());
}
}
return items;
}
public String findDiscoItemByFeature(final String feature) {
final List<String> items = findDiscoItemsByFeature(feature);
public Jid findDiscoItemByFeature(final String feature) {
final List<Jid> items = findDiscoItemsByFeature(feature);
if (items.size() >= 1) {
return items.get(0);
}
@ -1191,6 +1191,10 @@ public class XmppConnection implements Runnable {
public void setBlockListRequested(boolean value) {
this.blockListRequested = value;
}
public boolean httpUpload() {
return findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD).size() > 0;
}
}
private IqGenerator getIqGenerator() {

View file

@ -954,24 +954,4 @@ public class JingleConnection implements Downloadable {
public int getProgress() {
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;
}
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) {
IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setAttribute("to", proxy);
iq.setTo(proxy);
iq.query(Xmlns.BYTE_STREAMS);
account.getXmppConnection().sendIqPacket(iq,new OnIqPacketReceived() {
@ -105,11 +105,11 @@ public class JingleConnectionManager extends AbstractConnectionManager {
candidate.setHost(host);
candidate.setPort(Integer.parseInt(port));
candidate.setType(JingleCandidate.TYPE_PROXY);
candidate.setJid(Jid.fromString(proxy));
candidate.setJid(proxy);
candidate.setPriority(655360 + 65535);
primaryCandidates.put(account.getJid().toBareJid(),candidate);
listener.onPrimaryCandidateFound(true,candidate);
} catch (final NumberFormatException | InvalidJidException e) {
} catch (final NumberFormatException e) {
listener.onPrimaryCandidateFound(false,null);
return;
}