use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class XSSFWorkbook method setVBAProject.
/**
* Adds a vbaProject.bin file to the workbook. This will change the workbook
* type if necessary.
*
* @throws IOException
*/
public void setVBAProject(InputStream vbaProjectStream) throws IOException {
if (!isMacroEnabled()) {
setWorkbookType(XSSFWorkbookType.XLSM);
}
PackagePartName ppName;
try {
ppName = PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName());
} catch (InvalidFormatException e) {
throw new POIXMLException(e);
}
OPCPackage opc = getPackage();
OutputStream outputStream;
if (!opc.containPart(ppName)) {
POIXMLDocumentPart relationship = createRelationship(XSSFRelation.VBA_MACROS, XSSFFactory.getInstance());
outputStream = relationship.getPackagePart().getOutputStream();
} else {
PackagePart part = opc.getPart(ppName);
outputStream = part.getOutputStream();
}
try {
IOUtils.copy(vbaProjectStream, outputStream);
} finally {
IOUtils.closeQuietly(outputStream);
}
}
use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class XSSFWorkbook method addOlePackage.
@Override
public int addOlePackage(byte[] oleData, String label, String fileName, String command) throws IOException {
// find an unused part name
OPCPackage opc = getPackage();
PackagePartName pnOLE;
int oleId = 0;
do {
try {
pnOLE = PackagingURIHelper.createPartName("/xl/embeddings/oleObject" + (++oleId) + ".bin");
} catch (InvalidFormatException e) {
throw new IOException("ole object name not recognized", e);
}
} while (opc.containPart(pnOLE));
PackagePart pp = opc.createPart(pnOLE, "application/vnd.openxmlformats-officedocument.oleObject");
Ole10Native ole10 = new Ole10Native(label, fileName, command, oleData);
ByteArrayOutputStream bos = new ByteArrayOutputStream(oleData.length + 500);
ole10.writeOut(bos);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryNode root = poifs.getRoot();
root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));
root.setStorageClsid(ClassID.OLE10_PACKAGE);
// TODO: generate CombObj stream
OutputStream os = pp.getOutputStream();
poifs.writeFilesystem(os);
os.close();
poifs.close();
return oleId;
}
use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class XSSFWorkbook method newPackage.
/**
* Create a new SpreadsheetML package and setup the default minimal content
*/
protected static OPCPackage newPackage(XSSFWorkbookType workbookType) {
try {
OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream());
// Main part
PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName());
// Create main part relationship
pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
// Create main document part
pkg.createPart(corePartName, workbookType.getContentType());
pkg.getPackageProperties().setCreatorProperty(DOCUMENT_CREATOR);
return pkg;
} catch (Exception e) {
throw new POIXMLException(e);
}
}
use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestSignatureInfo method getSignerUnsigned.
@Test
public void getSignerUnsigned() throws Exception {
String[] testFiles = { "hello-world-unsigned.docx", "hello-world-unsigned.pptx", "hello-world-unsigned.xlsx", "hello-world-office-2010-technical-preview-unsigned.docx" };
for (String testFile : testFiles) {
OPCPackage pkg = OPCPackage.open(testdata.getFile(testFile), PackageAccess.READ);
SignatureConfig sic = new SignatureConfig();
sic.setOpcPackage(pkg);
SignatureInfo si = new SignatureInfo();
si.setSignatureConfig(sic);
List<X509Certificate> result = new ArrayList<X509Certificate>();
for (SignaturePart sp : si.getSignatureParts()) {
if (sp.validate()) {
result.add(sp.getSigner());
}
}
pkg.revert();
pkg.close();
assertNotNull(result);
assertTrue(result.isEmpty());
}
}
use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestSignatureInfo method testCertChain.
@Test
public void testCertChain() throws Exception {
KeyStore keystore = KeyStore.getInstance("PKCS12");
String password = "test";
InputStream is = testdata.openResourceAsStream("chaintest.pfx");
keystore.load(is, password.toCharArray());
is.close();
Key key = keystore.getKey("poitest", password.toCharArray());
Certificate[] chainList = keystore.getCertificateChain("poitest");
List<X509Certificate> certChain = new ArrayList<X509Certificate>();
for (Certificate c : chainList) {
certChain.add((X509Certificate) c);
}
x509 = certChain.get(0);
keyPair = new KeyPair(x509.getPublicKey(), (PrivateKey) key);
String testFile = "hello-world-unsigned.xlsx";
OPCPackage pkg = OPCPackage.open(copy(testdata.getFile(testFile)), PackageAccess.READ_WRITE);
SignatureConfig signatureConfig = new SignatureConfig();
signatureConfig.setKey(keyPair.getPrivate());
signatureConfig.setSigningCertificateChain(certChain);
Calendar oldCal = LocaleUtil.getLocaleCalendar(2007, 7, 1);
signatureConfig.setExecutionTime(oldCal.getTime());
signatureConfig.setDigestAlgo(HashAlgorithm.sha1);
signatureConfig.setOpcPackage(pkg);
SignatureInfo si = new SignatureInfo();
si.setSignatureConfig(signatureConfig);
si.confirmSignature();
for (SignaturePart sp : si.getSignatureParts()) {
assertTrue("Could not validate", sp.validate());
X509Certificate signer = sp.getSigner();
assertNotNull("signer undefined?!", signer);
List<X509Certificate> certChainRes = sp.getCertChain();
assertEquals(3, certChainRes.size());
}
pkg.close();
}
Aggregations