/*=============================================================================*
* 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 javax.wsdl.Binding;
import javax.wsdl.Definition;
import javax.wsdl.Port;
import javax.wsdl.PortType;
import javax.wsdl.extensions.ExtensibilityElement;
import javax.wsdl.extensions.soap.SOAPAddress;
import javax.xml.namespace.QName;
import java.util.List;
import java.net.URL;
/**
* A {@link ResourceDefinition} implementation.
*
* @author Ian Springer (ian DOT springer AT hp DOT com)
*/
public class ResourceDefinitionImpl extends ResourceCapabilityImpl implements ResourceDefinition
{
private Port m_port;
private String m_name;
private String m_endpointURL;
public ResourceDefinitionImpl(Definition def, javax.wsdl.Port port, URL baseUrl)
throws InvalidWsrfWsdlException
{
super( def, getResourcePortType( port ), baseUrl);
m_port = port;
m_endpointURL = extractEndpointURL( m_port );
m_name = m_port.getName();
}
private static PortType getResourcePortType( Port port ) throws InvalidWsrfWsdlException
{
Binding binding = port.getBinding();
if(binding != null)
{
PortType portType = binding.getPortType();
if(portType == null)
{
throw new InvalidWsrfWsdlException("The binding has no portType associated with it!");
}
return portType;
}
else
{
throw new InvalidWsrfWsdlException("The port has no binding associated with it!");
}
}
public Port getPort()
{
return m_port;
}
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( Port port ) throws InvalidWsrfWsdlException
{
String endpointURL = null;
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 port " + m_port.getName() +
" service's wsdl:port/soap:address/@location attribute" );
}
return endpointURL;
}
}