-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ProtectionSettings.java
187 lines (154 loc) · 5.25 KB
/
ProtectionSettings.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
/**
*
*/
package org.docx4j.openpackaging.packages;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.docx4j.docProps.extended.Properties;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Protection Settings which are common across
* docx, pptx, xlsx, namely mark as final, encrypt with password,
* and digital signature. Subclasses implement the
* docx and xlsx format specific features.
*
* @author jharrop
* @since 3.3.0
*/
public abstract class ProtectionSettings {
protected static Logger log = LoggerFactory.getLogger(ProtectionSettings.class);
protected OpcPackage pkg;
public ProtectionSettings(OpcPackage pkg) {
this.pkg = pkg;
}
// Mark as Final
/**
* @since 3.3.0
*/
public boolean getMarkAsFinal() {
if (pkg.getDocPropsCustomPart()==null) return false;
return pkg.getDocPropsCustomPart().getMarkAsFinal();
}
/**
* @since 3.3.0
*/
public void setMarkAsFinal(boolean val) {
// First
if (pkg.getDocPropsCustomPart()==null
&& val) // only create it if we're setting the value
{
pkg.addDocPropsCustomPart();
}
if (pkg.getDocPropsCustomPart()==null) {
return;
} else {
pkg.getDocPropsCustomPart().setMarkAsFinal(val);
}
// Second
if (pkg.getDocPropsExtendedPart()==null
&& val) // only create it if we're setting the value
{
pkg.addDocPropsExtendedPart();
}
setDocSecurity(0); // surprising, but this is what Word 2013 does (you'd expect 2)
// Third: <cp:contentStatus>Final</cp:contentStatus>
if (pkg.getDocPropsCorePart()==null
&& val) // only create it if we're setting the value
{
pkg.addDocPropsCorePart();
}
if (pkg.getDocPropsCorePart()!=null) {
pkg.getDocPropsCorePart().getJaxbElement().setContentStatus("Final");
}
}
/**
* Note, this won't create the DocPropsExtendedPart (app.xml) if it doesn't exist
* @param val
*/
protected void setDocSecurity(int val) {
if (pkg.getDocPropsExtendedPart()!=null) {
pkg.getDocPropsExtendedPart().setDocSecurity(val);
}
}
// Encrypt with Password - this is implemented in OpcPackage's load & save methods
// All we do here is record whether it was encrypted.
// We don't record the password.
// private boolean wasEncrypted = false;
//
// /**
// * Whether this package was created by loading an encrypted file.
// *
// * Encrypt with Password functionality is implemented in OpcPackage's load and save methods.
// *
// * @return
// */
// public boolean wasEncrypted() {
// return wasEncrypted;
// }
//
// /**
// * Flag to indicate this package was created by loading an encrypted file.
// */
// protected void setWasEncrypted(boolean wasEncrypted) {
// this.wasEncrypted = wasEncrypted;
// }
// Restrict Permission by People
// Microsoft's Information Rights Management; NOT IMPLEMENTED
// Digital Signature
// /**
// * Sign the pkg, using default settings.
// *
// * @param certificateIS
// * @param password
// * @throws Docx4JException
// */
// public void sign(InputStream PKCS12stream, String password) throws Docx4JException {
//
// Signing signing = null;
// try {
// Class<?> signingClass = Class.forName("com.plutext.dsig.SignatureHelper");
// Constructor<?> ctor = signingClass.getConstructor(OpcPackage.class);
// signing = (Signing) ctor.newInstance(this.pkg);
// } catch (Exception e) {
// log.warn("Docx4j Enterprise jar v3.3 or greater not found. Required for Digital Signatures.");
// throw new Docx4JException("missing Enterprise version required for Digital Signature functionality");
// }
//
// signing.configureSignature(PKCS12stream, password);
// signing.sign();
// }
/**
* get the SignatureHelper object, so you can sign the package using custom settings.
*
* @param certificateIS
* @param password
* @return
* @throws Docx4JException
*/
public Object getSignatureHelper() throws Docx4JException {
try {
Class<?> signingClass = Class.forName("com.plutext.dsig.SignatureHelper");
Constructor<?> ctor = signingClass.getConstructor(OpcPackage.class);
return ctor.newInstance(this.pkg);
} catch (Exception e) {
log.warn("Docx4j Enterprise jar v3.3 or greater not found. Required for Digital Signatures.");
throw new Docx4JException("missing Enterprise version required for Digital Signature functionality");
}
}
// public boolean areSignaturesValid() throws Docx4JException {
//
// Signing signing = null;
// try {
// Class<?> signingClass = Class.forName("com.plutext.dsig.SignatureHelper");
// Constructor<?> ctor = signingClass.getConstructor(OpcPackage.class);
// signing = (Signing) ctor.newInstance(this.pkg);
// // TODO: get method etc
// } catch (Exception e) {
// log.warn("Docx4j Enterprise jar v3.3 or greater not found. Required for Digital Signatures.");
// throw new Docx4JException("missing Enterprise version required for Digital Signature functionality");
// }
// }
}