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