package eu.dnetlib.soap.cxf;

import org.apache.cxf.endpoint.Endpoint;

/**
 * CxfEndpointReferenceBuilder is not able to create correct endpoint addresses outside a http request context. This
 * means that service initialization code cannot obtain the service address and thus cannot register himself.
 *
 * This subclass allows putting a local address (ip/dns + port) to be used when the runtime servlet context is not
 * available.
 *
 * TODO: automated tomcat port detection, trough org.apache.catalina.ServerFactory.getServer().getServices() TODO:
 * automated jetty port detection
 *
 *
 * @author marko
 *
 */
public class StandaloneCxfEndpointReferenceBuilder extends CxfEndpointReferenceBuilder {

	/**
	 * base url where all services are exported.
	 */
	private String baseAddress;

	public String getBaseAddress() {
		return baseAddress;
	}

	public void setBaseAddress(final String baseAddress) {
		this.baseAddress = baseAddress;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see eu.dnetlib.soap.cxf.CxfEndpointReferenceBuilder#getAddress(org.apache.cxf.endpoint.Endpoint)
	 */
	@Override
	public String getAddress(final Endpoint endpoint) {
		final String address = super.getAddress(endpoint);
		if (!address.startsWith("http://") && baseAddress != null)
			return baseAddress + address;
		return address;
	}
}
