1 package com.atlassian.plugin.refimpl.tenant;
2
3 import com.atlassian.plugin.refimpl.ParameterUtils;
4 import com.atlassian.plugin.webresource.UrlMode;
5 import com.atlassian.plugins.landlord.spi.LandlordRequestException;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 import java.net.URL;
10
11
12
13
14
15
16 public class RefappTenancyManager {
17 private static final Logger log = LoggerFactory.getLogger(RefappTenancyManager.class);
18 private final RefappLandlordRequests landlordRequests;
19
20 public RefappTenancyManager(final RefappLandlordRequests landlordRequests) {
21 this.landlordRequests = landlordRequests;
22 }
23
24 public void enableTenancy() {
25 if (!RefappTenancyCondition.isEnabled()) {
26 final String tenantId = getBaseUrlWithoutProtocol(ParameterUtils.getBaseUrl(UrlMode.ABSOLUTE));
27 try {
28 landlordRequests.acceptTenant(tenantId);
29 } catch (final LandlordRequestException elr) {
30 log.error("Unable to accept tenant", elr);
31 }
32 }
33 }
34
35
36 private String getBaseUrlWithoutProtocol(final String baseUrl) {
37 try {
38 final URL url = new URL(baseUrl);
39 return url.getAuthority() + url.getPath();
40 } catch (final Exception e) {
41 log.warn("Could not remove url protocol from '{}' : {}", baseUrl, e.getMessage());
42 return baseUrl;
43 }
44 }
45 }