Search in sources :

Example 1 with SuppressForbidden

use of org.apache.poi.util.SuppressForbidden in project poi by apache.

the class SignatureFacet method brokenJvmWorkaround.

// helper method ... will be removed soon
public static void brokenJvmWorkaround(final Reference reference) {
    final DigestMethod digestMethod = reference.getDigestMethod();
    final String digestMethodUri = digestMethod.getAlgorithm();
    final Provider bcProv = Security.getProvider("BC");
    if (bcProv != null && !DigestMethod.SHA1.equals(digestMethodUri)) {
        // workaround for https://bugzilla.redhat.com/show_bug.cgi?id=1155012
        // overwrite standard message digest, if a digest <> SHA1 is used
        AccessController.doPrivileged(new PrivilegedAction<Void>() {

            @Override
            @SuppressForbidden("Workaround for a bug, needs access to private JDK members (may fail in Java 9): https://bugzilla.redhat.com/show_bug.cgi?id=1155012")
            public Void run() {
                try {
                    Method m = DOMDigestMethod.class.getDeclaredMethod("getMessageDigestAlgorithm");
                    m.setAccessible(true);
                    String mdAlgo = (String) m.invoke(digestMethod);
                    MessageDigest md = MessageDigest.getInstance(mdAlgo, bcProv);
                    Field f = DOMReference.class.getDeclaredField("md");
                    f.setAccessible(true);
                    f.set(reference, md);
                } catch (Exception e) {
                    LOG.log(POILogger.WARN, "Can't overwrite message digest (workaround for https://bugzilla.redhat.com/show_bug.cgi?id=1155012)", e);
                }
                // Void
                return null;
            }
        });
    }
}
Also used : DOMDigestMethod(org.apache.jcp.xml.dsig.internal.dom.DOMDigestMethod) DigestMethod(javax.xml.crypto.dsig.DigestMethod) DOMDigestMethod(org.apache.jcp.xml.dsig.internal.dom.DOMDigestMethod) Method(java.lang.reflect.Method) DigestMethod(javax.xml.crypto.dsig.DigestMethod) DOMReference(org.apache.jcp.xml.dsig.internal.dom.DOMReference) MarshalException(javax.xml.crypto.MarshalException) GeneralSecurityException(java.security.GeneralSecurityException) XMLSignatureException(javax.xml.crypto.dsig.XMLSignatureException) Provider(java.security.Provider) Field(java.lang.reflect.Field) DOMDigestMethod(org.apache.jcp.xml.dsig.internal.dom.DOMDigestMethod) SuppressForbidden(org.apache.poi.util.SuppressForbidden) MessageDigest(java.security.MessageDigest)

Example 2 with SuppressForbidden

use of org.apache.poi.util.SuppressForbidden in project poi by apache.

the class TestDataFormatter method setUpClass.

@BeforeClass
@SuppressForbidden
public static void setUpClass() {
    // some pre-checks to hunt for a problem in the Maven build
    // these checks ensure that the correct locale is set, so a failure here
    // usually indicates an invalid locale during test-execution
    assertFalse(DateUtil.isADateFormat(-1, "_-* #,##0.00_-;-* #,##0.00_-;_-* \"-\"??_-;_-@_-"));
    Locale ul = LocaleUtil.getUserLocale();
    assertTrue(Locale.ROOT.equals(ul) || Locale.getDefault().equals(ul));
    final String textValue = NumberToTextConverter.toText(1234.56);
    assertEquals(-1, textValue.indexOf('E'));
    Object cellValueO = Double.valueOf(1234.56);
    /*CellFormat cellFormat = new CellFormat("_-* #,##0.00_-;-* #,##0.00_-;_-* \"-\"??_-;_-@_-");
        CellFormatResult result = cellFormat.apply(cellValueO);
        assertEquals("    1,234.56 ", result.text);*/
    CellFormat cfmt = CellFormat.getInstance("_-* #,##0.00_-;-* #,##0.00_-;_-* \"-\"??_-;_-@_-");
    CellFormatResult result = cfmt.apply(cellValueO);
    assertEquals("This failure can indicate that the wrong locale is used during test-execution, ensure you run with english/US via -Duser.language=en -Duser.country=US", "    1,234.56 ", result.text);
}
Also used : Locale(java.util.Locale) CellFormat(org.apache.poi.ss.format.CellFormat) CellFormatResult(org.apache.poi.ss.format.CellFormatResult) BeforeClass(org.junit.BeforeClass) SuppressForbidden(org.apache.poi.util.SuppressForbidden)

Aggregations

SuppressForbidden (org.apache.poi.util.SuppressForbidden)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 GeneralSecurityException (java.security.GeneralSecurityException)1 MessageDigest (java.security.MessageDigest)1 Provider (java.security.Provider)1 Locale (java.util.Locale)1 MarshalException (javax.xml.crypto.MarshalException)1 DigestMethod (javax.xml.crypto.dsig.DigestMethod)1 XMLSignatureException (javax.xml.crypto.dsig.XMLSignatureException)1 DOMDigestMethod (org.apache.jcp.xml.dsig.internal.dom.DOMDigestMethod)1 DOMReference (org.apache.jcp.xml.dsig.internal.dom.DOMReference)1 CellFormat (org.apache.poi.ss.format.CellFormat)1 CellFormatResult (org.apache.poi.ss.format.CellFormatResult)1 BeforeClass (org.junit.BeforeClass)1