Search in sources :

Example 26 with OPCPackage

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);
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 27 with OPCPackage

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;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) Ole10Native(org.apache.poi.poifs.filesystem.Ole10Native) ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 28 with OPCPackage

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);
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) POIXMLException(org.apache.poi.POIXMLException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) NoSuchElementException(java.util.NoSuchElementException) POIXMLException(org.apache.poi.POIXMLException) XLSBUnsupportedException(org.apache.poi.xssf.XLSBUnsupportedException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException)

Example 29 with OPCPackage

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());
    }
}
Also used : SignatureInfo(org.apache.poi.poifs.crypt.dsig.SignatureInfo) SignatureConfig(org.apache.poi.poifs.crypt.dsig.SignatureConfig) ArrayList(java.util.ArrayList) SignaturePart(org.apache.poi.poifs.crypt.dsig.SignatureInfo.SignaturePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Example 30 with OPCPackage

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();
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SignatureConfig(org.apache.poi.poifs.crypt.dsig.SignatureConfig) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) SignatureInfo(org.apache.poi.poifs.crypt.dsig.SignatureInfo) SignaturePart(org.apache.poi.poifs.crypt.dsig.SignatureInfo.SignaturePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Key(java.security.Key) PrivateKey(java.security.PrivateKey) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Test(org.junit.Test)

Aggregations

OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)116 Test (org.junit.Test)54 InputStream (java.io.InputStream)29 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)21 File (java.io.File)19 IOException (java.io.IOException)17 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)13 SignatureConfig (org.apache.poi.poifs.crypt.dsig.SignatureConfig)10 SignatureInfo (org.apache.poi.poifs.crypt.dsig.SignatureInfo)10 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)10 OutputStream (java.io.OutputStream)9 ArrayList (java.util.ArrayList)9 FileOutputStream (java.io.FileOutputStream)8 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)8 XmlException (org.apache.xmlbeans.XmlException)8 FileInputStream (java.io.FileInputStream)7