/*=============================================================================*
* 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.resource.impl;
import org.apache.ws.resource.InvalidWsrfWsdlException;
import org.apache.ws.resource.ResourceDefinition;
import org.apache.ws.util.WsdlUtils;
import javax.wsdl.Definition;
import javax.wsdl.Port;
import javax.wsdl.PortType;
import javax.wsdl.Service;
import javax.wsdl.extensions.ExtensibilityElement;
import javax.wsdl.extensions.soap.SOAPAddress;
import java.util.List;
import java.util.Map;
/**
* A {@link ResourceDefinition} implementation.
*
* @author Ian Springer (ian DOT springer AT hp DOT com)
*/
public class ResourceDefinitionImpl extends ResourceCapabilityImpl implements ResourceDefinition
{
private Service m_service;
private String m_name;
private String m_endpointURL;
public ResourceDefinitionImpl( Definition def, Service service )
throws InvalidWsrfWsdlException
{
super( def, getResourcePortType( service ) );
m_service = service;
m_endpointURL = extractEndpointURL( m_service );
m_name = extractName( m_endpointURL );
}
private static PortType getResourcePortType( Service service ) throws InvalidWsrfWsdlException
{
Map portTypes = WsdlUtils.getPortTypes( service );
if ( portTypes.isEmpty() )
{
throw new InvalidWsrfWsdlException(
"WSDL service " + service.getQName() +
" is not a valid service, as it does not contain any ports." );
}
if ( portTypes.size() > 1 )
{
throw new InvalidWsrfWsdlException(
"WSDL service " + service.getQName() +
" is not a valid WSRF service, as it contains more than one port." );
}
return (PortType) portTypes.values().iterator().next();
}
public Service getService()
{
return m_service;
}
public String getName()
{
return m_name;
}
public String getEndpointURL()
{
return m_endpointURL;
}
private String extractName( String endpointURL )
{
if ( endpointURL.endsWith( "/" ) )
{
endpointURL = endpointURL.substring( 0, endpointURL.length() - 1 );
}
String name = endpointURL.substring( endpointURL.lastIndexOf( "/" ) + 1 );
return name;
}
private String extractEndpointURL( Service service ) throws InvalidWsrfWsdlException
{
String endpointURL = null;
Port port = (Port) service.getPorts().values().iterator().next();
List extElems = port.getExtensibilityElements();
for ( int i = 0; i < extElems.size(); i++ )
{
ExtensibilityElement extElem = (ExtensibilityElement) extElems.get( i );
if ( extElem instanceof SOAPAddress )
{
SOAPAddress soapAddr = (SOAPAddress) extElem;
endpointURL = soapAddr.getLocationURI();
break;
}
}
if ( endpointURL == null )
{
throw new InvalidWsrfWsdlException(
"Failed to obtain service endpoint URL from " + m_service.getQName() +
" service's wsdl:port/soap:address/@location attribute" );
}
return endpointURL;
}
}