Continuando a discussão do Java.lang.NullPointerException:
public Boolean verificarEnvioDados(String idCliente, String chaveLiberacao){
Boolean result = false;
if (idCliente!=null && idCliente.length()>0){
String idClienteAux = desencriptar(idCliente, getControleInterno().getBytes());
if (idClienteAux.substring(10).equals("1")){
return true;
}
}
SimpleDateFormat referenciaFormat = new SimpleDateFormat("MM/yyyy");
SimpleDateFormat clientDateFormat = new SimpleDateFormat(FacesMessages.createFacesMessage(FacesMessage.SEVERITY_INFO, "pattern", "dd/MM/yyyy", new Object[]{}).getSummary());
GregorianCalendar gcRefAnterior = new GregorianCalendar();
try {
if (chaveLiberacao!=null && chaveLiberacao.length()>9){
gcRefAnterior.setTimeInMillis(clientDateFormat.parse("01/"+referenciaFormat.format(new java.util.Date())).getTime());
gcRefAnterior.add(Calendar.MONTH, -1);
String serialHDD = getControleInterno();
String refChave = desencriptar(chaveLiberacao, serialHDD.getBytes());
String refAnterior = referenciaFormat.format(gcRefAnterior.getTime());
if (Integer.parseInt(refChave.substring(3,7)+refChave.substring(0,2))>=Integer.parseInt(refAnterior.substring(3,7)+refAnterior.substring(0,2)) ){
result = true;
}else {
GregorianCalendar gcAtual = new GregorianCalendar();
gcRefAnterior.add(Calendar.MONTH, -1);
if (desencriptar(chaveLiberacao, serialHDD.getBytes()).equals(referenciaFormat.format(gcRefAnterior.getTime())) && gcAtual.get(Calendar.DATE) <=5 ){
result = true;
avisoEnvioDados = true;
}
}
}
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
public static String desencriptar (String texto){
String resultado = texto;
try {
String chave = texto.substring(0,16);
texto = texto.substring(16,texto.length());
return desencriptar(texto, hexStringToByteArray(chave));
}catch (Exception e){
e.printStackTrace();
}
return resultado;
}
private static String desencriptar (String texto, byte[] chave){
try {
DESKeySpec desKeySpec = new DESKeySpec(chave);
SecretKeyFactory keyFactory2 = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory2.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(hexStringToByteArray(texto)));
}catch (Exception e){
e.printStackTrace();
return null;
}
}
private static byte[] hexStringToByteArray(String hexa)
throws IllegalArgumentException {
if (hexa.length() % 2 != 0) {
throw new IllegalArgumentException("String hexa inválida");
}
byte[] b = new byte[hexa.length() / 2];
for (int i = 0; i < hexa.length(); i+=2) {
b[i / 2] = (byte) ((hexDigits.indexOf(hexa.charAt(i)) << 4) |
(hexDigits.indexOf(hexa.charAt(i + 1))));
}
return b;
}
/**
* @return the enviodeDados
*/
public Boolean isDadosEnviados() {
return dadosEnviados;
}
/**
* @return the informarIdCliente
*/
public Boolean isInformarIdCliente() {
return informarIdCliente;
}
public Boolean verificarIdCliente(String idCliente){
return !(idCliente!=null && idCliente.length()>0 && desencriptar(idCliente, getControleInterno().getBytes()).length()==11);
}
private Integer decodeIdCliente(String idCliente) {
Integer idClienteAux = null;
if (idCliente!=null && idCliente.length()>0) {
idClienteAux = Integer.parseInt(desencriptar(idCliente, getControleInterno().getBytes()).substring(0,10));
}
return idClienteAux;
}
public void informarIdCliente(ActionEvent event){
messageRetornoAjax = "";
try {
String idCliente = verificarChaveLiberacaoEnvioDados(idClienteChange);
if (idCliente!=null){
@SuppressWarnings("unchecked")
List<Parametro> parametros_result = entityManager.createQuery(
"from Parametro ").getResultList();
parametros_result.get(0).setIdCliente(idCliente);
informarIdCliente = false;
entityManager.merge(parametros_result.get(0));
entityManager.flush();
chaveLiberacaoEnvioDadosChange=idClienteChange;
liberarSistemaEnvioDados(event);
}else {
messageRetornoAjax = FacesMessages.createFacesMessage(FacesMessage.SEVERITY_INFO, "erro.validacao.chaveLiberacao", "erro.validacao.chaveLiberacao", new Object[]{}).getSummary();
}
}catch (Exception e) {
messageRetornoAjax = "Erro:"+Erros.getMessage(e);
e.printStackTrace();
}
}
/**
* @return the idClienteChange
*/
public String getIdClienteChange() {
return idClienteChange;
}
/**
* @param idClienteChange the idClienteChange to set
*/
public void setIdClienteChange(String idClienteChange) {
this.idClienteChange = idClienteChange;
}
public String verificarChaveLiberacaoEnvioDados(String idClienteChange){
String result = null;
try {
if (idClienteChange!=null){
SimpleDateFormat clientDateFormat = new SimpleDateFormat(FacesMessages.createFacesMessage(FacesMessage.SEVERITY_INFO, "pattern", "dd/MM/yyyy", new Object[]{}).getSummary());
String algo = clientDateFormat.format(new java.util.Date()).substring(0,2)+clientDateFormat.format(new java.util.Date()).substring(3,5);
String serialHDD = getControleInterno();
String idClienteDecode = desencriptar(idClienteChange, (algo+serialHDD).getBytes());
if (idClienteDecode.length()==11){
result = encriptar(idClienteDecode.substring(0,11), (serialHDD).getBytes());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private static String encriptar(String texto, byte[] chave) {
try{
DESKeySpec desKeySpec = new DESKeySpec(chave);
SecretKeyFactory keyFactory2 = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory2.generateSecret(desKeySpec);
byte[] data = texto.getBytes();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] resultado = cipher.doFinal(data);
return byteArrayToHexString(resultado);
}catch (Exception ioe) {
ioe.printStackTrace();
return null;
}
}
public static String byteArrayToHexString(byte[] b) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < b.length; i++) {
int j = b[i] & 0xFF;
buf.append(hexDigits.charAt(j / 16));
buf.append(hexDigits.charAt(j % 16));
}
return buf.toString();
}
public void liberarSistemaEnvioDados(ActionEvent event){
messageRetornoAjax = "";
try {
String chave = verificarChaveLiberacaoEnvioDados(chaveLiberacaoEnvioDadosChange);
if (chave!=null){
SimpleDateFormat referenciaFormat = new SimpleDateFormat("MM/yyyy");
SimpleDateFormat clientDateFormat = new SimpleDateFormat(FacesMessages.createFacesMessage(FacesMessage.SEVERITY_INFO, "pattern", "dd/MM/yyyy", new Object[]{}).getSummary());
GregorianCalendar gcRefAnterior = new GregorianCalendar();
try {
@SuppressWarnings("unchecked")
List<Parametro> parametros_result = entityManager.createQuery(
"from Parametro ").getResultList();
gcRefAnterior.setTimeInMillis(clientDateFormat.parse("01/"+referenciaFormat.format(new java.util.Date())).getTime());
gcRefAnterior.add(Calendar.MONTH, -1);
parametros_result.get(0).setChDados(encriptar(referenciaFormat.format(gcRefAnterior.getTime()), getControleInterno().getBytes()));
entityManager.merge(parametros_result.get(0));
entityManager.flush();
dadosEnviados = true;
} catch (ParseException e) {
e.printStackTrace();
}
}else {
messageRetornoAjax = FacesMessages.createFacesMessage(FacesMessage.SEVERITY_INFO, "erro.validacao.chaveLiberacao", "erro.validacao.chaveLiberacao", new Object[]{}).getSummary();
}
}catch (Exception e) {
messageRetornoAjax = "Erro:"+Erros.getMessage(e);
e.printStackTrace();
}
}
/**
* @return the chaveLiberacaoEnvioDadosChange
*/
public String getChaveLiberacaoEnvioDadosChange() {
return chaveLiberacaoEnvioDadosChange;
}
/**
* @param chaveLiberacaoEnvioDadosChange the chaveLiberacaoEnvioDadosChange to set
*/
public void setChaveLiberacaoEnvioDadosChange(
String chaveLiberacaoEnvioDadosChange) {
this.chaveLiberacaoEnvioDadosChange = chaveLiberacaoEnvioDadosChange;
}
public boolean getAvisoEnvioDados() {
return avisoEnvioDados;
}
public void setAvisoEnvioDados(boolean avisoEnvioDados) {
this.avisoEnvioDados = avisoEnvioDados;
}
}