diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2020-08-15 00:18:42 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2020-08-15 00:18:42 +0000 |
commit | 60303201dcb715a7738cd5bff3a85ffb424a2a90 (patch) | |
tree | 1154b264901ee5e32e7001175063d64b81f8b73f /src/ooxml | |
parent | e68bd7bd7ded6948a3289c6a6e3ad37453d12c42 (diff) | |
download | poi-60303201dcb715a7738cd5bff3a85ffb424a2a90.tar.gz poi-60303201dcb715a7738cd5bff3a85ffb424a2a90.zip |
use the forbidden-apis policies corresponding to the JRE
fix the forbidden apis issues
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1880866 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml')
9 files changed, 70 insertions, 65 deletions
diff --git a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/SignatureInfo.java b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/SignatureInfo.java index d6ce28b759..de73c19c0a 100644 --- a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/SignatureInfo.java +++ b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/SignatureInfo.java @@ -701,7 +701,7 @@ public class SignatureInfo { private Provider getProvider(String className) { try { - return (Provider)Class.forName(className).newInstance(); + return (Provider)Class.forName(className).getDeclaredConstructor().newInstance(); } catch (Exception e) { LOG.log(POILogger.DEBUG, "XMLDsig-Provider '"+className+"' can't be found - trying next."); return null; diff --git a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/services/RelationshipTransformService.java b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/services/RelationshipTransformService.java index 7cee2a0187..74b0945173 100644 --- a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/services/RelationshipTransformService.java +++ b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/services/RelationshipTransformService.java @@ -18,9 +18,9 @@ /* ==================================================================== This product contains an ASLv2 licensed version of the OOXML signer package from the eID Applet project - http://code.google.com/p/eid-applet/source/browse/trunk/README.txt + http://code.google.com/p/eid-applet/source/browse/trunk/README.txt Copyright (C) 2008-2014 FedICT. - ================================================================= */ + ================================================================= */ package org.apache.poi.poifs.crypt.dsig.services; @@ -52,6 +52,7 @@ import org.apache.jcp.xml.dsig.internal.dom.ApacheNodeSetData; import org.apache.poi.ooxml.util.DocumentHelper; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; +import org.apache.poi.util.SuppressForbidden; import org.apache.xml.security.signature.XMLSignatureInput; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; @@ -65,7 +66,7 @@ import org.w3c.dom.NodeList; /** * JSR105 implementation of the RelationshipTransform transformation. - * + * * <p> * Specs: http://openiso.org/Ecma/376/Part2/12.2.4#26 * </p> @@ -77,7 +78,7 @@ public class RelationshipTransformService extends TransformService { private final List<String> sourceIds; private static final POILogger LOG = POILogFactory.getLogger(RelationshipTransformService.class); - + /** * Relationship Transform parameter specification class. */ @@ -90,8 +91,20 @@ public class RelationshipTransformService extends TransformService { return !sourceIds.isEmpty(); } } - - + + @SuppressForbidden("new Provider(String,String,String) is not available in Java 8") + private static final class POIXmlDsigProvider extends Provider { + static final long serialVersionUID = 1L; + private static final String NAME = "POIXmlDsigProvider"; + + private POIXmlDsigProvider() { + super(NAME, 1d, NAME); + put("TransformService." + TRANSFORM_URI, RelationshipTransformService.class.getName()); + put("TransformService." + TRANSFORM_URI + " MechanismType", "DOM"); + } + } + + public RelationshipTransformService() { super(); LOG.log(POILogger.DEBUG, "constructor"); @@ -100,24 +113,18 @@ public class RelationshipTransformService extends TransformService { /** * Register the provider for this TransformService - * + * * @see javax.xml.crypto.dsig.TransformService */ public static synchronized void registerDsigProvider() { // the xml signature classes will try to find a special TransformerService, - // which is ofcourse unknown to JCE before ... - final String dsigProvider = "POIXmlDsigProvider"; - if (Security.getProperty(dsigProvider) == null) { - Provider p = new Provider(dsigProvider, 1.0, dsigProvider){ - static final long serialVersionUID = 1L; - }; - p.put("TransformService." + TRANSFORM_URI, RelationshipTransformService.class.getName()); - p.put("TransformService." + TRANSFORM_URI + " MechanismType", "DOM"); - Security.addProvider(p); + // which is of course unknown to JCE before ... + if (Security.getProperty(POIXmlDsigProvider.NAME) == null) { + Security.addProvider(new POIXmlDsigProvider()); } } - - + + @Override public void init(TransformParameterSpec params) throws InvalidAlgorithmParameterException { LOG.log(POILogger.DEBUG, "init(params)"); @@ -134,7 +141,7 @@ public class RelationshipTransformService extends TransformService { LOG.log(POILogger.DEBUG, "parent java type: " + parent.getClass().getName()); DOMStructure domParent = (DOMStructure) parent; Node parentNode = domParent.getNode(); - + try { TransformDocument transDoc = TransformDocument.Factory.parse(parentNode, DEFAULT_XML_OPTIONS); XmlObject[] xoList = transDoc.getTransform().selectChildren(RelationshipReferenceDocument.type.getDocumentElementName()); @@ -157,7 +164,7 @@ public class RelationshipTransformService extends TransformService { DOMStructure domParent = (DOMStructure) parent; Element parentNode = (Element)domParent.getNode(); Document doc = parentNode.getOwnerDocument(); - + for (String sourceId : this.sourceIds) { Element el = doc.createElementNS(OO_DIGSIG_NS, "mdssi:RelationshipReference"); el.setAttributeNS(XML_NS, "xmlns:mdssi", OO_DIGSIG_NS); @@ -165,7 +172,7 @@ public class RelationshipTransformService extends TransformService { parentNode.appendChild(el); } } - + public AlgorithmParameterSpec getParameterSpec() { LOG.log(POILogger.DEBUG, "getParameterSpec"); return null; @@ -174,7 +181,7 @@ public class RelationshipTransformService extends TransformService { /** * The relationships transform takes the XML document from the Relationships part * and converts it to another XML document. - * + * * @see <a href="https://www.ecma-international.org/activities/Office%20Open%20XML%20Formats/Draft%20ECMA-376%203rd%20edition,%20March%202011/Office%20Open%20XML%20Part%202%20-%20Open%20Packaging%20Conventions.pdf">13.2.4.24 Relationships Transform Algorithm</a> * @see <a href="https://stackoverflow.com/questions/36063375">XML Relationship Transform Algorithm</a> */ @@ -184,14 +191,14 @@ public class RelationshipTransformService extends TransformService { OctetStreamData octetStreamData = (OctetStreamData) data; LOG.log(POILogger.DEBUG, "URI: " + octetStreamData.getURI()); InputStream octetStream = octetStreamData.getOctetStream(); - + Document doc; try { doc = DocumentHelper.readDocument(octetStream); } catch (Exception e) { throw new TransformException(e.getMessage(), e); } - + // keep only those relationships which id is registered in the sourceIds Element root = doc.getDocumentElement(); NodeList nl = root.getChildNodes(); @@ -215,9 +222,9 @@ public class RelationshipTransformService extends TransformService { for (Element el : rsList.values()) { root.appendChild(el); } - + LOG.log(POILogger.DEBUG, "# Relationship elements: ", rsList.size()); - + return new ApacheNodeSetData(new XMLSignatureInput(root)); } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java index 2e8e7a7887..2c41313691 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java @@ -72,7 +72,7 @@ public class SheetDataWriter implements Closeable { _fd = createTempFile(); _out = createWriter(_fd); } - + public SheetDataWriter(Writer writer) throws IOException { _fd = null; _out = writer; @@ -188,8 +188,6 @@ public class SheetDataWriter implements Closeable { if (_fd.exists() && !_fd.delete()) { logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+_fd); } - - super.finalize(); } /** diff --git a/src/ooxml/testcases/org/apache/poi/sl/tests/TestFonts.java b/src/ooxml/testcases/org/apache/poi/sl/tests/TestFonts.java index 2b9963ecd7..28a958c8af 100644 --- a/src/ooxml/testcases/org/apache/poi/sl/tests/TestFonts.java +++ b/src/ooxml/testcases/org/apache/poi/sl/tests/TestFonts.java @@ -40,6 +40,7 @@ import org.apache.poi.common.usermodel.fonts.FontGroup; import org.apache.poi.sl.draw.Drawable; import org.apache.poi.sl.usermodel.Slide; import org.apache.poi.sl.usermodel.SlideShow; +import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.sl.usermodel.StrokeStyle.LineDash; import org.apache.poi.sl.usermodel.TextBox; import org.apache.poi.sl.usermodel.TextParagraph; @@ -53,7 +54,7 @@ import org.junit.Test; * Test rendering - specific to font handling */ public class TestFonts { - private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance(); + private static final POIDataSamples _slTests = POIDataSamples.getSlideShowInstance(); private static final String JPTEXT = "\u3061\u3087\u3063\u3068\u65E9\u3044\u3051\u3069T\u30B7\u30E3\u30C4\u304C\u7740\u305F\u304F\u306A" + @@ -78,9 +79,9 @@ public class TestFonts { } @Test - public void resizeToFitTextHSLF() throws IOException, ReflectiveOperationException { + public void resizeToFitTextHSLF() throws IOException { assumeFalse(xslfOnly()); - SlideShow<?,?> ppt = (SlideShow<?,?>)Class.forName("org.apache.poi.hslf.usermodel.HSLFSlideShow").newInstance(); + SlideShow<?,?> ppt = SlideShowFactory.create(false); resizeToFitText(ppt); ppt.close(); } diff --git a/src/ooxml/testcases/org/apache/poi/sl/tests/TestOleShape.java b/src/ooxml/testcases/org/apache/poi/sl/tests/TestOleShape.java index f652bcc0e0..061b628f33 100644 --- a/src/ooxml/testcases/org/apache/poi/sl/tests/TestOleShape.java +++ b/src/ooxml/testcases/org/apache/poi/sl/tests/TestOleShape.java @@ -147,12 +147,12 @@ public class TestOleShape { } } - private SlideShow<?,?> createSlideShow() throws ReflectiveOperationException { + private SlideShow<?,?> createSlideShow() throws IOException { if (api == Api.XSLF) { return new XMLSlideShow(); } else { assumeFalse(xslfOnly()); - return (SlideShow<?,?>)Class.forName("org.apache.poi.hslf.usermodel.HSLFSlideShow").newInstance(); + return SlideShowFactory.create(false); } } @@ -187,7 +187,7 @@ public class TestOleShape { } } - private void validateOleData(final InputStream in) throws IOException, InvalidFormatException, ReflectiveOperationException { + private void validateOleData(final InputStream in) throws IOException, ReflectiveOperationException { switch (app) { case EXCEL_V8: case EXCEL_V12: @@ -196,6 +196,7 @@ public class TestOleShape { } break; case WORD_V8: + @SuppressWarnings("unchecked") Class<? extends POIDocument> clazz = (Class<? extends POIDocument>)Class.forName("org.apache.poi.hwpf.HWPFDocument"); Constructor<? extends POIDocument> con = clazz.getDeclaredConstructor(InputStream.class); Method m = clazz.getMethod("getDocumentText"); diff --git a/src/ooxml/testcases/org/apache/poi/sl/tests/TestSlide.java b/src/ooxml/testcases/org/apache/poi/sl/tests/TestSlide.java index 47494b3407..19b8ab38b1 100644 --- a/src/ooxml/testcases/org/apache/poi/sl/tests/TestSlide.java +++ b/src/ooxml/testcases/org/apache/poi/sl/tests/TestSlide.java @@ -29,6 +29,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import org.apache.poi.sl.usermodel.Slide; import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.xslf.usermodel.XMLSlideShow; @@ -37,36 +38,36 @@ import org.junit.Test; public class TestSlide { @Test - public void hideHSLF() throws IOException, ReflectiveOperationException { + public void hideHSLF() throws IOException { assumeFalse(xslfOnly()); - SlideShow<?,?> ppt1 = (SlideShow<?,?>)Class.forName("org.apache.poi.hslf.usermodel.HSLFSlideShow").newInstance(); - hideSlide(ppt1); - ppt1.close(); + try (SlideShow<?,?> ppt1 = SlideShowFactory.create(false)) { + hideSlide(ppt1); + } } @Test public void hideXSLF() throws IOException { - SlideShow<?,?> ppt1 = new XMLSlideShow(); - hideSlide(ppt1); - ppt1.close(); + try (SlideShow<?,?> ppt1 = new XMLSlideShow()) { + hideSlide(ppt1); + } } private void hideSlide(SlideShow<?,?> ppt1) throws IOException { ppt1.createSlide().setHidden(true); ppt1.createSlide(); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ppt1.write(bos); - ppt1.close(); + try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { + ppt1.write(bos); - InputStream is = new ByteArrayInputStream(bos.toByteArray()); - SlideShow<?,?> ppt2 = SlideShowFactory.create(is); + try (InputStream is = new ByteArrayInputStream(bos.toByteArray()); + SlideShow<?, ?> ppt2 = SlideShowFactory.create(is)) { - Boolean[] hiddenState = ppt2.getSlides().stream().map(e -> e.isHidden()).toArray(Boolean[]::new); + Boolean[] hiddenState = ppt2.getSlides().stream().map(Slide::isHidden).toArray(Boolean[]::new); - assertTrue(hiddenState[0]); - assertFalse(hiddenState[1]); + assertTrue(hiddenState[0]); + assertFalse(hiddenState[1]); - ppt2.close(); + } + } } }
\ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/sl/tests/TestTable.java b/src/ooxml/testcases/org/apache/poi/sl/tests/TestTable.java index fd31762b8d..2ab404b030 100644 --- a/src/ooxml/testcases/org/apache/poi/sl/tests/TestTable.java +++ b/src/ooxml/testcases/org/apache/poi/sl/tests/TestTable.java @@ -95,9 +95,9 @@ public class TestTable { } @Test - public void directionHSLF() throws IOException, ReflectiveOperationException { + public void directionHSLF() throws IOException { assumeFalse(xslfOnly()); - SlideShow<?,?> ppt1 = (SlideShow<?,?>)Class.forName("org.apache.poi.hslf.usermodel.HSLFSlideShow").newInstance(); + SlideShow<?,?> ppt1 = SlideShowFactory.create(false); testTextDirection(ppt1); ppt1.close(); } diff --git a/src/ooxml/testcases/org/apache/poi/ss/tests/usermodel/TestEmbedOLEPackage.java b/src/ooxml/testcases/org/apache/poi/ss/tests/usermodel/TestEmbedOLEPackage.java index f184b01516..28c1bcc06a 100644 --- a/src/ooxml/testcases/org/apache/poi/ss/tests/usermodel/TestEmbedOLEPackage.java +++ b/src/ooxml/testcases/org/apache/poi/ss/tests/usermodel/TestEmbedOLEPackage.java @@ -50,6 +50,7 @@ import org.apache.poi.sl.usermodel.AutoShape; import org.apache.poi.sl.usermodel.ShapeType; import org.apache.poi.sl.usermodel.Slide; import org.apache.poi.sl.usermodel.SlideShow; +import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.ss.extractor.EmbeddedData; import org.apache.poi.ss.extractor.EmbeddedExtractor; import org.apache.poi.ss.usermodel.ClientAnchor; @@ -58,7 +59,6 @@ import org.apache.poi.ss.usermodel.ObjectData; import org.apache.poi.ss.usermodel.Shape; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.usermodel.XSSFObjectData; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @@ -71,7 +71,7 @@ public class TestEmbedOLEPackage { private static final POIDataSamples ssamples = POIDataSamples.getSpreadSheetInstance(); @BeforeClass - public static void init() throws IOException, ReflectiveOperationException { + public static void init() throws IOException { samplePPT = getSamplePPT(false); samplePPTX = getSamplePPT(true); samplePNG = ssamples.readFile("logoKarmokar4.png"); @@ -83,7 +83,6 @@ public class TestEmbedOLEPackage { XSSFWorkbook wb = new XSSFWorkbook(is)) { List<XSSFObjectData> oleShapes = new ArrayList<>(); List<Ole10Native> ole10s = new ArrayList<>(); - List<String> digests = new ArrayList<>(); final boolean digestMatch = wb.getSheetAt(0).getDrawingPatriarch().getShapes().stream() @@ -212,9 +211,8 @@ public class TestEmbedOLEPackage { pat2.createObjectData(anchor2, oleIdx2, picIdx); } - static byte[] getSamplePPT(boolean ooxml) throws IOException, ReflectiveOperationException { - SlideShow<?,?> ppt = (ooxml) ? new XMLSlideShow() - : (SlideShow<?,?>)Class.forName("org.apache.poi.hslf.usermodel.HSLFSlideShow").newInstance(); + static byte[] getSamplePPT(boolean ooxml) throws IOException { + SlideShow<?,?> ppt = SlideShowFactory.create(ooxml); Slide<?,?> slide = ppt.createSlide(); AutoShape<?,?> sh1 = slide.createAutoShape(); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java index 6ba55c0692..13e79a6ffc 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFImportFromXML.java @@ -21,7 +21,6 @@ import static org.apache.poi.xssf.usermodel.XSSFRelation.NS_SPREADSHEETML; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; import java.io.IOException; import java.text.DateFormatSymbols; @@ -156,10 +155,10 @@ public class TestXSSFImportFromXML { //Check for Schema element XSSFSheet sheet = wb.getSheetAt(0); - assertEquals(new Double(id), sheet.getRow(28).getCell(1).getNumericCellValue(), 0); + assertEquals(id, sheet.getRow(28).getCell(1).getNumericCellValue(), 0); assertEquals(displayName, sheet.getRow(11).getCell(5).getStringCellValue()); assertEquals(ref, sheet.getRow(14).getCell(7).getStringCellValue()); - assertEquals(new Double(count), sheet.getRow(18).getCell(3).getNumericCellValue(), 0); + assertEquals(count, sheet.getRow(18).getCell(3).getNumericCellValue(), 0); } } @@ -225,13 +224,13 @@ public class TestXSSFImportFromXML { assertEquals(date, rowData.getCell(0).getDateCellValue()); assertEquals("Amount Int", rowHeadings.getCell(1).getStringCellValue()); - assertEquals(new Double(Integer.MIN_VALUE), rowData.getCell(1).getNumericCellValue(), 0); + assertEquals(Integer.MIN_VALUE, rowData.getCell(1).getNumericCellValue(), 0); assertEquals("Amount Double", rowHeadings.getCell(2).getStringCellValue()); assertEquals(1.0000123, rowData.getCell(2).getNumericCellValue(), 0); assertEquals("Amount UnsignedInt", rowHeadings.getCell(3).getStringCellValue()); - assertEquals(new Double(12345), rowData.getCell(3).getNumericCellValue(), 0); + assertEquals(12345d, rowData.getCell(3).getNumericCellValue(), 0); wb.close(); } |