From d690769035dab881e57102f6178050b3e31f96b7 Mon Sep 17 00:00:00 2001 From: Guus der Kinderen Date: Tue, 14 Mar 2023 13:10:36 +0100 Subject: [PATCH] SPARK-2326: Upgrade Apache HttpClient from 3.1 to 5.2.1 --- core/pom.xml | 6 +- .../sparkimpl/plugin/viewer/PluginViewer.java | 266 ++++++------- .../AcceptAllCertsConnectionManager.java | 73 ++++ .../sparkimpl/updater/CheckUpdates.java | 376 +++++++++--------- .../updater/EasySSLProtocolSocketFactory.java | 242 ----------- .../updater/EasyX509TrustManager.java | 125 ------ .../plugin/fileupload/ChatRoomDecorator.java | 90 ++--- 7 files changed, 429 insertions(+), 749 deletions(-) create mode 100644 core/src/main/java/org/jivesoftware/sparkimpl/updater/AcceptAllCertsConnectionManager.java delete mode 100644 core/src/main/java/org/jivesoftware/sparkimpl/updater/EasySSLProtocolSocketFactory.java delete mode 100644 core/src/main/java/org/jivesoftware/sparkimpl/updater/EasyX509TrustManager.java diff --git a/core/pom.xml b/core/pom.xml index 52189c23..683f1129 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -164,9 +164,9 @@ 2.1.3 - commons-httpclient - commons-httpclient - 3.1 + org.apache.httpcomponents.client5 + httpclient5 + 5.2.1 org.apache.commons diff --git a/core/src/main/java/org/jivesoftware/sparkimpl/plugin/viewer/PluginViewer.java b/core/src/main/java/org/jivesoftware/sparkimpl/plugin/viewer/PluginViewer.java index 6c30caec..26bb125a 100644 --- a/core/src/main/java/org/jivesoftware/sparkimpl/plugin/viewer/PluginViewer.java +++ b/core/src/main/java/org/jivesoftware/sparkimpl/plugin/viewer/PluginViewer.java @@ -15,9 +15,12 @@ */ package org.jivesoftware.sparkimpl.plugin.viewer; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; @@ -42,7 +45,7 @@ import org.jivesoftware.sparkimpl.plugin.manager.Enterprise; import org.jivesoftware.sparkimpl.settings.JiveInfo; import org.jivesoftware.sparkimpl.settings.local.LocalPreferences; import org.jivesoftware.sparkimpl.settings.local.SettingsManager; -import org.jivesoftware.sparkimpl.updater.EasySSLProtocolSocketFactory; +import org.jivesoftware.sparkimpl.updater.AcceptAllCertsConnectionManager; import org.xml.sax.SAXException; import javax.swing.*; @@ -300,47 +303,41 @@ public class PluginViewer extends JPanel implements Plugin @Override public Object construct() { - // Prepare HTTP post - final GetMethod post = new GetMethod( retrieveListURL ); - - // Get HTTP client - Protocol.registerProtocol( "https", new Protocol( "https", new EasySSLProtocolSocketFactory(), 443 ) ); - final HttpClient httpclient = new HttpClient(); + final HttpGet request = new HttpGet(retrieveListURL); + HttpHost proxy = null; if ( Default.getBoolean( Default.PLUGIN_REPOSITORY_USE_PROXY ) ) { String proxyHost = System.getProperty( "http.proxyHost" ); String proxyPort = System.getProperty( "http.proxyPort" ); - if ( ModelUtil.hasLength( proxyHost ) && ModelUtil.hasLength( proxyPort ) ) - { - try - { - httpclient.getHostConfiguration().setProxy( proxyHost, Integer.parseInt( proxyPort ) ); - } - catch ( NumberFormatException e ) - { + if ( ModelUtil.hasLength( proxyHost ) && ModelUtil.hasLength(proxyPort) ) { + try{ + proxy = new HttpHost(proxyHost, Integer.parseInt(proxyPort)); + } catch ( NumberFormatException e ) { Log.error( e ); } } } - // Execute request - - try - { - int result = httpclient.executeMethod( post ); - if ( result != 200 ) - { - return null; - } - - pluginList = getPluginList( post.getResponseBodyAsStream() ); + try (final CloseableHttpClient httpClient = + HttpClients.custom() + .setConnectionManager(AcceptAllCertsConnectionManager.getInstance()) + .setProxy(proxy) + .build(); + ) { + return httpClient.execute(request, response -> { + if (response.getCode() != 200) { + return null; + } + final HttpEntity entity = response.getEntity(); + pluginList = getPluginList( entity.getContent() ); + EntityUtils.consume(entity); + return "ok"; + }); + } catch (Exception e) { + Log.error(e); + return e.getMessage(); } - catch ( Exception ex ) - { - // Nothing to do - } - return "ok"; } @Override @@ -383,126 +380,107 @@ public class PluginViewer extends JPanel implements Plugin private void downloadPlugin( final PublicPlugin plugin ) { - // Prepare HTTP post - final GetMethod post = new GetMethod( plugin.getDownloadURL() ); - - // Get HTTP client - Protocol.registerProtocol( "https", new Protocol( "https", new EasySSLProtocolSocketFactory(), 443 ) ); - final HttpClient httpclient = new HttpClient(); - String proxyHost = System.getProperty( "http.proxyHost" ); - String proxyPort = System.getProperty( "http.proxyPort" ); + final HttpGet request = new HttpGet(plugin.getDownloadURL()); + HttpHost proxy = null; if ( Default.getBoolean( Default.PLUGIN_REPOSITORY_USE_PROXY ) ) { - if ( ModelUtil.hasLength( proxyHost ) - && ModelUtil.hasLength( proxyPort ) ) - { - try - { - httpclient.getHostConfiguration().setProxy( proxyHost, - Integer.parseInt( proxyPort ) ); - } - catch ( NumberFormatException e ) - { + String proxyHost = System.getProperty( "http.proxyHost" ); + String proxyPort = System.getProperty( "http.proxyPort" ); + if ( ModelUtil.hasLength( proxyHost ) && ModelUtil.hasLength(proxyPort) ) { + try{ + proxy = new HttpHost(proxyHost, Integer.parseInt(proxyPort)); + } catch ( NumberFormatException e ) { Log.error( e ); } } } - // Execute request + + try (final CloseableHttpClient httpClient = + HttpClients.custom() + .setConnectionManager(AcceptAllCertsConnectionManager.getInstance()) + .setProxy(proxy) + .build(); + ) { + httpClient.execute(request, response -> { + if (response.getCode() != 200) { + return null; + } + + final HttpEntity entity = response.getEntity(); + + progressBar = new JProgressBar( 0, (int) entity.getContentLength()); + + final JFrame frame = new JFrame( Res.getString( "message.downloading", plugin.getName() ) ); + + frame.setIconImage( SparkRes.getImageIcon( SparkRes.SMALL_MESSAGE_IMAGE ).getImage() ); + + try + { + Thread.sleep( 2000 ); + InputStream stream = entity.getContent(); + + URL url = new URL( plugin.getDownloadURL() ); + String name1 = URLFileSystem.getFileName( url ); + String directoryName = URLFileSystem.getName( url ); + + File pluginDownload = new File( PluginManager.PLUGINS_DIRECTORY, name1 ); + + FileOutputStream out = new FileOutputStream( pluginDownload ); + copy( stream, out ); + out.close(); + + frame.dispose(); + + // Remove SparkPlugUI + // Clear all selections + Component[] comps = availablePanel.getComponents(); + for ( Component comp : comps ) + { + if ( comp instanceof SparkPlugUI ) + { + SparkPlugUI sparkPlug = (SparkPlugUI) comp; + if ( sparkPlug.getPlugin().getDownloadURL().equals( plugin.getDownloadURL() ) ) + { + availablePanel.remove( sparkPlug ); + + _deactivatedPlugins.remove( sparkPlug.getPlugin().getName() ); + _prefs.setDeactivatedPlugins( _deactivatedPlugins ); + + PluginManager.getInstance().addPlugin( sparkPlug.getPlugin() ); + + sparkPlug.showOperationButton(); + installedPanel.add( sparkPlug ); + sparkPlug.getPlugin().setPluginDir( new File( PluginManager.PLUGINS_DIRECTORY, directoryName ) ); + installedPanel.invalidate(); + installedPanel.repaint(); + availablePanel.invalidate(); + availablePanel.invalidate(); + availablePanel.validate(); + availablePanel.repaint(); + } + } + } + } + catch ( Exception ex ) + { + // Nothing to do + Log.error(ex); + } + + frame.getContentPane().setLayout( new GridBagLayout() ); + frame.getContentPane().add( new JLabel( Res.getString( "message.downloading.spark.plug" ) ), new GridBagConstraints( 0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 5, 5, 5, 5 ), 0, 0 ) ); + frame.getContentPane().add( progressBar, new GridBagConstraints( 0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 5, 5, 5, 5 ), 0, 0 ) ); + frame.pack(); + frame.setSize( 400, 100 ); + GraphicUtils.centerWindowOnComponent( frame, this ); - try - { - int result = httpclient.executeMethod( post ); - if ( result != 200 ) - { - return; - } + frame.setVisible( true ); - long length = post.getResponseContentLength(); - int contentLength = (int) length; - - progressBar = new JProgressBar( 0, contentLength ); - - final JFrame frame = new JFrame( Res.getString( "message.downloading", plugin.getName() ) ); - - frame.setIconImage( SparkRes.getImageIcon( SparkRes.SMALL_MESSAGE_IMAGE ).getImage() ); - - final Thread thread = new Thread( () -> - { - try - { - Thread.sleep( 2000 ); - InputStream stream = post.getResponseBodyAsStream(); - - URL url = new URL( plugin.getDownloadURL() ); - String name1 = URLFileSystem.getFileName( url ); - String directoryName = URLFileSystem.getName( url ); - - File pluginDownload = new File( PluginManager.PLUGINS_DIRECTORY, name1 ); - - FileOutputStream out = new FileOutputStream( pluginDownload ); - copy( stream, out ); - out.close(); - - frame.dispose(); - - // Remove SparkPlugUI - // Clear all selections - Component[] comps = availablePanel.getComponents(); - for ( Component comp : comps ) - { - if ( comp instanceof SparkPlugUI ) - { - SparkPlugUI sparkPlug = (SparkPlugUI) comp; - if ( sparkPlug.getPlugin().getDownloadURL().equals( plugin.getDownloadURL() ) ) - { - availablePanel.remove( sparkPlug ); - - _deactivatedPlugins.remove( sparkPlug.getPlugin().getName() ); - _prefs.setDeactivatedPlugins( _deactivatedPlugins ); - - PluginManager.getInstance().addPlugin( sparkPlug.getPlugin() ); - - sparkPlug.showOperationButton(); - installedPanel.add( sparkPlug ); - sparkPlug.getPlugin().setPluginDir( new File( PluginManager.PLUGINS_DIRECTORY, directoryName ) ); - installedPanel.invalidate(); - installedPanel.repaint(); - availablePanel.invalidate(); - availablePanel.invalidate(); - availablePanel.validate(); - availablePanel.repaint(); - } - } - } - } - catch ( Exception ex ) - { - // Nothing to do - } - finally - { - // Release current connection to the connection pool once you are done - post.releaseConnection(); - } - } ); - - - frame.getContentPane().setLayout( new GridBagLayout() ); - frame.getContentPane().add( new JLabel( Res.getString( "message.downloading.spark.plug" ) ), new GridBagConstraints( 0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 5, 5, 5, 5 ), 0, 0 ) ); - frame.getContentPane().add( progressBar, new GridBagConstraints( 0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 5, 5, 5, 5 ), 0, 0 ) ); - frame.pack(); - frame.setSize( 400, 100 ); - GraphicUtils.centerWindowOnComponent( frame, this ); - - - frame.setVisible( true ); - thread.start(); - - } - catch ( IOException e ) - { + return null; + }); + } catch (Exception e) { Log.error( e ); } } diff --git a/core/src/main/java/org/jivesoftware/sparkimpl/updater/AcceptAllCertsConnectionManager.java b/core/src/main/java/org/jivesoftware/sparkimpl/updater/AcceptAllCertsConnectionManager.java new file mode 100644 index 00000000..75c2ada7 --- /dev/null +++ b/core/src/main/java/org/jivesoftware/sparkimpl/updater/AcceptAllCertsConnectionManager.java @@ -0,0 +1,73 @@ +/** + * Copyright (C) 2023 Ignite Realtime Foundation. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.sparkimpl.updater; + +import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager; +import org.apache.hc.client5.http.socket.ConnectionSocketFactory; +import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory; +import org.apache.hc.client5.http.ssl.NoopHostnameVerifier; +import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; +import org.apache.hc.core5.http.config.Registry; +import org.apache.hc.core5.http.config.RegistryBuilder; +import org.apache.hc.core5.ssl.SSLContexts; +import org.apache.hc.core5.ssl.TrustStrategy; + +import javax.net.ssl.SSLContext; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + +/** + * A HTTP Client connection manager that knowingly by-passes all verification of TLS certificates. + * + * This SHOULD NOT be used for productive systems due to security reasons, unless it is a conscious decision and you are + * perfectly aware of security implications of accepting self-signed certificates. + * + * Usage example: + * + * AcceptAllCertsConnectionManager connectionManager = AcceptAllCertsConnectionManager.getInstance(); + * try( CloseableHttpClient httpClient = HttpClients.custom() + * .setConnectionManager(connectionManager) + * .build(); + * + * CloseableHttpResponse response = (CloseableHttpResponse) httpClient + * .execute(getMethod, new CustomHttpClientResponseHandler())) { + * + * final int statusCode = response.getCode(); + * assertThat(statusCode, equalTo(HttpStatus.SC_OK)); + * }; + * + * @author Guus der Kinderen, guus.der.kinderen@gmail.com + */ +public class AcceptAllCertsConnectionManager extends BasicHttpClientConnectionManager +{ + public static BasicHttpClientConnectionManager getInstance() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException + { + // Taken from https://www.baeldung.com/httpclient-ssl + final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; + final SSLContext sslContext = SSLContexts.custom() + .loadTrustMaterial(null, acceptingTrustStrategy) + .build(); + final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); + final Registry socketFactoryRegistry = + RegistryBuilder. create() + .register("https", sslsf) + .register("http", new PlainConnectionSocketFactory()) + .build(); + + return new BasicHttpClientConnectionManager(socketFactoryRegistry); + } +} diff --git a/core/src/main/java/org/jivesoftware/sparkimpl/updater/CheckUpdates.java b/core/src/main/java/org/jivesoftware/sparkimpl/updater/CheckUpdates.java index 72f9a2e4..04c532b4 100644 --- a/core/src/main/java/org/jivesoftware/sparkimpl/updater/CheckUpdates.java +++ b/core/src/main/java/org/jivesoftware/sparkimpl/updater/CheckUpdates.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2004-2011 Jive Software. All rights reserved. + * Copyright (C) 2004-2011 Jive Software, 2023 Ignite Realtime Foundation. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,14 +17,21 @@ package org.jivesoftware.sparkimpl.updater; import com.thoughtworks.xstream.XStream; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.ClassicHttpRequest; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.io.support.ClassicRequestBuilder; import org.jivesoftware.Spark; import org.jivesoftware.resource.Res; import org.jivesoftware.resource.SparkRes; -import org.jivesoftware.smack.*; +import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.StanzaCollector; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.filter.IQReplyFilter; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.provider.ProviderManager; @@ -33,36 +40,20 @@ import org.jivesoftware.spark.SparkManager; import org.jivesoftware.spark.component.ConfirmDialog; import org.jivesoftware.spark.component.ConfirmDialog.ConfirmListener; import org.jivesoftware.spark.component.TitlePanel; -import org.jivesoftware.spark.util.BrowserLauncher; -import org.jivesoftware.spark.util.ByteFormat; -import org.jivesoftware.spark.util.GraphicUtils; -import org.jivesoftware.spark.util.ModelUtil; import org.jivesoftware.spark.util.SwingWorker; +import org.jivesoftware.spark.util.*; import org.jivesoftware.spark.util.log.Log; import org.jivesoftware.sparkimpl.settings.JiveInfo; import org.jivesoftware.sparkimpl.settings.local.LocalPreferences; import org.jivesoftware.sparkimpl.settings.local.SettingsManager; import org.jxmpp.jid.impl.JidCreate; -import javax.swing.JEditorPane; -import javax.swing.JFrame; -import javax.swing.JOptionPane; -import javax.swing.JProgressBar; -import javax.swing.JScrollPane; +import javax.swing.*; import javax.swing.text.html.HTMLEditorKit; -import javax.swing.UIManager; - -import java.awt.Color; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; +import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.util.Calendar; import java.util.Date; import java.util.TimerTask; @@ -120,54 +111,61 @@ public class CheckUpdates { * @return true if there is a new build available for download. */ public SparkVersion isNewBuildAvailableFromJivesoftware() { - PostMethod post = new PostMethod(mainUpdateURL); + + HttpHost proxy = null; + String proxyHost = System.getProperty( "http.proxyHost" ); + String proxyPort = System.getProperty( "http.proxyPort" ); + if ( ModelUtil.hasLength( proxyHost ) && ModelUtil.hasLength(proxyPort) ) { + try{ + proxy = new HttpHost(proxyHost, Integer.parseInt(proxyPort)); + } catch ( NumberFormatException e ) { + Log.error( e ); + } + } + + final String os; if (Spark.isWindows()) { - post.addParameter("os", "windows"); + os = "windows"; } else if (Spark.isMac()) { - post.addParameter("os", "mac"); + os = "mac"; } else { - post.addParameter("os", "linux"); + os = "linux"; } -// Properties isBetaCheckingEnabled is now used to indicate if updates are allowed -// // Check to see if the beta should be included. -// LocalPreferences pref = SettingsManager.getLocalPreferences(); -// boolean isBetaCheckingEnabled = pref.isBetaCheckingEnabled(); -// if (isBetaCheckingEnabled) { -// post.addParameter("beta", "true"); -// } + // Properties isBetaCheckingEnabled is now used to indicate if updates are allowed + // // Check to see if the beta should be included. + // LocalPreferences pref = SettingsManager.getLocalPreferences(); + // boolean isBetaCheckingEnabled = pref.isBetaCheckingEnabled(); + // if (isBetaCheckingEnabled) { + // post.addParameter("beta", "true"); + // } + try (final CloseableHttpClient httpClient = + HttpClients.custom() + .setConnectionManager(AcceptAllCertsConnectionManager.getInstance()) + .setProxy(proxy) + .build() + ) { + final ClassicHttpRequest request = ClassicRequestBuilder.post(mainUpdateURL) + .addParameter("os", os) + .setHeader("User-Agent", "Spark HttpFileUpload") + .build(); + return httpClient.execute(request, httpResponse -> { + final int statusCode = httpResponse.getCode(); + if ((statusCode >= 200) && (statusCode <= 202)) { + String xml = EntityUtils.toString(httpResponse.getEntity()); - Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443)); - HttpClient httpclient = new HttpClient(); - String proxyHost = System.getProperty("http.proxyHost"); - String proxyPort = System.getProperty("http.proxyPort"); - if (ModelUtil.hasLength(proxyHost) && ModelUtil.hasLength(proxyPort)) { - try { - httpclient.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort)); - } - catch (NumberFormatException e) { - Log.error(e); - } - } - try { - int result = httpclient.executeMethod(post); - if (result != 200) { + // Server Version + SparkVersion serverVersion = (SparkVersion)xstream.fromXML(xml); + if (isGreater(serverVersion.getVersion(), JiveInfo.getVersion())) { + return serverVersion; + } + } return null; - } - - - String xml = post.getResponseBodyAsString(); - - // Server Version - SparkVersion serverVersion = (SparkVersion)xstream.fromXML(xml); - if (isGreater(serverVersion.getVersion(), JiveInfo.getVersion())) { - return serverVersion; - } - } - catch (IOException e) { + }); + } catch (Exception e) { Log.error(e); } return null; @@ -177,154 +175,152 @@ public class CheckUpdates { public void downloadUpdate(final File downloadedFile, final SparkVersion version) { final java.util.Timer timer = new java.util.Timer(); - // Prepare HTTP post - final GetMethod post = new GetMethod(version.getDownloadURL()); + final HttpGet request = new HttpGet(version.getDownloadURL()); - // Get HTTP client - Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443)); - final HttpClient httpclient = new HttpClient(); - String proxyHost = System.getProperty("http.proxyHost"); - String proxyPort = System.getProperty("http.proxyPort"); - if (ModelUtil.hasLength(proxyHost) && ModelUtil.hasLength(proxyPort)) { - try { - httpclient.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort)); - } - catch (NumberFormatException e) { - Log.error(e); + HttpHost proxy = null; + String proxyHost = System.getProperty( "http.proxyHost" ); + String proxyPort = System.getProperty( "http.proxyPort" ); + if ( ModelUtil.hasLength( proxyHost ) && ModelUtil.hasLength(proxyPort) ) { + try{ + proxy = new HttpHost(proxyHost, Integer.parseInt(proxyPort)); + } catch ( NumberFormatException e ) { + Log.error( e ); } } - // Execute request + try (final CloseableHttpClient httpClient = + HttpClients.custom() + .setConnectionManager(AcceptAllCertsConnectionManager.getInstance()) + .setProxy(proxy) + .build(); + ) { + httpClient.execute(request, response -> { + if (response.getCode() != 200) { + return null; + } + final HttpEntity entity = response.getEntity(); + int contentLength = (int) entity.getContentLength(); - try { - int result = httpclient.executeMethod(post); - if (result != 200) { - return; - } + bar = new JProgressBar(0, contentLength); - long length = post.getResponseContentLength(); - int contentLength = (int)length; + final JFrame frame = new JFrame(Res.getString("title.downloading.im.client")); - bar = new JProgressBar(0, contentLength); - } - catch (IOException e) { - Log.error(e); - } + frame.setIconImage(SparkRes.getImageIcon(SparkRes.SMALL_MESSAGE_IMAGE).getImage()); - final JFrame frame = new JFrame(Res.getString("title.downloading.im.client")); + titlePanel = new TitlePanel(Res.getString("title.upgrading.client"), Res.getString("message.version", version.getVersion()), SparkRes.getImageIcon(SparkRes.SEND_FILE_24x24), true); - frame.setIconImage(SparkRes.getImageIcon(SparkRes.SMALL_MESSAGE_IMAGE).getImage()); - - titlePanel = new TitlePanel(Res.getString("title.upgrading.client"), Res.getString("message.version", version.getVersion()), SparkRes.getImageIcon(SparkRes.SEND_FILE_24x24), true); - - final Thread thread = new Thread( () -> { - try { - InputStream stream = post.getResponseBodyAsStream(); - long size = post.getResponseContentLength(); - ByteFormat formater = new ByteFormat(); - sizeText = formater.format(size); - titlePanel.setDescription(Res.getString("message.version", version.getVersion()) + " \n" + Res.getString("message.file.size", sizeText)); + final Thread thread = new Thread( () -> { + try { + InputStream stream = entity.getContent(); + long size = entity.getContentLength(); + ByteFormat formater = new ByteFormat(); + sizeText = formater.format(size); + titlePanel.setDescription(Res.getString("message.version", version.getVersion()) + " \n" + Res.getString("message.file.size", sizeText)); - downloadedFile.getParentFile().mkdirs(); + downloadedFile.getParentFile().mkdirs(); - FileOutputStream out = new FileOutputStream(downloadedFile); - copy(stream, out); - out.close(); + FileOutputStream out = new FileOutputStream(downloadedFile); + copy(stream, out); + out.close(); - if (!cancel) { - downloadComplete = true; - promptForInstallation(downloadedFile, Res.getString("title.download.complete"), Res.getString("message.restart.spark")); + if (!cancel) { + downloadComplete = true; + promptForInstallation(downloadedFile, Res.getString("title.download.complete"), Res.getString("message.restart.spark")); + } + else { + out.close(); + downloadedFile.delete(); + } + + + UPDATING = false; + frame.dispose(); + } + catch (Exception ex) { + // Nothing to do + } + finally { + timer.cancel(); + // Release current connection to the connection pool once you are done + } + } ); + + + frame.getContentPane().setLayout(new GridBagLayout()); + frame.getContentPane().add(titlePanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); + frame.getContentPane().add(bar, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); + + JEditorPane pane = new JEditorPane(); + boolean displayContentPane = version.getChangeLogURL() != null || version.getDisplayMessage() != null; + + try { + pane.setEditable(false); + if (version.getChangeLogURL() != null) { + pane.setEditorKit(new HTMLEditorKit()); + pane.setPage(version.getChangeLogURL()); + } + else if (version.getDisplayMessage() != null) { + pane.setText(version.getDisplayMessage()); + } + + if (displayContentPane) { + frame.getContentPane().add(new JScrollPane(pane), new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); + } + } + catch (IOException e) { + Log.error(e); + } + + frame.getContentPane().setBackground(Color.WHITE); + frame.pack(); + if (displayContentPane) { + frame.setSize(600, 400); } else { - out.close(); - downloadedFile.delete(); + frame.setSize(400, 100); } + frame.setLocationRelativeTo(SparkManager.getMainWindow()); + GraphicUtils.centerWindowOnScreen(frame); + frame.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent windowEvent) { + thread.interrupt(); + cancel = true; + + UPDATING = false; + + if (!downloadComplete) { + UIManager.put("OptionPane.okButtonText", Res.getString("ok")); + JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Res.getString("message.updating.cancelled"), Res.getString("title.cancelled"), JOptionPane.ERROR_MESSAGE); + } + + } + }); + frame.setVisible(true); + thread.start(); - UPDATING = false; - frame.dispose(); - } - catch (Exception ex) { - // Nothing to do - } - finally { - timer.cancel(); - // Release current connection to the connection pool once you are done - post.releaseConnection(); - } - } ); + timer.scheduleAtFixedRate(new TimerTask() { + int seconds = 1; + @Override + public void run() { + ByteFormat formatter = new ByteFormat(); + long value = bar.getValue(); + long average = value / seconds; + String text = formatter.format(average) + "/Sec"; - frame.getContentPane().setLayout(new GridBagLayout()); - frame.getContentPane().add(titlePanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); - frame.getContentPane().add(bar, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); - - JEditorPane pane = new JEditorPane(); - boolean displayContentPane = version.getChangeLogURL() != null || version.getDisplayMessage() != null; - - try { - pane.setEditable(false); - if (version.getChangeLogURL() != null) { - pane.setEditorKit(new HTMLEditorKit()); - pane.setPage(version.getChangeLogURL()); - } - else if (version.getDisplayMessage() != null) { - pane.setText(version.getDisplayMessage()); - } - - if (displayContentPane) { - frame.getContentPane().add(new JScrollPane(pane), new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); - } - } - catch (IOException e) { + String total = formatter.format(value); + titlePanel.setDescription(Res.getString("message.version", version.getVersion()) + " \n" + Res.getString("message.file.size", sizeText) + "\n" + Res.getString("message.transfer.rate") + ": " + text + "\n" + Res.getString("message.total.downloaded") + ": " + total); + seconds++; + } + }, 1000, 1000); + return null; + }); + } catch (Exception e) { Log.error(e); } - - frame.getContentPane().setBackground(Color.WHITE); - frame.pack(); - if (displayContentPane) { - frame.setSize(600, 400); - } - else { - frame.setSize(400, 100); - } - frame.setLocationRelativeTo(SparkManager.getMainWindow()); - GraphicUtils.centerWindowOnScreen(frame); - frame.addWindowListener(new WindowAdapter() { - @Override - public void windowClosing(WindowEvent windowEvent) { - thread.interrupt(); - cancel = true; - - UPDATING = false; - - if (!downloadComplete) { - UIManager.put("OptionPane.okButtonText", Res.getString("ok")); - JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Res.getString("message.updating.cancelled"), Res.getString("title.cancelled"), JOptionPane.ERROR_MESSAGE); - } - - } - }); - frame.setVisible(true); - thread.start(); - - - timer.scheduleAtFixedRate(new TimerTask() { - int seconds = 1; - - @Override - public void run() { - ByteFormat formatter = new ByteFormat(); - long value = bar.getValue(); - long average = value / seconds; - String text = formatter.format(average) + "/Sec"; - - String total = formatter.format(value); - titlePanel.setDescription(Res.getString("message.version", version.getVersion()) + " \n" + Res.getString("message.file.size", sizeText) + "\n" + Res.getString("message.transfer.rate") + ": " + text + "\n" + Res.getString("message.total.downloaded") + ": " + total); - seconds++; - } - }, 1000, 1000); } /** diff --git a/core/src/main/java/org/jivesoftware/sparkimpl/updater/EasySSLProtocolSocketFactory.java b/core/src/main/java/org/jivesoftware/sparkimpl/updater/EasySSLProtocolSocketFactory.java deleted file mode 100644 index d12f5a81..00000000 --- a/core/src/main/java/org/jivesoftware/sparkimpl/updater/EasySSLProtocolSocketFactory.java +++ /dev/null @@ -1,242 +0,0 @@ -/** - * Copyright (C) 2004-2011 Jive Software. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jivesoftware.sparkimpl.updater; -/* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/contrib/org/apache/commons/httpclient/contrib/ssl/EasySSLProtocolSocketFactory.java,v 1.7 2004/06/11 19:26:27 olegk Exp $ - * - * ==================================================================== - * - * Copyright 2002-2004 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - - -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import org.apache.commons.httpclient.ConnectTimeoutException; -import org.apache.commons.httpclient.HttpClientError; -import org.apache.commons.httpclient.params.HttpConnectionParams; -import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory; -import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; -import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.Socket; -import java.net.UnknownHostException; - -/** - *

- * EasySSLProtocolSocketFactory can be used to creats SSL {@link Socket}s - * that accept self-signed certificates. - *

- *

- * This socket factory SHOULD NOT be used for productive systems - * due to security reasons, unless it is a concious decision and - * you are perfectly aware of security implications of accepting - * self-signed certificates - *

- *

- *

- * Example of using custom protocol socket factory for a specific host: - *

- *     Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
- * 

- * HttpClient client = new HttpClient(); - * client.getHostConfiguration().setHost("localhost", 443, easyhttps); - * // use relative url only - * GetMethod httpget = new GetMethod("/"); - * client.executeMethod(httpget); - *

- *

- *

- * Example of using custom protocol socket factory per default instead of the standard one: - *

- *     Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
- *     Protocol.registerProtocol("https", easyhttps);
- * 

- * HttpClient client = new HttpClient(); - * GetMethod httpget = new GetMethod("https://localhost/"); - * client.executeMethod(httpget); - *

- *

- * - * @author Oleg Kalnichevski - *

- *

- * DISCLAIMER: HttpClient developers DO NOT actively support this component. - * The component is provided as a reference material, which may be inappropriate - * for use without additional customization. - *

- */ - -public class EasySSLProtocolSocketFactory implements ProtocolSocketFactory { - - /** - * Log object for this class. - */ - private static final Log LOG = LogFactory.getLog(EasySSLProtocolSocketFactory.class); - - private SSLContext sslcontext = null; - - /** - * Constructor for EasySSLProtocolSocketFactory. - */ - public EasySSLProtocolSocketFactory() { - super(); - } - - private static SSLContext createEasySSLContext() { - try { - SSLContext context = SSLContext.getInstance("SSL"); - context.init( - null, - new TrustManager[]{new EasyX509TrustManager(null)}, - null); - return context; - } - catch (Exception e) { - LOG.error(e.getMessage(), e); - throw new HttpClientError(e.toString()); - } - } - - private SSLContext getSSLContext() { - if (this.sslcontext == null) { - this.sslcontext = createEasySSLContext(); - } - return this.sslcontext; - } - - /** - * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int) - */ - @Override - public Socket createSocket( - String host, - int port, - InetAddress clientHost, - int clientPort) - throws IOException { - - return getSSLContext().getSocketFactory().createSocket( - host, - port, - clientHost, - clientPort - ); - } - - /** - * Attempts to get a new socket connection to the given host within the given time limit. - *

- * To circumvent the limitations of older JREs that do not support connect timeout a - * controller thread is executed. The controller thread attempts to create a new socket - * within the given limit of time. If socket constructor does not return until the - * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException} - *

- * - * @param host the host name/IP - * @param port the port on the host - * @param params {@link HttpConnectionParams Http connection parameters} - * @return Socket a new socket - * @throws IOException if an I/O error occurs while creating the socket - * @throws UnknownHostException if the IP address of the host cannot be - * determined - */ - @Override - public Socket createSocket( - final String host, - final int port, - final InetAddress localAddress, - final int localPort, - final HttpConnectionParams params - ) throws IOException { - if (params == null) { - throw new IllegalArgumentException("Parameters may not be null"); - } - int timeout = params.getConnectionTimeout(); - if (timeout == 0) { - return createSocket(host, port, localAddress, localPort); - } - else { - // To be eventually deprecated when migrated to Java 1.4 or above - return ControllerThreadSocketFactory.createSocket( - this, host, port, localAddress, localPort, timeout); - } - } - - /** - * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int) - */ - @Override - public Socket createSocket(String host, int port) - throws IOException { - return getSSLContext().getSocketFactory().createSocket( - host, - port - ); - } - - /** - * @see SecureProtocolSocketFactory#createSocket(java.net.Socket, String, int, boolean) - */ - public Socket createSocket( - Socket socket, - String host, - int port, - boolean autoClose) - throws IOException { - return getSSLContext().getSocketFactory().createSocket( - socket, - host, - port, - autoClose - ); - } - - @Override - public boolean equals(Object obj) { - return ((obj != null) && obj.getClass().equals(EasySSLProtocolSocketFactory.class)); - } - - @Override - public int hashCode() { - return EasySSLProtocolSocketFactory.class.hashCode(); - } - -} diff --git a/core/src/main/java/org/jivesoftware/sparkimpl/updater/EasyX509TrustManager.java b/core/src/main/java/org/jivesoftware/sparkimpl/updater/EasyX509TrustManager.java deleted file mode 100644 index 50c16a54..00000000 --- a/core/src/main/java/org/jivesoftware/sparkimpl/updater/EasyX509TrustManager.java +++ /dev/null @@ -1,125 +0,0 @@ -/** - * Copyright (C) 2004-2011 Jive Software. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jivesoftware.sparkimpl.updater; - -import javax.net.ssl.TrustManager; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509TrustManager; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; - -/** - *

- * EasyX509TrustManager unlike default {@link X509TrustManager} accepts - * self-signed certificates. - *

- *

- * This trust manager SHOULD NOT be used for productive systems - * due to security reasons, unless it is a conscious decision and - * you are perfectly aware of security implications of accepting - * self-signed certificates - *

- * - * @author Adrian Sutton - * @author Oleg Kalnichevski - *

- *

- * DISCLAIMER: HttpClient developers DO NOT actively support this component. - * The component is provided as a reference material, which may be inappropriate - * for use without additional customization. - *

- */ - -public class EasyX509TrustManager implements X509TrustManager { - private final X509TrustManager standardTrustManager; - - /** - * Log object for this class. - */ - private static final Log LOG = LogFactory.getLog(EasyX509TrustManager.class); - - public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException { - super(); - TrustManagerFactory factory = TrustManagerFactory.getInstance("SunX509"); - factory.init(keystore); - TrustManager[] trustmanagers = factory.getTrustManagers(); - if (trustmanagers.length == 0) { - throw new NoSuchAlgorithmException("SunX509 trust manager not supported"); - } - this.standardTrustManager = (X509TrustManager)trustmanagers[0]; - } - - public boolean isClientTrusted(X509Certificate[] certificates) { - try { - this.standardTrustManager.checkClientTrusted(certificates, null); - return true; - } - catch (CertificateException e) { - return false; - } - } - - public boolean isServerTrusted(X509Certificate[] certificates) { - if ((certificates != null) && LOG.isDebugEnabled()) { - LOG.debug("Server certificate chain:"); - for (int i = 0; i < certificates.length; i++) { - LOG.debug("X509Certificate[" + i + "]=" + certificates[i]); - } - } - if ((certificates != null) && (certificates.length == 1)) { - X509Certificate certificate = certificates[0]; - try { - certificate.checkValidity(); - } - catch (CertificateException e) { - LOG.error(e.toString()); - return false; - } - return true; - } - else { - try { - this.standardTrustManager.checkServerTrusted(certificates, null); - return true; - } - catch (CertificateException e) { - return false; - } - } - } - - @Override - public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { - } - - @Override - public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { - } - - /** - * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers() - */ - @Override - public X509Certificate[] getAcceptedIssuers() { - return this.standardTrustManager.getAcceptedIssuers(); - } -} diff --git a/plugins/fileupload/src/main/java/org/jivesoftware/spark/plugin/fileupload/ChatRoomDecorator.java b/plugins/fileupload/src/main/java/org/jivesoftware/spark/plugin/fileupload/ChatRoomDecorator.java index 69438fe5..0d4e1226 100644 --- a/plugins/fileupload/src/main/java/org/jivesoftware/spark/plugin/fileupload/ChatRoomDecorator.java +++ b/plugins/fileupload/src/main/java/org/jivesoftware/spark/plugin/fileupload/ChatRoomDecorator.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2004-2010 Jive Software. All rights reserved. + * Copyright (C) 2004-2010 Jive Software. 2023 Ignite Realtime Foundation. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,32 +16,30 @@ package org.jivesoftware.spark.plugin.fileupload; -import java.io.*; -import java.net.*; - -import java.awt.*; -import javax.swing.*; - +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.ClassicHttpRequest; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.io.entity.FileEntity; +import org.apache.hc.core5.http.io.support.ClassicRequestBuilder; import org.jivesoftware.resource.Res; +import org.jivesoftware.resource.SparkRes; +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.packet.Message; import org.jivesoftware.spark.SparkManager; import org.jivesoftware.spark.component.RolloverButton; import org.jivesoftware.spark.ui.ChatRoom; -import org.jivesoftware.spark.util.*; -import org.jivesoftware.spark.util.log.*; -import org.jivesoftware.smack.packet.*; +import org.jivesoftware.spark.util.GraphicUtils; +import org.jivesoftware.spark.util.log.Log; +import org.jivesoftware.sparkimpl.updater.AcceptAllCertsConnectionManager; import org.jxmpp.jid.EntityBareJid; - -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.methods.FileRequestEntity; -import org.apache.commons.httpclient.methods.PutMethod; -import org.apache.commons.httpclient.methods.RequestEntity; -import org.apache.commons.httpclient.protocol.Protocol; - -import org.jivesoftware.sparkimpl.updater.EasySSLProtocolSocketFactory; - import org.jxmpp.jid.impl.JidCreate; -import javax.xml.bind.DatatypeConverter; -import org.jivesoftware.resource.SparkRes; + +import javax.swing.*; +import java.awt.*; +import java.io.File; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; public class ChatRoomDecorator { @@ -127,31 +125,33 @@ public class ChatRoomDecorator { Log.debug("About to upload file for room " + room.getBareJid() + " via HTTP PUT to URL " + response.putUrl); - try { - PutMethod put = new PutMethod(response.putUrl); - int port = put.getURI().getPort(); - if (port > 0) - { - Protocol.registerProtocol( "https", new Protocol( "https", new EasySSLProtocolSocketFactory(), port ) ); - } - - HttpClient client = new HttpClient(); - RequestEntity entity = new FileRequestEntity(file, "application/binary"); - put.setRequestEntity(entity); - put.setRequestHeader("User-Agent", "Spark HttpFileUpload"); - client.executeMethod(put); - - int statusCode = put.getStatusCode(); - String responseBody = put.getResponseBodyAsString(); - - if ((statusCode >= 200) && (statusCode <= 202)) - { - Log.debug("Upload file success. HTTP response: " + statusCode + " " + responseBody); - broadcastUploadUrl(room.getBareJid(), response.getUrl, type); - } else { - Log.error("Failed to upload file. HTTP response: " + statusCode + " " + responseBody); - } + try (final CloseableHttpClient httpClient = + HttpClients.custom() + .setConnectionManager(AcceptAllCertsConnectionManager.getInstance()) + .build() + ) { + final ClassicHttpRequest request = ClassicRequestBuilder.put(response.putUrl) + .setEntity(new FileEntity(file, ContentType.create("application/binary"))) + .setHeader("User-Agent", "Spark HttpFileUpload") + .build(); + httpClient.execute(request, httpResponse -> { + try { + final int statusCode = httpResponse.getCode(); + final String reasonPhrase = httpResponse.getReasonPhrase(); + if ((statusCode >= 200) && (statusCode <= 202)) { + Log.debug("Upload file success. HTTP response: " + statusCode + " " + reasonPhrase); + broadcastUploadUrl(room.getBareJid(), response.getUrl, type); + } else { + throw new IllegalStateException("Server responded to upload request with: " + statusCode + ": " + reasonPhrase); + } + } catch (Exception e) { + Log.error("Error encountered whilst uploading the file", e); + UIManager.put("OptionPane.okButtonText", Res.getString("ok")); + JOptionPane.showMessageDialog(room, "Upload failed: " + e.getMessage(), "Http File Upload Plugin", JOptionPane.ERROR_MESSAGE); + } + return null; + }); } catch (Exception e) { Log.error("Error encountered whilst uploading the file", e); UIManager.put("OptionPane.okButtonText", Res.getString("ok"));