Skip to content

Commit 44724f6

Browse files
Daan HooglandDaanHoogland
authored andcommitted
cleanup
1 parent f2d6356 commit 44724f6

25 files changed

+287
-355
lines changed

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapAddConfigurationCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class LdapAddConfigurationCmd extends BaseCmd {
4646
@Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, required = true, description = "Port")
4747
private int port;
4848

49-
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, required = false, entityType = DomainResponse.class, description = "linked domain")
49+
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "linked domain")
5050
private Long domainId;
5151

5252
public LdapAddConfigurationCmd() {

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.cloud.user.AccountService;
2222
import com.cloud.user.User;
2323
import com.cloud.user.UserAccount;
24+
import com.cloud.utils.StringUtils;
2425
import org.apache.cloudstack.acl.RoleType;
2526
import org.apache.cloudstack.api.APICommand;
2627
import org.apache.cloudstack.api.ApiConstants;
@@ -39,7 +40,6 @@
3940
import org.bouncycastle.util.encoders.Base64;
4041

4142
import javax.inject.Inject;
42-
import java.io.UnsupportedEncodingException;
4343
import java.security.NoSuchAlgorithmException;
4444
import java.security.SecureRandom;
4545
import java.util.Map;
@@ -107,7 +107,7 @@ public Account.Type getAccountType() {
107107
if (accountType == null) {
108108
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), null);
109109
}
110-
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), Account.Type.getFromValue(accountType.intValue()));
110+
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), Account.Type.getFromValue(accountType));
111111
}
112112

113113
public Long getRoleId() {
@@ -158,10 +158,10 @@ public void execute() throws ServerApiException {
158158
private String generatePassword() throws ServerApiException {
159159
try {
160160
final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG");
161-
final byte bytes[] = new byte[20];
161+
final byte[] bytes = new byte[20];
162162
randomGen.nextBytes(bytes);
163-
return new String(Base64.encode(bytes), "UTF-8");
164-
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
163+
return new String(Base64.encode(bytes), StringUtils.getPreferredCharset());
164+
} catch (NoSuchAlgorithmException e) {
165165
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate random password");
166166
}
167167
}
@@ -180,7 +180,7 @@ public long getEntityOwnerId() {
180180
return Account.ACCOUNT_ID_SYSTEM;
181181
}
182182

183-
private boolean validateUser(final LdapUser user) throws ServerApiException {
183+
private void validateUser(final LdapUser user) throws ServerApiException {
184184
if (user.getEmail() == null) {
185185
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username + " has no email address set within LDAP");
186186
}
@@ -190,6 +190,5 @@ private boolean validateUser(final LdapUser user) throws ServerApiException {
190190
if (user.getLastname() == null) {
191191
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, username + " has no lastname set within LDAP");
192192
}
193-
return true;
194193
}
195194
}

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapDeleteConfigurationCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public class LdapDeleteConfigurationCmd extends BaseCmd {
4444
@Parameter(name = ApiConstants.HOST_NAME, type = CommandType.STRING, required = true, description = "Hostname")
4545
private String hostname;
4646

47-
@Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, required = false, description = "port")
47+
@Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, description = "port")
4848
private int port;
4949

50-
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, required = false, entityType = DomainResponse.class, description = "linked domain")
50+
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "linked domain")
5151
private Long domainId;
5252

5353
public LdapDeleteConfigurationCmd() {

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapImportUsersCmd.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command;
1818

19-
import java.io.UnsupportedEncodingException;
2019
import java.security.NoSuchAlgorithmException;
2120
import java.security.SecureRandom;
2221
import java.util.ArrayList;
@@ -41,7 +40,6 @@
4140
import org.apache.cloudstack.ldap.LdapManager;
4241
import org.apache.cloudstack.ldap.LdapUser;
4342
import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException;
44-
import org.apache.commons.lang3.StringUtils;
4543
import org.bouncycastle.util.encoders.Base64;
4644

4745
import com.cloud.domain.Domain;
@@ -56,6 +54,7 @@
5654
import com.cloud.user.DomainService;
5755
import com.cloud.user.User;
5856
import com.cloud.user.UserAccount;
57+
import com.cloud.utils.StringUtils;
5958

6059
@APICommand(name = "importLdapUsers", description = "Import LDAP users", responseObject = LdapUserResponse.class, since = "4.3.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
6160
public class LdapImportUsersCmd extends BaseListCmd {
@@ -106,14 +105,14 @@ public LdapImportUsersCmd(final LdapManager ldapManager, final DomainService dom
106105
private void createCloudstackUserAccount(LdapUser user, String accountName, Domain domain) {
107106
Account account = _accountService.getActiveAccountByName(accountName, domain.getId());
108107
if (account == null) {
109-
logger.debug("No account exists with name: " + accountName + " creating the account and an user with name: " + user.getUsername() + " in the account");
108+
logger.debug("No account exists with name: {} creating the account and an user with name: {} in the account", accountName, user.getUsername());
110109
_accountService.createUserAccount(user.getUsername(), generatePassword(), user.getFirstname(), user.getLastname(), user.getEmail(), timezone, accountName, getAccountType(), getRoleId(),
111110
domain.getId(), domain.getNetworkDomain(), details, UUID.randomUUID().toString(), UUID.randomUUID().toString(), User.Source.LDAP);
112111
} else {
113112
// check if the user exists. if yes, call update
114113
UserAccount csuser = _accountService.getActiveUserAccount(user.getUsername(), domain.getId());
115114
if (csuser == null) {
116-
logger.debug("No user exists with name: " + user.getUsername() + " creating a user in the account: " + accountName);
115+
logger.debug("No user exists with name: {} creating a user in the account: {}", user.getUsername(), accountName);
117116
_accountService.createUser(user.getUsername(), generatePassword(), user.getFirstname(), user.getLastname(), user.getEmail(), timezone, accountName, domain.getId(),
118117
UUID.randomUUID().toString(), User.Source.LDAP);
119118
} else {
@@ -145,21 +144,21 @@ public void execute()
145144
users = _ldapManager.getUsers(domainId);
146145
}
147146
} catch (NoLdapUserMatchingQueryException ex) {
148-
users = new ArrayList<LdapUser>();
149-
logger.info("No Ldap user matching query. " + " ::: " + ex.getMessage());
147+
users = new ArrayList<>();
148+
logger.info("No Ldap user matching query. ::: {}", ex.getMessage());
150149
}
151150

152-
List<LdapUser> addedUsers = new ArrayList<LdapUser>();
151+
List<LdapUser> addedUsers = new ArrayList<>();
153152
for (LdapUser user : users) {
154153
Domain domain = getDomain(user);
155154
try {
156155
createCloudstackUserAccount(user, getAccountName(user), domain);
157156
addedUsers.add(user);
158157
} catch (InvalidParameterValueException ex) {
159-
logger.error("Failed to create user with username: " + user.getUsername() + " ::: " + ex.getMessage());
158+
logger.error("Failed to create user with username: {} ::: {}", user.getUsername(), ex.getMessage());
160159
}
161160
}
162-
ListResponse<LdapUserResponse> response = new ListResponse<LdapUserResponse>();
161+
ListResponse<LdapUserResponse> response = new ListResponse<>();
163162
response.setResponses(createLdapUserResponse(addedUsers));
164163
response.setResponseName(getCommandName());
165164
setResponseObject(response);
@@ -169,7 +168,7 @@ public Account.Type getAccountType() {
169168
if (accountType == null) {
170169
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), null);
171170
}
172-
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), Account.Type.getFromValue(accountType.intValue()));
171+
return RoleType.getAccountTypeByRole(roleService.findRole(roleId), Account.Type.getFromValue(accountType));
173172
}
174173

175174
public Long getRoleId() {
@@ -202,11 +201,11 @@ private Domain getDomainForName(String name) {
202201
private Domain getDomain(LdapUser user) {
203202
Domain domain;
204203
if (_domain != null) {
205-
//this means either domain id or groupname is passed and this will be same for all the users in this call. hence returning it.
204+
//this means either domain id or group name is passed and this will be same for all the users in this call. hence returning it.
206205
domain = _domain;
207206
} else {
208207
if (domainId != null) {
209-
// a domain Id is passed. use it for this user and all the users in the same api call (by setting _domain)
208+
// a domain ID is passed. use it for this user and all the users in the same api call (by setting _domain)
210209
domain = _domain = _domainService.getDomain(domainId);
211210
} else {
212211
// a group name is passed. use it for this user and all the users in the same api call(by setting _domain)
@@ -225,7 +224,7 @@ private Domain getDomain(LdapUser user) {
225224
}
226225

227226
private List<LdapUserResponse> createLdapUserResponse(List<LdapUser> users) {
228-
final List<LdapUserResponse> ldapResponses = new ArrayList<LdapUserResponse>();
227+
final List<LdapUserResponse> ldapResponses = new ArrayList<>();
229228
for (final LdapUser user : users) {
230229
final LdapUserResponse ldapResponse = _ldapManager.createLdapUserResponse(user);
231230
ldapResponse.setObjectName("LdapUser");
@@ -242,10 +241,10 @@ public String getCommandName() {
242241
private String generatePassword() throws ServerApiException {
243242
try {
244243
final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG");
245-
final byte bytes[] = new byte[20];
244+
final byte[] bytes = new byte[20];
246245
randomGen.nextBytes(bytes);
247-
return new String(Base64.encode(bytes), "UTF-8");
248-
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
246+
return new String(Base64.encode(bytes), StringUtils.getPreferredCharset());
247+
} catch (NoSuchAlgorithmException e) {
249248
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate random password");
250249
}
251250
}

plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapListConfigurationCmd.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ public class LdapListConfigurationCmd extends BaseListCmd {
4444
@Inject
4545
private LdapManager _ldapManager;
4646

47-
@Parameter(name = ApiConstants. HOST_NAME, type = CommandType.STRING, required = false, description = "Hostname")
47+
@Parameter(name = ApiConstants. HOST_NAME, type = CommandType.STRING, description = "Hostname")
4848
private String hostname;
4949

50-
@Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, required = false, description = "Port")
50+
@Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, description = "Port")
5151
private int port;
5252

53-
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, required = false, entityType = DomainResponse.class, description = "linked domain")
53+
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class,
54+
description = "linked domain")
5455
private Long domainId;
5556

56-
@Parameter(name = ApiConstants.LIST_ALL, type = CommandType.BOOLEAN, description = "If set to true, "
57-
+ " and no domainid specified, list all LDAP configurations irrespective of the linked domain", since = "4.13.2")
57+
@Parameter(name = ApiConstants.LIST_ALL, type = CommandType.BOOLEAN,
58+
description = "If set to true, and no `domainid` specified, list all LDAP configurations irrespective of the linked domain",
59+
since = "4.13.2")
5860
private Boolean listAll;
5961

6062
public LdapListConfigurationCmd() {
@@ -67,7 +69,7 @@ public LdapListConfigurationCmd(final LdapManager ldapManager) {
6769
}
6870

6971
private List<LdapConfigurationResponse> createLdapConfigurationResponses(final List<? extends LdapConfigurationVO> configurations) {
70-
final List<LdapConfigurationResponse> responses = new ArrayList<LdapConfigurationResponse>();
72+
final List<LdapConfigurationResponse> responses = new ArrayList<>();
7173
for (final LdapConfigurationVO resource : configurations) {
7274
final LdapConfigurationResponse configurationResponse = _ldapManager.createLdapConfigurationResponse(resource);
7375
configurationResponse.setObjectName("LdapConfiguration");
@@ -80,7 +82,7 @@ private List<LdapConfigurationResponse> createLdapConfigurationResponses(final L
8082
public void execute() {
8183
final Pair<List<? extends LdapConfigurationVO>, Integer> result = _ldapManager.listConfigurations(this);
8284
final List<LdapConfigurationResponse> responses = createLdapConfigurationResponses(result.first());
83-
final ListResponse<LdapConfigurationResponse> response = new ListResponse<LdapConfigurationResponse>();
85+
final ListResponse<LdapConfigurationResponse> response = new ListResponse<>();
8486
response.setResponses(responses, result.second());
8587
response.setResponseName(getCommandName());
8688
setResponseObject(response);

0 commit comments

Comments
 (0)