1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 10:37:59 +01:00

Added proxy support for XEP-0363

This commit is contained in:
Vshnv 2021-03-29 19:01:58 +05:30
parent 525db827a9
commit 4736d080a1

View file

@ -27,7 +27,6 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects; import java.util.Objects;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.logging.Level; import java.util.logging.Level;
@ -37,6 +36,7 @@ import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionCreationListener; import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.ConnectionListener; import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.Manager;
@ -46,6 +46,7 @@ import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.proxy.ProxyInfo;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo; import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.httpfileupload.UploadService.Version; import org.jivesoftware.smackx.httpfileupload.UploadService.Version;
@ -429,15 +430,15 @@ public final class HttpFileUploadManager extends Manager {
private void upload(InputStream iStream, long fileSize, Slot slot, UploadProgressListener listener) throws IOException { private void upload(InputStream iStream, long fileSize, Slot slot, UploadProgressListener listener) throws IOException {
final URL putUrl = slot.getPutUrl(); final URL putUrl = slot.getPutUrl();
final XMPPConnection connection = connection();
final HttpURLConnection urlConnection = (HttpURLConnection) putUrl.openConnection(); final HttpURLConnection urlConnection = createURLConnection(connection, putUrl);
urlConnection.setRequestMethod("PUT"); urlConnection.setRequestMethod("PUT");
urlConnection.setUseCaches(false); urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true); urlConnection.setDoOutput(true);
urlConnection.setFixedLengthStreamingMode(fileSize); urlConnection.setFixedLengthStreamingMode(fileSize);
urlConnection.setRequestProperty("Content-Type", "application/octet-stream"); urlConnection.setRequestProperty("Content-Type", "application/octet-stream");
for (Entry<String, String> header : slot.getHeaders().entrySet()) { for (Map.Entry<String, String> header : slot.getHeaders().entrySet()) {
urlConnection.setRequestProperty(header.getKey(), header.getValue()); urlConnection.setRequestProperty(header.getKey(), header.getValue());
} }
@ -503,6 +504,30 @@ public final class HttpFileUploadManager extends Manager {
} }
} }
private static HttpURLConnection createURLConnection(XMPPConnection connection, URL putUrl) throws IOException {
Objects.requireNonNull(connection);
Objects.requireNonNull(putUrl);
ProxyInfo proxyInfo = fetchProxyInfo(connection);
if (proxyInfo != null) {
return createProxiedURLConnection(proxyInfo, putUrl);
}
return (HttpURLConnection) putUrl.openConnection();
}
private static HttpURLConnection createProxiedURLConnection(ProxyInfo proxyInfo, URL putUrl) throws IOException {
Objects.requireNonNull(proxyInfo);
Objects.requireNonNull(putUrl);
return (HttpURLConnection) putUrl.openConnection(proxyInfo.toJavaProxy());
}
private static ProxyInfo fetchProxyInfo(XMPPConnection connection) {
if (!(connection instanceof AbstractXMPPConnection)) {
return null;
}
AbstractXMPPConnection xmppConnection = (AbstractXMPPConnection) connection;
return xmppConnection.getConfiguration().getProxyInfo();
}
public static UploadService.Version namespaceToVersion(String namespace) { public static UploadService.Version namespaceToVersion(String namespace) {
UploadService.Version version; UploadService.Version version;
switch (namespace) { switch (namespace) {