WADLReader.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.io.IOException;
  23. import java.net.URI;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.slf4j.Logger;
  27. import org.jvnet.ws.wadl.ast.ApplicationNode;
  28. import org.jvnet.ws.wadl.ast.InvalidWADLException;
  29. import org.jvnet.ws.wadl.ast.ResourceTypeNode;
  30. import org.jvnet.ws.wadl.ast.WadlAstBuilder;
  31. import org.openspcoop2.utils.resources.FileSystemUtilities;
  32. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  33. import org.openspcoop2.utils.xml.XMLException;
  34. import org.w3c.dom.Document;

  35. /**
  36.  * WADLReader
  37.  *
  38.  *
  39.  * @author Andrea Poli (apoli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public class WADLReader {
  44.     // concettualmente รจ come si estenda il builder di wadl
  45.     //extends org.jvnet.ws.wadl.ast.WadlAstBuilder {

  46.     private org.openspcoop2.utils.wadl.SchemaCallback schemaCallback;
  47.     private boolean processInclude;
  48.     private org.openspcoop2.utils.wadl.MessageListener messageListener;
  49.     private org.jvnet.ws.wadl.ast.WadlAstBuilder wadlBuilder;
  50.     private AbstractXMLUtils xmlUtils;
  51.     private WADLUtilities wadlUtilities;
  52.     private Logger log;
  53.    
  54.     public WADLReader(Logger log,AbstractXMLUtils xmlUtils,boolean verbose,boolean processInclude, boolean processInlineSchema){
  55.         this.log = log;
  56.         this.xmlUtils = xmlUtils;
  57.         this.wadlUtilities = new WADLUtilities(this.xmlUtils);
  58.        
  59.         this.schemaCallback = new org.openspcoop2.utils.wadl.SchemaCallback(this.log, this.xmlUtils, processInclude, processInlineSchema);
  60.         this.processInclude = processInclude;
  61.        
  62.         this.messageListener = new org.openspcoop2.utils.wadl.MessageListener(this.log, verbose, true);
  63.        
  64.         this.wadlBuilder = new WadlAstBuilder(this.schemaCallback, this.messageListener);
  65.     }
  66.    
  67.     public void addSchema(String name, byte[] content) throws XMLException {
  68.         this.schemaCallback.addResource(name, content);
  69.     }

  70.     public ApplicationNode readWADL(String file) throws InvalidWADLException,IOException {
  71.         return this.readWADL(new File(file));
  72.     }
  73.    
  74.     public ApplicationNode readWADL(File file) throws InvalidWADLException,IOException {
  75.         File fTpm = null;
  76.         try{
  77.             if(this.processInclude){
  78.                 return this.wadlBuilder.buildAst(file.toURI());
  79.             }
  80.             else{
  81.                 byte[]wadl = FileSystemUtilities.readBytesFromFile(file);
  82.                 Document d = this.xmlUtils.newDocument(wadl);
  83.                 this.wadlUtilities.removeIncludes(d);
  84.                 fTpm = FileSystemUtilities.createTempFile("wadl", "tmp");
  85.                 this.xmlUtils.writeTo(d, fTpm);
  86.                 return this.wadlBuilder.buildAst(fTpm.toURI());
  87.             }
  88.            
  89.         }catch(InvalidWADLException e){
  90.             throw e;
  91.         }catch(IOException e){
  92.             throw e;
  93.         }catch(Exception e){
  94.             throw new IOException(e.getMessage(),e);
  95.         }finally{
  96.             if(fTpm!=null) {
  97.                 if(!fTpm.delete()) {
  98.                     // ignore
  99.                 }
  100.             }
  101.         }
  102.     }
  103.    
  104.     public ApplicationNode readWADL(URI uri) throws InvalidWADLException, IOException {
  105.         if(this.processInclude){
  106.             return this.wadlBuilder.buildAst(uri);
  107.         }
  108.         else{
  109.             return this.readWADL(new File(uri));
  110.         }
  111.     }

  112.     public Map<String, ResourceTypeNode> getInterfaceMap() {
  113.         return this.wadlBuilder.getInterfaceMap();
  114.     }
  115.    
  116.     public Map<String, byte[]> getResources() {
  117.         return this.schemaCallback.getResources();
  118.     }

  119.     public Map<String, List<String>> getMappingNamespaceLocations() {
  120.         return this.schemaCallback.getMappingNamespaceLocations();
  121.     }
  122. }