View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.wsdl.support.soap;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import javax.xml.namespace.QName;
19  
20  import org.apache.log4j.Logger;
21  import org.apache.xmlbeans.SchemaType;
22  import org.apache.xmlbeans.SchemaTypeLoader;
23  import org.apache.xmlbeans.XmlError;
24  import org.apache.xmlbeans.XmlException;
25  import org.apache.xmlbeans.XmlObject;
26  import org.apache.xmlbeans.XmlOptions;
27  import org.apache.xmlbeans.XmlValidationError;
28  
29  /***
30   * Common behaviour for all SOAP Versions
31   * 
32   * @author ole.matzura
33   */
34  
35  public abstract class AbstractSoapVersion implements SoapVersion
36  {
37  	private final static Logger log = Logger.getLogger( AbstractSoapVersion.class );
38  
39  	@SuppressWarnings( "unchecked" )
40  	public void validateSoapEnvelope( String soapMessage, List<XmlError> errors )
41  	{
42  		List<XmlError> errorList = new ArrayList<XmlError>();
43  
44  		try
45  		{
46  			XmlOptions xmlOptions = new XmlOptions();
47  			xmlOptions.setLoadLineNumbers();
48  			xmlOptions.setValidateTreatLaxAsSkip();
49  			xmlOptions.setLoadLineNumbers( XmlOptions.LOAD_LINE_NUMBERS_END_ELEMENT );
50  			XmlObject xmlObject = getSoapEnvelopeSchemaLoader().parse( soapMessage, getEnvelopeType(), xmlOptions );
51  			xmlOptions.setErrorListener( errorList );
52  			xmlObject.validate( xmlOptions );
53  		}
54  		catch( XmlException e )
55  		{
56  			if( e.getErrors() != null )
57  				errorList.addAll( e.getErrors() );
58  
59  			errors.add( XmlError.forMessage( e.getMessage() ) );
60  		}
61  		catch( Exception e )
62  		{
63  			errors.add( XmlError.forMessage( e.getMessage() ) );
64  		}
65  		finally
66  		{
67  			for( XmlError error : errorList )
68  			{
69  				if( error instanceof XmlValidationError && shouldIgnore( ( XmlValidationError )error ) )
70  				{
71  					log.warn( "Ignoring validation error: " + error.toString() );
72  					continue;
73  				}
74  
75  				errors.add( error );
76  			}
77  		}
78  	}
79  
80  	protected abstract SchemaTypeLoader getSoapEnvelopeSchemaLoader();
81  
82  	public boolean shouldIgnore( XmlValidationError error )
83  	{
84  		QName offendingQName = error.getOffendingQName();
85  		if( offendingQName != null )
86  		{
87  			if( offendingQName.equals( new QName( getEnvelopeNamespace(), "encodingStyle" ) ) )
88  			{
89  				return true;
90  			}
91  			else if( offendingQName.equals( new QName( getEnvelopeNamespace(), "mustUnderstand" ) ) )
92  			{
93  				return true;
94  			}
95  		}
96  
97  		return false;
98  	}
99  
100 	public abstract SchemaType getFaultType();
101 
102 	public abstract SchemaType getEnvelopeType();
103 }