View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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  public abstract class AbstractSoapVersion implements SoapVersion
30  {
31  	private final static Logger log = Logger.getLogger(AbstractSoapVersion.class);
32  	
33  	public void validateSoapEnvelope(String soapMessage, List<XmlError> errors)
34  	{
35  		List<XmlError> errorList = new ArrayList<XmlError>();
36  		
37  	   try
38  		{
39  			XmlOptions xmlOptions = new XmlOptions();
40  			xmlOptions.setLoadLineNumbers();
41  			xmlOptions.setValidateTreatLaxAsSkip();
42  			xmlOptions.setLoadLineNumbers(XmlOptions.LOAD_LINE_NUMBERS_END_ELEMENT);
43  			XmlObject xmlObject = getSoapEnvelopeSchemaLoader().parse( soapMessage, getEnvelopeType(), xmlOptions);
44  			xmlOptions.setErrorListener(errorList);
45  			xmlObject.validate(xmlOptions);
46  		}
47  	   catch ( XmlException e )
48  	   {
49  	   	errorList.addAll( e.getErrors() );
50  	   }
51  	   catch (Exception e)
52  	   {
53  	   	errors.add( XmlError.forMessage( e.getMessage() ));
54  	   }
55  	   finally
56  	   {
57  	   	for( XmlError error : errorList )
58  	   	{
59  	   		if( error instanceof XmlValidationError && shouldIgnore( (XmlValidationError) error ))
60  	   		{
61  	   			log.warn( "Ignoring validation error: " + error.toString() );
62  	   			continue;
63  	   		}
64  	   		
65  	   		errors.add( error );
66  	   	}
67  	   }
68  	}
69  
70  	protected abstract SchemaTypeLoader getSoapEnvelopeSchemaLoader();
71  
72  	public boolean shouldIgnore(XmlValidationError error)
73  	{
74  		QName offendingQName = error.getOffendingQName();
75  		if( offendingQName != null )
76  		{
77  			if( offendingQName.equals( new QName( getEnvelopeNamespace(), "encodingStyle")))
78  			{
79  				return true;
80  			}
81  			else if( offendingQName.equals( new QName( getEnvelopeNamespace(), "mustUnderstand")))
82  			{
83  				return true;
84  			}
85  		}
86  	
87  		return false;
88  	}
89  	
90  	public abstract SchemaType getFaultType();
91  
92  	public abstract SchemaType getEnvelopeType();
93  }