Search in sources :

Example 1 with EasySSLProtocolSocketFactory

use of org.jivesoftware.sparkimpl.updater.EasySSLProtocolSocketFactory in project Spark by igniterealtime.

the class PluginViewer method downloadPlugin.

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");
    if (Default.getBoolean("PLUGIN_REPOSITORY_USE_PROXY")) {
        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) {
            return;
        }
        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) {
        Log.error(e);
    }
}
Also used : URL(java.net.URL) DocumentException(org.dom4j.DocumentException) EasySSLProtocolSocketFactory(org.jivesoftware.sparkimpl.updater.EasySSLProtocolSocketFactory) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Protocol(org.apache.commons.httpclient.protocol.Protocol)

Example 2 with EasySSLProtocolSocketFactory

use of org.jivesoftware.sparkimpl.updater.EasySSLProtocolSocketFactory in project Spark by igniterealtime.

the class PluginViewer method loadAvailablePlugins.

private void loadAvailablePlugins() {
    availablePanel.removeAll();
    availablePanel.invalidate();
    availablePanel.validate();
    availablePanel.repaint();
    JLabel label = new JLabel(Res.getString("message.loading.please.wait"));
    availablePanel.add(label);
    SwingWorker worker = new SwingWorker() {

        Collection<PublicPlugin> pluginList = null;

        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();
            if (Default.getBoolean("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) {
                        Log.error(e);
                    }
                }
            }
            try {
                int result = httpclient.executeMethod(post);
                if (result != 200) {
                    return null;
                }
                pluginList = getPluginList(post.getResponseBodyAsStream());
            } catch (Exception ex) {
            // Nothing to do
            }
            return "ok";
        }

        public void finished() {
            final PluginManager pluginManager = PluginManager.getInstance();
            if (pluginList == null) {
                availablePanel.removeAll();
                availablePanel.invalidate();
                availablePanel.validate();
                availablePanel.repaint();
                UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
                JOptionPane.showMessageDialog(availablePanel, Res.getString("message.plugins.not.available"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                return;
            }
            Iterator<PublicPlugin> plugs = pluginList.iterator();
            availablePanel.removeAll();
            while (plugs.hasNext()) {
                PublicPlugin plugin = plugs.next();
                if (!pluginManager.isInstalled(plugin)) {
                    SparkPlugUI ui = new SparkPlugUI(plugin);
                    availablePanel.add(ui);
                    addSparkPlugUIListener(ui);
                }
            }
            availablePanel.invalidate();
            availablePanel.validate();
            availablePanel.repaint();
        }
    };
    worker.start();
}
Also used : PublicPlugin(org.jivesoftware.spark.plugin.PublicPlugin) DocumentException(org.dom4j.DocumentException) EasySSLProtocolSocketFactory(org.jivesoftware.sparkimpl.updater.EasySSLProtocolSocketFactory) PluginManager(org.jivesoftware.spark.PluginManager) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) SwingWorker(org.jivesoftware.spark.util.SwingWorker) Collection(java.util.Collection) Protocol(org.apache.commons.httpclient.protocol.Protocol)

Example 3 with EasySSLProtocolSocketFactory

use of org.jivesoftware.sparkimpl.updater.EasySSLProtocolSocketFactory in project Spark by igniterealtime.

the class ChatRoomDecorator method uploadFile.

private void uploadFile(File file, UploadRequest response, ChatRoom room, Message.Type type) {
    Log.warning("uploadFile request " + room.getRoomname() + " " + response.putUrl);
    URLConnection urlconnection = null;
    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();
        Log.warning("uploadFile response " + statusCode + " " + responseBody);
        if ((statusCode >= 200) && (statusCode <= 202)) {
            broadcastUploadUrl(room.getRoomname(), response.getUrl, type);
        }
    } catch (Exception e) {
        Log.error("uploadFile error", e);
    }
}
Also used : FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) HttpClient(org.apache.commons.httpclient.HttpClient) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Protocol(org.apache.commons.httpclient.protocol.Protocol) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) EasySSLProtocolSocketFactory(org.jivesoftware.sparkimpl.updater.EasySSLProtocolSocketFactory)

Aggregations

HttpClient (org.apache.commons.httpclient.HttpClient)3 Protocol (org.apache.commons.httpclient.protocol.Protocol)3 EasySSLProtocolSocketFactory (org.jivesoftware.sparkimpl.updater.EasySSLProtocolSocketFactory)3 GetMethod (org.apache.commons.httpclient.methods.GetMethod)2 DocumentException (org.dom4j.DocumentException)2 URL (java.net.URL)1 Collection (java.util.Collection)1 FileRequestEntity (org.apache.commons.httpclient.methods.FileRequestEntity)1 PutMethod (org.apache.commons.httpclient.methods.PutMethod)1 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)1 PluginManager (org.jivesoftware.spark.PluginManager)1 PublicPlugin (org.jivesoftware.spark.plugin.PublicPlugin)1 SwingWorker (org.jivesoftware.spark.util.SwingWorker)1