Package org.apache.xml.security.transforms

Examples of org.apache.xml.security.transforms.TransformationException


   }

   private String canonicalize(Element element, Element method) throws TransformationException, InvalidCanonicalizerException, CanonicalizationException
   {
      if (method == null || ! method.getLocalName().equals("CanonicalizationMethod"))
         throw new TransformationException("CanonicalizationMethod expected!");

      String algorithm = method.getAttribute("Algorithm");
      if (algorithm == null || algorithm.length() == 0)
         throw new TransformationException("CanonicalizationMethod missing algorithm!");

      Canonicalizer canon = Canonicalizer.getInstance(algorithm);

      return new String(canon.canonicalizeSubtree(element, "#default"));
   }
View Full Code Here


         // Obtain the canonicalizer specified in the transformation parameters
         Element parameters = XMLUtils.selectNode(this._transformObject.getElement().getFirstChild(), Constants.WSSE_NS,
               "TransformationParameters", 0);
         if (parameters == null)
            throw new TransformationException("wsse:TransformationParameters expected!");

         Element method = Util.getFirstChildElement(parameters);
         String transformed = canonicalize(element, method);

         // Now WS-Security says we must augment the transformed output to ensure that there is
         // a default namespace
         int startTag = transformed.indexOf('<');
         int endTag = transformed.indexOf('>', startTag + 1);
         String within = transformed.substring(startTag + 1, endTag);
         if (! within.contains("xmlns="))
         {
            int insPos = within.indexOf(" ") + startTag + 1;
            transformed = new StringBuilder(transformed).insert(insPos, " xmlns=\"\"").toString();
         }

         return new XMLSignatureInput(transformed.getBytes());
      }
      catch (WSSecurityException e)
      {
         throw new TransformationException(e.getMessage(), e);
      }
      finally
      {
         ThreadLocalAssociation.localStrTransformAssoc().set(null);
      }
View Full Code Here

   }

   private String canonicalize(Element element, Element method) throws TransformationException, InvalidCanonicalizerException, CanonicalizationException
   {
      if (method == null || ! method.getLocalName().equals("CanonicalizationMethod"))
         throw new TransformationException("CanonicalizationMethod expected!");

      String algorithm = method.getAttribute("Algorithm");
      if (algorithm == null || algorithm.length() == 0)
         throw new TransformationException("CanonicalizationMethod missing algorithm!");

      Canonicalizer canon = Canonicalizer.getInstance(algorithm);

      return new String(canon.canonicalizeSubtree(element, "#default"));
   }
View Full Code Here

         // Obtain the canonicalizer specified in the transformation parameters
         Element parameters = XMLUtils.selectNode(this._transformObject.getElement().getFirstChild(), Constants.WSSE_NS,
               "TransformationParameters", 0);
         if (parameters == null)
            throw new TransformationException("wsse:TransformationParameters expected!");

         Element method = Util.getFirstChildElement(parameters);
         String transformed = canonicalize(element, method);

         // Now WS-Security says we must augment the transformed output to ensure that there is
         // a default namespace
         int startTag = transformed.indexOf('<');
         int endTag = transformed.indexOf('>', startTag + 1);
         String within = transformed.substring(startTag + 1, endTag);
         if (! within.contains("xmlns="))
         {
            int insPos = within.indexOf(" ") + startTag + 1;
            transformed = new StringBuilder(transformed).insert(insPos, " xmlns=\"\"").toString();
         }

         return new XMLSignatureInput(transformed.getBytes());
      }
      catch (WSSecurityException e)
      {
         throw new TransformationException(e.getMessage(), e);
      }
      finally
      {
         ThreadLocalAssociation.localStrTransformAssoc().set(null);
      }
View Full Code Here

                    transformObject.getElement().getFirstChild(), Constants._TAG_XPATH, 0);

            if (xpathElement == null) {
                Object exArgs[] = { "ds:XPath", "Transform" };

                throw new TransformationException("xml.WrongContent", exArgs);
            }
            Node xpathnode = xpathElement.getChildNodes().item(0);
            String str = XMLUtils.getStrFromNode(xpathnode);
            input.setNeedsToBeExpanded(needsCircumvent(str));
            if (xpathnode == null) {
                throw new DOMException(
                    DOMException.HIERARCHY_REQUEST_ERR, "Text must be in ds:Xpath"
                );
            }
           
            XPathAPI xpathAPIInstance = new XalanXPathAPI();
            if (!XalanXPathAPI.isInstalled()) {
                xpathAPIInstance = new JDKXPathAPI();
            }

            input.addNodeFilter(new XPathNodeFilter(xpathElement, xpathnode, str, xpathAPIInstance));
            input.setNodeSet(true);
            return input;
        } catch (DOMException ex) {
            throw new TransformationException("empty", ex);
        }
    }
View Full Code Here

                    XPath2FilterContainer._TAG_XPATH2
                );
            if (xpathElements.length == 0) {
                Object exArgs[] = { Transforms.TRANSFORM_XPATH2FILTER, "XPath" };

                throw new TransformationException("xml.WrongContent", exArgs);
            }

            Document inputDoc = null;
            if (input.getSubNode() != null) {  
                inputDoc = XMLUtils.getOwnerDocument(input.getSubNode());
            } else {
                inputDoc = XMLUtils.getOwnerDocument(input.getNodeSet());
            }

            for (int i = 0; i < xpathElements.length; i++) {
                Element xpathElement = xpathElements[i];
               
                XPath2FilterContainer xpathContainer =
                    XPath2FilterContainer.newInstance(xpathElement, input.getSourceURI());

                String str =
                    XMLUtils.getStrFromNode(xpathContainer.getXPathFilterTextNode());
               
                XPathAPI xpathAPIInstance = new XalanXPathAPI();
                if (!XalanXPathAPI.isInstalled()) {
                    xpathAPIInstance = new JDKXPathAPI();
                }
               
                NodeList subtreeRoots =
                    xpathAPIInstance.selectNodeList(
                        inputDoc,
                        xpathContainer.getXPathFilterTextNode(),
                        str,
                        xpathContainer.getElement());
                if (xpathContainer.isIntersect()) {
                    intersectNodes.add(subtreeRoots);
                } else if (xpathContainer.isSubtract()) {
                    subtractNodes.add(subtreeRoots);
                } else if (xpathContainer.isUnion()) {
                    unionNodes.add(subtreeRoots);
                }
            }

            input.addNodeFilter(
                new XPath2NodeFilter(unionNodes, subtractNodes, intersectNodes)
            );
            input.setNodeSet(true);
            return input;
        } catch (TransformerException ex) {
            throw new TransformationException("empty", ex);
        } catch (DOMException ex) {
            throw new TransformationException("empty", ex);
        } catch (CanonicalizationException ex) {
            throw new TransformationException("empty", ex);
        } catch (InvalidCanonicalizerException ex) {
            throw new TransformationException("empty", ex);
        } catch (XMLSecurityException ex) {
            throw new TransformationException("empty", ex);
        } catch (SAXException ex) {
            throw new TransformationException("empty", ex);
        } catch (IOException ex) {
            throw new TransformationException("empty", ex);
        } catch (ParserConfigurationException ex) {
            throw new TransformationException("empty", ex);
        }
    }
View Full Code Here

            new CachedXPathFuncHereAPI(input.getCachedXPathAPI().getCachedXPathAPI());        

         if (this.inputSet.size() == 0) {
            Object exArgs[] = { "input node set contains no nodes" };

            throw new TransformationException("empty", exArgs);
         }

         Element transformElement = this._transformObject.getElement();
         Document doc = transformElement.getOwnerDocument();

         // create the XPathFilterCHGPContainer so that we easily can read it out
         Element xpathElement = XMLUtils.selectNode(transformElement.getFirstChild(),
                Transforms.TRANSFORM_XPATHFILTERCHGP,
                XPathFilterCHGPContainer._TAG_XPATHCHGP,0);
           
         if (xpathElement == null) {
            Object exArgs[] = { "{" + TransformXPathFilterCHGP.implementedTransformURI + "}XPath",
                                "Transform" };

            throw new TransformationException("xml.WrongContent", exArgs);
         }

         XPathFilterCHGPContainer xpathContainer =
            XPathFilterCHGPContainer.getInstance(xpathElement,
                                                 input.getSourceURI());

         // get the document (root node) for the traversal
         Document inputDoc = null;

         {
            Iterator it = this.inputSet.iterator();

            if (it.hasNext()) {
               inputDoc = XMLUtils.getOwnerDocument((Node) it.next());
            }
         }

         {

            // 'tag' the include-but-search nodes
            Node includeButSearchCtxNode =
               xpathContainer.getHereContextNodeIncludeButSearch();
            NodeList includeButSearchNodes = null;

            if (includeButSearchCtxNode != null) {
               includeButSearchNodes = xPathFuncHereAPI.selectNodeList(doc,
                       includeButSearchCtxNode,CachedXPathFuncHereAPI.getStrFromNode(includeButSearchCtxNode),
                       xpathContainer.getElement());
            }

            this.includeSearchSet = nodeListToSet(includeButSearchNodes);
         }

         {

            // 'tag' the exclude-but-search nodes
            Node excludeButSearchCtxNode =
               xpathContainer.getHereContextNodeExcludeButSearch();
            NodeList excludeButSearchNodes = null;

            if (excludeButSearchCtxNode != null) {
               excludeButSearchNodes = xPathFuncHereAPI.selectNodeList(doc,
                       excludeButSearchCtxNode,
                       CachedXPathFuncHereAPI.getStrFromNode(excludeButSearchCtxNode),xpathContainer.getElement());
            }

            this.excludeSearchSet = nodeListToSet(excludeButSearchNodes);
         }

         {

            // 'tag' the exclude nodes
            Node excludeCtxNode = xpathContainer.getHereContextNodeExclude();
            NodeList excludeNodes = null;

            if (excludeCtxNode != null) {
               excludeNodes = xPathFuncHereAPI.selectNodeList(doc,
                       excludeCtxNode,
                       CachedXPathFuncHereAPI.getStrFromNode(excludeCtxNode),
                       xpathContainer.getElement());
            }

            this.excludeSet = nodeListToSet(excludeNodes);
         }

         if (xpathContainer.getIncludeSlashPolicy()
                 == XPathFilterCHGPContainer.IncludeSlash) {
            this.includeSearchSet.add(inputDoc);
         } else {
            this.excludeSearchSet.add(inputDoc);
         }

         // create empty set for results
         this.resultSet = new HashSet();

         {
            DocumentTraversal dt = ((DocumentTraversal) inputDoc);
            Node rootNode =  inputDoc;

            // we accept all nodes
            NodeFilter nodefilter = new AlwaysAcceptNodeFilter();
            TreeWalker treewalker = dt.createTreeWalker(rootNode,
                                                        NodeFilter.SHOW_ALL,
                                                        nodefilter, true);

            /* do the magic here
             *
             * We make a traversal of the full DOM tree and check all nodes
             * which are in the inputSet whether we put them into the resultSet.
             *
             */
            process(treewalker);
         }

         XMLSignatureInput result = new XMLSignatureInput(resultSet,
                                       input.getCachedXPathAPI());

         result.setSourceURI(input.getSourceURI());

         return result;
      } catch (TransformerException ex) {
         throw new TransformationException("empty", ex);
      } catch (DOMException ex) {
         throw new TransformationException("empty", ex);
      } catch (IOException ex) {
         throw new TransformationException("empty", ex);
      } catch (CanonicalizationException ex) {
         throw new TransformationException("empty", ex);
      } catch (InvalidCanonicalizerException ex) {
         throw new TransformationException("empty", ex);
      } catch (ParserConfigurationException ex) {
         throw new TransformationException("empty", ex);
      } catch (XMLSecurityException ex) {
         throw new TransformationException("empty", ex);
      } catch (SAXException ex) {
         throw new TransformationException("empty", ex);
      }
   }
View Full Code Here


         if (noOfSteps == 0) {
            Object exArgs[] = { Transforms.TRANSFORM_XPATH2FILTER, "XPath" };

            throw new TransformationException("xml.WrongContent", exArgs);
         }

         if (true) {
            _filterTypes.add(FUnion);

            // Set root = new HashSet(); root.add(inputDoc);
            HelperNodeList root = new HelperNodeList();

            root.appendChild(inputDoc);
            _filterNodes.add(root);
         }

         for (int i = 0; i < noOfSteps; i++) {
            Element xpathElement =XMLUtils.selectNode(
               this._transformObject.getElement().getFirstChild(),
                  XPath2FilterContainer.XPathFilter2NS,
                  XPath2FilterContainer._TAG_XPATH2,i);
            XPath2FilterContainer xpathContainer =
               XPath2FilterContainer.newInstance(xpathElement,
                                                   input.getSourceURI());

            if (xpathContainer.isIntersect()) {
               _filterTypes.add(FIntersect);
            } else if (xpathContainer.isSubtract()) {
               _filterTypes.add(FSubtract);
            } else if (xpathContainer.isUnion()) {
               _filterTypes.add(FUnion);
            } else {
               _filterTypes.add(null);
            }

            NodeList subtreeRoots = xPathFuncHereAPI.selectNodeList(inputDoc,
                                       xpathContainer.getXPathFilterTextNode(),
                                       CachedXPathFuncHereAPI.getStrFromNode(xpathContainer.getXPathFilterTextNode()),
                                       xpathContainer.getElement());

            // _filterNodes.add(XMLUtils.convertNodelistToSet(subtreeRoots));
            _filterNodes.add(subtreeRoots);
         }

         this._F = new HashSet();
         this._ancestors = new ArrayList();

         this.traversal(inputDoc);

         Set resultSet = new HashSet();
         Iterator it = this._inputSet.iterator();
         while (it.hasNext()) {
            Object n = it.next();
            if (this._F.contains(n)) {
               resultSet.add(n);
            }
         }

         XMLSignatureInput result = new XMLSignatureInput(resultSet,
                                       input.getCachedXPathAPI());


         result.setSourceURI(input.getSourceURI());

         return result;
      } catch (TransformerException ex) {
         throw new TransformationException("empty", ex);
      } catch (DOMException ex) {
         throw new TransformationException("empty", ex);
      } catch (IOException ex) {
         throw new TransformationException("empty", ex);
      } catch (CanonicalizationException ex) {
         throw new TransformationException("empty", ex);
      } catch (InvalidCanonicalizerException ex) {
         throw new TransformationException("empty", ex);
      } catch (ParserConfigurationException ex) {
         throw new TransformationException("empty", ex);
      } catch (XMLSecurityException ex) {
         throw new TransformationException("empty", ex);
      } catch (SAXException ex) {
         throw new TransformationException("empty", ex);
      }
   }
View Full Code Here

            traverseElement(rootNode,sb);
            byte[] decodedBytes = Base64.decode(sb.toString());
     
            return new XMLSignatureInput(decodedBytes);
      } catch (ParserConfigurationException e) {
        throw new TransformationException("c14n.Canonicalizer.Exception",e);
      } catch (SAXException e) {
        throw new TransformationException("SAX exception", e);
      }     
  } catch (Base64DecodingException e) {
        throw new TransformationException("Base64Decoding", e);
  }
   }
View Full Code Here

            new CachedXPathAPI(input.getCachedXPathAPI().getCachedXPathAPI());

         if (inputSet.size() == 0) {
            Object exArgs[] = { "input node set contains no nodes" };

            throw new TransformationException("empty", exArgs);
         }

         Element xpathElement =XMLUtils.selectNode(
            this._transformObject.getElement().getFirstChild(),
               XPath2FilterContainer04.XPathFilter2NS,
               XPath2FilterContainer04._TAG_XPATH2,0);

         if (xpathElement == null) {
            Object exArgs[] = { "dsig-xpath:XPath", "Transform" };

            throw new TransformationException("xml.WrongContent", exArgs);
         }

         XPath2FilterContainer04 xpathContainer =
            XPath2FilterContainer04.newInstance(xpathElement,
                                              input.getSourceURI());
         Document doc = this._transformObject.getElement().getOwnerDocument();
         NodeList subtreeRoots = xPathFuncHereAPI.selectNodeList(doc,
                                    xpathContainer.getXPathFilterTextNode(),
                                    CachedXPathFuncHereAPI.getStrFromNode(xpathContainer.getXPathFilterTextNode()),
                                    xpathContainer.getElement());
         if (log.isDebugEnabled())
           log.debug("subtreeRoots contains " + subtreeRoots.getLength()
                   + " nodes");

         /*
          * foreach subtree
          */
         Set selectedNodes = new HashSet();

         for (int i = 0; i < subtreeRoots.getLength(); i++) {
            Node currentRootNode = subtreeRoots.item(i);
            int currentRootNodeType = currentRootNode.getNodeType();

            if ((currentRootNodeType == Node.ELEMENT_NODE)
                    || (currentRootNodeType == Node.DOCUMENT_NODE)) {
               NodeList nodesInSubtree =
                  myXPathAPI
                     .selectNodeList(currentRootNode, Canonicalizer
                        .XPATH_C14N_WITH_COMMENTS_SINGLE_NODE);
               int jMax = nodesInSubtree.getLength();

               for (int j = 0; j < jMax; j++) {
                  selectedNodes.add(nodesInSubtree.item(j));
               }
            } else if ((currentRootNodeType == Node.ATTRIBUTE_NODE)
                       || (currentRootNodeType == Node.TEXT_NODE)
                       || (currentRootNodeType == Node.CDATA_SECTION_NODE)
                       || (currentRootNodeType
                           == Node.PROCESSING_INSTRUCTION_NODE)) {
               selectedNodes.add(currentRootNode);
            } else {
               throw new RuntimeException("unknown node type: " + currentRootNodeType + " " + currentRootNode);
            }
         }
         if (log.isDebugEnabled())
           log.debug("selection process identified " + selectedNodes.size()
                   + " nodes");

         Set resultNodes = new HashSet();

         if (xpathContainer.isIntersect()) {
            Iterator inputSetIterator = inputSet.iterator();

            while (inputSetIterator.hasNext()) {
               Node currentInputNode = (Node) inputSetIterator.next();

               // if the input node is selected, include it in the output
               if (selectedNodes.contains(currentInputNode)) {
                  resultNodes.add(currentInputNode);
               }
            }
         } else if (xpathContainer.isSubtract()) {
            Iterator inputSetIterator = inputSet.iterator();

            while (inputSetIterator.hasNext()) {
               Node currentInputNode = (Node) inputSetIterator.next();

               // if the input node is selected, do not include it
               // otherwise, include it
               if (!selectedNodes.contains(currentInputNode)) {
                  resultNodes.add(currentInputNode);
               }
            }
         } else if (xpathContainer.isUnion()) {
            Iterator inputSetIterator = inputSet.iterator();

            while (inputSetIterator.hasNext()) {
               Node currentInputNode = (Node) inputSetIterator.next();

               // add all input nodes to the result
               resultNodes.add(currentInputNode);
            }

            Iterator selectedSetIterator = selectedNodes.iterator();

            while (selectedSetIterator.hasNext()) {
               Node currentSelectedNode = (Node) selectedSetIterator.next();

               // add all selected nodes to the result
               resultNodes.add(currentSelectedNode);
            }
         } else {
            throw new TransformationException("empty");
         }

         XMLSignatureInput result = new XMLSignatureInput(resultNodes,
                                       input.getCachedXPathAPI());

         result.setSourceURI(input.getSourceURI());

         return result;
      } catch (TransformerException ex) {
         throw new TransformationException("empty", ex);
      } catch (DOMException ex) {
         throw new TransformationException("empty", ex);
      } catch (IOException ex) {
         throw new TransformationException("empty", ex);
      } catch (CanonicalizationException ex) {
         throw new TransformationException("empty", ex);
      } catch (InvalidCanonicalizerException ex) {
         throw new TransformationException("empty", ex);
      } catch (ParserConfigurationException ex) {
         throw new TransformationException("empty", ex);
      } catch (XMLSecurityException ex) {
         throw new TransformationException("empty", ex);
      } catch (SAXException ex) {
         throw new TransformationException("empty", ex);
      }
   }
View Full Code Here

TOP

Related Classes of org.apache.xml.security.transforms.TransformationException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.