use of javax.xml.crypto.URIReferenceException in project poi by apache.
the class OOXMLURIDereferencer method dereference.
public Data dereference(URIReference uriReference, XMLCryptoContext context) throws URIReferenceException {
if (baseUriDereferencer == null) {
baseUriDereferencer = signatureConfig.getSignatureFactory().getURIDereferencer();
}
if (null == uriReference) {
throw new NullPointerException("URIReference cannot be null");
}
if (null == context) {
throw new NullPointerException("XMLCrytoContext cannot be null");
}
URI uri;
try {
uri = new URI(uriReference.getURI());
} catch (URISyntaxException e) {
throw new URIReferenceException("could not URL decode the uri: " + uriReference.getURI(), e);
}
PackagePart part = findPart(uri);
if (part == null) {
LOG.log(POILogger.DEBUG, "cannot resolve, delegating to base DOM URI dereferencer", uri);
return this.baseUriDereferencer.dereference(uriReference, context);
}
InputStream dataStream;
try {
dataStream = part.getInputStream();
// workaround for office 2007 pretty-printed .rels files
if (part.getPartName().toString().endsWith(".rels")) {
// although xmlsec has an option to ignore line breaks, currently this
// only affects .rels files, so we only modify these
// http://stackoverflow.com/questions/4728300
ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (int ch; (ch = dataStream.read()) != -1; ) {
if (ch == 10 || ch == 13)
continue;
bos.write(ch);
}
dataStream = new ByteArrayInputStream(bos.toByteArray());
}
} catch (IOException e) {
throw new URIReferenceException("I/O error: " + e.getMessage(), e);
}
return new OctetStreamData(dataStream, uri.toString(), null);
}
use of javax.xml.crypto.URIReferenceException in project santuario-java by apache.
the class DOMRetrievalMethod method dereferenceAsXMLStructure.
public XMLStructure dereferenceAsXMLStructure(XMLCryptoContext context) throws URIReferenceException {
DocumentBuilder db = null;
boolean secVal = Utils.secureValidation(context);
ApacheData data = (ApacheData) dereference(context);
try (InputStream is = new ByteArrayInputStream(data.getXMLSignatureInput().getBytes())) {
db = XMLUtils.createDocumentBuilder(false, secVal);
Document doc = db.parse(is);
Element kiElem = doc.getDocumentElement();
if (kiElem.getLocalName().equals("X509Data") && XMLSignature.XMLNS.equals(kiElem.getNamespaceURI())) {
return new DOMX509Data(kiElem);
} else {
// unsupported
return null;
}
} catch (Exception e) {
throw new URIReferenceException(e);
} finally {
if (db != null) {
XMLUtils.repoolDocumentBuilder(db);
}
}
}
use of javax.xml.crypto.URIReferenceException in project santuario-java by apache.
the class DOMRetrievalMethod method dereference.
@Override
public Data dereference(XMLCryptoContext context) throws URIReferenceException {
if (context == null) {
throw new NullPointerException("context cannot be null");
}
/*
* If URIDereferencer is specified in context; use it, otherwise use
* built-in.
*/
URIDereferencer deref = context.getURIDereferencer();
if (deref == null) {
deref = DOMURIDereferencer.INSTANCE;
}
Data data = deref.dereference(this, context);
// pass dereferenced data through Transforms
try {
for (Transform transform : transforms) {
data = transform.transform(data, context);
}
} catch (Exception e) {
throw new URIReferenceException(e);
}
// guard against RetrievalMethod loops
if (data instanceof NodeSetData && Utils.secureValidation(context)) {
NodeSetData nsd = (NodeSetData) data;
Iterator<?> i = nsd.iterator();
if (i.hasNext()) {
Node root = (Node) i.next();
if ("RetrievalMethod".equals(root.getLocalName())) {
throw new URIReferenceException("It is forbidden to have one RetrievalMethod point " + "to another when secure validation is enabled");
}
}
}
return data;
}
Aggregations