/*=============================================================================*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*=============================================================================*/
package org.apache.ws.addressing;
import org.apache.ws.XmlObjectWrapper;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.xmlbeans.XmlObject;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.AttributedQName;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.AttributedURI;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceType;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.ReferencePropertiesType;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.ServiceNameType;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.ReferenceParametersType;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* This class wraps XmlBean-generated XmlBeansEndpointReference types for use throughout the system.
* <p/>
* As a new version of the schema is generated, a new constructor should be added to encompass the new version.
* <p/>
* Note..Use fully qualified class name in constructors for different versions.
*
* @author Sal Campana
*/
public class XmlBeansEndpointReference
implements XmlObjectWrapper,
EndpointReference
{
private XmlObject m_xmlObjectEPR;
private String m_address;
private QName m_portTypeQName;
private String m_servicePortName;
private QName m_serviceQName;
private String m_addressingVersionURI;
private List m_refParams = new ArrayList( );
private List m_refProps = new ArrayList( );
/**
* Constructs an EPR (and the underlying XmlObject representation) given the params.
*
* @param address - Endpoint Address. Must Not Be Null
* @param addressingURI - WS-Addressing URI - Must Not Be Null
*/
public XmlBeansEndpointReference( String address,
String addressingURI )
{
if ( address == null )
{
throw new IllegalArgumentException( "Address must not be null!" );
}
if ( addressingURI == null )
{
throw new IllegalArgumentException( "WS-Addressing addresingURI must not be null!" );
}
m_address = address;
m_addressingVersionURI = addressingURI;
}
/**
* Creates a new {@link XmlBeansEndpointReference} object.
*
* @param epr org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceType XMLBean generated type
*/
public XmlBeansEndpointReference( org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceType epr )
{
m_xmlObjectEPR = epr;
if ( epr.getAddress( ) != null )
{
m_address = epr.getAddress( ).getStringValue( );
}
if ( epr.isSetPortType( ) )
{
m_portTypeQName = epr.getPortType( ).getQNameValue( );
}
if ( epr.isSetServiceName( ) && epr.getServiceName( ).isSetPortName( ) )
{
m_servicePortName = epr.getServiceName( ).getPortName( );
}
if ( epr.isSetServiceName( ) )
{
m_serviceQName = epr.getServiceName( ).getQNameValue( );
}
if ( epr.isSetReferenceProperties( ) )
{
m_refProps = Arrays.asList( XmlBeanUtils.getChildElements( epr.getReferenceProperties( ) ) );
}
}
/**
* Creates a new {@link XmlBeansEndpointReference} object.
*
* @param epr org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType XMLBean generated type
*/
public XmlBeansEndpointReference( org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType epr )
{
m_xmlObjectEPR = epr;
if ( epr.getAddress( ) != null )
{
m_address = epr.getAddress( ).getStringValue( );
}
if ( epr.isSetPortType( ) )
{
m_portTypeQName = epr.getPortType( ).getQNameValue( );
}
if ( epr.isSetServiceName( ) && epr.getServiceName( ).isSetPortName( ) )
{
m_servicePortName = epr.getServiceName( ).getPortName( );
}
if ( epr.isSetServiceName( ) )
{
m_serviceQName = epr.getServiceName( ).getQNameValue( );
}
if ( epr.isSetReferenceProperties( ) )
{
m_refProps = Arrays.asList( XmlBeanUtils.getChildElements( epr.getReferenceProperties( ) ) );
}
if ( epr.isSetReferenceParameters( ) )
{
m_refParams = Arrays.asList( XmlBeanUtils.getChildElements( epr.getReferenceParameters( ) ) );
}
}
/**
* Returns the Address from the EPR as a String.
*
* @return Address
*/
public String getAddress( )
{
return m_address;
}
/**
* Returns the PortName associated with the Service in the EPR as a String
*
* @return Service's Port Name
*/
public String getPortName( )
{
return m_servicePortName;
}
/**
* Returns the PortType QName
*
* @return PortType QName
*/
public QName getPortType( )
{
return m_portTypeQName;
}
/**
* DOCUMENT_ME
*
* @param portTypeQName DOCUMENT_ME
*/
public void setPortTypeQName( QName portTypeQName )
{
m_portTypeQName = portTypeQName;
}
/**
* Returns the ReferenceParameters Object.
*
* @return Object[] The ReferenceParameters
*/
public Object[] getReferenceParameters( )
{
return m_refParams.toArray( new XmlObject[m_refParams.size( )] );
}
/**
* Returns the ReferenceProperties directly from the underlying XmlBean-generated class. The signature returns
* Object to make it generic for other impls.
*
* @return Object[] containing the Reference Property elements
*/
public Object[] getReferenceProperties( )
{
return m_refProps.toArray( new XmlObject[m_refParams.size( )] );
}
/**
* Service QName
*
* @return Service QName
*/
public QName getServiceName( )
{
return m_serviceQName;
}
/**
* DOCUMENT_ME
*
* @param servicePortName DOCUMENT_ME
*/
public void setServicePortName( String servicePortName )
{
m_servicePortName = servicePortName;
}
/**
* DOCUMENT_ME
*
* @param serviceQName DOCUMENT_ME
*/
public void setServiceQName( QName serviceQName )
{
m_serviceQName = serviceQName;
}
/**
* The underlying XmlBean-Generated EPR
*
* @return The generated EPR
*/
public XmlObject getXmlObject( )
{
XmlObject epr = null;
if ( m_xmlObjectEPR != null )
{
epr = m_xmlObjectEPR;
}
else if ( ( m_addressingVersionURI != null ) && ( m_address != null ) )
{
if ( org.apache.ws.addressing.v2003_03.AddressingConstants.NSURI_ADDRESSING_SCHEMA.equals( m_addressingVersionURI ) )
{
EndpointReferenceType endpointReferenceType = build2003_03_EPR( );
m_xmlObjectEPR = endpointReferenceType;
epr = endpointReferenceType;
}
else if ( org.apache.ws.addressing.v2004_08.AddressingConstants.NSURI_ADDRESSING_SCHEMA.equals( m_addressingVersionURI ) )
{
org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType endpointReferenceType =
build2004_08_EPR( );
m_xmlObjectEPR = endpointReferenceType;
epr = endpointReferenceType;
}
}
return epr;
}
/**
* Returns a version-specific XmlBean-Generated EPR
*
* @param namespace the WS-Addressing namespace for which you would like an EPR returned.
*
* @return Version-Specific EPR
*/
public XmlObject getXmlObject( String namespace )
{
XmlObject epr = null;
if ( ( namespace != null ) && ( m_address != null ) )
{
if ( org.apache.ws.addressing.v2003_03.AddressingConstants.NSURI_ADDRESSING_SCHEMA.equals( namespace ) )
{
EndpointReferenceType endpointReferenceType = build2003_03_EPR( );
epr = endpointReferenceType;
}
else if ( org.apache.ws.addressing.v2004_08.AddressingConstants.NSURI_ADDRESSING_SCHEMA.equals( namespace ) )
{
org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType endpointReferenceType =
build2004_08_EPR( );
epr = endpointReferenceType;
}
}
return epr;
}
/**
* DOCUMENT_ME
*
* @param refProp DOCUMENT_ME
*/
public void addReferenceParameter( Object refProp )
{
try
{
m_refProps.add( XmlBeanUtils.toXmlObject( refProp ) );
}
catch ( Exception e )
{
throw new IllegalArgumentException( "Unable to convert specified " + refProp.getClass( ).getName( )
+ " object to an XmlObject." );
}
}
/**
* DOCUMENT_ME
*
* @param refParam DOCUMENT_ME
*/
public void addReferenceProperty( Object refParam )
{
try
{
m_refParams.add( XmlBeanUtils.toXmlObject( refParam ) );
}
catch ( Exception e )
{
throw new IllegalArgumentException( "Unable to convert specified " + refParam.getClass( ).getName( )
+ " object to an XmlObject." );
}
}
private static void addToHolderType( XmlObject refPropsType,
List refProps )
{
for ( int i = 0; i < refProps.size( ); i++ )
{
XmlObject refProp = (XmlObject) refProps.get( i );
XmlBeanUtils.addChildElement( refPropsType, refProp );
}
}
private EndpointReferenceType build2003_03_EPR( )
{
EndpointReferenceType endpointReferenceType = EndpointReferenceType.Factory.newInstance( );
AttributedURI attributedURI = endpointReferenceType.addNewAddress( );
attributedURI.setStringValue( m_address );
if ( m_portTypeQName != null )
{
AttributedQName attributedQName = endpointReferenceType.addNewPortType( );
attributedQName.setQNameValue( m_portTypeQName );
}
if ( m_servicePortName != null )
{
ServiceNameType serviceNameType = endpointReferenceType.addNewServiceName( );
if ( m_serviceQName != null )
{
serviceNameType.setQNameValue( m_serviceQName );
}
serviceNameType.setPortName( m_servicePortName );
}
if ( m_refProps != null )
{
ReferencePropertiesType refPropsType = endpointReferenceType.addNewReferenceProperties( );
addToHolderType( refPropsType, m_refProps );
}
return endpointReferenceType;
}
private org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType build2004_08_EPR( )
{
org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType endpointReferenceType =
org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType.Factory.newInstance( );
org.xmlsoap.schemas.ws.x2004.x08.addressing.AttributedURI attributedURI =
endpointReferenceType.addNewAddress( );
attributedURI.setStringValue( m_address );
if ( m_portTypeQName != null )
{
org.xmlsoap.schemas.ws.x2004.x08.addressing.AttributedQName attributedQName =
endpointReferenceType.addNewPortType( );
attributedQName.setQNameValue( m_portTypeQName );
}
if ( m_servicePortName != null )
{
org.xmlsoap.schemas.ws.x2004.x08.addressing.ServiceNameType serviceNameType =
endpointReferenceType.addNewServiceName( );
if ( m_serviceQName != null )
{
serviceNameType.setQNameValue( m_serviceQName );
}
serviceNameType.setPortName( m_servicePortName );
}
if ( m_refProps != null )
{
org.xmlsoap.schemas.ws.x2004.x08.addressing.ReferencePropertiesType refPropsType =
endpointReferenceType.addNewReferenceProperties( );
addToHolderType( refPropsType, m_refProps );
}
if ( m_refParams != null )
{
ReferenceParametersType refParamsType = endpointReferenceType.addNewReferenceParameters( );
addToHolderType( refParamsType, m_refParams );
}
return endpointReferenceType;
}
}