Search in sources :

Example 1 with VerificaPECTask

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);
            }
        });
    }
}
Also used : Order(it.cnr.si.opencmis.criteria.Order) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) InlineResponse201(it.cnr.si.cool.jconon.io.model.InlineResponse201) ApplicationService(it.cnr.si.cool.jconon.service.application.ApplicationService) Autowired(org.springframework.beans.factory.annotation.Autowired) CallRepository(it.cnr.si.cool.jconon.repository.CallRepository) StringUtils(org.apache.commons.lang3.StringUtils) BigDecimal(java.math.BigDecimal) CriteriaFactory(it.cnr.si.opencmis.criteria.CriteriaFactory) OperationContextUtils(org.apache.chemistry.opencmis.client.util.OperationContextUtils) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) CoolPropertyIds(it.cnr.cool.cmis.model.CoolPropertyIds) ObjectIdImpl(org.apache.chemistry.opencmis.client.runtime.ObjectIdImpl) BigInteger(java.math.BigInteger) HttpStatus(org.apache.commons.httpclient.HttpStatus) PrintParameterModel(it.cnr.si.cool.jconon.model.PrintParameterModel) PropertyDefinition(org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition) CompetitionFolderService(it.cnr.si.cool.jconon.service.cache.CompetitionFolderService) JCONONPolicyType(it.cnr.si.cool.jconon.cmis.model.JCONONPolicyType) org.apache.chemistry.opencmis.commons.enums(org.apache.chemistry.opencmis.commons.enums) ZoneId(java.time.ZoneId) CMISUser(it.cnr.cool.security.service.impl.alfresco.CMISUser) MailService(it.cnr.cool.mail.MailService) Stream(java.util.stream.Stream) MessageContent2(it.cnr.si.cool.jconon.io.model.MessageContent2) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) VerificaPECTask(it.cnr.si.cool.jconon.dto.VerificaPECTask) it.cnr.si.cool.jconon.util(it.cnr.si.cool.jconon.util) java.util(java.util) MimeTypes(it.cnr.cool.util.MimeTypes) CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) LocalDateTime(java.time.LocalDateTime) SimpleDateFormat(java.text.SimpleDateFormat) JsonParser(com.google.gson.JsonParser) NumberFormat(java.text.NumberFormat) InternetAddress(javax.mail.internet.InternetAddress) HttpServletRequest(javax.servlet.http.HttpServletRequest) GroupsEnum(it.cnr.cool.security.GroupsEnum) Service(org.springframework.stereotype.Service) StreamSupport(java.util.stream.StreamSupport) Restrictions(it.cnr.si.opencmis.criteria.restrictions.Restrictions) UrlBuilder(org.apache.chemistry.opencmis.commons.impl.UrlBuilder) org.apache.chemistry.opencmis.client.api(org.apache.chemistry.opencmis.client.api) JSONTokener(org.json.JSONTokener) PropertyData(org.apache.chemistry.opencmis.commons.data.PropertyData) JCONONFolderType(it.cnr.si.cool.jconon.cmis.model.JCONONFolderType) StrServ(it.cnr.cool.util.StrServ) HelpdeskService(it.cnr.si.cool.jconon.service.helpdesk.HelpdeskService) ProtocolRepository(it.cnr.si.cool.jconon.repository.ProtocolRepository) UserService(it.cnr.cool.security.service.UserService) Message(javax.mail.Message) JsonObject(com.google.gson.JsonObject) PermissionServiceImpl(it.cnr.cool.web.PermissionServiceImpl) URLName(javax.mail.URLName) LoggerFactory(org.slf4j.LoggerFactory) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) MessagingException(javax.mail.MessagingException) JSONObject(org.json.JSONObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cell(org.apache.poi.ss.usermodel.Cell) ParseException(java.text.ParseException) Store(javax.mail.Store) CmisBindingsHelper(org.apache.chemistry.opencmis.client.bindings.impl.CmisBindingsHelper) Response(org.apache.chemistry.opencmis.client.bindings.spi.http.Response) AddressException(javax.mail.internet.AddressException) NewMessage(it.cnr.si.cool.jconon.io.model.NewMessage) QueueService(it.cnr.si.cool.jconon.service.QueueService) StringUtil(it.cnr.cool.util.StringUtil) SubjectTerm(javax.mail.search.SubjectTerm) ACLType(it.cnr.cool.cmis.model.ACLType) Collectors(java.util.stream.Collectors) Environment(org.springframework.core.env.Environment) PropertyIds(org.apache.chemistry.opencmis.commons.PropertyIds) LocalDate(java.time.LocalDate) EmailException(org.apache.commons.mail.EmailException) CoolUserFactoryException(it.cnr.cool.exception.CoolUserFactoryException) JCONONPropertyIds(it.cnr.si.cool.jconon.cmis.model.JCONONPropertyIds) Async(org.springframework.scheduling.annotation.Async) PECConfiguration(it.cnr.si.cool.jconon.configuration.PECConfiguration) ContentStreamImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl) CommonsMultipartResolver(org.springframework.web.multipart.commons.CommonsMultipartResolver) IO(it.cnr.si.cool.jconon.io.repository.IO) PrintService(it.cnr.si.cool.jconon.service.PrintService) Inject(javax.inject.Inject) SearchTerm(javax.mail.search.SearchTerm) it.cnr.cool.cmis.service(it.cnr.cool.cmis.service) EmailAttachment(org.apache.commons.mail.EmailAttachment) Criteria(it.cnr.si.opencmis.criteria.Criteria) Util(it.cnr.cool.rest.util.Util) JCONONDocumentType(it.cnr.si.cool.jconon.cmis.model.JCONONDocumentType) TypeService(it.cnr.si.cool.jconon.service.TypeService) CacheRepository(it.cnr.si.cool.jconon.repository.CacheRepository) Logger(org.slf4j.Logger) BindingSession(org.apache.chemistry.opencmis.client.bindings.spi.BindingSession) ClientMessageException(it.cnr.cool.web.scripts.exception.ClientMessageException) Output(org.apache.chemistry.opencmis.client.bindings.spi.http.Output) StrSubstitutor(org.apache.commons.text.StrSubstitutor) ApplicationContext(org.springframework.context.ApplicationContext) EmailMessage(it.cnr.cool.mail.model.EmailMessage) java.io(java.io) DateTimeFormatter(java.time.format.DateTimeFormatter) Row(org.apache.poi.ss.usermodel.Row) MultipartFile(org.springframework.web.multipart.MultipartFile) CMISGroup(it.cnr.cool.security.service.impl.alfresco.CMISGroup) CMISUtil(it.cnr.cool.util.CMISUtil) I18nService(it.cnr.cool.service.I18nService) Message(javax.mail.Message) NewMessage(it.cnr.si.cool.jconon.io.model.NewMessage) EmailMessage(it.cnr.cool.mail.model.EmailMessage) MessagingException(javax.mail.MessagingException) URLName(javax.mail.URLName) Store(javax.mail.Store) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) LocalDate(java.time.LocalDate) CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) MessagingException(javax.mail.MessagingException) ParseException(java.text.ParseException) AddressException(javax.mail.internet.AddressException) EmailException(org.apache.commons.mail.EmailException) CoolUserFactoryException(it.cnr.cool.exception.CoolUserFactoryException) ClientMessageException(it.cnr.cool.web.scripts.exception.ClientMessageException) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.JSONObject)

Aggregations

JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 ACLType (it.cnr.cool.cmis.model.ACLType)1 CoolPropertyIds (it.cnr.cool.cmis.model.CoolPropertyIds)1 it.cnr.cool.cmis.service (it.cnr.cool.cmis.service)1 CoolUserFactoryException (it.cnr.cool.exception.CoolUserFactoryException)1 MailService (it.cnr.cool.mail.MailService)1 EmailMessage (it.cnr.cool.mail.model.EmailMessage)1 Util (it.cnr.cool.rest.util.Util)1 GroupsEnum (it.cnr.cool.security.GroupsEnum)1 UserService (it.cnr.cool.security.service.UserService)1 CMISGroup (it.cnr.cool.security.service.impl.alfresco.CMISGroup)1 CMISUser (it.cnr.cool.security.service.impl.alfresco.CMISUser)1 I18nService (it.cnr.cool.service.I18nService)1 CMISUtil (it.cnr.cool.util.CMISUtil)1 MimeTypes (it.cnr.cool.util.MimeTypes)1 StrServ (it.cnr.cool.util.StrServ)1 StringUtil (it.cnr.cool.util.StringUtil)1 PermissionServiceImpl (it.cnr.cool.web.PermissionServiceImpl)1 ClientMessageException (it.cnr.cool.web.scripts.exception.ClientMessageException)1