use of org.apache.cloudstack.api.response.ListResponse in project cloudstack by apache.
the class LdapListUsersCmdTest method successfulEmptyResponseFromExecute.
/**
* given: "We have an LdapManager with no users, QueryService and a LdapListUsersCmd"
* when: "LdapListUsersCmd is executed"
* then: "An array of size 0 is returned"
*
* @throws NoLdapUserMatchingQueryException
*/
@Test
public void successfulEmptyResponseFromExecute() throws NoLdapUserMatchingQueryException {
doThrow(new NoLdapUserMatchingQueryException("")).when(ldapManager).getUsers(null);
ldapListUsersCmd.execute();
assertEquals(0, ((ListResponse) ldapListUsersCmd.getResponseObject()).getResponses().size());
}
use of org.apache.cloudstack.api.response.ListResponse in project cloudstack by apache.
the class LdapListUsersCmdTest method applyAnyDomain.
/**
* filter all acs users
*
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
@Test
public void applyAnyDomain() throws NoSuchFieldException, IllegalAccessException, NoLdapUserMatchingQueryException {
mockACSUserSearch();
mockResponseCreation();
useSubdomain();
setHiddenField(ldapListUsersCmd, "userFilter", "AnyDomain");
setHiddenField(ldapListUsersCmd, "domainId", 2l);
ldapListUsersCmd.execute();
// 'rmurphy' annotated with native
// 'bob' still in
// 'abhi' is filtered out
List<ResponseObject> responses = ((ListResponse) ldapListUsersCmd.getResponseObject()).getResponses();
assertEquals(2, responses.size());
for (ResponseObject response : responses) {
if (!(response instanceof LdapUserResponse)) {
fail("unexpected return-type from API backend method");
} else {
LdapUserResponse userResponse = (LdapUserResponse) response;
// further validate this user
if ("rmurphy".equals(userResponse.getUsername()) && !User.Source.NATIVE.toString().equalsIgnoreCase(userResponse.getUserSource())) {
fail("expected murphy from ldap");
}
if ("bob".equals(userResponse.getUsername()) && !"".equals(userResponse.getUserSource())) {
fail("expected bob from without usersource");
}
}
}
}
use of org.apache.cloudstack.api.response.ListResponse in project cloudstack by apache.
the class LdapImportUsersCmd method execute.
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
if (getAccountType() == null && getRoleId() == null) {
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Both account type and role ID are not provided");
}
List<LdapUser> users;
try {
if (StringUtils.isNotBlank(groupName)) {
users = _ldapManager.getUsersInGroup(groupName, domainId);
} else {
users = _ldapManager.getUsers(domainId);
}
} catch (NoLdapUserMatchingQueryException ex) {
users = new ArrayList<LdapUser>();
s_logger.info("No Ldap user matching query. " + " ::: " + ex.getMessage());
}
List<LdapUser> addedUsers = new ArrayList<LdapUser>();
for (LdapUser user : users) {
Domain domain = getDomain(user);
try {
createCloudstackUserAccount(user, getAccountName(user), domain);
addedUsers.add(user);
} catch (InvalidParameterValueException ex) {
s_logger.error("Failed to create user with username: " + user.getUsername() + " ::: " + ex.getMessage());
}
}
ListResponse<LdapUserResponse> response = new ListResponse<LdapUserResponse>();
response.setResponses(createLdapUserResponse(addedUsers));
response.setResponseName(getCommandName());
setResponseObject(response);
}
use of org.apache.cloudstack.api.response.ListResponse in project cloudstack by apache.
the class ApiResponseSerializer method toXMLSerializedString.
private static String toXMLSerializedString(ResponseObject result, StringBuilder log) {
if (result != null && log != null) {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<").append(result.getResponseName()).append(" cloud-stack-version=\"").append(ApiDBUtils.getVersion()).append("\">");
log.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
log.append("<").append(result.getResponseName()).append(" cloud-stack-version=\"").append(ApiDBUtils.getVersion()).append("\">");
if (result instanceof ListResponse) {
Integer count = ((ListResponse) result).getCount();
if (count != null && count != 0) {
sb.append("<").append(ApiConstants.COUNT).append(">").append(((ListResponse) result).getCount()).append("</").append(ApiConstants.COUNT).append(">");
log.append("<").append(ApiConstants.COUNT).append(">").append(((ListResponse) result).getCount()).append("</").append(ApiConstants.COUNT).append(">");
}
List<? extends ResponseObject> responses = ((ListResponse) result).getResponses();
if ((responses != null) && !responses.isEmpty()) {
for (ResponseObject obj : responses) {
serializeResponseObjXML(sb, log, obj);
}
}
} else {
if (result instanceof CreateCmdResponse || result instanceof AsyncJobResponse || result instanceof AuthenticationCmdResponse) {
serializeResponseObjFieldsXML(sb, log, result);
} else {
serializeResponseObjXML(sb, log, result);
}
}
sb.append("</").append(result.getResponseName()).append(">");
log.append("</").append(result.getResponseName()).append(">");
return sb.toString();
}
return null;
}
use of org.apache.cloudstack.api.response.ListResponse in project cloudstack by apache.
the class ApiResponseSerializer method toJSONSerializedString.
public static String toJSONSerializedString(ResponseObject result, StringBuilder log) {
if (result != null && log != null) {
Gson responseBuilder = ApiResponseGsonHelper.getBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create();
Gson logBuilder = ApiResponseGsonHelper.getLogBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create();
StringBuilder sb = new StringBuilder();
sb.append("{\"").append(result.getResponseName()).append("\":");
log.append("{\"").append(result.getResponseName()).append("\":");
if (result instanceof ListResponse) {
List<? extends ResponseObject> responses = ((ListResponse) result).getResponses();
Integer count = ((ListResponse) result).getCount();
boolean nonZeroCount = (count != null && count.longValue() != 0);
if (nonZeroCount) {
sb.append("{\"").append(ApiConstants.COUNT).append("\":").append(count);
log.append("{\"").append(ApiConstants.COUNT).append("\":").append(count);
}
if ((responses != null) && !responses.isEmpty()) {
String jsonStr = responseBuilder.toJson(responses.get(0));
jsonStr = unescape(jsonStr);
String logStr = logBuilder.toJson(responses.get(0));
logStr = unescape(logStr);
if (nonZeroCount) {
sb.append(",\"").append(responses.get(0).getObjectName()).append("\":[").append(jsonStr);
log.append(",\"").append(responses.get(0).getObjectName()).append("\":[").append(logStr);
}
for (int i = 1; i < ((ListResponse) result).getResponses().size(); i++) {
jsonStr = responseBuilder.toJson(responses.get(i));
jsonStr = unescape(jsonStr);
logStr = logBuilder.toJson(responses.get(i));
logStr = unescape(logStr);
sb.append(",").append(jsonStr);
log.append(",").append(logStr);
}
sb.append("]}");
log.append("]}");
} else {
if (!nonZeroCount) {
sb.append("{");
log.append("{");
}
sb.append("}");
log.append("}");
}
} else if (result instanceof SuccessResponse || result instanceof ExceptionResponse) {
final String jsonErrorText = unescape(responseBuilder.toJson(result));
sb.append(jsonErrorText);
log.append(jsonErrorText);
} else {
String jsonStr = responseBuilder.toJson(result);
if (jsonStr != null && !jsonStr.isEmpty()) {
jsonStr = unescape(jsonStr);
if (result instanceof AsyncJobResponse || result instanceof CreateCmdResponse || result instanceof AuthenticationCmdResponse) {
sb.append(jsonStr);
} else {
sb.append("{\"").append(result.getObjectName()).append("\":").append(jsonStr).append("}");
}
} else {
sb.append("{}");
}
String logStr = logBuilder.toJson(result);
if (logStr != null && !logStr.isEmpty()) {
logStr = unescape(logStr);
if (result instanceof AsyncJobResponse || result instanceof CreateCmdResponse || result instanceof AuthenticationCmdResponse) {
log.append(logStr);
} else {
log.append("{\"").append(result.getObjectName()).append("\":").append(logStr).append("}");
}
} else {
log.append("{}");
}
}
sb.append("}");
log.append("}");
return sb.toString();
}
return null;
}
Aggregations