make sure that http upload/download is not startetd multiple times

This commit is contained in:
Daniel Gultsch 2019-01-28 10:39:01 +01:00
parent 0898783309
commit b30fb46c85
3 changed files with 94 additions and 68 deletions

View File

@ -11,8 +11,8 @@ import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
@ -25,33 +25,51 @@ import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.AbstractConnectionManager; import eu.siacs.conversations.services.AbstractConnectionManager;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.TLSSocketFactory; import eu.siacs.conversations.utils.TLSSocketFactory;
import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
public class HttpConnectionManager extends AbstractConnectionManager { public class HttpConnectionManager extends AbstractConnectionManager {
private final List<HttpDownloadConnection> downloadConnections = new ArrayList<>();
private final List<HttpUploadConnection> uploadConnections = new ArrayList<>();
public HttpConnectionManager(XmppConnectionService service) { public HttpConnectionManager(XmppConnectionService service) {
super(service); super(service);
} }
private List<HttpDownloadConnection> downloadConnections = new CopyOnWriteArrayList<>(); public static Proxy getProxy() throws IOException {
private List<HttpUploadConnection> uploadConnections = new CopyOnWriteArrayList<>(); return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), 8118));
public HttpDownloadConnection createNewDownloadConnection(Message message) {
return this.createNewDownloadConnection(message, false);
} }
public HttpDownloadConnection createNewDownloadConnection(Message message, boolean interactive) { public void createNewDownloadConnection(Message message) {
HttpDownloadConnection connection = new HttpDownloadConnection(this); this.createNewDownloadConnection(message, false);
connection.init(message,interactive); }
public void createNewDownloadConnection(final Message message, boolean interactive) {
synchronized (this.downloadConnections) {
for(HttpDownloadConnection connection : this.downloadConnections) {
if (connection.getMessage() == message) {
Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": download already in progress");
return;
}
}
final HttpDownloadConnection connection = new HttpDownloadConnection(message, this);
connection.init(interactive);
this.downloadConnections.add(connection); this.downloadConnections.add(connection);
return connection; }
} }
public void createNewUploadConnection(Message message, boolean delay) { public void createNewUploadConnection(final Message message, boolean delay) {
HttpUploadConnection connection = new HttpUploadConnection(Method.determine(message.getConversation().getAccount()), this); synchronized (this.uploadConnections) {
connection.init(message,delay); for (HttpUploadConnection connection : this.uploadConnections) {
if (connection.getMessage() == message) {
Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": upload already in progress");
return;
}
}
HttpUploadConnection connection = new HttpUploadConnection(message, Method.determine(message.getConversation().getAccount()), this);
connection.init(delay);
this.uploadConnections.add(connection); this.uploadConnections.add(connection);
} }
}
public boolean checkConnection(Message message) { public boolean checkConnection(Message message) {
final Account account = message.getConversation().getAccount(); final Account account = message.getConversation().getAccount();
@ -62,15 +80,19 @@ public class HttpConnectionManager extends AbstractConnectionManager {
return mXmppConnectionService.hasInternetConnection(); return mXmppConnectionService.hasInternetConnection();
} }
public void finishConnection(HttpDownloadConnection connection) { void finishConnection(HttpDownloadConnection connection) {
synchronized (this.downloadConnections) {
this.downloadConnections.remove(connection); this.downloadConnections.remove(connection);
} }
public void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
this.uploadConnections.remove(httpUploadConnection);
} }
public void setupTrustManager(final HttpsURLConnection connection, final boolean interactive) { void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
synchronized (this.uploadConnections) {
this.uploadConnections.remove(httpUploadConnection);
}
}
void setupTrustManager(final HttpsURLConnection connection, final boolean interactive) {
final X509TrustManager trustManager; final X509TrustManager trustManager;
final HostnameVerifier hostnameVerifier = mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier(), interactive); final HostnameVerifier hostnameVerifier = mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier(), interactive);
if (interactive) { if (interactive) {
@ -85,8 +107,4 @@ public class HttpConnectionManager extends AbstractConnectionManager {
} catch (final KeyManagementException | NoSuchAlgorithmException ignored) { } catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
} }
} }
public static Proxy getProxy() throws IOException {
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByAddress(new byte[]{127,0,0,1}), 8118));
}
} }

View File

@ -39,7 +39,7 @@ public class HttpDownloadConnection implements Transferable {
private XmppConnectionService mXmppConnectionService; private XmppConnectionService mXmppConnectionService;
private URL mUrl; private URL mUrl;
private Message message; private final Message message;
private DownloadableFile file; private DownloadableFile file;
private int mStatus = Transferable.STATUS_UNKNOWN; private int mStatus = Transferable.STATUS_UNKNOWN;
private boolean acceptedAutomatically = false; private boolean acceptedAutomatically = false;
@ -48,7 +48,8 @@ public class HttpDownloadConnection implements Transferable {
private boolean canceled = false; private boolean canceled = false;
private Method method = Method.HTTP_UPLOAD; private Method method = Method.HTTP_UPLOAD;
HttpDownloadConnection(HttpConnectionManager manager) { HttpDownloadConnection(Message message, HttpConnectionManager manager) {
this.message = message;
this.mHttpConnectionManager = manager; this.mHttpConnectionManager = manager;
this.mXmppConnectionService = manager.getXmppConnectionService(); this.mXmppConnectionService = manager.getXmppConnectionService();
this.mUseTor = mXmppConnectionService.useTorToConnect(); this.mUseTor = mXmppConnectionService.useTorToConnect();
@ -68,12 +69,7 @@ public class HttpDownloadConnection implements Transferable {
} }
} }
public void init(Message message) { public void init(boolean interactive) {
init(message, false);
}
public void init(Message message, boolean interactive) {
this.message = message;
this.message.setTransferable(this); this.message.setTransferable(this);
try { try {
if (message.hasFileOnRemoteHost()) { if (message.hasFileOnRemoteHost()) {
@ -195,6 +191,10 @@ public class HttpDownloadConnection implements Transferable {
return this.mProgress; return this.mProgress;
} }
public Message getMessage() {
return message;
}
private class FileSizeChecker implements Runnable { private class FileSizeChecker implements Runnable {
private final boolean interactive; private final boolean interactive;

View File

@ -43,14 +43,15 @@ public class HttpUploadConnection implements Transferable {
private boolean cancelled = false; private boolean cancelled = false;
private boolean delayed = false; private boolean delayed = false;
private DownloadableFile file; private DownloadableFile file;
private Message message; private final Message message;
private String mime; private String mime;
private SlotRequester.Slot slot; private SlotRequester.Slot slot;
private byte[] key = null; private byte[] key = null;
private long transmitted = 0; private long transmitted = 0;
public HttpUploadConnection(Method method, HttpConnectionManager httpConnectionManager) { public HttpUploadConnection(Message message, Method method, HttpConnectionManager httpConnectionManager) {
this.message = message;
this.method = method; this.method = method;
this.mHttpConnectionManager = httpConnectionManager; this.mHttpConnectionManager = httpConnectionManager;
this.mXmppConnectionService = httpConnectionManager.getXmppConnectionService(); this.mXmppConnectionService = httpConnectionManager.getXmppConnectionService();
@ -87,13 +88,16 @@ public class HttpUploadConnection implements Transferable {
} }
private void fail(String errorMessage) { private void fail(String errorMessage) {
mHttpConnectionManager.finishUploadConnection(this); finish();
message.setTransferable(null);
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED, cancelled ? Message.ERROR_MESSAGE_CANCELLED : errorMessage); mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED, cancelled ? Message.ERROR_MESSAGE_CANCELLED : errorMessage);
} }
public void init(Message message, boolean delay) { private void finish() {
this.message = message; mHttpConnectionManager.finishUploadConnection(this);
message.setTransferable(null);
}
public void init(boolean delay) {
final Account account = message.getConversation().getAccount(); final Account account = message.getConversation().getAccount();
this.file = mXmppConnectionService.getFileBackend().getFile(message, false); this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
@ -211,7 +215,7 @@ public class HttpUploadConnection implements Transferable {
} }
mXmppConnectionService.getFileBackend().updateFileParams(message, get); mXmppConnectionService.getFileBackend().updateFileParams(message, get);
mXmppConnectionService.getFileBackend().updateMediaScanner(file); mXmppConnectionService.getFileBackend().updateMediaScanner(file);
message.setTransferable(null); finish();
message.setCounterpart(message.getConversation().getJid().asBareJid()); message.setCounterpart(message.getConversation().getJid().asBareJid());
mXmppConnectionService.resendMessage(message, delayed); mXmppConnectionService.resendMessage(message, delayed);
} else { } else {
@ -231,4 +235,8 @@ public class HttpUploadConnection implements Transferable {
WakeLockHelper.release(wakeLock); WakeLockHelper.release(wakeLock);
} }
} }
public Message getMessage() {
return message;
}
} }