List of usage examples for org.w3c.dom Attr getNodeValue
public String getNodeValue() throws DOMException;
From source file:org.apache.xml.security.samples.utils.resolver.OfflineResolver.java
/** * We resolve http URIs <I>without</I> fragment... * * @param uri/*from ww w . j a v a2 s. c o m*/ * @param BaseURI * */ public boolean engineCanResolve(Attr uri, String BaseURI) { String uriNodeValue = uri.getNodeValue(); if (uriNodeValue.equals("") || uriNodeValue.startsWith("#")) { return false; } try { URI uriNew = new URI(new URI(BaseURI), uri.getNodeValue()); if (uriNew.getScheme().equals("http")) { log.debug("I state that I can resolve " + uriNew.toString()); return true; } log.debug("I state that I can't resolve " + uriNew.toString()); } catch (URI.MalformedURIException ex) { } return false; }
From source file:org.apache.xml.security.signature.Reference.java
/** * Returns the XMLSignatureInput which is created by de-referencing the URI attribute. * @return the XMLSignatureInput of the source of this reference * @throws ReferenceNotInitializedException If the resolver found any * problem resolving the reference/*from w w w . j a va 2 s . c o m*/ */ public XMLSignatureInput getContentsBeforeTransformation() throws ReferenceNotInitializedException { try { Attr URIAttr = this.constructionElement.getAttributeNodeNS(null, Constants._ATT_URI); String URI; if (URIAttr == null) { URI = null; } else { URI = URIAttr.getNodeValue(); } ResourceResolver resolver = ResourceResolver.getInstance(URIAttr, this.baseURI, this.manifest.getPerManifestResolvers()); if (resolver == null) { Object exArgs[] = { URI }; throw new ReferenceNotInitializedException("signature.Verification.Reference.NoInput", exArgs); } resolver.addProperties(this.manifest.getResolverProperties()); return resolver.resolve(URIAttr, this.baseURI); } catch (ResourceResolverException ex) { throw new ReferenceNotInitializedException("empty", ex); } catch (XMLSecurityException ex) { throw new ReferenceNotInitializedException("empty", ex); } }
From source file:org.apache.xml.security.test.signature.XPointerResourceResolver.java
public boolean engineCanResolve(Attr uri, String BaseURI) { String v = uri.getNodeValue(); if (v == null || v.length() <= 0) return false; if (v.charAt(0) != '#') return false; String xpURI;/*from w w w .ja v a2s . c o m*/ try { xpURI = URLDecoder.decode(v, "utf-8"); } catch (UnsupportedEncodingException e) { log.warn("utf-8 not a valid encoding", e); return false; } String parts[] = xpURI.substring(1).split("\\s"); // plain ID reference. if (parts.length == 1 && !parts[0].startsWith(XNS_OPEN)) return true; int i = 0; for (; i < parts.length - 1; ++i) if (!parts[i].endsWith(")") || !parts[i].startsWith(XNS_OPEN)) return false; if (!parts[i].endsWith(")") || !parts[i].startsWith(XP_OPEN)) return false; log.debug("xpURI=" + xpURI); log.debug("BaseURI=" + BaseURI); return true; }
From source file:org.apache.xml.security.test.signature.XPointerResourceResolver.java
public XMLSignatureInput engineResolve(Attr uri, String BaseURI) throws ResourceResolverException { String v = uri.getNodeValue(); if (v.charAt(0) != '#') return null; String xpURI;/*from w w w.ja v a2s. c o m*/ try { xpURI = URLDecoder.decode(v, "utf-8"); } catch (UnsupportedEncodingException e) { log.warn("utf-8 not a valid encoding", e); return null; } String parts[] = xpURI.substring(1).split("\\s"); int i = 0; XPointerPrefixResolver nsContext = null; if (parts.length > 1) { nsContext = new XPointerPrefixResolver(this.baseNode); for (; i < parts.length - 1; ++i) { if (!parts[i].endsWith(")") || !parts[i].startsWith(XNS_OPEN)) return null; String mapping = parts[i].substring(XNS_OPEN.length(), parts[i].length() - 1); int pos = mapping.indexOf('='); if (pos <= 0 || pos >= mapping.length() - 1) throw new ResourceResolverException("malformed namespace part of XPointer expression", uri, BaseURI); nsContext.addExtraPrefix(mapping.substring(0, pos), mapping.substring(pos + 1)); } } try { Node node = null; NodeList nodes = null; // plain ID reference. if (i == 0 && !parts[i].startsWith(XP_OPEN)) { node = this.baseNode.getOwnerDocument().getElementById(parts[i]); } else { if (!parts[i].endsWith(")") || !parts[i].startsWith(XP_OPEN)) return null; String xpathExpr = parts[i].substring(XP_OPEN.length(), parts[i].length() - 1); XObject xo; if (nsContext != null) xo = XPathAPI.eval(this.baseNode, xpathExpr, nsContext); else xo = XPathAPI.eval(this.baseNode, xpathExpr); if (!(xo instanceof NodeSequence)) return null; nodes = ((NodeSequence) xo).nodelist(); if (nodes.getLength() == 0) return null; if (nodes.getLength() == 1) node = nodes.item(0); } XMLSignatureInput result = null; if (node != null) { result = new XMLSignatureInput(node); } else if (nodes != null) { Set nodeSet = new HashSet(nodes.getLength()); for (int j = 0; j < nodes.getLength(); ++j) { nodeSet.add(nodes.item(j)); } result = new XMLSignatureInput(nodeSet); } else return null; result.setMIMEType("text/xml"); result.setExcludeComments(true); result.setSourceURI((BaseURI != null) ? BaseURI.concat(v) : v); return result; } catch (TransformerException e) { throw new ResourceResolverException("TransformerException inside XPointer expression", e, uri, BaseURI); } }
From source file:org.apache.xml.security.utils.ElementProxy.java
/** * Adds an xmlns: definition to the Element. This can be called as follows: * * <PRE>/* ww w. j av a2s.c o m*/ * // set namespace with ds prefix * xpathContainer.setXPathNamespaceContext("ds", "http://www.w3.org/2000/09/xmldsig#"); * xpathContainer.setXPathNamespaceContext("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#"); * </PRE> * * @param prefix * @param uri * @throws XMLSecurityException */ public void setXPathNamespaceContext(String prefix, String uri) throws XMLSecurityException { String ns; if ((prefix == null) || (prefix.length() == 0)) { throw new XMLSecurityException("defaultNamespaceCannotBeSetHere"); } else if (prefix.equals("xmlns")) { throw new XMLSecurityException("defaultNamespaceCannotBeSetHere"); } else if (prefix.startsWith("xmlns:")) { ns = prefix;//"xmlns:" + prefix.substring("xmlns:".length()); } else { ns = "xmlns:" + prefix; } Attr a = this.constructionElement.getAttributeNodeNS(Constants.NamespaceSpecNS, ns); if (a != null) { if (!a.getNodeValue().equals(uri)) { Object exArgs[] = { ns, this.constructionElement.getAttributeNS(null, ns) }; throw new XMLSecurityException("namespacePrefixAlreadyUsedByOtherURI", exArgs); } return; } this.constructionElement.setAttributeNS(Constants.NamespaceSpecNS, ns, uri); }
From source file:org.apache.xml.security.utils.IdResolver.java
/** * Method registerElementById/*from w w w. j a v a 2 s . c om*/ * * @param element the element to register * @param id the ID attribute */ public static void registerElementById(Element element, Attr id) { IdResolver.registerElementById(element, id.getNodeValue()); }
From source file:org.apache.xml.security.utils.IdResolver.java
public static int isElement(Element el, String id, Element[] els) { if (!el.hasAttributes()) { return 0; }/* w ww . j a v a 2 s .c o m*/ NamedNodeMap ns = el.getAttributes(); int elementIndex = names.indexOf(el.getNamespaceURI()); elementIndex = (elementIndex < 0) ? namesLength : elementIndex; for (int length = ns.getLength(), i = 0; i < length; i++) { Attr n = (Attr) ns.item(i); String s = n.getNamespaceURI(); int index = s == null ? elementIndex : names.indexOf(n.getNamespaceURI()); index = (index < 0) ? namesLength : index; String name = n.getLocalName(); if (name == null) { name = n.getName(); } if (name.length() > 2) { continue; } String value = n.getNodeValue(); if (name.charAt(0) == 'I') { char ch = name.charAt(1); if (ch == 'd' && value.equals(id)) { els[index] = el; if (index == 0) { return 1; } } else if (ch == 'D' && value.endsWith(id)) { if (index != 3) { index = namesLength; } els[index] = el; } } else if ("id".equals(name) && value.equals(id)) { if (index != 2) { index = namesLength; } els[index] = el; } } //For an element namespace search for importants if ((elementIndex == 3) && (el.getAttribute("OriginalRequestID").equals(id) || el.getAttribute("RequestID").equals(id) || el.getAttribute("ResponseID").equals(id))) { els[3] = el; } else if ((elementIndex == 4) && (el.getAttribute("AssertionID").equals(id))) { els[4] = el; } else if ((elementIndex == 5) && (el.getAttribute("RequestID").equals(id) || el.getAttribute("ResponseID").equals(id))) { els[5] = el; } return 0; }
From source file:org.apache.xml.security.utils.resolver.implementations.ResolverDirectHTTP.java
/** * Method resolve/* w ww .ja va2 s .c o m*/ * * @param uri * @param baseURI * * @throws ResourceResolverException * @return * $todo$ calculate the correct URI from the attribute and the baseURI */ public XMLSignatureInput engineResolve(Attr uri, String baseURI) throws ResourceResolverException { try { boolean useProxy = false; String proxyHost = engineGetProperty(ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpProxyHost]); String proxyPort = engineGetProperty(ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpProxyPort]); if ((proxyHost != null) && (proxyPort != null)) { useProxy = true; } String oldProxySet = null; String oldProxyHost = null; String oldProxyPort = null; // switch on proxy usage if (useProxy) { if (log.isDebugEnabled()) { log.debug("Use of HTTP proxy enabled: " + proxyHost + ":" + proxyPort); } oldProxySet = System.getProperty("http.proxySet"); oldProxyHost = System.getProperty("http.proxyHost"); oldProxyPort = System.getProperty("http.proxyPort"); System.setProperty("http.proxySet", "true"); System.setProperty("http.proxyHost", proxyHost); System.setProperty("http.proxyPort", proxyPort); } boolean switchBackProxy = ((oldProxySet != null) && (oldProxyHost != null) && (oldProxyPort != null)); // calculate new URI URI uriNew = null; try { uriNew = getNewURI(uri.getNodeValue(), baseURI); } catch (URISyntaxException ex) { throw new ResourceResolverException("generic.EmptyMessage", ex, uri, baseURI); } URL url = uriNew.toURL(); URLConnection urlConnection = url.openConnection(); { // set proxy pass String proxyUser = engineGetProperty( ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpProxyUser]); String proxyPass = engineGetProperty( ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpProxyPass]); if ((proxyUser != null) && (proxyPass != null)) { String password = proxyUser + ":" + proxyPass; String encodedPassword = Base64.encode(password.getBytes("ISO-8859-1")); // or was it Proxy-Authenticate ? urlConnection.setRequestProperty("Proxy-Authorization", encodedPassword); } } { // check if Basic authentication is required String auth = urlConnection.getHeaderField("WWW-Authenticate"); if (auth != null && auth.startsWith("Basic")) { // do http basic authentication String user = engineGetProperty( ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpBasicUser]); String pass = engineGetProperty( ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpBasicPass]); if ((user != null) && (pass != null)) { urlConnection = url.openConnection(); String password = user + ":" + pass; String encodedPassword = Base64.encode(password.getBytes("ISO-8859-1")); // set authentication property in the http header urlConnection.setRequestProperty("Authorization", "Basic " + encodedPassword); } } } String mimeType = urlConnection.getHeaderField("Content-Type"); InputStream inputStream = urlConnection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte buf[] = new byte[4096]; int read = 0; int summarized = 0; while ((read = inputStream.read(buf)) >= 0) { baos.write(buf, 0, read); summarized += read; } if (log.isDebugEnabled()) { log.debug("Fetched " + summarized + " bytes from URI " + uriNew.toString()); } XMLSignatureInput result = new XMLSignatureInput(baos.toByteArray()); result.setSourceURI(uriNew.toString()); result.setMIMEType(mimeType); // switch off proxy usage if (useProxy && switchBackProxy) { System.setProperty("http.proxySet", oldProxySet); System.setProperty("http.proxyHost", oldProxyHost); System.setProperty("http.proxyPort", oldProxyPort); } return result; } catch (MalformedURLException ex) { throw new ResourceResolverException("generic.EmptyMessage", ex, uri, baseURI); } catch (IOException ex) { throw new ResourceResolverException("generic.EmptyMessage", ex, uri, baseURI); } }
From source file:org.apache.xml.security.utils.resolver.implementations.ResolverDirectHTTP.java
/** * We resolve http URIs <I>without</I> fragment... * * @param uri/*from www.ja v a 2 s . c o m*/ * @param baseURI * @return true if can be resolved */ public boolean engineCanResolve(Attr uri, String baseURI) { if (uri == null) { if (log.isDebugEnabled()) { log.debug("quick fail, uri == null"); } return false; } String uriNodeValue = uri.getNodeValue(); if (uriNodeValue.equals("") || (uriNodeValue.charAt(0) == '#')) { if (log.isDebugEnabled()) { log.debug("quick fail for empty URIs and local ones"); } return false; } if (log.isDebugEnabled()) { log.debug("I was asked whether I can resolve " + uriNodeValue); } if (uriNodeValue.startsWith("http:") || (baseURI != null && baseURI.startsWith("http:"))) { if (log.isDebugEnabled()) { log.debug("I state that I can resolve " + uriNodeValue); } return true; } if (log.isDebugEnabled()) { log.debug("I state that I can't resolve " + uriNodeValue); } return false; }
From source file:org.apache.xml.security.utils.resolver.implementations.ResolverFragment.java
/** * Method engineResolve/*ww w.jav a2 s .c o m*/ * * @inheritDoc * @param uri * @param baseURI */ public XMLSignatureInput engineResolve(Attr uri, String baseURI) throws ResourceResolverException { String uriNodeValue = uri.getNodeValue(); Document doc = uri.getOwnerElement().getOwnerDocument(); Node selectedElem = null; if (uriNodeValue.equals("")) { /* * Identifies the node-set (minus any comment nodes) of the XML * resource containing the signature */ if (log.isDebugEnabled()) { log.debug("ResolverFragment with empty URI (means complete document)"); } selectedElem = doc; } else { /* * URI="#chapter1" * Identifies a node-set containing the element with ID attribute * value 'chapter1' of the XML resource containing the signature. * XML Signature (and its applications) modify this node-set to * include the element plus all descendents including namespaces and * attributes -- but not comments. */ String id = uriNodeValue.substring(1); selectedElem = IdResolver.getElementById(doc, id); if (selectedElem == null) { Object exArgs[] = { id }; throw new ResourceResolverException("signature.Verification.MissingID", exArgs, uri, baseURI); } if (log.isDebugEnabled()) { log.debug("Try to catch an Element with ID " + id + " and Element was " + selectedElem); } } XMLSignatureInput result = new XMLSignatureInput(selectedElem); result.setExcludeComments(true); result.setMIMEType("text/xml"); if (baseURI != null && baseURI.length() > 0) { result.setSourceURI(baseURI.concat(uri.getNodeValue())); } else { result.setSourceURI(uri.getNodeValue()); } return result; }