View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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  	public void validateSoapEnvelope(String soapMessage, List<XmlError> errors)
    40  	{
    41  		List<XmlError> errorList = new ArrayList<XmlError>();
    42
    43  	   try
    44  		{
    45  			XmlOptions xmlOptions = new XmlOptions();
    46  			xmlOptions.setLoadLineNumbers();
    47  			xmlOptions.setValidateTreatLaxAsSkip();
    48  			xmlOptions.setLoadLineNumbers(XmlOptions.LOAD_LINE_NUMBERS_END_ELEMENT);
    49  			XmlObject xmlObject = getSoapEnvelopeSchemaLoader().parse( soapMessage, getEnvelopeType(), xmlOptions);
    50  			xmlOptions.setErrorListener(errorList);
    51  			xmlObject.validate(xmlOptions);
    52  		}
    53  	   catch ( XmlException e )
    54  	   {
    55     		errorList.addAll( e.getErrors() );
    56  	   	errors.add( XmlError.forMessage( e.getMessage() ));
    57  	   }
    58  	   catch (Exception e)
    59  	   {
    60  	   	errors.add( XmlError.forMessage( e.getMessage() ));
    61  	   }
    62  	   finally
    63  	   {
    64  	   	for( XmlError error : errorList )
    65  	   	{
    66  	   		if( error instanceof XmlValidationError && shouldIgnore( (XmlValidationError) error ))
    67  	   		{
    68  	   			log.warn( "Ignoring validation error: " + error.toString() );
    69  	   			continue;
    70  	   		}
    71
    72  	   		errors.add( error );
    73  	   	}
    74  	   }
    75  	}
    76
    77  	protected abstract SchemaTypeLoader getSoapEnvelopeSchemaLoader();
    78
    79  	public boolean shouldIgnore(XmlValidationError error)
    80  	{
    81  		QName offendingQName = error.getOffendingQName();
    82  		if( offendingQName != null )
    83  		{
    84  			if( offendingQName.equals( new QName( getEnvelopeNamespace(), "encodingStyle")))
    85  			{
    86  				return true;
    87  			}
    88  			else if( offendingQName.equals( new QName( getEnvelopeNamespace(), "mustUnderstand")))
    89  			{
    90  				return true;
    91  			}
    92  		}
    93
    94  		return false;
    95  	}
    96
    97  	public abstract SchemaType getFaultType();
    98
    99  	public abstract SchemaType getEnvelopeType();
    100 }