16
16
17
17
package org .wso2 .carbon .apimgt .rest .api .util .interceptors ;
18
18
19
+ import org .apache .commons .lang3 .StringUtils ;
19
20
import org .apache .commons .logging .Log ;
20
21
import org .apache .commons .logging .LogFactory ;
21
22
import org .apache .cxf .message .Message ;
22
23
import org .apache .cxf .phase .AbstractPhaseInterceptor ;
23
24
import org .apache .cxf .phase .Phase ;
24
25
import org .wso2 .carbon .apimgt .api .APIConsumer ;
25
26
import org .wso2 .carbon .apimgt .api .APIManagementException ;
27
+ import org .wso2 .carbon .apimgt .api .model .Application ;
28
+ import org .wso2 .carbon .apimgt .api .model .OrganizationInfo ;
26
29
import org .wso2 .carbon .apimgt .api .model .Subscriber ;
27
30
import org .wso2 .carbon .apimgt .impl .APIConstants ;
28
31
import org .wso2 .carbon .apimgt .impl .utils .APIUtil ;
32
+ import org .wso2 .carbon .apimgt .impl .utils .ApplicationUtils ;
29
33
import org .wso2 .carbon .apimgt .rest .api .common .RestApiCommonUtil ;
30
34
import org .wso2 .carbon .apimgt .rest .api .common .RestApiConstants ;
31
35
import org .wso2 .carbon .apimgt .rest .api .util .MethodStats ;
32
36
import org .wso2 .carbon .apimgt .rest .api .util .utils .RestApiUtil ;
33
37
import org .wso2 .carbon .context .PrivilegedCarbonContext ;
34
38
import org .wso2 .carbon .registry .core .exceptions .RegistryException ;
39
+ import org .wso2 .carbon .utils .multitenancy .MultitenantUtils ;
35
40
36
41
import java .util .Arrays ;
37
42
import javax .cache .Cache ;
@@ -76,6 +81,11 @@ public void handleMessage(Message message) {
76
81
// check the existence in the database
77
82
String groupId = RestApiUtil .getLoggedInUserGroupId ();
78
83
String tenantDomain = RestApiCommonUtil .getLoggedInUserTenantDomain ();
84
+ OrganizationInfo orgInfo = (OrganizationInfo ) message .get (RestApiConstants .ORGANIZATION_INFO );
85
+ String organizationId = null ;
86
+ if (orgInfo != null && !StringUtils .isEmpty (orgInfo .getOrganizationId ())) {
87
+ organizationId = orgInfo .getOrganizationId ();
88
+ }
79
89
try {
80
90
//takes a consumer object using the user set in thread local carbon context
81
91
APIConsumer apiConsumer = RestApiCommonUtil .getLoggedInUserConsumer ();
@@ -98,7 +108,7 @@ public void handleMessage(Message message) {
98
108
if (!APIConstants .SUPER_TENANT_DOMAIN .equalsIgnoreCase (tenantDomain )) {
99
109
loadTenantRegistry ();
100
110
}
101
- apiConsumer .addSubscriber (username , groupId );
111
+ apiConsumer .addSubscriber (username , groupId , organizationId );
102
112
103
113
// The subscriber object added here is not a complete subscriber object. It will only contain
104
114
// username
@@ -108,9 +118,24 @@ public void handleMessage(Message message) {
108
118
}
109
119
}
110
120
}
111
- } else {
112
- subscriberCache .put (username , subscriber );
113
- }
121
+ } else {
122
+ if (organizationId != null && APIUtil .isDefaultApplicationCreationEnabled () && !APIUtil
123
+ .isDefaultApplicationCreationDisabledForTenant (MultitenantUtils .getTenantDomain (username ))) {
124
+ Application defaultAPP = ApplicationUtils .retrieveApplication (APIConstants .DEFAULT_APPLICATION_NAME ,
125
+ username , groupId );
126
+ if (defaultAPP .getSubOrganization () == null ) {
127
+ synchronized ((username + LOCK_POSTFIX ).intern ()) {
128
+ defaultAPP = ApplicationUtils .retrieveApplication (APIConstants .DEFAULT_APPLICATION_NAME ,
129
+ username , groupId );
130
+ if (defaultAPP .getSubOrganization () == null ) {
131
+ defaultAPP .setSubOrganization (organizationId );
132
+ apiConsumer .updateApplication (defaultAPP );
133
+ }
134
+ }
135
+ }
136
+ }
137
+ subscriberCache .put (username , subscriber );
138
+ }
114
139
} catch (APIManagementException e ) {
115
140
RestApiUtil .handleInternalServerError ("Unable to add the subscriber " + username , e , logger );
116
141
}
0 commit comments