salve salve galera!
Seguinte, minha applet está assinada… está sendo reconhecida pelo browser, ma hora de executar o JFileChooer pro usuário escolher qual arquivo digital vai assinar, dá excessão de permissão negada na leitura do diretório padrão do usuário.
tem muito tempo que estou tentando isso e já não tenho nem mais luz… então vim parar aqui! segue o código abaixo.
a applet é iniciada junto com a jsp e é armada a thread que faz expirar a senha do smart card com o tempo.
a função que é chamada dentro do flex é a função launchEditor … que abre um filechooser, seleciona um pdf, assina e carimba digitalmente e retorna a string encodada do novo pdf para eu armazernar no banco de dados.
[code]mport java.applet.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import sun.misc.BASE64Encoder;
public class Launcher extends Applet {
public static String senha;
@Override
public void init() {
senha = null;
( new LauncherThread() ).start();
}
public String launchEditor() {
File fonte = null;
ConfigService cs = null;
try {
fonte = chooseFile();
if (fonte != null) {
if (fonte.getAbsolutePath().toLowerCase().endsWith(".pdf")) {
String saidaEncodada64 = null;
if(senha == null){
senha = jPasswordEncriptado();
}
if (!senha.equals("") && senha != null) {
AssinaCarimbaBry acb = new AssinaCarimbaBry();
File fileSaida = acb.assina(fonte, senha);
byte[] fileSaidaArrayBytes = getBytesFromFile(fileSaida);
saidaEncodada64 = encode64ToDB(fileSaidaArrayBytes);
String retorno = fonte.getName() + "?" + fonte.getAbsolutePath() + "?" + String.valueOf(fileSaida.length()) + "?" + saidaEncodada64;
fileSaida.delete();
return retorno;
} else {
JOptionPane.showMessageDialog(null, "Nenhuma senha foi digitada", "Erro", JOptionPane.ERROR_MESSAGE);
senha = null;
return null;
}
} else {
JOptionPane.showMessageDialog(null, "Arquivo selecionado não é compatível com o Peticione", "Erro", JOptionPane.ERROR_MESSAGE);
return null;
}
} else {
JOptionPane.showMessageDialog(null, "Nenhum Arquivo foi selecionado", "Erro", JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Windows - Acesso negado(" + e.getMessage() + ")");
}
return null;
}
public File chooseFile() {
JFileChooser fc = new JFileChooser();
fc.setAcceptAllFileFilterUsed(false);
fc.setFileFilter(new FileFilterPDF());
fc.setDialogTitle("Escolha o arquivo PDF para anexar");
if (fc.showDialog(null, "Carregar Arquivo") == JFileChooser.APPROVE_OPTION) {
//try {
File fonte = null;
fonte = fc.getSelectedFile();
return fonte;
} else {
return null;
}
}
public byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
long length = file.length();
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset))
>= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Não pode completar a leitura do arquivo "
+ file.getName());
}
is.close();
return bytes;
}
public static String encode64ToDB(byte[] array) {
BASE64Encoder encoder = new BASE64Encoder();
String retorno = encoder.encodeBuffer(array);
return retorno;
}
public String jPasswordEncriptado() {
JPasswordField password = new JPasswordField(10);
password.setEchoChar('*');
JLabel rotulo = new JLabel("Entre com a senha:");
JPanel entUsuario = new JPanel();
entUsuario.add(rotulo);
entUsuario.add(password);
JOptionPane.showMessageDialog(null, entUsuario, "Acesso restrito", JOptionPane.PLAIN_MESSAGE);
return new String(password.getPassword());
}
class LauncherThread extends Thread {
public void run() {
while (true) {
try {
//5 minuto espira a senha
Thread.sleep(300000);
senha = null;
} catch (InterruptedException ex) {
Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
[/code]
Galera… ajuda ae por favor!