source: trunk/jabberit_messenger/java_source/src/nu/fw/jeti/applet/Jeti.java @ 1423

Revision 1423, 12.5 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #588 - Conexão através do proxy ( Squid ) , utilizando mode Digest e Basic.

Line 
1package nu.fw.jeti.applet;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
5import java.net.MalformedURLException;
6import java.net.URL;
7import java.text.MessageFormat;
8import java.util.LinkedList;
9import java.util.List;
10import java.util.Map;
11
12import javax.swing.JApplet;
13import javax.swing.JLabel;
14import javax.swing.JPanel;
15import javax.swing.SwingUtilities;
16import nu.fw.jeti.backend.LoginInfo;
17import nu.fw.jeti.backend.Start;
18import nu.fw.jeti.events.StatusChangeListener;
19import nu.fw.jeti.jabber.Backend;
20import nu.fw.jeti.jabber.JID;
21import nu.fw.jeti.jabber.UnknownJIDStatus;
22import nu.fw.jeti.jabber.elements.IQPrivate;
23import nu.fw.jeti.jabber.elements.InfoQuery;
24import nu.fw.jeti.jabber.elements.InfoQueryBuilder;
25import nu.fw.jeti.jabber.elements.Presence;
26import nu.fw.jeti.jabber.elements.RosterBuilder;
27import nu.fw.jeti.jabber.elements.RosterItemBuilder;
28import nu.fw.jeti.ui.AddContact;
29import nu.fw.jeti.ui.JIDInput;
30import nu.fw.jeti.ui.StatusButton;
31import nu.fw.jeti.util.I18N;
32import nu.fw.jeti.util.JavaScriptServerExpresso;
33import nu.fw.jeti.util.Popups;
34import nu.fw.jeti.util.Preferences;
35import nu.fw.jeti.util.ProxyIM;
36
37import netscape.javascript.JSObject;
38
39/*
40 *      @author E.S. de Boer
41 *      Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer 
42 *
43 *  This program is free software; you can redistribute it and/or modify
44 *  it under the terms of the GNU General Public License as published by
45 *  the Free Software Foundation; either version 2 of the License, or
46 *  (at your option) any later version.
47 *
48 *  This program is distributed in the hope that it will be useful,
49 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
50 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
51 *      GNU General Public License for more details.
52 *
53 *  You should have received a copy of the GNU General Public License
54 *  along with this program; if not, write to the Free Software
55 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
56 *
57 *      For questions, comments etc,
58 *      use the website at http://jeti.jabberstudio.org
59 *  or jabber/mail me at jeti@jabber.org
60 *
61 *      Created on 14-nov-2003
62 */
63
64public class Jeti extends JApplet
65{
66        private Start start;
67        private static Jeti applet;
68        private URL exitURL;
69        private static String groupchatRoom;
70        private static String groupchatServer;
71        private Backend backend;
72        private boolean started = false;
73        private SecureMethodRunner secureMethodInvoker;
74        private Thread initIM;
75        public static JLabel from;
76        public static LoginInfo loginInfo;
77        public static String USERID;
78        public static String SERVEREXPRESSO;
79        public static String NAMECOMPANY;
80        public static String USEHTTPS;
81        public static String CNNAME;
82        public static List GROUPSUSER;
83        public static String SERVERJABBER;
84        public static String COUNTRY;
85        public static String LANGUAGE;
86        public static String JAVAPLUGINS;
87       
88        public static JSObject WINDOWAPPLET = null;     
89
90        /**
91         * @Author      : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
92         * @date        : 10/06/2009
93         * @Brief       : A variavel DEBUG_APPLET_VIEWER = true, para usar o appletviewer
94         */
95        public static boolean DEBUG_APPLET_VIEWER = false;
96
97        public void init()
98        {
99                splash();
100                loadApplet();
101        }
102       
103        public void loadApplet()
104        {
105                applet = this;
106               
107                String server = new String(applet.getParameter("SERVER"));
108                String portText = new String(applet.getParameter("PORT"));
109                boolean ssl = Boolean.valueOf(applet.getParameter("SSL")).booleanValue();
110                String user = new String(applet.getParameter("USER"));
111                String password = new String(applet.getParameter("PASSWORD"));
112                String resource = new String(applet.getParameter("RESOURCE"));
113                String host = new String(applet.getParameter("HOST"));
114                String serverExpresso = new String(applet.getParameter("EXPRESSO"));
115                String useHttps = new String(applet.getParameter("USEHTTPS"));
116                String nameCompany = new String(applet.getParameter("NAMECOMPANY"));
117                String cnName = new String(applet.getParameter("CNNAME"));
118                String country = new String(applet.getParameter("COUNTRY"));
119                String language = new String(applet.getParameter("LANGUAGE"));
120                String javaPlugins = new String(applet.getParameter("JAVAPLUGINS"));
121               
122                /**
123                 * @Author : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
124                 * @Date : 20/10/2008
125                 * @Description : - Nome Servidor IM-Jabber para Constant SERVERJABBER
126                 *                                - Nome da Maquina Expresso para Constant SERVEREXPRESSO
127                 *                                - Nome da Empresa para Constant NAMECOMPANY
128                 *                                - Informa qual o tipo de protocolo a ser usado http ou https.
129                 *                                - Passa ao java o cn do ldap do servidor expresso.
130                 *                                - Carrega plugins para o Java.
131                 *                                - uid expresso;                                                         
132                 */
133               
134                SERVERJABBER    = server;       
135                SERVEREXPRESSO  = serverExpresso;
136                NAMECOMPANY             = nameCompany;
137                USEHTTPS                = useHttps;
138                CNNAME                  = cnName;
139                LANGUAGE                = language;
140                COUNTRY                 = country;
141                JAVAPLUGINS             = javaPlugins;
142                USERID                  = user;
143
144                // Reinit languages incase they changed
145                I18N.init(language, country);
146               
147                groupchatRoom = getParameter("GROUPCHATROOM");
148                groupchatServer = getParameter("GROUPCHATSERVER");
149               
150                Start.programURL = getCodeBase();
151                Start.applet = true;
152                final JPanel panel = new JPanel(new BorderLayout());
153                start = new Start( getCodeBase().toString(), panel );
154               
155                backend = start.getBackend();
156                backend.getMain().translate();
157
158                if( server !=null || resource != null || portText !=null )
159                {
160                        int port = Integer.parseInt(portText);
161                       
162                        ProxyIM.setProxy("http://" + server);
163                        String proxyServer = ProxyIM.getProxyServer();
164                        String proxyPort   = String.valueOf(ProxyIM.getProxyPort());
165
166                        loginInfo = new LoginInfo(server, host.trim(), user, password, resource, port, ssl, proxyServer, proxyPort);
167                        backend.autoLogin(loginInfo, 2);
168
169                        if( loginInfo != null )
170                        {
171                                secureMethodInvoker = new SecureMethodRunner(backend);
172                                secureMethodInvoker.start();
173
174                                backend.addExtensionHandler("jeti:prefs",new PreferencesHandler());
175                                try
176                                {
177                                        backend.addListener(StatusChangeListener.class,new StatusChangeListener()
178                                        {
179                                                public void connectionChanged(boolean online)
180                                                {
181                                                        if( online )
182                                                        {       
183                                                                backend.send(new InfoQuery("get",new IQPrivate(new JetiPrivatePreferencesExtension())));
184                                                               
185                                                                if( !nu.fw.jeti.applet.Jeti.DEBUG_APPLET_VIEWER )
186                                                                        WINDOWAPPLET = JSObject.getWindow(applet);
187                                                        }
188                                                }
189                                                public void ownPresenceChanged(int show, String status) {}
190                                                public void exit(){}
191                                        });
192                                }
193                                catch(NoClassDefFoundError e)
194                                {       
195                                        e.printStackTrace();
196                                }
197                               
198                                new Popups(Jeti.this);
199                                SwingUtilities.invokeLater(new Runnable()
200                                {
201                                        public void run()
202                                        {
203                                                setContentPane(panel);
204                                                validate();
205                                        }
206                                });
207                                started = true;
208                        }
209                        else
210                        {
211                                started = false;
212                        }
213                }
214        }
215       
216        private void splash()
217        {
218                /**
219                 * Splash - Inicializador -
220                 * Modificado by Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
221                 */
222               
223       JLabel label = new JLabel(  "<html>" +
224                                                                   "<span style='margin-left:10px;font:20pt Verdana,Arial,serif'>Expresso IM </span>" +
225                                                                   "<br/>" +
226                                                                   "<span style='font:9pt Verdana,Arial,serif'> Jeti - http://www.jeti-im.org </span></html>",
227                                                                   JLabel.CENTER );
228       
229       label.setBackground(new Color(255,255,255));
230       getContentPane().setBackground(new Color(255,255,255));
231       getContentPane().add(label,BorderLayout.CENTER);
232       
233       JPanel panel = new JPanel();
234       panel.setBackground(new Color(255,255,255));
235       from = new JLabel(NAMECOMPANY, JLabel.CENTER);
236       from.setBackground(new Color(255,255,255));
237       from.setFont(new java.awt.Font("Serif,Verdana,Arial",1,10));
238       panel.add(from);
239       getContentPane().add(panel,BorderLayout.SOUTH);
240        }
241       
242        public String getStatusMessages(int status)
243        {
244        List messages = Preferences.getStatusMessages(status);
245                return messages.toString();
246        }
247       
248        public String getGroupsToExpresso()
249        {
250                String[] Groups = applet.backend.getAllGroups();
251                String groups = "";
252               
253                for(int i = 0 ; i < Groups.length; i++ )
254                        groups += Groups[i] + ";";
255               
256                return groups.toString();
257        }
258       
259        public void changeStatusfromExpresso(int status, String msgStatus)
260        {
261                switch (status)
262        {
263                    case Presence.AVAILABLE:     
264                        StatusButton.changeStatusExpresso(Presence.AVAILABLE, msgStatus);
265                        break;
266                    case Presence.FREE_FOR_CHAT:
267                        StatusButton.changeStatusExpresso(Presence.FREE_FOR_CHAT, msgStatus);
268                        break;
269                    case Presence.AWAY:
270                        StatusButton.changeStatusExpresso(Presence.AWAY, msgStatus);
271                        break;
272                    case Presence.XA:           
273                        StatusButton.changeStatusExpresso(Presence.XA, msgStatus);
274                        break;
275                    case Presence.DND:           
276                        StatusButton.changeStatusExpresso(Presence.DND, msgStatus);
277                        break;
278                    case Presence.UNAVAILABLE:   
279                        StatusButton.changeStatusExpresso(Presence.UNAVAILABLE, msgStatus);
280                        break;
281                    case Presence.INVISIBLE:     
282                        StatusButton.changeStatusExpresso(Presence.INVISIBLE, msgStatus);
283                        break;
284        }
285        }
286       
287        public static void showURL(String url)
288        {
289                try
290                {
291                        applet.getAppletContext().showDocument(new URL(url),"_blank");
292                }
293                catch (MalformedURLException e){}
294        }
295               
296        public void destroy()
297        {
298                try
299                {
300                        if( secureMethodInvoker != null )
301                                secureMethodInvoker.stopRunning();
302                        start.close();
303                }
304                catch (Exception e)
305                {
306                        e.printStackTrace();
307                }
308        }
309               
310        public static void exit()
311        {
312                applet.getAppletContext().showDocument(applet.exitURL);
313        }
314       
315        public boolean isReady()
316        {
317                System.out.println("ready " + started);
318                return started;
319        }
320       
321        public boolean isLoggedIn()
322        {
323                if( !started )
324                        return false;
325               
326                return backend.isLoggedIn();
327        }
328       
329        public void openChat(String jid)
330        {
331                try
332                {
333                        JID j = JID.checkedJIDFromString(jid);
334                        secureMethodInvoker.addData(new Object[]{"openChat",j});
335                }
336                catch (InstantiationException e)
337                {
338                        e.printStackTrace();
339                }
340
341        }
342       
343        public void addContact(String jid, String name, String group)
344        {
345                /**
346                 * @Author(s) : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
347                 * @Date : 24/04/2009
348                 * @Brief : Adiciona novos contatos e ja pede autorizacao.
349                 */
350                try
351                {
352                        JID Jid = JID.checkedJIDFromString(jid);
353                        backend.send(new Presence(Jid, "subscribe"));
354                        RosterBuilder rb = new RosterBuilder();
355                        RosterItemBuilder rib = new RosterItemBuilder();
356                        rib.addGroup(group);
357                        rib.jid = Jid;
358                        rib.name = name;
359                        rib.ask = "subscribe";
360                        rib.subscription = "none";
361                        rb.addItem(rib.build());
362                        InfoQueryBuilder iqb = new InfoQueryBuilder();
363                        iqb.addExtension(rb.build());
364                        iqb.setType("set");
365                        backend.send(iqb.build());
366                }
367                catch(Exception error)
368                {
369                        error.printStackTrace();
370                }
371        }
372       
373        public void login(String username,String server,String password)
374        {
375                if( backend.isLoggedIn() )
376                {
377                        JID j = backend.getMyJID();
378                       
379                        if( j.getDomain().equals(server) && j.getUser().equals(username) )
380                                return;
381                }
382                secureMethodInvoker.addData(new Object[]{"login",username,server,password});
383        }
384       
385        public static Class getPlugin(String name)
386        {
387                try
388        {
389            return Class.forName("nu.fw.jeti.plugins."+name+".Plugin");
390                }
391        catch (ClassNotFoundException e)
392                {
393                        System.err.println(MessageFormat.format(I18N.gettext("main.error.{0}_plugin_not_found"), new Object[]{name}));
394                        return null;
395                }
396        }
397       
398        public static void getPrefPanel(String name,Map loadedPreferencePanels)
399        {
400                Class prefPanel = null;
401                try
402                {
403                        prefPanel = Class.forName("nu.fw.jeti.plugins."+name+".PrefPanel");
404                }
405                catch (ClassNotFoundException e)
406                {
407                        return;
408                }
409                loadedPreferencePanels.put(name,prefPanel);
410        }
411       
412        static class SecureMethodRunner extends Thread
413        {
414                private Backend backend;
415                private LinkedList queue = new LinkedList();
416                private volatile boolean isRunning=true;
417
418            SecureMethodRunner(Backend backend)
419                {
420                this.backend = backend;
421                }
422               
423            public void addData(Object method)
424            {
425                synchronized(queue)
426                        {
427                                queue.addLast(method);
428                                queue.notifyAll();
429                        }
430            }
431           
432            public void stopRunning()
433            {
434                isRunning = false;
435                        synchronized(queue){queue.notifyAll();}
436            }
437
438            public void run()
439                {
440                        Object[] method;
441                        while (isRunning)
442                        {
443                                synchronized (queue)
444                                {
445                                        if (queue.isEmpty())
446                                        {
447                                                try
448                                                {
449                                                        queue.wait();
450                                                }
451                                                catch (InterruptedException e)
452                                                {
453                                                        //bug when thrown? called when interrupted
454                                                        e.printStackTrace();
455                                                        return;
456                                                }
457                                                continue;
458                                        }
459                                        method = (Object[]) queue.removeFirst();
460                                }
461
462                                String m = (String)method[0];
463                               
464                                if( m.equals("openChat") )
465                                        backend.getMain().startChat((JID)method[1]);
466                        }
467                }
468        }
469}
470
471/*
472 * Overrides for emacs
473 * Local variables:
474 * tab-width: 4
475 * End:
476 */
Note: See TracBrowser for help on using the repository browser.