/
FatturaPassivaElettronicaService.java
1048 lines (975 loc) · 42.9 KB
/
FatturaPassivaElettronicaService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package it.cnr.contab.docamm00.service;
import it.cnr.contab.chiusura00.ejb.RicercaDocContComponentSession;
import it.cnr.contab.config00.bulk.Configurazione_cnrBulk;
import it.cnr.contab.docamm00.ejb.DocAmmFatturazioneElettronicaComponentSession;
import it.cnr.contab.docamm00.ejb.FatturaElettronicaPassivaComponentSession;
import it.cnr.contab.docamm00.ejb.RicezioneFatturePA;
import it.cnr.contab.docamm00.ejb.TrasmissioneFatturePA;
import it.cnr.contab.docamm00.fatturapa.bulk.DocumentoEleTestataBulk;
import it.cnr.contab.docamm00.fatturapa.bulk.TipoIntegrazioneSDI;
import it.cnr.contab.pdd.ws.client.FatturazioneElettronicaClient;
import it.cnr.contab.utenze00.bp.WSUserContext;
import it.cnr.contab.util.StringEncrypter;
import it.cnr.contab.util.StringEncrypter.EncryptionException;
import it.cnr.jada.UserContext;
import it.cnr.jada.comp.ComponentException;
import it.cnr.jada.util.DateUtils;
import it.cnr.jada.util.SendMail;
import it.cnr.jada.util.mail.SimplePECMail;
import it.gov.fatturapa.sdi.messaggi.v1.MetadatiInvioFileType;
import it.gov.fatturapa.sdi.messaggi.v1.NotificaEsitoCommittenteType;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.MimetypesFileTypeMap;
import javax.mail.Address;
import javax.mail.AuthenticationFailedException;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.mail.search.AndTerm;
import javax.mail.search.ComparisonTerm;
import javax.mail.search.FromStringTerm;
import javax.mail.search.OrTerm;
import javax.mail.search.ReceivedDateTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;
import javax.xml.bind.JAXBElement;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.oxm.XmlMappingException;
public class FatturaPassivaElettronicaService implements InitializingBean{
private transient final static Logger logger = LoggerFactory.getLogger(FatturaPassivaElettronicaService.class);
private FatturaElettronicaPassivaComponentSession fatturaElettronicaPassivaComponentSession;
private RicercaDocContComponentSession ricercaDocContComponentSession;
private DocAmmFatturazioneElettronicaComponentSession docAmmFatturazioneElettronicaComponentSession;
private RicezioneFatturePA ricezioneFattureService;
private TrasmissioneFatturePA trasmissioneFattureService;
private FatturazioneElettronicaClient fatturazioneElettronicaClient;
private UserContext userContext;
private Boolean pecScanDisable;
private Properties pecMailConf;
private List<String> listaMessageIdAlreadyScanned = new ArrayList<String>();
private String pecHostName, pecURLName, pecSDIAddress, pecSDISubjectFatturaAttivaInvioTerm, pecSDISubjectNotificaPecTerm, pecSDISubjectFatturaPassivaNotificaScartoEsitoTerm,
pecSDIFromStringTerm, pecSDISubjectRiceviFattureTerm, pecSDISubjectFatturaAttivaRicevutaConsegnaTerm, pecSDISubjectFatturaAttivaNotificaScartoTerm, pecSDISubjectFatturaAttivaMancataConsegnaTerm,
pecSDISubjectNotificaEsitoTerm, pecSDISubjectFatturaAttivaDecorrenzaTerminiTerm, pecSDISubjectFatturaAttivaAttestazioneTrasmissioneFatturaTerm, pecHostAddressReturn, pecSDISubjectMancataConsegnaPecTerm;
private List<String> pecScanFolderName, pecScanReceiptFolderName, pecHostAddress;
public RicercaDocContComponentSession getRicercaDocContComponentSession() {
return ricercaDocContComponentSession;
}
public void setRicercaDocContComponentSession(
RicercaDocContComponentSession ricercaDocContComponentSession) {
this.ricercaDocContComponentSession = ricercaDocContComponentSession;
}
public void setPecSDISubjectFatturaAttivaRicevutaConsegnaTerm(
String pecSDISubjectFatturaAttivaRicevutaConsegnaTerm) {
this.pecSDISubjectFatturaAttivaRicevutaConsegnaTerm = pecSDISubjectFatturaAttivaRicevutaConsegnaTerm;
}
public void setPecSDISubjectFatturaAttivaNotificaScartoTerm(
String pecSDISubjectFatturaAttivaNotificaScartoTerm) {
this.pecSDISubjectFatturaAttivaNotificaScartoTerm = pecSDISubjectFatturaAttivaNotificaScartoTerm;
}
public void setPecSDISubjectFatturaAttivaMancataConsegnaTerm(
String pecSDISubjectFatturaAttivaMancataConsegnaTerm) {
this.pecSDISubjectFatturaAttivaMancataConsegnaTerm = pecSDISubjectFatturaAttivaMancataConsegnaTerm;
}
public void setPecSDISubjectNotificaEsitoTerm(
String pecSDISubjectNotificaEsitoTerm) {
this.pecSDISubjectNotificaEsitoTerm = pecSDISubjectNotificaEsitoTerm;
}
public void setPecSDISubjectFatturaAttivaDecorrenzaTerminiTerm(
String pecSDISubjectFatturaAttivaDecorrenzaTerminiTerm) {
this.pecSDISubjectFatturaAttivaDecorrenzaTerminiTerm = pecSDISubjectFatturaAttivaDecorrenzaTerminiTerm;
}
public void setPecSDISubjectFatturaAttivaAttestazioneTrasmissioneFatturaTerm(
String pecSDISubjectFatturaAttivaAttestazioneTrasmissioneFatturaTerm) {
this.pecSDISubjectFatturaAttivaAttestazioneTrasmissioneFatturaTerm = pecSDISubjectFatturaAttivaAttestazioneTrasmissioneFatturaTerm;
}
public String getPecSDISubjectMancataConsegnaPecTerm() {
return pecSDISubjectMancataConsegnaPecTerm;
}
public void setPecSDISubjectMancataConsegnaPecTerm(
String pecSDISubjectMancataConsegnaPecTerm) {
this.pecSDISubjectMancataConsegnaPecTerm = pecSDISubjectMancataConsegnaPecTerm;
}
public void setFatturaElettronicaPassivaComponentSession(
FatturaElettronicaPassivaComponentSession fatturaElettronicaPassivaComponentSession) {
this.fatturaElettronicaPassivaComponentSession = fatturaElettronicaPassivaComponentSession;
}
public void setDocAmmFatturazioneElettronicaComponentSession(
DocAmmFatturazioneElettronicaComponentSession docAmmFatturazioneElettronicaComponentSession) {
this.docAmmFatturazioneElettronicaComponentSession = docAmmFatturazioneElettronicaComponentSession;
}
public void setRicezioneFattureService(
RicezioneFatturePA ricezioneFattureService) {
this.ricezioneFattureService = ricezioneFattureService;
}
public void setTrasmissioneFattureService(
TrasmissioneFatturePA trasmissioneFattureService) {
this.trasmissioneFattureService = trasmissioneFattureService;
}
public void setFatturazioneElettronicaClient(
FatturazioneElettronicaClient fatturazioneElettronicaClient) {
this.fatturazioneElettronicaClient = fatturazioneElettronicaClient;
}
public void setPecScanDisable(Boolean pecScanDisable) {
this.pecScanDisable = pecScanDisable;
}
public void setPecMailConf(Properties pecMailConf) {
this.pecMailConf = pecMailConf;
}
public void setPecHostName(String pecHostName) {
this.pecHostName = pecHostName;
}
public void setPecURLName(String pecURLName) {
this.pecURLName = pecURLName;
}
public void setPecSDIAddress(String pecSDIAddress) {
this.pecSDIAddress = pecSDIAddress;
}
public void setPecScanReceiptFolderName(String pecScanReceiptFolderName) {
this.pecScanReceiptFolderName = Arrays.asList(StringUtils.split(pecScanReceiptFolderName, ","));;
}
public void setPecSDISubjectFatturaAttivaInvioTerm(
String pecSDISubjectFatturaAttivaInvioTerm) {
this.pecSDISubjectFatturaAttivaInvioTerm = pecSDISubjectFatturaAttivaInvioTerm;
}
public void setPecSDISubjectFatturaPassivaNotificaScartoEsitoTerm(
String pecSDISubjectFatturaPassivaNotificaScartoEsitoTerm) {
this.pecSDISubjectFatturaPassivaNotificaScartoEsitoTerm = pecSDISubjectFatturaPassivaNotificaScartoEsitoTerm;
}
public void setPecSDISubjectNotificaPecTerm(
String pecSDISubjectNotificaPecTerm) {
this.pecSDISubjectNotificaPecTerm = pecSDISubjectNotificaPecTerm;
}
public void setPecSDIFromStringTerm(String pecSDIFromStringTerm) {
this.pecSDIFromStringTerm = pecSDIFromStringTerm;
}
public void setPecSDISubjectRiceviFattureTerm(
String pecSDISubjectRiceviFattureTerm) {
this.pecSDISubjectRiceviFattureTerm = pecSDISubjectRiceviFattureTerm;
}
public void setPecScanFolderName(String pecScanFolderName) {
this.pecScanFolderName = Arrays.asList(StringUtils.split(pecScanFolderName, ","));
}
public void setPecHostAddress(String pecHostAddress) {
this.pecHostAddress = Arrays.asList(StringUtils.split(pecHostAddress, ","));
}
public void setPecHostAddressReturn(
String pecHostAddressReturn) {
this.pecHostAddressReturn = pecHostAddressReturn;
}
public void execute() {
caricaFatture(-1);
}
public void caricaFatture(Integer daysBefore) {
try{
if (pecScanDisable){
logger.info("PEC scan is disabled");
} else {
logger.info("PEC SCAN for ricevi Fatture started at: "+new Date());
Configurazione_cnrBulk email = fatturaElettronicaPassivaComponentSession.getEmailPecSdi(userContext);
if (email == null) {
logger.info("PEC SCAN for ricevi Fatture alredy started in another server.");
} else {
try {
pecScan(email.getVal01(), email.getVal02(), daysBefore);
} finally {
fatturaElettronicaPassivaComponentSession.unlockEmailPEC(userContext);
}
}
logger.info("PEC SCAN for ricevi Fatture finished at: "+new Date());
}
} catch(Throwable _ex){
logger.error("ScheduleExecutor error", _ex);
}
}
public void allineaNotificheExecute() {
try{
if (pecScanDisable){
logger.info("PEC scan is disabled");
} else {
logger.info("Allinea notifiche started at: "+new Date());
fatturaElettronicaPassivaComponentSession.
allineaEsitoCommitente(userContext, TipoIntegrazioneSDI.PEC);
logger.info("Allinea notifiche finished at: "+new Date());
}
}catch(Throwable _ex){
logger.error("ScheduleExecutor error", _ex);
}
}
private void riceviFattura(Message message, String userName) throws ComponentException {
try {
List<BodyPart> bodyParts = estraiBodyPart(message.getContent());
if (bodyParts.isEmpty()) {
logger.warn("Il messaggio con id:"+message.getMessageNumber()+" recuperato dalla casella PEC:"+userName +
" non ha file allegati.");
return;
}
if (bodyParts.size() > 2) {
logger.warn("Il messaggio con id:"+message.getMessageNumber()+" recuperato dalla casella PEC:"+userName +
" ha più di due file allegati.");
return;
}
BodyPart bodyPartFattura = null, bodyPartMetadati = null;
for (BodyPart bodyPart : bodyParts) {
if (isBodyPartMetadati(bodyPart)){
bodyPartMetadati = bodyPart;
} else {
bodyPartFattura = bodyPart;
}
}
if (bodyPartFattura != null && bodyPartMetadati != null) {
@SuppressWarnings("unchecked")
JAXBElement<MetadatiInvioFileType> metadatiInvioFileType = ((JAXBElement<MetadatiInvioFileType>)
fatturazioneElettronicaClient.getUnmarshaller().
unmarshal(new StreamSource(bodyPartMetadati.getInputStream())));
String replyTo = getReplyTo(message);
if (!fatturaElettronicaPassivaComponentSession.existsIdentificativo(userContext, metadatiInvioFileType.getValue().getIdentificativoSdI().longValue())) {
DataSource byteArrayDataSourceFattura = new UploadedFileDataSource(bodyPartFattura);
DataSource byteArrayDataSourceMetadati = new UploadedFileDataSource(bodyPartMetadati);
ricezioneFattureService.riceviFatturaSIGLA(
metadatiInvioFileType.getValue().getIdentificativoSdI(),
bodyPartFattura.getFileName(),
replyTo,
new DataHandler(byteArrayDataSourceFattura),
bodyPartMetadati.getFileName(),
new DataHandler(byteArrayDataSourceMetadati));
}
} else {
logger.warn("Il messaggio con id:"+message.getMessageNumber()+" recuperato dalla casella PEC:"+userName + " è stato precessato ma gli allegati presenti non sono conformi.");
}
} catch (Throwable _ex) {
logger.error("PEC scan error while importing file.", _ex);
}
}
private String getReplyTo(Message message) throws MessagingException {
Address[] repliesAddress = message.getReplyTo();
String replyTo = null;
if (repliesAddress != null){
if (repliesAddress.length > 0){
replyTo = repliesAddress[0].toString();
}
}
logger.info("ReplyTo: "+replyTo);
return replyTo;
}
private void notificaFatturaPassivaScartoEsito(Message message, String userName) throws ComponentException {
try{
String[] headerMessage = getMessageID(message);
if (headerMessage != null){
String messageID = Arrays.asList(headerMessage).toString();
String messageIDWithUser = userName + " "+messageID;
logger.info("MessageIDWithUser " + messageIDWithUser);
if (!listaMessageIdAlreadyScanned.contains(messageIDWithUser)){
logger.info(messageIDWithUser);
try {
List<BodyPart> bodyParts = estraiBodyPart(message.getContent());
if (bodyParts.isEmpty()) {
logger.warn("Il messaggio con id:"+message.getMessageNumber()+" recuperato dalla casella PEC:"+userName +
" non ha file allegati.");
return;
}
if (bodyParts.size() > 2) {
logger.warn("Il messaggio con id:"+message.getMessageNumber()+" recuperato dalla casella PEC:"+userName +
" ha pi� di due file allegati.");
return;
}
boolean trovatoFile = false;
for (BodyPart bodyPart : bodyParts) {
String fileName = extractFileName(bodyPart);
if (fileName.toLowerCase().endsWith("xml")){
ricezioneFattureService.notificaScartoEsito(fileName, createDataHandler(bodyPart), message.getReceivedDate());
trovatoFile = true;
break;
}
}
if (!trovatoFile){
logger.warn("Il messaggio con id:"+message.getMessageNumber()+" recuperato dalla casella PEC:"+userName + " è stato precessato ma gli allegati presenti non sono conformi.");
SendMail.sendErrorMail("Fatture Elettroniche: Passive: Scarto Esito. Allegati non conformi. Mail:"+userName, message.getDescription());
}
listaMessageIdAlreadyScanned.add(messageIDWithUser);
logger.info("Message Added");
} catch (Exception e) {
logger.error("PEC scan error while importing file.", e);
}
}
}
} catch (MessagingException e) {
logger.error("Errore durante il recupero dell'ID del messaggio.", e);
}
}
public void pecScan(String userName, String password, Integer daysBefore) throws ComponentException {
logger.info("PEC SCAN for ricevi Fatture email: "+userName + "pwd :" +password);
Properties props = System.getProperties();
props.putAll(pecMailConf);
try {
try {
password = StringEncrypter.decrypt(userName, password);
} catch (EncryptionException e1) {
new AuthenticationFailedException("Cannot decrypt password");
}
final Session session = Session.getInstance(props);
URLName urlName = new URLName(pecURLName);
Store store = session.getStore(urlName);
store.connect(userName, password);
searchMailFromPec(userName, password, store, daysBefore);
searchMailFromSdi(userName, store, daysBefore);
searchMailFromReturn(userName, store, daysBefore);
store.close();
} catch (AuthenticationFailedException e) {
logger.error("Error while scan PEC email:" +userName + " - password:"+password);
} catch (NoSuchProviderException e) {
logger.error("Error while scan PEC email:" +userName, e);
} catch (MessagingException e) {
logger.error("Error while scan PEC email:" +userName, e);
}
}
private void searchMailFromPec(String userName, String password, final Store store, Integer daysBefore)
throws MessagingException {
List<Folder> folders = new ArrayList<Folder>();
for (String folderName : pecScanReceiptFolderName) {
folders.add(store.getFolder(folderName));
}
for (Folder folder : folders) {
if (folder.exists()) {
folder.open(Folder.READ_ONLY);
processingMailFromHostPec(folder, userName, password, daysBefore);
folder.close(true);
}
}
}
private void searchMailFromSdi(String userName, final Store store, Integer daysBefore)
throws MessagingException {
List<Folder> folders = new ArrayList<Folder>();
for (String folderName : pecScanFolderName) {
folders.add(store.getFolder(folderName));
}
for (Folder folder : folders) {
if (folder.exists()) {
folder.open(Folder.READ_ONLY);
processingMailFromSdi(folder, userName, daysBefore);
folder.close(true);
}
}
}
private void searchMailFromReturn(String userName, final Store store, Integer daysBefore)
throws MessagingException {
List<Folder> folders = new ArrayList<Folder>();
for (String folderName : pecScanFolderName) {
folders.add(store.getFolder(folderName));
}
for (Folder folder : folders) {
if (folder.exists()) {
folder.open(Folder.READ_ONLY);
processingMailFromReturn(folder, userName, daysBefore);
folder.close(true);
}
}
}
private void processingMailFromSdi(Folder folder, String userName, Integer daysBefore) throws MessagingException{
if (isDateForRecoveryNotifier()){
List<SearchTerm> newTerms = createTermsForSearchSdi(true, daysBefore);
Message newMessages[] = folder.search(new AndTerm(newTerms.toArray(new SearchTerm[newTerms.size()])));
logger.info("Recuperati " + newMessages.length +" messaggi SDI dalla casella PEC:" + userName + " nella folder:" + folder.getFullName());
for (int i = 0; i < newMessages.length; i++) {
try {
Message message = newMessages[i];
if (message.getSubject() != null){
if (message.getSubject().contains(pecSDISubjectNotificaEsitoTerm)){
notificaFatturaAttivaEsito(message);
} else if (message.getSubject().contains(pecSDISubjectFatturaAttivaNotificaScartoTerm)){
notificaFatturaAttivaScarto(message);
} else if (message.getSubject().contains(pecSDISubjectFatturaAttivaAttestazioneTrasmissioneFatturaTerm)){
notificaFatturaAttivaAvvenutaTrasmissioneNonRecapitata(message);
}
}
} catch (Exception e) {
logger.error("PEC scan error while importing file.", e);
java.io.StringWriter sw = new java.io.StringWriter();
e.printStackTrace(new java.io.PrintWriter(sw));
SendMail.sendErrorMail("Errore. Fatture Elettroniche: Processing mail SDI ", sw.toString());
}
}
}
List<SearchTerm> terms = createTermsForSearchSdi(false, daysBefore);
Message messages[] = folder.search(new AndTerm(terms.toArray(new SearchTerm[terms.size()])));
logger.info("Recuperati " + messages.length +" messaggi SDI dalla casella PEC:" + userName + " nella folder:" + folder.getFullName());
for (int i = 0; i < messages.length; i++) {
try {
Message message = messages[i];
if (message.getSubject() != null){
if (message.getSubject().contains(pecSDISubjectRiceviFattureTerm)){
riceviFattura(message, userName);
} else if (message.getSubject().contains(pecSDISubjectFatturaPassivaNotificaScartoEsitoTerm)){
notificaFatturaPassivaScartoEsito(message, userName);
} else if (message.getSubject().contains(pecSDISubjectFatturaAttivaRicevutaConsegnaTerm)){
notificaFatturaAttivaRicevutaConsegna(message, userName);
} else if (message.getSubject().contains(pecSDISubjectNotificaEsitoTerm) && !isDateForRecoveryNotifier()){
notificaFatturaAttivaEsito(message);
} else if (message.getSubject().contains(pecSDISubjectFatturaAttivaNotificaScartoTerm) && !isDateForRecoveryNotifier()){
notificaFatturaAttivaScarto(message);
} else if (message.getSubject().contains(pecSDISubjectFatturaAttivaMancataConsegnaTerm)){
notificaFatturaAttivaMancataConsegna(message);
} else if (message.getSubject().contains(pecSDISubjectFatturaAttivaDecorrenzaTerminiTerm)){
notificaFatturaAttivaDecorrenzaTermini(message);
} else if (message.getSubject().contains(pecSDISubjectFatturaAttivaAttestazioneTrasmissioneFatturaTerm) && !isDateForRecoveryNotifier()){
notificaFatturaAttivaAvvenutaTrasmissioneNonRecapitata(message);
} else {
logger.warn("Fatture Elettroniche: Attive: Oggetto dell'e-mail non gestito." + message.getSubject());
SendMail.sendErrorMail("Fatture Elettroniche: Oggetto dell'e-mail non gestito. Mail:"+userName, message.getDescription());
}
}
} catch (Exception e) {
logger.error("PEC scan error while importing file.", e);
java.io.StringWriter sw = new java.io.StringWriter();
e.printStackTrace(new java.io.PrintWriter(sw));
SendMail.sendErrorMail("Errore. Fatture Elettroniche: Processing mail SDI ", sw.toString());
}
}
}
private Boolean isDateForRecoveryNotifier(){
Calendar today = Calendar.getInstance();
today.setTime(new Date());
if (today.get(Calendar.MONTH) == Calendar.JANUARY){
return true;
}
return false;
}
private void processingMailFromReturn(Folder folder, String userName, Integer daysBefore) throws MessagingException{
List<SearchTerm> terms = createTermsForSearchReturn(daysBefore);
Message messages[] = folder.search(new AndTerm(terms.toArray(new SearchTerm[terms.size()])));
logger.info("Recuperati " + messages.length +" messaggi di mail non recapitate dalla casella PEC:" + userName + " nella folder:" + folder.getFullName());
for (int i = 0; i < messages.length; i++) {
try {
Message message = messages[i];
if (message.getSubject() != null){
try{
String[] headerMessage = getMessageID(message);
if (headerMessage != null){
String messageID = Arrays.asList(headerMessage).toString();
String messageIDWithUser = userName + " "+messageID;
logger.info("MessageIDWithUser " + messageIDWithUser);
if (!listaMessageIdAlreadyScanned.contains(messageIDWithUser)){
logger.info(messageIDWithUser);
try {
DataHandler data = message.getDataHandler();
estraiBodyPart(data.getContent(), false);
listaMessageIdAlreadyScanned.add(messageIDWithUser);
logger.info("Message Added");
} catch (Exception e) {
logger.error("PEC scan error while importing file.", e);
}
}
}
} catch (MessagingException e) {
logger.error("Errore durante il recupero dell'ID del messaggio.", e);
}
}
} catch (Exception e) {
logger.error("PEC scan error while importing file.", e);
java.io.StringWriter sw = new java.io.StringWriter();
e.printStackTrace(new java.io.PrintWriter(sw));
SendMail.sendErrorMail("Errore. Fatture Elettroniche: Processing mail SDI ", sw.toString());
}
}
}
public List<SearchTerm> createTermsForSearchSdi(Boolean dateForRecoveryNotifier, Integer daysBefore) {
List<SearchTerm> terms = new ArrayList<SearchTerm>();
addConditionDate(terms,dateForRecoveryNotifier, daysBefore);
terms.add(new FromStringTerm(pecSDIFromStringTerm));
return terms;
}
public List<SearchTerm> createTermsForSearchReturn(Integer daysBefore) {
List<SearchTerm> terms = new ArrayList<SearchTerm>();
addConditionDate(terms, daysBefore);
terms.add(new FromStringTerm(pecHostAddressReturn));
return terms;
}
private void processingMailFromHostPec(Folder folder, String userName, String password, Integer daysBefore) throws MessagingException{
List<SearchTerm> terms = createTermsForSearchPec(daysBefore);
Message messages[] = folder.search(new AndTerm(terms.toArray(new SearchTerm[terms.size()])));
logger.info("Recuperati " + messages.length +" messaggi PEC dalla casella PEC:" + userName + " nella folder:" + folder.getFullName());
for (int i = 0; i < messages.length; i++) {
try {
Message message = messages[i];
if (message.getSubject().contains(getSubjectTermForMancataConsegnaFatturaAttivaPec())){
String nomeFile = message.getSubject().substring(getSubjectTermForMancataConsegnaFatturaAttivaPec().length() + 1 );
if (nomeFile != null){
InputStream streamSigned = trasmissioneFattureService.mancataConsegnaPecInvioFatturaAttiva(userContext, nomeFile);
if (streamSigned != null){
File fileSigned = new File(System.getProperty("tmp.dir.SIGLAWeb")+"/tmp/", nomeFile);
OutputStream outputStream = new FileOutputStream(fileSigned);
IOUtils.copy(streamSigned, outputStream);
outputStream.close();
inviaFatturaElettronica(userName, password, fileSigned, nomeFile);
}
}
} else if (message.getSubject().contains(getSubjectTermForFatturaAttivaPec())){
String nomeFile = message.getSubject().substring(getSubjectTermForFatturaAttivaPec().length() + 1 );
if (nomeFile != null){
trasmissioneFattureService.notificaFatturaAttivaConsegnaPec(userContext, nomeFile, message.getSentDate());
}
} else if (message.getSubject().contains(getSubjectTermForFatturaPassivaPec())){
String[] headerMessage = getMessageID(message);
if (headerMessage != null){
String messageID = Arrays.asList(headerMessage).toString();
String messageIDWithUser = userName + " "+messageID;
logger.info("MessageIDWithUser " + messageIDWithUser);
if (!listaMessageIdAlreadyScanned.contains(messageIDWithUser)){
logger.info(messageIDWithUser);
String identificativoSdi = message.getSubject().substring(getSubjectTermForFatturaPassivaPec().length() + 1 );
if (identificativoSdi != null){
ricezioneFattureService.notificaFatturaPassivaConsegnaEsitoPec(identificativoSdi.trim(), message.getReceivedDate());
listaMessageIdAlreadyScanned.add(messageIDWithUser);
logger.info("Message Added");
}
}
}
}
} catch (Exception e) {
logger.error("PEC scan error while updating send PEC. ", e);
}
}
}
private String[] getMessageID(Message message) throws MessagingException {
String [] headerMessage = message.getHeader("Message-ID");
return headerMessage;
}
public List<SearchTerm> createTermsForSearchPec(Integer daysBefore) {
List<SearchTerm> terms = new ArrayList<SearchTerm>();
addConditionDate(terms, daysBefore);
addConditionFromAddress(terms);
addConditionSubject(terms);
return terms;
}
private void addConditionSubject(List<SearchTerm> terms) {
List<SearchTerm> listSubject = new ArrayList<SearchTerm>();
listSubject.add(new SubjectTerm(getSubjectTermForFatturaAttivaPec()));
listSubject.add(new SubjectTerm(getSubjectTermForFatturaPassivaPec()));
listSubject.add(new SubjectTerm(getSubjectTermForMancataConsegnaFatturaAttivaPec()));
SearchTerm[] termsSubjects = listSubject.toArray(new SearchTerm[listSubject.size()]);
terms.add(new OrTerm(termsSubjects));
}
private void addConditionFromAddress(List<SearchTerm> terms) {
List<SearchTerm> listAddress = new ArrayList<SearchTerm>();
for (String address : pecHostAddress) {
listAddress.add(new FromStringTerm(address));
}
SearchTerm[] termsAddress = listAddress.toArray(new SearchTerm[listAddress.size()]);
terms.add(new OrTerm(termsAddress));
}
public String getSubjectTermForFatturaAttivaPec() {
String subjectTerm = pecSDISubjectNotificaPecTerm+" "+pecSDISubjectFatturaAttivaInvioTerm;
return subjectTerm;
}
public String getSubjectTermForMancataConsegnaFatturaAttivaPec() {
String subjectTerm = pecSDISubjectMancataConsegnaPecTerm+" "+pecSDISubjectFatturaAttivaInvioTerm;
return subjectTerm;
}
public String getSubjectTermForFatturaPassivaPec() {
String subjectTerm = pecSDISubjectNotificaPecTerm+" "+pecSDISubjectNotificaEsitoTerm;
return subjectTerm;
}
public void addConditionDate(List<SearchTerm> terms, Integer daysBefore) {
addConditionDate(terms, false, daysBefore);
}
public void addConditionDate(List<SearchTerm> terms, Boolean dateForRecoveryNotifier, Integer daysBefore) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
Date dateFromSearch = null;
if (dateForRecoveryNotifier){
dateFromSearch = DateUtils.dataContabile(cal.getTime(), cal.get(Calendar.YEAR) - 1);
dateFromSearch = cal.getTime();
} else {
cal.add(Calendar.DATE, daysBefore);
dateFromSearch = cal.getTime();
}
terms.add(new ReceivedDateTerm(ComparisonTerm.GE, dateFromSearch));
}
public Date getSystemDate() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
Date data = cal.getTime();
return data;
}
@SuppressWarnings("unchecked")
private boolean isBodyPartMetadati(BodyPart bodyPart) {
try {
JAXBElement<MetadatiInvioFileType> metadatiInvioFileType = (JAXBElement<MetadatiInvioFileType>)
fatturazioneElettronicaClient.getUnmarshaller().
unmarshal(new StreamSource(bodyPart.getInputStream()));
if (logger.isDebugEnabled())
logger.debug("metadatiInvioFileType :" + metadatiInvioFileType.getValue().getIdentificativoSdI());
} catch (Exception e) {
return false;
}
return true;
}
private void notificaFatturaAttivaAvvenutaTrasmissioneNonRecapitata(Message message) throws ComponentException {
logger.info("Fatture Elettroniche: Attive: Inizio Avvenuta Trasmissione con impossibilit� di recapito.");
try {
BodyPart bodyPartZip = estraiBodyPartZipNotificaFatturaAttiva(message);
if (bodyPartZip != null){
logger.info("Fatture Elettroniche: Attive: Estratto Body Part.");
ZipInputStream zis = new ZipInputStream(bodyPartZip.getInputStream());
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null)
{
System.out.println("entry: " + entry.getName() + ", " + entry.getSize());
if (entry.getName().toLowerCase().endsWith("xml")){
String contentType = new MimetypesFileTypeMap().getContentType(entry.getName());
DataHandler dataHandler = createDataHandler(zis, contentType);
trasmissioneFattureService.notificaFatturaAttivaAvvenutaTrasmissioneNonRecapitata(userContext, entry.getName(), dataHandler);
break;
}
}
}
} catch (Exception e) {
throw new ComponentException(e);
}
}
private void notificaFatturaAttivaDecorrenzaTermini(Message message) throws ComponentException {
logger.info("Fatture Elettroniche: Inizio Notifica Decorrenza Termini.");
try {
BodyPart bodyPartXml = estraiBodyPartXmlNotificaFatturaAttiva(message);
if (bodyPartXml != null){
logger.info("Fatture Elettroniche: Decorrenza Termini. Estratto Body Part.");
String fileName = extractFileName(bodyPartXml);
if (fileName.startsWith(docAmmFatturazioneElettronicaComponentSession.recuperoInizioNomeFile(userContext))){
if (!trasmissioneFattureService.notificaFatturaAttivaDecorrenzaTermini(userContext, fileName, createDataHandler(bodyPartXml))){
logger.info("Fatture Elettroniche: Decorrenza Termini. Incongruenza nel nome del file. Verifica se esistono fatture passive con l'ID.");
ricezioneFattureService.notificaDecorrenzaTermini(fileName, createDataHandler(bodyPartXml));
}
} else {
ricezioneFattureService.notificaDecorrenzaTermini(fileName, createDataHandler(bodyPartXml));
}
}
} catch (Exception e) {
throw new ComponentException(e);
}
}
private DataHandler createDataHandler(BodyPart bodyPart)
throws IOException, MessagingException {
DataSource byteArrayDataSource = new UploadedFileDataSource(bodyPart);
return new DataHandler(byteArrayDataSource);
}
private DataHandler createDataHandler(InputStream stream, String contentType)
throws IOException, MessagingException {
DataSource byteArrayDataSource = new UploadedFileDataSourceStream(stream, contentType);
return new DataHandler(byteArrayDataSource);
}
private void notificaFatturaAttivaEsito(Message message) throws ComponentException {
logger.info("Fatture Elettroniche: Attive: Inizio Notifica Esito.");
try {
BodyPart bodyPartXml = estraiBodyPartXmlNotificaFatturaAttiva(message);
if (bodyPartXml != null){
trasmissioneFattureService.notificaFatturaAttivaEsito(userContext, bodyPartXml.getFileName(), createDataHandler(bodyPartXml));
}
} catch (Exception e) {
throw new ComponentException(e);
}
}
private void notificaFatturaAttivaScarto(Message message) throws ComponentException {
logger.info("Fatture Elettroniche: Attive: Inizio Notifica Scarto.");
try {
BodyPart bodyPartXml = estraiBodyPartXmlNotificaFatturaAttiva(message);
if (bodyPartXml != null){
logger.info("Fatture Elettroniche: Attive: Estratto Body Part.");
trasmissioneFattureService.notificaFatturaAttivaScarto(userContext, bodyPartXml.getFileName(), createDataHandler(bodyPartXml));
}
} catch (Exception e) {
throw new ComponentException(e);
}
}
private void notificaFatturaAttivaRicevutaConsegna(Message message, String username) throws ComponentException {
logger.info("Fatture Elettroniche: Attive: Inizio Ricevuta Consegna.");
try {
BodyPart bodyPartXml = estraiBodyPartXmlNotificaFatturaAttiva(message);
if (bodyPartXml != null){
logger.info("Fatture Elettroniche: Attive: Estratto Body Part.");
trasmissioneFattureService.notificaFatturaAttivaRicevutaConsegna(userContext, bodyPartXml.getFileName(), createDataHandler(bodyPartXml));
String[] replies = message.getHeader("Reply-To");
if (replies != null){
String replyTo = replies[0];
logger.info("Reply To: "+ replyTo + ". Username: "+username);
}
}
} catch (Exception e) {
throw new ComponentException(e);
}
}
private void notificaFatturaAttivaMancataConsegna(Message message) throws ComponentException {
logger.info("Fatture Elettroniche: Attive: Inizio Mancata Consegna.");
try {
BodyPart bodyPartXml = estraiBodyPartXmlNotificaFatturaAttiva(message);
if (bodyPartXml != null){
logger.info("Fatture Elettroniche: Attive: Estratto Body Part.");
trasmissioneFattureService.notificaFatturaAttivaMancataConsegna(userContext, bodyPartXml.getFileName(), createDataHandler(bodyPartXml));
}
} catch (Exception e) {
throw new ComponentException(e);
}
}
private BodyPart estraiBodyPartXmlNotificaFatturaAttiva(Message message) throws ComponentException{
try{
List<BodyPart> bodyParts = estraiBodyPart(message.getContent());
for (BodyPart bodyPart : bodyParts) {
String fileName = extractFileName(bodyPart);
if (fileName.toLowerCase().endsWith("xml")){
return bodyPart;
}
}
return null;
} catch (Exception e) {
throw new ComponentException(e);
}
}
private BodyPart estraiBodyPartZipNotificaFatturaAttiva(Message message) throws ComponentException{
try{
List<BodyPart> bodyParts = estraiBodyPart(message.getContent());
for (BodyPart bodyPart : bodyParts) {
String fileName = extractFileName(bodyPart);
if (fileName.toLowerCase().endsWith("zip")){
return bodyPart;
}
}
return null;
} catch (Exception e) {
throw new ComponentException(e);
}
}
private String extractFileName(BodyPart bodyPart) throws MessagingException,
UnsupportedEncodingException {
String fileName = bodyPart.getFileName();
if (fileName != null && fileName.startsWith("=?") && fileName.endsWith("?=")){
fileName = MimeUtility.decodeText(fileName);
}
return fileName;
}
public void notificaEsito(String userName, String password, DocumentoEleTestataBulk bulk, JAXBElement<NotificaEsitoCommittenteType> notificaEsitoCommittenteType) throws EmailException, XmlMappingException, IOException {
// Create the email message
SimplePECMail email = new SimplePECMail(userName, password);
email.setHostName(pecHostName);
String replyTo = null;
if (bulk.getDocumentoEleTrasmissione() != null){
replyTo = bulk.getDocumentoEleTrasmissione().getReplyTo();
}
email.addTo(replyTo == null ? pecSDIAddress : replyTo , "SdI - Sistema Di Interscambio");
email.setFrom(userName, userName);
email.setSubject(" Notifica di esito " + bulk.getIdentificativoSdi());
email.setMsg("Il file trasmesso con identificativo SdI:" + bulk.getIdentificativoSdi() +
(bulk.isRifiutata() ? " è stato Rifiutato (EC02) poiché " + bulk.getMotivoRifiuto() :" É stato Accettato (EC01)") +
", in allegato la notifica di esito.");
// add the attachment
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
fatturazioneElettronicaClient.getMarshaller().marshal(notificaEsitoCommittenteType, new StreamResult(outputStream));
DataSource byteArrayDataSource = new UploadedFileDataSourceOutputStream(outputStream);
email.attach(byteArrayDataSource, bulk.getNomeFile("EC"), "", EmailAttachment.ATTACHMENT);
// send the email
email.send();
logger.info("Inviata notifica di esito per IdentificativoSdi:"+bulk.getIdentificativoSdi());
}
public void inviaFatturaElettronica(String userName, String password, File fatturaAttivaSigned, String idFattura) throws EmailException, XmlMappingException, IOException {
// Create the email message
SimplePECMail email = new SimplePECMail(userName, password);
email.setHostName(pecHostName);
email.addTo(pecSDIAddress, "SdI - Sistema Di Interscambio");
email.setFrom(userName, userName);
email.setSubject(pecSDISubjectFatturaAttivaInvioTerm + " "+idFattura);
email.setMsg("Invio Fattura Elettronica. "+ idFattura);
email.attach(fatturaAttivaSigned);
// send the email
email.send();
}
private List<BodyPart> estraiBodyPart(Object content, Boolean perAllegati) throws MessagingException, IOException {
List<BodyPart> results = new ArrayList<BodyPart>();
estraiParte(content, results, perAllegati);
return results;
}
private List<BodyPart> estraiBodyPart(Object content) throws MessagingException, IOException {
return estraiBodyPart(content, true);
}
private void estraiParte(Object obj, List<BodyPart> bodyParts, Boolean perAllegati) throws MessagingException, IOException {
if (obj instanceof MimeMultipart) {
MimeMultipart multipart = (MimeMultipart) obj;
for (int j = 0; j < multipart.getCount(); j++) {
BodyPart bodyPart = multipart.getBodyPart(j);
estraiParte(bodyPart.getContent(), bodyParts, perAllegati);
}
} else if (obj instanceof MimeBodyPart) {
MimeBodyPart multipart = (MimeBodyPart) obj;
estraiParte(multipart.getContent(), bodyParts, perAllegati);
} else if (obj instanceof MimeMessage) {
MimeMessage mimemessage = (MimeMessage) obj;
if (!perAllegati && mimemessage.getSubject() != null && mimemessage.getSubject().contains(pecSDISubjectNotificaEsitoTerm)){
int partenza = mimemessage.getSubject().indexOf(pecSDISubjectNotificaEsitoTerm);
String identificativoSdi = mimemessage.getSubject().substring(partenza + pecSDISubjectNotificaEsitoTerm.length() + 1 );
try {
ricezioneFattureService.notificaScartoMailNotificaNonRicevibile(mimemessage, identificativoSdi, getSystemDate());
} catch (ComponentException e) {
logger.error("NotificaScartoMailNotificaNonRicevibile", e);
}
} else {
forwardedEmail(mimemessage.getContent(), bodyParts, perAllegati);
}
}
}
private void forwardedEmail(Object obj, List<BodyPart> bodyParts, Boolean perAllegati) throws MessagingException, IOException {
if (obj instanceof MimeMultipart) {
MimeMultipart multipart = (MimeMultipart) obj;
for (int j = 0; j < multipart.getCount(); j++) {
BodyPart bodyPart = multipart.getBodyPart(j);
if (perAllegati){
String disposition = bodyPart.getDisposition();
if (disposition != null && disposition.equals(Part.ATTACHMENT)) {
bodyParts.add(bodyPart);
if (logger.isDebugEnabled()) {
logger.debug("Content type:" + bodyPart.getContentType());
logger.debug("File name:" + bodyPart.getFileName());
}
}
} else {
if (bodyPart instanceof MimeBodyPart) {
MimeBodyPart body = (MimeBodyPart) bodyPart;
estraiParte(body.getContent(), bodyParts, perAllegati);
}
}
}
}
}
public void afterPropertiesSet() throws Exception {
userContext = new WSUserContext("SDI",null,
new Integer(java.util.Calendar.getInstance().get(java.util.Calendar.YEAR)),
null,null,null);
}
class UploadedFileDataSourceStream implements DataSource {
private InputStream inputStream;
private String contentType;
public UploadedFileDataSourceStream(InputStream inputStream, String contentType) {
this.inputStream = inputStream;
this.contentType = contentType;
}
@Override
public OutputStream getOutputStream() throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
IOUtils.copy(inputStream, output);
return output;
}
@Override
public String getName() {
return "DATASOURCE";
}
@Override
public InputStream getInputStream() throws IOException {
return inputStream;
}
@Override
public String getContentType() {
return contentType;
}
}
class UploadedFileDataSourceOutputStream implements DataSource {
private OutputStream outputStream;
public UploadedFileDataSourceOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
}
@Override
public OutputStream getOutputStream() throws IOException {
return outputStream;
}
@Override
public String getName() {
return "DATASOURCE";
}
@Override
public InputStream getInputStream() throws IOException {
if (outputStream != null){
ByteArrayOutputStream bos = (ByteArrayOutputStream)outputStream;
byte[] byteArray = bos.toByteArray();
ByteArrayInputStream inStream = new ByteArrayInputStream( byteArray );
return inStream;
}
return null;
}
@Override
public String getContentType() {
return "www/unknow";
}
}
class UploadedFileDataSource implements DataSource {