use of it.cnr.si.cool.jconon.dto.VerificaPECTask in project cool-jconon by consiglionazionaledellericerche.
the class CallService method verifyPEC.
public void verifyPEC(VerificaPECTask verificaPECTask) {
Properties props = new Properties();
props.put("mail.imap.host", pecConfiguration.getHostImap());
props.put("mail.imap.auth", pecConfiguration.getAuth());
props.put("mail.imap.ssl.enable", pecConfiguration.getSslEnable());
props.put("mail.imap.port", pecConfiguration.getPort());
props.put("mail.imap.socketFactory.class", pecConfiguration.getSocketFactoryClass());
props.put("mail.imap.connectiontimeout", pecConfiguration.getConnectiontimeout());
props.put("mail.imap.timeout", pecConfiguration.getTimeout());
final javax.mail.Session session = javax.mail.Session.getInstance(props);
URLName urlName = new URLName(pecConfiguration.getUrl());
Store store = null;
javax.mail.Folder folder = null;
try {
store = session.getStore(urlName);
store.connect(verificaPECTask.getUserName(), verificaPECTask.getPassword());
folder = store.getFolder("INBOX");
folder.open(javax.mail.Folder.READ_ONLY);
SearchTerm searchTerm = new SubjectTerm("CONSEGNA: " + verificaPECTask.getOggetto());
List<Message> messages = Arrays.asList(folder.search(searchTerm)).stream().sorted(Comparator.comparing(o -> {
try {
return o.getReceivedDate();
} catch (MessagingException e) {
return null;
}
})).collect(Collectors.toList());
for (Message message : messages) {
final String subjectMessage = message.getSubject();
try {
Optional<String> cmisObjectId = Optional.of(message.getSubject()).map(subject -> subject.indexOf("$$")).filter(index -> index > -1).map(index -> subjectMessage.substring(index + 3));
if (cmisObjectId.isPresent()) {
final Date receivedDate = message.getReceivedDate();
LOGGER.info("Trovato messaggio sulla PEC con id: {} data spedizione: {} data ricezione: {}", cmisObjectId.get(), verificaPECTask.getSendDate(), receivedDate);
if (Optional.ofNullable(verificaPECTask.getSendDate()).map(date -> date.before(receivedDate)).orElse(Boolean.TRUE)) {
CmisObject cmisObject = cmisService.createAdminSession().getObject(cmisObjectId.get());
if (Optional.ofNullable(cmisObject.getPropertyValue(verificaPECTask.getPropertyName())).filter(x -> x.equals(StatoComunicazione.SPEDITO.name())).isPresent()) {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(verificaPECTask.getPropertyName(), subjectMessage.startsWith("AVVISO DI MANCATA CONSEGNA") ? StatoComunicazione.NON_CONSEGNATO.name() : StatoComunicazione.CONSEGNATO.name());
cmisObject.updateProperties(properties);
}
}
}
} catch (CmisObjectNotFoundException _ex) {
LOGGER.error("ERROR while SCAN PEC MAIL cannot find object with subject {}", subjectMessage, _ex);
}
}
} catch (MessagingException e) {
LOGGER.error("ERROR while SCAN PEC MAIL", e);
} finally {
Optional.ofNullable(folder).ifPresent(x -> {
try {
x.close(true);
} catch (Exception e) {
LOGGER.error("CANNOT CLOSE FOLDER", e);
}
});
Optional.ofNullable(store).ifPresent(x -> {
try {
x.close();
} catch (Exception e) {
LOGGER.error("CANNOT CLOSE FOLDER", e);
}
});
}
}
Aggregations