WADLUtilities.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */



  20. package org.openspcoop2.utils.wadl;

  21. import java.io.File;
  22. import java.net.URI;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.jvnet.ws.wadl.HTTPMethods;
  27. import org.jvnet.ws.wadl.ast.ApplicationNode;
  28. import org.jvnet.ws.wadl.ast.MethodNode;
  29. import org.jvnet.ws.wadl.ast.ResourceNode;
  30. import org.openspcoop2.utils.resources.FileSystemUtilities;
  31. import org.openspcoop2.utils.transport.http.HttpRequestMethod;
  32. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  33. import org.slf4j.Logger;
  34. import org.w3c.dom.Document;
  35. import org.w3c.dom.Element;
  36. import org.w3c.dom.Node;
  37. import org.w3c.dom.NodeList;




  38. /**
  39.  * Utilities per i WADL
  40.  *
  41.  *
  42.  * @author Poli Andrea (apoli@link.it)
  43.  * @author $Author$
  44.  * @version $Rev$, $Date$
  45.  */


  46. public class WADLUtilities {


  47.     //  public static WADLUtilities getInstance(){
  48.     //      return new WADLUtilities(null); // senza xmlUtils
  49.     //  }
  50.     public static WADLUtilities getInstance(AbstractXMLUtils xmlUtils){
  51.         return new WADLUtilities(xmlUtils);
  52.     }




  53.     private AbstractXMLUtils xmlUtils = null;
  54.     public WADLUtilities(AbstractXMLUtils xmlUtils){
  55.         this.xmlUtils = xmlUtils;
  56.     }






  57.     /* ---------------- METODI STATICI PUBBLICI ------------------------- */

  58.     /** WADLReader */
  59.     public WADLReader getWADLReader(Logger log, boolean verbose, boolean processInclude, boolean processInlineSchema){
  60.         return new WADLReader(log, this.xmlUtils, verbose, processInclude,processInlineSchema);
  61.     }



  62.     // IS WADL
  63.     public boolean isWADL(byte[]wadl) throws org.openspcoop2.utils.wadl.WADLException {
  64.         try{
  65.             if(this.xmlUtils==null){
  66.                 throw new Exception("XMLUtils not initialized in WADLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  67.             }

  68.             if(!this.xmlUtils.isDocument(wadl)){
  69.                 return false;
  70.             }
  71.             Document docXML = this.xmlUtils.newDocument(wadl);
  72.             Element elemXML = docXML.getDocumentElement();
  73.             return isWADL(elemXML);
  74.         }catch(Exception e){
  75.             throw new org.openspcoop2.utils.wadl.WADLException(e.getMessage(),e);
  76.         }
  77.     }
  78.     public boolean isWADL(Document wadl) throws org.openspcoop2.utils.wadl.WADLException {
  79.         Element elemXML = wadl.getDocumentElement();
  80.         return isWADL(elemXML);
  81.     }

  82.     public boolean isWADL(Element wadl) throws org.openspcoop2.utils.wadl.WADLException {
  83.         return isWADL((Node)wadl);
  84.     }
  85.     public boolean isWADL(Node wadl) throws org.openspcoop2.utils.wadl.WADLException {
  86.         try{
  87.             if(wadl == null){
  88.                 throw new Exception("Documento wadl da verificare non definito");
  89.             }
  90.             //System.out.println("LOCAL["+wadl.getLocalName()+"]  NAMESPACE["+wadl.getNamespaceURI()+"]");
  91.             if(!"application".equals(wadl.getLocalName())){
  92.                 return false;
  93.             }
  94.             if(!"http://wadl.dev.java.net/2009/02".equals(wadl.getNamespaceURI())){
  95.                 return false;
  96.             }
  97.             return true;
  98.         }catch(Exception e){
  99.             throw new org.openspcoop2.utils.wadl.WADLException(e.getMessage(),e);
  100.         }
  101.     }




  102.     // TARGET NAMESPACE

  103.     public String getTargetNamespace(byte[]xsd) throws org.openspcoop2.utils.wadl.WADLException {
  104.         try{
  105.             if(this.xmlUtils==null){
  106.                 throw new Exception("XMLUtils not initialized in WADLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  107.             }

  108.             if(!this.xmlUtils.isDocument(xsd)){
  109.                 throw new Exception("Wadl non e' un documento valido");
  110.             }
  111.             Document docXML = this.xmlUtils.newDocument(xsd);
  112.             Element elemXML = docXML.getDocumentElement();
  113.             return getTargetNamespace(elemXML);
  114.         }catch(Exception e){
  115.             throw new org.openspcoop2.utils.wadl.WADLException(e.getMessage(),e);
  116.         }
  117.     }

  118.     public String getTargetNamespace(Document xsd) throws org.openspcoop2.utils.wadl.WADLException {
  119.         Element elemXML = xsd.getDocumentElement();
  120.         return getTargetNamespace(elemXML);
  121.     }

  122.     public String getTargetNamespace(Element elemXML) throws org.openspcoop2.utils.wadl.WADLException {
  123.         return getTargetNamespace((Node)elemXML);
  124.     }

  125.     public String getTargetNamespace(Node elemXML) throws org.openspcoop2.utils.wadl.WADLException {
  126.         try{
  127.             if(this.xmlUtils==null){
  128.                 throw new Exception("XMLUtils not initialized in WADLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  129.             }

  130.             if(elemXML == null){
  131.                 throw new Exception("Wadl non e' un documento valido");
  132.             }
  133.             //System.out.println("LOCAL["+elemXML.getLocalName()+"]  NAMESPACE["+elemXML.getNamespaceURI()+"]");
  134.             if(!"application".equals(elemXML.getLocalName())){
  135.                 throw new Exception("Root element non e' un application wadl ("+elemXML.getLocalName()+")");
  136.             }
  137.             String targetNamespace = this.xmlUtils.getAttributeValue(elemXML, "targetNamespace");
  138.             //System.out.println("TARGET["+targetNamespace+"]");
  139.             return targetNamespace;
  140.         }catch(Exception e){
  141.             throw new org.openspcoop2.utils.wadl.WADLException(e.getMessage(),e);
  142.         }
  143.     }

















  144.     /* ------------------ LETTURA---------------------------- */

  145.     /**
  146.      * Legge un WADL dal path in parametro e ne ritorna la rappresentazione a oggetti.
  147.      * @param file Il file da cui leggere il WADL.
  148.      * @return L'oggetto o null in caso di path mancante o errori.
  149.      */
  150.     public ApplicationWrapper readWADLFromFile(Logger log,File file) throws WADLException{
  151.         return readWADLFromFile(log, file, false, true, true);
  152.     }
  153.     public ApplicationWrapper readWADLFromFile(Logger log, File file,boolean verbose,boolean processInclude, boolean processInlineSchema) throws WADLException{
  154.         return this.readWADLFromFile(log, file, verbose, processInclude, processInlineSchema, null);
  155.     }
  156.     public ApplicationWrapper readWADLFromFile(Logger log, File file,boolean verbose,boolean processInclude, boolean processInlineSchema,
  157.             Map<String, byte[]> xsdSchemas) throws WADLException{
  158.         try{
  159.             if (file == null) throw new Exception("Path non definito");
  160.             WADLReader reader = getWADLReader(log, verbose, processInclude, processInlineSchema);
  161.             if(xsdSchemas!=null && xsdSchemas.size()>0) {
  162.                 for (String name : xsdSchemas.keySet()) {
  163.                     byte[] content = xsdSchemas.get(name);
  164.                     reader.addSchema(name, content);
  165.                 }
  166.             }
  167.             ApplicationNode appNode = reader.readWADL(file.getAbsolutePath());
  168.             return new ApplicationWrapper(appNode, reader.getResources(), reader.getMappingNamespaceLocations(), this.xmlUtils);
  169.         }catch(Exception e){
  170.             throw new WADLException("Lettura del wadl non riuscita (File): "+e.getMessage(),e);
  171.         }
  172.     }
  173.     /**
  174.      * Legge un WADL dal path in parametro e ne ritorna la rappresentazione a oggetti.
  175.      * @param path Il path da cui leggere il WADL.
  176.      * @return La definition corretta o null in caso di path mancante o errori.
  177.      */
  178.     public ApplicationWrapper readWADLFromLocation(Logger log,String path) throws WADLException{
  179.         return readWADLFromLocation(log, path, false, true, true);
  180.     }
  181.     public ApplicationWrapper readWADLFromLocation(Logger log, String path,boolean verbose,boolean processInclude, boolean processInlineSchema) throws WADLException{
  182.         return this.readWADLFromLocation(log, path, verbose, processInclude, processInlineSchema, null);
  183.     }
  184.     public ApplicationWrapper readWADLFromLocation(Logger log, String path,boolean verbose,boolean processInclude, boolean processInlineSchema,
  185.             Map<String, byte[]> xsdSchemas) throws WADLException{
  186.         try{
  187.             if (path == null)
  188.                 throw new Exception("Path non definito");
  189.             WADLReader reader = getWADLReader(log, verbose, processInclude, processInlineSchema);
  190.             if(xsdSchemas!=null && xsdSchemas.size()>0) {
  191.                 for (String name : xsdSchemas.keySet()) {
  192.                     byte[] content = xsdSchemas.get(name);
  193.                     reader.addSchema(name, content);
  194.                 }
  195.             }
  196.             ApplicationNode appNode = reader.readWADL(path);
  197.             ApplicationWrapper ap = new ApplicationWrapper(appNode, reader.getResources(), reader.getMappingNamespaceLocations(), this.xmlUtils);
  198.             return ap;
  199.         }catch(Exception e){
  200.             throw new WADLException("Lettura del wadl non riuscita (Path): "+e.getMessage(),e);
  201.         }
  202.     }
  203.     /**
  204.      * Legge un WADL dal path in parametro e ne ritorna la rappresentazione a oggetti.
  205.      * @param wadl I bytes da cui leggere il WADL.
  206.      * @return La definition corretta o null in caso di path mancante o errori.
  207.      */
  208.     public ApplicationWrapper readWADLFromBytes(Logger log, byte[] wadl) throws WADLException{
  209.         return readWADLFromBytes(log, wadl, false, true, true);
  210.     }
  211.     public ApplicationWrapper readWADLFromBytes(Logger log, byte[] wadl,boolean verbose,boolean processInclude, boolean processInlineSchema) throws WADLException{
  212.         return this.readWADLFromBytes(log, wadl, verbose, processInclude, processInlineSchema, null);
  213.     }
  214.     public ApplicationWrapper readWADLFromBytes(Logger log, byte[] wadl,boolean verbose,boolean processInclude, boolean processInlineSchema,
  215.             Map<String, byte[]> xsdSchemas) throws WADLException{
  216.         try{
  217.             if (wadl == null)
  218.                 throw new Exception("Bytes non definiti");
  219.             File tmp = FileSystemUtilities.createTempFile("wadl", ".tmp");
  220.             try {
  221.                 FileSystemUtilities.writeFile(tmp, wadl);
  222.                 return this.readWADLFromFile(log, tmp, verbose, processInclude, processInlineSchema, xsdSchemas);
  223.             } catch (WADLException e) {
  224.                 throw e;
  225.             }finally{
  226.                 if(!tmp.delete()) {
  227.                     // ignore
  228.                 }
  229.             }
  230.         }catch(Exception e){
  231.             throw new WADLException("Lettura del wadl non riuscita (byte[]): "+e.getMessage(),e);
  232.         }
  233.     }
  234.     /**
  235.      * Legge un WADL dal path in parametro e ne ritorna la rappresentazione a oggetti.
  236.      * @param doc Document da cui leggere il WADL.
  237.      * @return La definition corretta o null in caso di path mancante o errori.
  238.      */
  239.     public ApplicationWrapper readWADLFromDocument(Logger log,Document doc) throws WADLException{
  240.         return readWADLFromDocument(log, doc, false, true, true);
  241.     }
  242.     public ApplicationWrapper readWADLFromDocument(Logger log, Document doc,boolean verbose,boolean processInclude, boolean processInlineSchema) throws WADLException{
  243.         return this.readWADLFromDocument(log, doc, verbose, processInclude, processInlineSchema, null);
  244.     }
  245.     public ApplicationWrapper readWADLFromDocument(Logger log, Document doc,boolean verbose,boolean processInclude, boolean processInlineSchema,
  246.             Map<String, byte[]> xsdSchemas) throws WADLException{
  247.         try{
  248.             if(this.xmlUtils==null){
  249.                 throw new Exception("XMLUtils not initialized in WADLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  250.             }
  251.             if (doc == null)
  252.                 throw new Exception("Document non definito");
  253.             try {
  254.                 byte[]wadl = this.xmlUtils.toByteArray(doc);
  255.                 return this.readWADLFromBytes(log, wadl, verbose, processInclude, processInlineSchema, xsdSchemas);
  256.             } catch (WADLException e) {
  257.                 throw e;
  258.             }
  259.         }catch(Exception e){
  260.             throw new WADLException("Lettura del wadl non riuscita (Document): "+e.getMessage(),e);
  261.         }
  262.     }
  263.     /**
  264.      * Legge un WADL dal path in parametro e ne ritorna la rappresentazione a oggetti.
  265.      * @param elem Element da cui leggere il WADL.
  266.      * @return La definition corretta o null in caso di path mancante o errori.
  267.      */
  268.     public ApplicationWrapper readWADLFromDocument(Logger log,Element elem) throws WADLException{
  269.         return readWADLFromDocument(log, elem, false, true, true);
  270.     }
  271.     public ApplicationWrapper readWADLFromDocument(Logger log, Element elem,boolean verbose,boolean processInclude, boolean processInlineSchema) throws WADLException{
  272.         return this.readWADLFromDocument(log, elem, verbose, processInclude, processInlineSchema, null);
  273.     }
  274.     public ApplicationWrapper readWADLFromDocument(Logger log, Element elem,boolean verbose,boolean processInclude, boolean processInlineSchema,
  275.             Map<String, byte[]> xsdSchemas) throws WADLException{
  276.         try{
  277.             if(this.xmlUtils==null){
  278.                 throw new Exception("XMLUtils not initialized in WADLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  279.             }
  280.             if (elem == null)
  281.                 throw new Exception("Element non definito");
  282.             try {
  283.                 byte[]wadl = this.xmlUtils.toByteArray(elem);
  284.                 return this.readWADLFromBytes(log, wadl, verbose, processInclude, processInlineSchema, xsdSchemas);
  285.             } catch (WADLException e) {
  286.                 throw e;
  287.             }
  288.         }catch(Exception e){
  289.             throw new WADLException("Lettura del wadl non riuscita (Document): "+e.getMessage(),e);
  290.         }
  291.     }
  292.     /**
  293.      * Legge un WADL dal path in parametro e ne ritorna la rappresentazione a oggetti.
  294.      * @param uri La URI da cui leggere il WADL.
  295.      * @return La definition corretta o null in caso di path mancante o errori.
  296.      */
  297.     public ApplicationWrapper readWADLFromURI(Logger log,URI uri) throws WADLException{
  298.         return readWADLFromURI(log, uri, false, true, true);
  299.     }
  300.     public ApplicationWrapper readWADLFromURI(Logger log, URI uri, boolean verbose,boolean processInclude, boolean processInlineSchema) throws WADLException{
  301.         return this.readWADLFromURI(log, uri, verbose, processInclude, processInlineSchema, null);
  302.     }
  303.     public ApplicationWrapper readWADLFromURI(Logger log, URI uri, boolean verbose,boolean processInclude, boolean processInlineSchema,
  304.             Map<String, byte[]> xsdSchemas) throws WADLException{
  305.         try{
  306.             if (uri == null)
  307.                 throw new Exception("URI non definita");
  308.             WADLReader reader = getWADLReader(log, verbose, processInclude, processInlineSchema);
  309.             if(xsdSchemas!=null && xsdSchemas.size()>0) {
  310.                 for (String name : xsdSchemas.keySet()) {
  311.                     byte[] content = xsdSchemas.get(name);
  312.                     reader.addSchema(name, content);
  313.                 }
  314.             }
  315.             ApplicationNode appNode = reader.readWADL(uri);
  316.             ApplicationWrapper ap = new ApplicationWrapper(appNode, reader.getResources(), reader.getMappingNamespaceLocations(), this.xmlUtils);
  317.             return ap;
  318.         }catch(Exception e){
  319.             throw new WADLException("Lettura del wadl non riuscita (URI): "+e.getMessage(),e);
  320.         }
  321.     }













  322.     /* ---------------- METODI GET ------------------------- */

  323.     public Node getIfExistsApplicationElementIntoWADL(Document wadl) throws org.openspcoop2.utils.wadl.WADLException{

  324.         try{
  325.             NodeList list = wadl.getChildNodes();
  326.             if(list!=null){
  327.                 for(int i=0; i<list.getLength(); i++){
  328.                     Node child = list.item(i);
  329.                     if("application".equals(child.getLocalName())){
  330.                         return child;
  331.                     }
  332.                 }
  333.             }
  334.             return null;
  335.         }catch(Exception e){
  336.             throw new org.openspcoop2.utils.wadl.WADLException("Riscontrato errore durante la lettura del wadl: "+e.getMessage(),e);
  337.         }
  338.     }

  339.     public Node getIfExistsGrammarsElementIntoWADL(Document wadl) throws org.openspcoop2.utils.wadl.WADLException{

  340.         try{
  341.             NodeList list = wadl.getChildNodes();
  342.             if(list!=null){
  343.                 for(int i=0; i<list.getLength(); i++){
  344.                     Node child = list.item(i);
  345.                     if("application".equals(child.getLocalName())){
  346.                         NodeList listDefinition = child.getChildNodes();
  347.                         if(listDefinition!=null){
  348.                             for(int j=0; j<listDefinition.getLength(); j++){
  349.                                 Node childDefinition = listDefinition.item(j);
  350.                                 if("grammars".equals(childDefinition.getLocalName())){
  351.                                     return childDefinition;
  352.                                 }
  353.                             }
  354.                         }
  355.                     }
  356.                 }
  357.             }
  358.             return null;
  359.         }catch(Exception e){
  360.             throw new org.openspcoop2.utils.wadl.WADLException("Riscontrato errore durante la lettura del wadl: "+e.getMessage(),e);
  361.         }
  362.     }








  363.     /* ---------------- METODI READ IMPORTS ------------------------- */

  364.     public List<Node> readIncludes(Document wadl) throws org.openspcoop2.utils.wadl.WADLException{

  365.         try{
  366.             List<Node> includes = new ArrayList<Node>();

  367.             NodeList list = wadl.getChildNodes();
  368.             if(list!=null){
  369.                 for(int i=0; i<list.getLength(); i++){
  370.                     Node child = list.item(i);
  371.                     if("application".equals(child.getLocalName())){
  372.                         NodeList listDefinition = child.getChildNodes();
  373.                         if(listDefinition!=null){
  374.                             for(int j=0; j<listDefinition.getLength(); j++){
  375.                                 Node childDefinition = listDefinition.item(j);
  376.                                 if("grammars".equals(childDefinition.getLocalName())){
  377.                                     NodeList listGrammars = childDefinition.getChildNodes();
  378.                                     if(listGrammars!=null){
  379.                                         for (int k = 0; k < listGrammars.getLength(); k++) {
  380.                                             Node childGrammar = listGrammars.item(k);
  381.                                             if("include".equals(childGrammar.getLocalName())){
  382.                                                 includes.add(childGrammar);
  383.                                             }
  384.                                         }
  385.                                     }
  386.                                 }
  387.                             }
  388.                         }
  389.                     }
  390.                 }
  391.             }
  392.             return includes;
  393.         }catch(Exception e){
  394.             throw new org.openspcoop2.utils.wadl.WADLException("Riscontrato errore durante la lettura del wadl: "+e.getMessage(),e);
  395.         }
  396.     }
  397.    
  398.    
  399.    
  400.    
  401.    
  402.    
  403.    
  404.    
  405.    
  406.    
  407.    
  408.     /* ---------------- METODI REMOVE IMPORTS ------------------------- */
  409.    
  410.     public List<Node> removeIncludes(Document wadl) throws org.openspcoop2.utils.wadl.WADLException{
  411.        
  412.         try{
  413.             List<Node> includes = new ArrayList<Node>();
  414.            
  415.             NodeList list = wadl.getChildNodes();
  416.             if(list!=null){
  417.                 for(int i=0; i<list.getLength(); i++){
  418.                     Node child = list.item(i);
  419.                     if("application".equals(child.getLocalName())){
  420.                         NodeList listDefinition = child.getChildNodes();
  421.                         if(listDefinition!=null){
  422.                             for(int j=0; j<listDefinition.getLength(); j++){
  423.                                 Node childDefinition = listDefinition.item(j);
  424.                                 if("grammars".equals(childDefinition.getLocalName())){
  425.                                     NodeList listGrammars = childDefinition.getChildNodes();
  426.                                     if(listGrammars!=null){
  427.                                         for (int k = 0; k < listGrammars.getLength(); k++) {
  428.                                             Node childGrammar = listGrammars.item(k);
  429.                                             if("include".equals(childGrammar.getLocalName())){
  430.                                                 includes.add(childGrammar);
  431.                                                 childDefinition.removeChild(childGrammar);
  432.                                             }
  433.                                         }
  434.                                     }
  435.                                 }
  436.                             }
  437.                         }
  438.                     }
  439.                 }
  440.             }
  441.             return includes;
  442.         }catch(Exception e){
  443.             throw new org.openspcoop2.utils.wadl.WADLException("Riscontrato errore durante la lettura del wadl: "+e.getMessage(),e);
  444.         }
  445.     }
  446.    
  447.    
  448.    





  449.     /* ---------------- METODI NAVIGAZIONE WADL ------------------------- */

  450.     public static ResourceNode findResourceNode(ApplicationNode application,String url) throws WADLException{
  451.         if(application == null)
  452.             throw new WADLException("ApplicationNode non fornita");
  453.         return findResourceNode(application.getResources(), url);
  454.     }

  455.     private static ResourceNode findResourceNode(List<ResourceNode> resources,String url) throws WADLException{


  456.         String baseURI = null;

  457.         for (int i = 0; i< resources.size() && baseURI == null; i++) {

  458.             ResourceNode resourceNode = resources.get(i);

  459.             if(baseURI == null && resourceNode.getAllResourceUriTemplate().equals("/")) { //significa che siamo nel primo nodo, che ci serve per ricavare la baseURI
  460.                 baseURI = resourceNode.getUriTemplate();
  461.             }

  462.         }

  463.         String[] urlList = extractUrlList(baseURI, url);

  464.         return getResourceNode(urlList, 0, resources);
  465.     }

  466.     private static String[] extractUrlList(String baseURI, String url) throws WADLException{
  467.         if(url == null)
  468.             throw new WADLException("URL non fornita");

  469.         List<String> urlList = new ArrayList<>();

  470.         if(baseURI != null) {
  471.             urlList.add(baseURI);
  472.             if(url.startsWith(baseURI)) {
  473.                 url = url.substring(baseURI.length(), url.length());
  474.             }
  475.         }

  476.         for(String s : url.split("/")) {
  477.             if(!s.equals("")) {
  478.                 urlList.add("/"+s);
  479.             }
  480.         }

  481.         return urlList.toArray(new String[] {});

  482.     }

  483.     public static boolean isTemplate(String partialURL) {
  484.         String realPartialUrl = partialURL;
  485.         if(realPartialUrl.startsWith("/")) {
  486.             realPartialUrl = realPartialUrl.substring(1);
  487.         }
  488.         return realPartialUrl.startsWith("$");
  489.     }
  490.    
  491.     public static WADLOperation findOperation(List<WADLOperation> operations, String url, HTTPMethods method) throws WADLException {
  492.         String[] urlSplit = extractUrlList(null, url);
  493.         for(WADLOperation op: operations) {
  494.             String[] urlList = extractUrlList(null, op.getPath());
  495.             List<WADLOperation> lst = new ArrayList<WADLOperation>();
  496.             WADLOperation root =  getTreeWadlOperation(op, urlList[0]);
  497.             lst.add(root);
  498.            
  499.             for (int i = 1; i < urlList.length; i++) {
  500.                 WADLOperation wadlOp = getTreeWadlOperation(root, urlList[i]);
  501.                 lst.add(wadlOp);
  502.             }
  503.             WADLOperation found = getWADLOperation(urlSplit, 0, lst, method);
  504.             if(found != null) {
  505.                 return found;
  506.             }
  507.         }
  508.         throw new WADLException("Risorsa ["+url+"] non definita");
  509.     }
  510.    
  511.     private static WADLOperation getTreeWadlOperation(WADLOperation father, String url) {
  512.         WADLOperation child = new WADLOperation();
  513.         child.setMethod(father.getMethod());
  514.         child.setName(father.getName());
  515.         child.setPath(url);
  516.         child.setTemplate(isTemplate(url));
  517.         return child;
  518.     }

  519.     private static WADLOperation getWADLOperation(String[] url, int index, List<WADLOperation> operations, HTTPMethods method) throws WADLException{
  520.        
  521.         if(url.length != operations.size()) { // Non e' sicuramente l'operation giusta
  522.             return null;
  523.         }
  524.        
  525.         WADLOperation operationFound = null;

  526.         if(method.equals(operations.get(index).getMethod())) {
  527.             if(operations.get(index).getPath().equals(url[index])) { //risorsa precisa
  528.                 operationFound = operations.get(index);
  529.             } else if(operations.get(index).isTemplate()) { //risorsa template
  530.                 operationFound = operations.get(index);
  531.             }
  532.         }

  533.         if(operationFound != null) {
  534.             if(index < url.length-1) {
  535.                 WADLOperation rn = getWADLOperation(url, index+1, operations, method);
  536.                 if(rn != null) {
  537.                     return rn;
  538.                 }
  539.             } else {
  540.                 return operationFound;
  541.             }
  542.         }

  543.         return null;

  544.     }  

  545.     private static ResourceNode getResourceNode(String[] url, int index, List<ResourceNode> resources) throws WADLException{
  546.         ResourceNode resourceFound = null;
  547.         for(int i = 0; i< resources.size() && resourceFound == null; i++) {
  548.             ResourceNode resourceNode = resources.get(i);

  549.             if(resourceNode.getPathSegment().getTemplate().equals(url[index])) { //risorsa precisa
  550.                 resourceFound = resourceNode;
  551.             } else if(resourceNode.getPathSegment().getTemplateParameters().size() > 0) { //risorsa template
  552.                 resourceFound = resourceNode;
  553.             }

  554.         }
  555.         if(resourceFound != null) {
  556.             if(index < url.length-1) {
  557.                 ResourceNode rn = getResourceNode(url, index+1, resourceFound.getChildResources());
  558.                 if(rn != null) {
  559.                     return rn;
  560.                 }
  561.             } else {
  562.                 return resourceFound;
  563.             }
  564.         } else {
  565.             String urlString = "";

  566.             for(String s: url) {
  567.                 urlString += s;
  568.             }

  569.             throw new WADLException("Risorsa ["+urlString+"] non definita");
  570.         }

  571.         return null;

  572.     }

  573.     public static MethodNode findMethodNode(ResourceNode resourceNode,HttpRequestMethod httpMethod) throws org.openspcoop2.utils.wadl.WADLException{

  574.         if(httpMethod == null)
  575.             throw new WADLException("Metodo HTTP da cercare non fornito");

  576.         for(MethodNode method: resourceNode.getMethods()) {
  577.             if(httpMethod.name().equalsIgnoreCase(method.getName())) {
  578.                 return method;
  579.             }
  580.         }

  581.         throw new WADLException("Metodo ["+httpMethod+"] non definito per la risorsa ["+resourceNode.getAllResourceUriTemplate()+"]");
  582.     }


  583. }