source: 3thparty/jmessenger/src/nu/fw/jeti/util/ServerExpresso.java @ 3952

Revision 3952, 5.5 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1710 - Adicao do codigo fonte java do componente jmessenger(jabberit_messenger)

  • Property svn:executable set to *
Line 
1/**
2 * @Authors : Alexandre Correia - alexandrecorreia@celepar.pr.gov.br
3 *                        Rodrigo Souza - rodsouza@celepar.pr.gov.br
4 * @Date : 29/10/2008
5 * @Description : Conecta com o servidor do Expresso (http ou https),
6 * faz um post para receber os dados nome e organizacao do contato.
7 */
8
9package nu.fw.jeti.util;
10
11
12import java.io.*;
13import java.net.*;
14import javax.net.ssl.*;
15import java.nio.charset.Charset;
16import java.nio.charset.CharsetDecoder;
17
18class MyHostnameVerifier implements HostnameVerifier
19{
20
21        public boolean verify(String hostname, SSLSession session)
22        {
23                return true;
24    }
25}
26
27public class ServerExpresso
28{
29
30        private void acceptSSL()
31        {
32                // Create a trust manager that does not validate certificate chains
33        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
34            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
35                return null;
36            }
37 
38            public void checkClientTrusted(
39                    java.security.cert.X509Certificate[] certs, String authType) {
40            }
41 
42            public void checkServerTrusted(
43                    java.security.cert.X509Certificate[] certs, String authType) {
44            }
45        } };
46 
47        // Install the all-trusting trust manager
48        try {
49            SSLContext sc = SSLContext.getInstance("SSL");
50 
51            sc.init(null, trustAllCerts, new java.security.SecureRandom());
52            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
53        }
54        catch (Exception e){}           
55    }
56       
57        private String connectHttp(String typeProtocol, String NameServer, String Param)
58        {
59                String returnResult = "";
60                String line;
61               
62                try
63                {
64                        URLConnection conn = (URLConnection)(new URL( typeProtocol + "://" + NameServer + "inc/webservice.php")).openConnection();
65                conn.setDoOutput(true);
66                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
67                wr.write(Param);
68                wr.flush();
69           
70                // Get the response
71                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
72
73                while ( (line = rd.readLine()) != null )
74                {
75                        if( line.trim() != "" )
76                                returnResult += line;
77                }
78
79                wr.close();
80                rd.close();
81               
82                }
83                catch(Exception e)
84                {
85                        returnResult = "Erro : " + e ; 
86                }
87               
88                return returnResult;
89        }
90
91        private String connectHttps(String typeProtocol, String NameServer, String Param)
92        {
93                String returnResult = "";
94                String line;
95
96        try
97        {
98                this.acceptSSL();
99                HttpsURLConnection conn = (HttpsURLConnection)(new URL(typeProtocol + "://" + NameServer + "inc/webservice.php")).openConnection();
100                conn.setHostnameVerifier(new MyHostnameVerifier());
101                conn.setDoOutput(true);
102
103                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
104                wr.write(Param);
105                wr.flush();
106           
107                // Get the response
108                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
109
110                while ( (line = rd.readLine()) != null )
111                {
112                        if( line.trim() != "" )
113                                returnResult += line;
114                }
115
116                wr.close();
117                rd.close();
118               
119                conn.disconnect();
120        }
121        catch(Exception e)
122        {
123                returnResult = "Erro : " + e ;
124        }
125
126        return returnResult;
127        }
128       
129        public String NameCN(String Jid)
130        {
131                return this.connectServerExpresso(Jid);
132        }
133       
134        private String connectServerExpresso(String Jid)
135        {
136
137                String cnName = "";
138
139        // Construct data
140        String data = "jid=" + Jid;                     
141       
142        // Type Charset
143        CharsetDecoder decoder = Charset.defaultCharset().newDecoder();
144        String charsetType = decoder.charset().toString();
145       
146                if ( charsetType.equals("UTF-8"))
147                        data += "&charset=1";
148                else
149                        data += "&charset=2";
150               
151        // Name Server Expresso
152        String serverExpresso = nu.fw.jeti.applet.Jeti.SERVEREXPRESSO;
153       
154        // Type Protocol Http / Https
155        String typeProtocol = nu.fw.jeti.applet.Jeti.USEHTTPS;
156       
157        if( typeProtocol.equals("http") )
158                cnName = this.connectHttp(typeProtocol, serverExpresso, data);
159        else
160                cnName = this.connectHttps(typeProtocol, serverExpresso, data);
161
162                return cnName;
163        }
164       
165        public String PhotoUid(String Jid)
166        {
167                String photo = "";
168                String data = "javaPhoto=" + Jid;
169               
170        // Name Server Expresso
171        String serverExpresso = nu.fw.jeti.applet.Jeti.SERVEREXPRESSO;
172       
173        // Type Protocol Http / Https
174        String typeProtocol = nu.fw.jeti.applet.Jeti.USEHTTPS;
175       
176        if( typeProtocol.equals("http") )
177        {
178                photo = this.connectHttp(typeProtocol, serverExpresso, data);
179        }
180        else
181        {
182                photo = this.connectHttps(typeProtocol, serverExpresso, data);
183        }
184       
185        return photo;
186        }
187       
188        public String connectCallVoip(String jidFrom, String jidTo)
189        {
190                String callVoip = "";
191                String data = "javaVoipFrom=" + jidFrom + "&javaVoipTo=" + jidTo;
192
193                // Name Server Expresso
194        String serverExpresso = nu.fw.jeti.applet.Jeti.SERVEREXPRESSO;
195       
196        // Type Protocol Http / Https
197        String typeProtocol = nu.fw.jeti.applet.Jeti.USEHTTPS;
198       
199        if( typeProtocol.equals("http") )
200        {
201                callVoip = this.connectHttp(typeProtocol, serverExpresso, data);
202        }
203        else
204        {
205                callVoip = this.connectHttps(typeProtocol, serverExpresso, data);
206        }
207       
208        return callVoip;
209        }
210}
Note: See TracBrowser for help on using the repository browser.