View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 eviware.com
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wadl;
14  
15  import org.apache.xmlbeans.SchemaTypeSystem;
16  
17  import com.eviware.soapui.impl.rest.RestService;
18  import com.eviware.soapui.impl.rest.panels.request.inspectors.schema.InferredSchemaManager;
19  import com.eviware.soapui.impl.support.definition.DefinitionCache;
20  import com.eviware.soapui.impl.support.definition.DefinitionLoader;
21  import com.eviware.soapui.impl.support.definition.export.WadlDefinitionExporter;
22  import com.eviware.soapui.impl.support.definition.support.AbstractDefinitionContext;
23  import com.eviware.soapui.impl.support.definition.support.InterfaceCacheDefinitionLoader;
24  import com.eviware.soapui.impl.wadl.support.GeneratedWadlDefinitionLoader;
25  import com.eviware.soapui.impl.wadl.support.WadlInterfaceDefinition;
26  import com.eviware.soapui.impl.wsdl.support.wsdl.UrlWsdlLoader;
27  import com.eviware.soapui.support.StringUtils;
28  
29  public class WadlDefinitionContext extends
30  		AbstractDefinitionContext<RestService, DefinitionLoader, WadlInterfaceDefinition>
31  {
32  	public WadlDefinitionContext( String url, RestService iface )
33  	{
34  		super( url, iface );
35  	}
36  
37  	public WadlDefinitionContext( String wadlUrl )
38  	{
39  		super( wadlUrl );
40  	}
41  
42  	protected DefinitionLoader createDefinitionLoader( DefinitionCache restServiceDefinitionCache )
43  	{
44  		if( getInterface().isGenerated() || StringUtils.isNullOrEmpty( getInterface().getWadlUrl() ) )
45  			return new GeneratedWadlDefinitionLoader( getInterface() );
46  		else
47  			return new InterfaceCacheDefinitionLoader( restServiceDefinitionCache );
48  	}
49  
50  	protected DefinitionLoader createDefinitionLoader( String url )
51  	{
52  		if( getInterface().isGenerated() || StringUtils.isNullOrEmpty( url ) )
53  			return new GeneratedWadlDefinitionLoader( getInterface() );
54  		else
55  			return new UrlWsdlLoader( url, getInterface() );
56  	}
57  
58  	protected WadlInterfaceDefinition loadDefinition( DefinitionLoader loader ) throws Exception
59  	{
60  		return new WadlInterfaceDefinition( getInterface() ).load( loader );
61  	}
62  
63  	public String export( String path ) throws Exception
64  	{
65  		return new WadlDefinitionExporter( getInterface() ).export( path );
66  	}
67  
68  	public WadlInterfaceDefinition regenerateWadl()
69  	{
70  		try
71  		{
72  			if( getInterface().isGenerated() )
73  				reload();
74  
75  			return getInterfaceDefinition();
76  		}
77  		catch( Exception e )
78  		{
79  			e.printStackTrace();
80  		}
81  
82  		return null;
83  	}
84  
85  	public boolean hasSchemaTypes()
86  	{
87  		return( super.hasSchemaTypes() || InferredSchemaManager.getInferredSchema( getInterface() ).getNamespaces().length > 0 );
88  	}
89  
90  	public SchemaTypeSystem getSchemaTypeSystem() throws Exception
91  	{
92  		if( super.hasSchemaTypes() )
93  			return InferredSchemaManager.getInferredSchema( getInterface() ).getSchemaTypeSystem(
94  					super.getSchemaTypeSystem() );
95  		return InferredSchemaManager.getInferredSchema( getInterface() ).getSchemaTypeSystem();
96  	}
97  }