Project

General

Profile

1
package eu.dnetlib.soap.cxf;
2

    
3
import java.net.URI;
4
import java.net.URISyntaxException;
5

    
6
import javax.annotation.PostConstruct;
7

    
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.apache.cxf.endpoint.Endpoint;
11
import org.springframework.beans.factory.annotation.Required;
12

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

    
28
	private static final Log log = LogFactory.getLog(StandaloneCxfEndpointReferenceBuilder.class); // NOPMD by marko on 11/24/08 5:02 PM
29

    
30
	/**
31
	 * base url where all services are exported.
32
	 */
33
	private String baseAddress;
34

    
35
	private String absoluteBase;
36

    
37
	private boolean forceLocalAddress = false;
38

    
39
	public String getBaseAddress() {
40
		return baseAddress;
41
	}
42

    
43
	public void setBaseAddress(final String baseAddress) {
44
		this.baseAddress = baseAddress;
45
	}
46

    
47
	@PostConstruct
48
	public void init() throws URISyntaxException {
49
		URI base = new URI(baseAddress);
50
		log.info("base address: " + baseAddress);
51

    
52
		this.absoluteBase = (new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), null, null, null)).toString().trim();
53
		log.info("absolute base address: " + absoluteBase);
54
	}
55

    
56
	/**
57
	 * {@inheritDoc}
58
	 * 
59
	 * @see eu.dnetlib.soap.cxf.CxfEndpointReferenceBuilder#getAddress(org.apache.cxf.endpoint.Endpoint)
60
	 */
61
	@Override
62
	public String getAddress(final Endpoint endpoint) {
63
		final String address = super.getAddress(endpoint);
64

    
65
		if (forceLocalAddress) {
66
			try {
67
				URI uri = new URI(address);
68
				if (!address.startsWith("http://")) {
69
					String res = baseAddress + uri.getPath();
70
					if (log.isDebugEnabled()) {
71
						log.debug("fixing address to: " + res);
72
					}
73
					return res;
74
				}
75
				String res = absoluteBase + uri.getPath();
76
				if (log.isDebugEnabled()) {
77
					log.debug("forcing address to: " + res);
78
				}
79
				return res;
80
			} catch (URISyntaxException e) {
81
				throw new IllegalArgumentException(e);
82
			}
83
		}
84

    
85
		if (!address.startsWith("http://") && (baseAddress != null)) { return baseAddress + address; }
86
		return address;
87
	}
88

    
89
	public boolean isForceLocalAddress() {
90
		return forceLocalAddress;
91
	}
92

    
93
	@Required
94
	public void setForceLocalAddress(final boolean forceLocalAddress) {
95
		this.forceLocalAddress = forceLocalAddress;
96
	}
97
}
(3-3/3)