source: trunk/seguranca/ExpressoCertMail/src/br/gov/serpro/js/ParamReaderThread.java @ 1035

Revision 1035, 9.4 KB checked in by rafaelraymundo, 15 years ago (diff)

Ticket #558 - Adicionada funcionalidade de assinatura e criptografia de e-mails.

Line 
1package br.gov.serpro.js;
2
3import java.awt.Frame;
4import java.io.IOException;
5import java.security.GeneralSecurityException;
6import java.security.ProviderException;
7import java.util.Map;
8
9import javax.mail.MessagingException;
10
11import org.bouncycastle.mail.smime.SMIMEException;
12import br.gov.serpro.cert.DigitalCertificate;
13import br.gov.serpro.setup.Setup;
14import br.gov.serpro.ui.DialogBuilder;
15import br.gov.serpro.util.Base64Utils;
16
17import netscape.javascript.JSObject;
18import org.bouncycastle.cms.CMSException;
19
20
21public class ParamReaderThread extends Thread {
22
23        JSObject page;
24        Javascript2AppletPassingData data;
25        Setup setup;
26        Frame parentFrame;
27
28        public ParamReaderThread(JSObject page, Javascript2AppletPassingData data, Setup setup, Frame parent) {
29        //public ParamReaderThread(JSObject page, Javascript2AppletPassingData data, Setup setup) {
30                super();
31                this.page = page;
32                this.data = data;
33                this.setup = setup;
34                this.parentFrame = parent;
35        }
36
37        @Override
38        public void run() {
39                // TODO Stub de método gerado automaticamente
40                super.run();
41
42        while (true){
43                        if (setup.getParameter("debug").equalsIgnoreCase("true")){
44                                System.out.println("Classe ParamReaderThread: pegando resultado.");
45                        }
46           
47            // processa o smime. Método sign implementado na classe DigitalCertificate
48            String smime = null;
49            DigitalCertificate dc = null;
50
51            try {
52
53                //Map<String, String> parsedData = parseData(resultado);
54                Map<String, String> parsedData = data.getMap();
55
56                dc = new DigitalCertificate(this.parentFrame, this.setup);
57                dc.init();
58
59                // Testa a operação e se for
60                if (parsedData.get("operation").equals("sign")){
61
62                    smime = dc.signMail(parsedData);
63                    if (setup.getParameter("debug").equalsIgnoreCase("true")) {
64                        System.out.println("\nMensagem assinada: " + smime);
65                    }
66
67                }
68              else if (parsedData.get("operation").equals("decript")){
69                    String decryptedMsg = dc.cipherMail(parsedData);
70                    if (setup.getParameter("debug").equalsIgnoreCase("true")) {
71                        System.out.println("Mensagem decifrada: " + decryptedMsg);
72                    }
73                    if (decryptedMsg == null){
74                        smime = null;
75                    } else {
76                        smime = Base64Utils.base64Encode(decryptedMsg.getBytes());
77                    }
78                   
79                }
80                else {
81                    throw new UnsupportedOperationException("Operation not supported: " + parsedData.get("operation"));
82                    // Lança
83                }
84
85                // Retorna para a página
86                // se smime = null, a assinatura não funcionou
87                if (smime != null){
88                    page.call("appletReturn", new String[] {smime, parsedData.get("ID"), parsedData.get("operation"), parsedData.get("folder")});
89                }
90
91            } catch (IOException e) {
92                //DialogBuilder.showMessageDialog(this.parentFrame, "Não foi possível carregar Token/SmartCard: senha incorreta", this.setup);
93                DialogBuilder.showMessageDialog(this.parentFrame, setup.getLang("ExpressoCertMailMessages", "ParamReaderThread001"), this.setup);
94                //JOptionPane.showMessageDialog(this.parentFrame, "Não foi possível carregar Token/SmartCard: senha incorreta",
95                //        "Aviso", JOptionPane.INFORMATION_MESSAGE);
96                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
97                    e.printStackTrace();
98                }
99            } catch (GeneralSecurityException e) {
100                if (e.getCause() != null){
101                    DialogBuilder.showMessageDialog(this.parentFrame, "GeneralSecurityException: " + e.getCause().getMessage(), this.setup);
102                    //JOptionPane.showMessageDialog(this.parentFrame, "GeneralSecurityException: " + e.getCause().getMessage(),
103                    //        "Aviso", JOptionPane.INFORMATION_MESSAGE);
104                }
105                else {
106                    DialogBuilder.showMessageDialog(this.parentFrame, "GeneralSecurityException: " + e.getMessage(), this.setup);
107                    //JOptionPane.showMessageDialog(this.parentFrame, "GeneralSecurityException: " + e.getMessage(),
108                    //        "Aviso", JOptionPane.INFORMATION_MESSAGE);
109                }
110                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
111                    e.printStackTrace();
112                }
113            } catch (SMIMEException e) {
114                //DialogBuilder.showMessageDialog(this.parentFrame, "Erro no processamento da assinatura: " + e.getMessage(), this.setup);
115                DialogBuilder.showMessageDialog(this.parentFrame, setup.getLang("ExpressoCertMailMessages", "ParamReaderThread002"), this.setup);
116                //JOptionPane.showMessageDialog(this.parentFrame, "Erro no processamento da assinatura: " + e.getMessage(),
117                //        "Aviso", JOptionPane.INFORMATION_MESSAGE);
118                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
119                    e.printStackTrace();
120                }
121            } catch (MessagingException e) {
122                //DialogBuilder.showMessageDialog(this.parentFrame, "Erro no processamento da mensagem: " + e.getMessage(), this.setup);
123                DialogBuilder.showMessageDialog(this.parentFrame, setup.getLang("ExpressoCertMailMessages", "ParamReaderThread003"), this.setup);
124                //JOptionPane.showMessageDialog(this.parentFrame, "Erro no processamento da mensagem: " + e.getMessage(),
125                //        "Aviso", JOptionPane.INFORMATION_MESSAGE);
126                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
127                    e.printStackTrace();
128                }
129            }
130            catch (CMSException e){
131                //DialogBuilder.showMessageDialog(this.parentFrame, "Erro ao decifrar mensagem: Detectado problema na integridade da mensagem cifrada!", this.setup);
132                DialogBuilder.showMessageDialog(this.parentFrame, setup.getLang("ExpressoCertMailMessages", "ParamReaderThread004"), this.setup);
133                //if (setup.getParameter("debug").equalsIgnoreCase("true")) {
134                Throwable cause = e.getCause();
135                System.out.println(e.getClass().getCanonicalName() + ": " + e.getMessage());
136                if (cause != null){
137                    System.out.println(cause.getClass().getCanonicalName() + ": " + cause.getMessage());
138                }
139                e.printStackTrace();
140                //}
141            }
142            catch (ProviderException e){
143                //DialogBuilder.showMessageDialog(this.parentFrame, "Problema no acesso às informações do Token: " + e.getMessage(), this.setup);
144                DialogBuilder.showMessageDialog(this.parentFrame, setup.getLang("ExpressoCertMailMessages", "ParamReaderThread005"), this.setup);
145                //JOptionPane.showMessageDialog(this.parentFrame, "Problema no acesso às informações do Token: " + e.getMessage(),
146                //        "Aviso", JOptionPane.INFORMATION_MESSAGE);
147                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
148                    e.printStackTrace();
149                }
150            }
151            catch (UnsupportedOperationException e){
152                // DialogBuilder.showMessageDialog(this.parentFrame, e.getMessage());
153                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
154                    e.printStackTrace();
155                }
156            }
157            catch (IllegalArgumentException e){
158                //DialogBuilder.showMessageDialog(this.parentFrame, e.getMessage());
159                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
160                    e.printStackTrace();
161                }
162            }
163            catch (InterruptedException e){
164                if (setup.getParameter("debug").equalsIgnoreCase("true")){
165                    System.out.println("Classe ParamReaderThread: Thread has been interrupted! Break.");
166                    e.printStackTrace();
167                }
168                break;
169            }
170
171                        dc.destroy();
172                        dc = null;
173                        System.gc();
174            Thread.yield();
175
176        }
177        }
178
179        //TODO: Documentar o que recebe!!!
180        /*private Map<String, String> parseData(String expressoMailData){
181
182        if ((expressoMailData == null) || (expressoMailData.length() == 0)){
183            throw new IllegalArgumentException("Can't unserialize NULL or empty value!");
184        }
185
186                Map<String, String> parsedData = new HashMap<String, String> ();
187                //Map<String, String> headersData = new HashMap<String, String>();
188
189                for (String paramsArray : expressoMailData.split(";")){
190
191                        if (this.setup.getParameter("debug").equalsIgnoreCase("true")){
192                                System.out.println("sendo parseado: " + paramsArray);
193                        }
194                        String[] param = paramsArray.split(":");
195                        //if (temp[0].equals("headers")){
196                        //      String[] headersArray = new String(Base64Utils.base64Decode(temp[1])).split(";");
197                        //      for (String header: headersArray){
198                        //              String[] keyValueType = header.split(":");
199                        //              headersData.put(keyValueType[0], keyValueType[1]);
200                        //      }
201                        //}
202                        //else{
203                        parsedData.put(param[0], new String(Base64Utils.base64Decode(param[1])));
204                        //}
205
206                }
207
208                return parsedData;
209        }
210
211        static private String processReturn(Map smimeData){
212
213                return new String();
214        }
215*/
216}
Note: See TracBrowser for help on using the repository browser.