1
1
package es .keensoft .alfresco .behaviour ;
2
2
3
- import java .io .IOException ;
4
3
import java .io .InputStream ;
5
4
import java .io .Serializable ;
6
5
import java .security .KeyStore ;
7
- import java .security .KeyStoreException ;
8
- import java .security .cert .CertificateException ;
9
6
import java .security .cert .X509Certificate ;
10
7
import java .util .ArrayList ;
11
- import java .util .Calendar ;
12
- import java .util .Date ;
13
8
import java .util .HashMap ;
14
9
import java .util .Map ;
15
10
28
23
import org .alfresco .service .cmr .version .VersionService ;
29
24
import org .alfresco .service .namespace .QName ;
30
25
import org .apache .commons .io .FilenameUtils ;
31
- import org .apache .commons .logging .Log ;
32
- import org .apache .commons .logging .LogFactory ;
33
26
34
27
import com .itextpdf .text .pdf .AcroFields ;
35
28
import com .itextpdf .text .pdf .PdfPKCS7 ;
36
29
import com .itextpdf .text .pdf .PdfReader ;
37
30
38
31
import es .keensoft .alfresco .model .SignModel ;
39
32
40
- public class CustomBehaviour implements NodeServicePolicies .OnDeleteAssociationPolicy , NodeServicePolicies .OnCreateNodePolicy {
33
+ public class CustomBehaviour implements
34
+ NodeServicePolicies .OnDeleteAssociationPolicy ,
35
+ NodeServicePolicies .OnCreateNodePolicy {
41
36
42
37
private PolicyComponent policyComponent ;
43
38
private NodeService nodeService ;
44
39
private VersionService versionService ;
45
40
private ContentService contentService ;
46
41
47
42
private static final String PADES = "PAdES" ;
48
- private static Log log = LogFactory .getLog (CustomBehaviour .class );
49
43
50
44
public void init () {
51
45
policyComponent .bindAssociationBehaviour (NodeServicePolicies .OnDeleteAssociationPolicy .QNAME ,
52
46
SignModel .ASPECT_SIGNATURE , new JavaBehaviour (this , "onDeleteAssociation" ,
53
47
NotificationFrequency .TRANSACTION_COMMIT ));
54
- log .debug ("Enter create node" );
55
48
policyComponent .bindClassBehaviour (
56
49
NodeServicePolicies .OnCreateNodePolicy .QNAME ,
57
50
ContentModel .PROP_CONTENT ,
@@ -63,23 +56,19 @@ SignModel.ASPECT_SIGNATURE, new JavaBehaviour(this, "onDeleteAssociation",
63
56
@ Override
64
57
public void onCreateNode (ChildAssociationRef childNodeRef ) {
65
58
66
- log .debug ("Enter create node" );
67
59
NodeRef node = childNodeRef .getChildRef ();
68
- ContentData contentData = (ContentData ) nodeService .getProperty (node , ContentModel .PROP_CONTENT );
69
- //Do this check only if the uploaded document is a PDF
70
- if (contentData .getMimetype ().equalsIgnoreCase ("application/pdf" )) {
71
-
72
- log .debug ("Is PDF" );
73
- try {
60
+ if (nodeService .exists (node )) {
61
+ ContentData contentData = (ContentData ) nodeService .getProperty (node , ContentModel .PROP_CONTENT );
62
+ // Do this check only if the uploaded document is a PDF
63
+ if (contentData .getMimetype ().equalsIgnoreCase ("application/pdf" )) {
74
64
ArrayList <Map <QName , Serializable >> signatures = getDigitalSignatures (node );
75
- //Add the aspect asociation
76
65
if (signatures != null ) {
77
66
for (Map <QName , Serializable > aspectProperties : signatures ) {
78
67
String originalFileName = nodeService .getProperty (node , ContentModel .PROP_NAME ).toString ();
79
68
String signatureFileName = FilenameUtils .getBaseName (originalFileName ) + "-"
80
69
+ System .currentTimeMillis () + "-" + PADES ;
81
70
82
- // Creating a node reference without type (no content and no folder), remains invisible for Share
71
+ // Creating a node reference without type (no content and no folder): remains invisible for Share
83
72
NodeRef signatureNodeRef = nodeService .createNode (
84
73
nodeService .getPrimaryParent (node ).getParentRef (),
85
74
ContentModel .ASSOC_CONTAINS ,
@@ -94,62 +83,51 @@ public void onCreateNode(ChildAssociationRef childNodeRef) {
94
83
}
95
84
}
96
85
}
97
- catch (Exception ex ) {
98
- log .error (ex .toString ());
99
- }
100
86
}
101
87
}
102
88
103
89
@ Override
104
90
public void onDeleteAssociation (AssociationRef nodeAssocRef ) {
105
- // Delete SIGNED aspect on SIGNATURE deletion
106
91
if (nodeService .exists (nodeAssocRef .getTargetRef ())) {
107
92
nodeService .removeAspect (nodeAssocRef .getTargetRef (), SignModel .ASPECT_SIGNED );
108
93
}
109
94
}
110
95
111
- public ArrayList <Map <QName , Serializable >> getDigitalSignatures (NodeRef node ) throws IOException , KeyStoreException , Exception , CertificateException {
96
+ public ArrayList <Map <QName , Serializable >> getDigitalSignatures (NodeRef node ) {
112
97
113
- ContentReader contentReader = contentService .getReader (node , ContentModel .PROP_CONTENT );
114
- InputStream is = contentReader .getContentInputStream ();
98
+ try {
115
99
116
- PdfReader reader = new PdfReader (is );
117
- AcroFields af = reader .getAcroFields ();
118
- ArrayList <String > names = af .getSignatureNames ();
119
- if (names == null || names .isEmpty ()) return null ;
120
- KeyStore ks = KeyStore .getInstance (KeyStore .getDefaultType ());
121
- ks .load (null , null );
122
- ArrayList <Map <QName , Serializable >> aspects = new ArrayList <Map <QName , Serializable >>();
123
- for (String name : names ) {
124
- PdfPKCS7 pk = af .verifySignature (name );
125
- X509Certificate certificate = pk .getSigningCertificate ();
126
-
127
- //Set aspect properties for each signature
128
- Map <QName , Serializable > aspectSignatureProperties = new HashMap <QName , Serializable >();
129
- aspectSignatureProperties .put (SignModel .PROP_DATE , convertCalendarToDate (pk .getSignDate ()));
130
- aspectSignatureProperties .put (SignModel .PROP_CERTIFICATE_PRINCIPAL , certificate .getSubjectX500Principal ().toString ());
131
- aspectSignatureProperties .put (SignModel .PROP_CERTIFICATE_SERIAL_NUMBER , certificate .getSerialNumber ().toString ());
132
- aspectSignatureProperties .put (SignModel .PROP_CERTIFICATE_NOT_AFTER , certificate .getNotAfter ());
133
- aspectSignatureProperties .put (SignModel .PROP_CERTIFICATE_ISSUER , certificate .getIssuerX500Principal ().toString ());
134
- aspects .add (aspectSignatureProperties );
135
- }
136
- return aspects ;
100
+ ContentReader contentReader = contentService .getReader (node , ContentModel .PROP_CONTENT );
101
+ InputStream is = contentReader .getContentInputStream ();
102
+
103
+ PdfReader reader = new PdfReader (is );
104
+ AcroFields af = reader .getAcroFields ();
105
+ ArrayList <String > names = af .getSignatureNames ();
106
+ if (names == null || names .isEmpty ()) return null ;
107
+ KeyStore ks = KeyStore .getInstance (KeyStore .getDefaultType ());
108
+ ks .load (null , null );
109
+ ArrayList <Map <QName , Serializable >> aspects = new ArrayList <Map <QName , Serializable >>();
110
+ for (String name : names ) {
111
+ PdfPKCS7 pk = af .verifySignature (name );
112
+ X509Certificate certificate = pk .getSigningCertificate ();
113
+
114
+ //Set aspect properties for each signature
115
+ Map <QName , Serializable > aspectSignatureProperties = new HashMap <QName , Serializable >();
116
+ aspectSignatureProperties .put (SignModel .PROP_DATE , pk .getSignDate ().getTime ());
117
+ aspectSignatureProperties .put (SignModel .PROP_CERTIFICATE_PRINCIPAL , certificate .getSubjectX500Principal ().toString ());
118
+ aspectSignatureProperties .put (SignModel .PROP_CERTIFICATE_SERIAL_NUMBER , certificate .getSerialNumber ().toString ());
119
+ aspectSignatureProperties .put (SignModel .PROP_CERTIFICATE_NOT_AFTER , certificate .getNotAfter ());
120
+ aspectSignatureProperties .put (SignModel .PROP_CERTIFICATE_ISSUER , certificate .getIssuerX500Principal ().toString ());
121
+ aspects .add (aspectSignatureProperties );
122
+ }
123
+ return aspects ;
124
+
125
+ } catch (Exception e ) {
126
+ throw new RuntimeException (e );
127
+ }
137
128
}
138
129
139
130
140
- @ SuppressWarnings ({"deprecation" })
141
- private Date convertCalendarToDate (Calendar cal ) {
142
- Date date = new Date ();
143
- date .setDate (cal .get (Calendar .DATE ));
144
- date .setMonth (cal .get (Calendar .MONTH ));
145
- date .setYear (cal .get (Calendar .YEAR ) - 1900 );
146
- date .setHours (cal .get (Calendar .HOUR ));
147
- date .setMinutes (cal .get (Calendar .MINUTE ));
148
- date .setSeconds (cal .get (Calendar .SECOND ));
149
- System .out .println (date );
150
- return date ;
151
- }
152
-
153
131
public PolicyComponent getPolicyComponent () {
154
132
return policyComponent ;
155
133
}
@@ -181,4 +159,6 @@ public ContentService getContentService() {
181
159
public void setContentService (ContentService contentService ) {
182
160
this .contentService = contentService ;
183
161
}
162
+
163
+
184
164
}
0 commit comments