use of org.demoiselle.signer.core.exception.CertificateCoreException in project signer by demoiselle.
the class TimeStampOperator method invoke.
/**
* Sends the time stamp request {@link createRequest} to a time stamp server
*
* @param request request to be sent
* @return The time stamp returned by the server
*/
public byte[] invoke(byte[] request) throws CertificateCoreException {
try {
logger.info(timeStampMessagesBundle.getString("info.timestamp.init.request"));
Connector connector = ConnectorFactory.buildConnector(ConnectionType.SOCKET);
connector.setHostname(TimeStampConfig.getInstance().getTspHostname());
connector.setPort(TimeStampConfig.getInstance().getTSPPort());
logger.info(timeStampMessagesBundle.getString("info.timestamp.response"));
inputStream = connector.connect(request);
long tempo;
// Valor do timeout da verificacao de dados disponiveis para leitura
int timeOut = 3500;
// Verificando se os 4 bytes iniciais estao disponiveis para leitura
for (tempo = System.currentTimeMillis() + timeOut; inputStream.available() < 4 && System.currentTimeMillis() < tempo; ) {
try {
Thread.sleep(1L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Lendo tamanho total
byte[] tamanhoRetorno = new byte[4];
inputStream.read(tamanhoRetorno, 0, 4);
int tamanho = new BigInteger(tamanhoRetorno).intValue();
// Verificando se os bytes na quantidade "tamanho" estao disponiveis
if (System.currentTimeMillis() < tempo) {
while (inputStream.available() < tamanho && System.currentTimeMillis() < tempo) {
try {
Thread.sleep(1L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (System.currentTimeMillis() >= tempo) {
logger.error(timeStampMessagesBundle.getString("info.timestamp.timeout"));
}
} else {
logger.error(timeStampMessagesBundle.getString("info.timestamp.timeout"));
}
// Lendo flag
byte[] retornoFlag = new byte[1];
inputStream.read(retornoFlag, 0, 1);
// tamanho total menos o tamanho da flag
tamanho -= 1;
// Lendo dados carimbo
byte[] retornoCarimboDeTempo = new byte[tamanho];
inputStream.read(retornoCarimboDeTempo, 0, tamanho);
timeStampResponse = new TimeStampResponse(retornoCarimboDeTempo);
logger.info(timeStampMessagesBundle.getString("info.timestamp.status", timeStampResponse.getStatus()));
switch(timeStampResponse.getStatus()) {
case 0:
{
logger.info(timeStampMessagesBundle.getString("info.pkistatus.granted"));
break;
}
case 1:
{
logger.info(timeStampMessagesBundle.getString("info.pkistatus.grantedWithMods"));
break;
}
case 2:
{
logger.info(timeStampMessagesBundle.getString("error.pkistatus.rejection"));
throw new CertificateCoreException(timeStampMessagesBundle.getString("error.pkistatus.rejection"));
}
case 3:
{
logger.info(timeStampMessagesBundle.getString("error.pkistatus.waiting"));
throw new CertificateCoreException(timeStampMessagesBundle.getString("error.pkistatus.waiting"));
}
case 4:
{
logger.info(timeStampMessagesBundle.getString("error.pkistatus.revocation.warn"));
throw new CertificateCoreException(timeStampMessagesBundle.getString("error.pkistatus.revocation.warn"));
}
case 5:
{
logger.info(timeStampMessagesBundle.getString("error.pkistatus.revocation.notification"));
throw new CertificateCoreException(timeStampMessagesBundle.getString("error.pkistatus.revocation.notification"));
}
default:
{
logger.info(timeStampMessagesBundle.getString("error.pkistatus.unknown"));
throw new CertificateCoreException(timeStampMessagesBundle.getString("error.pkistatus.unknown"));
}
}
// ok
int failInfo = -1;
if (timeStampResponse.getFailInfo() != null) {
failInfo = Integer.parseInt(new String(timeStampResponse.getFailInfo().getBytes()));
}
logger.info(timeStampMessagesBundle.getString("info.timestamp.failinfo", failInfo));
switch(failInfo) {
case 0:
logger.info(timeStampMessagesBundle.getString("error.pkifailureinfo.badAlg"));
break;
case 2:
logger.info(timeStampMessagesBundle.getString("error.pkifailureinfo.badRequest"));
break;
case 5:
logger.info(timeStampMessagesBundle.getString("error.pkifailureinfo.badDataFormat"));
break;
case 14:
logger.info(timeStampMessagesBundle.getString("error.pkifailureinfo.timeNotAvailable"));
break;
case 15:
logger.info(timeStampMessagesBundle.getString("error.pkifailureinfo.unacceptedPolicy"));
break;
case 16:
logger.info(timeStampMessagesBundle.getString("error.pkifailureinfo.unacceptedExtension"));
break;
case 17:
logger.info(timeStampMessagesBundle.getString("error.pkifailureinfo.addInfoNotAvailable"));
break;
case 25:
logger.info(timeStampMessagesBundle.getString("error.pkifailureinfo.systemFailure"));
break;
}
timeStampResponse.validate(timeStampRequest);
TimeStampToken timeStampToken = timeStampResponse.getTimeStampToken();
this.setTimestamp(new Timestamp(timeStampToken));
if (timeStampToken == null) {
throw new CertificateCoreException(timeStampMessagesBundle.getString("error.timestamp.token.null"));
}
connector.close();
// Imprime os dados do carimbo de tempo
logger.info(timestamp.toString());
// Retorna o carimbo de tempo gerado
return timestamp.getEncoded();
} catch (CertificateCoreException | TSPException | IOException e) {
throw new CertificateCoreException(e.getMessage());
}
}
Aggregations