Example usage for javax.xml.crypto URIReference getURI

List of usage examples for javax.xml.crypto URIReference getURI

Introduction

In this page you can find the example usage for javax.xml.crypto URIReference getURI.

Prototype

String getURI();

Source Link

Document

Returns the URI of the referenced data object.

Usage

From source file:be.fedict.eid.applet.service.signer.asic.ASiCURIDereferencer.java

public Data dereference(URIReference uriReference, XMLCryptoContext context) throws URIReferenceException {
    if (null == uriReference) {
        throw new URIReferenceException("URIReference cannot be null");
    }/*from w  ww.  j a  v  a  2 s . com*/
    if (null == context) {
        throw new URIReferenceException("XMLCrytoContext cannot be null");
    }

    String uri = uriReference.getURI();
    try {
        uri = URLDecoder.decode(uri, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        LOG.warn("could not URL decode the uri: " + uri);
    }
    LOG.debug("dereference: " + uri);

    InputStream zipInputStream;
    if (null != this.tmpFile) {
        try {
            zipInputStream = new FileInputStream(this.tmpFile);
        } catch (FileNotFoundException e) {
            throw new URIReferenceException("file not found error: " + e.getMessage(), e);
        }
    } else {
        zipInputStream = new ByteArrayInputStream(this.data);
    }
    InputStream dataInputStream;
    try {
        dataInputStream = ODFUtil.findDataInputStream(zipInputStream, uri);
    } catch (IOException e) {
        throw new URIReferenceException("I/O error: " + e.getMessage(), e);
    }
    if (null == dataInputStream) {
        return this.baseUriDereferener.dereference(uriReference, context);
    }
    return new OctetStreamData(dataInputStream, uri, null);
}

From source file:be.fedict.eid.applet.service.signer.ooxml.OOXMLURIDereferencer.java

public Data dereference(URIReference uriReference, XMLCryptoContext context) throws URIReferenceException {
    if (null == uriReference) {
        throw new NullPointerException("URIReference cannot be null");
    }//from   w w w.  j av a2  s  . c o m
    if (null == context) {
        throw new NullPointerException("XMLCrytoContext cannot be null");
    }

    String uri = uriReference.getURI();
    try {
        uri = URLDecoder.decode(uri, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        LOG.warn("could not URL decode the uri: " + uri);
    }
    LOG.debug("dereference: " + uri);
    try {
        InputStream dataInputStream = findDataInputStream(uri);
        if (null == dataInputStream) {
            LOG.debug("cannot resolve, delegating to base DOM URI dereferencer: " + uri);
            return this.baseUriDereferencer.dereference(uriReference, context);
        }
        return new OctetStreamData(dataInputStream, uri, null);
    } catch (IOException e) {
        throw new URIReferenceException("I/O error: " + e.getMessage(), e);
    }
}

From source file:be.fedict.eid.applet.service.signer.odf.ODFURIDereferencer.java

public Data dereference(URIReference uriReference, XMLCryptoContext context) throws URIReferenceException {
    if (null == uriReference) {
        throw new NullPointerException("URIReference cannot be null");
    }/*from   ww  w . j  a va  2  s . c  o m*/
    if (null == context) {
        throw new NullPointerException("XMLCrytoContext cannot be null");
    }

    String uri = uriReference.getURI();
    try {
        uri = URLDecoder.decode(uri, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        LOG.warn("could not URL decode the uri: " + uri);
    }
    LOG.debug("dereference: " + uri);
    try {
        InputStream dataInputStream = findDataInputStream(uri);
        if (null == dataInputStream) {
            LOG.debug("cannot resolve, delegating to base DOM URI dereferener: " + uri);
            return this.baseUriDereferener.dereference(uriReference, context);
        }
        if (uri.endsWith(".xml")) {
            /*
             * We parse the XML ourselves as we might need to resolve MathML
             * DTD.
             */
            byte[] data = IOUtils.toByteArray(dataInputStream);
            if (0 == data.length) {
                return new OctetStreamData(dataInputStream, uri, null);
            }
            Document document = documentBuilder.parse(new ByteArrayInputStream(data));
            XMLSignatureInput xmlSignatureInput = new XMLSignatureInput(document);
            ApacheNodeSetData apacheNodeSetData = new ApacheNodeSetData(xmlSignatureInput);
            return apacheNodeSetData;
        }
        return new OctetStreamData(dataInputStream, uri, null);
    } catch (IOException e) {
        throw new URIReferenceException("I/O error: " + e.getMessage(), e);
    } catch (SAXException e) {
        throw new URIReferenceException("SAX error: " + e.getMessage(), e);
    }
}

From source file:no.difi.sdp.client.asice.signature.CreateSignatureTest.java

private boolean verify_signature(final Signature signature2) {
    try {//  www  .  j  av a  2 s .  co m
        signature2.getBytes();
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        fac.setNamespaceAware(true);
        DocumentBuilder builder = fac.newDocumentBuilder();
        final Document doc = builder.parse(new ByteArrayInputStream(signature2.getBytes()));
        //System.err.println(new String(signature2.getBytes()));
        NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
        DOMValidateContext valContext = new DOMValidateContext(
                noekkelpar.getSertifikat().getX509Certificate().getPublicKey(), nl.item(0));
        valContext.setURIDereferencer(new URIDereferencer() {
            @Override
            public Data dereference(final URIReference uriReference, final XMLCryptoContext context)
                    throws URIReferenceException {
                //System.out.println("$$$$ " + uriReference.getURI());
                for (AsicEAttachable file : files) {
                    if (file.getFileName().equals(uriReference.getURI().toString())) {
                        return new OctetStreamData(new ByteArrayInputStream(file.getBytes()));
                    }
                }
                uriReference.getURI().toString().replace("#", "");
                Node element = doc.getElementsByTagName("SignedProperties").item(0);
                return new DOMSubTreeData(element, false);

            }
        });
        XMLSignatureFactory fact = XMLSignatureFactory.getInstance("DOM");
        XMLSignature signature = fact.unmarshalXMLSignature(valContext);
        boolean coreValidity = signature.validate(valContext);
        if (coreValidity == false) {
            System.err.println("Signature failed core validation");
            boolean sv = signature.getSignatureValue().validate(valContext);
            System.out.println("signature validation status: " + sv);
            if (sv == false) {
                // Check the validation status of each Reference.
                Iterator i = signature.getSignedInfo().getReferences().iterator();
                for (int j = 0; i.hasNext(); j++) {
                    boolean refValid = ((javax.xml.crypto.dsig.Reference) i.next()).validate(valContext);
                    System.out.println("ref[" + j + "] validity status: " + refValid);
                }
            }
        }
        return coreValidity;
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
        return false;
    }
}