public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { LOG.debug("resolve resource"); LOG.debug("type: " + type); LOG.debug("namespace URI: " + namespaceURI); LOG.debug("public Id: " + publicId); LOG.debug("system Id: " + systemId); LOG.debug("base URI: " + baseURI); if (false == "http://www.w3.org/2001/XMLSchema".equals(type)) { throw new RuntimeException("unsupported type: " + type); } byte[] xsd = this.context.getXmlSchema(namespaceURI); if (null != xsd) { SignatureServiceLSInput lsInput = new SignatureServiceLSInput(xsd, publicId, systemId, baseURI); return lsInput; } throw new RuntimeException("unsupported namespace: " + namespaceURI); /* * Cannot return null here, else the system starts downloading the file. */ } }
public void checkIncomingDocument(byte[] document) throws Exception { LOG.debug("checking incoming document"); ByteArrayInputStream documentInputStream = new ByteArrayInputStream( document); Document dom = this.documentBuilder.parse(documentInputStream); String namespace = dom.getDocumentElement().getNamespaceURI(); if (null == namespace) { LOG.debug("no namespace defined"); return; } byte[] xsd = this.context.getXmlSchema(namespace); if (null == xsd) { LOG.debug("no XML schema available for namespace: " + namespace); return; } LOG.debug("validating against XML schema: " + namespace); SchemaFactory schemaFactory = SchemaFactory .newInstance("http://www.w3.org/2001/XMLSchema"); schemaFactory .setResourceResolver(new SignatureServiceLSResourceResolver( this.context)); StreamSource schemaSource = new StreamSource(new ByteArrayInputStream( xsd)); Schema schema = schemaFactory.newSchema(schemaSource); Validator validator = schema.newValidator(); DOMSource domSource = new DOMSource(dom); validator.validate(domSource); }