Search in sources :

Example 1 with ResponseObject

use of org.apache.cloudstack.api.ResponseObject in project cloudstack by apache.

the class AsyncJobJoinDaoImpl method newAsyncJobResponse.

@Override
public AsyncJobResponse newAsyncJobResponse(final AsyncJobJoinVO job) {
    final AsyncJobResponse jobResponse = new AsyncJobResponse();
    jobResponse.setAccountId(job.getAccountUuid());
    jobResponse.setUserId(job.getUserUuid());
    jobResponse.setCmd(job.getCmd());
    jobResponse.setCreated(job.getCreated());
    jobResponse.setRemoved(job.getRemoved());
    jobResponse.setJobId(job.getUuid());
    jobResponse.setJobStatus(job.getStatus());
    jobResponse.setJobProcStatus(job.getProcessStatus());
    if (job.getInstanceType() != null && job.getInstanceId() != null) {
        jobResponse.setJobInstanceType(job.getInstanceType().toString());
        jobResponse.setJobInstanceId(job.getInstanceUuid());
    }
    jobResponse.setJobResultCode(job.getResultCode());
    final boolean savedValue = SerializationContext.current().getUuidTranslation();
    SerializationContext.current().setUuidTranslation(false);
    final Object resultObject = ApiSerializerHelper.fromSerializedString(job.getResult());
    jobResponse.setJobResult((ResponseObject) resultObject);
    SerializationContext.current().setUuidTranslation(savedValue);
    if (resultObject != null) {
        final Class<?> clz = resultObject.getClass();
        if (clz.isPrimitive() || clz.getSuperclass() == Number.class || clz == String.class || clz == Date.class) {
            jobResponse.setJobResultType("text");
        } else {
            jobResponse.setJobResultType("object");
        }
    }
    jobResponse.setObjectName("asyncjobs");
    return jobResponse;
}
Also used : AsyncJobResponse(org.apache.cloudstack.api.response.AsyncJobResponse) ResponseObject(org.apache.cloudstack.api.ResponseObject) Date(java.util.Date)

Example 2 with ResponseObject

use of org.apache.cloudstack.api.ResponseObject 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");
            }
        }
    }
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ResponseObject(org.apache.cloudstack.api.ResponseObject) LdapUserResponse(org.apache.cloudstack.api.response.LdapUserResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with ResponseObject

use of org.apache.cloudstack.api.ResponseObject 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;
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) AsyncJobResponse(org.apache.cloudstack.api.response.AsyncJobResponse) ResponseObject(org.apache.cloudstack.api.ResponseObject) CreateCmdResponse(org.apache.cloudstack.api.response.CreateCmdResponse) AuthenticationCmdResponse(org.apache.cloudstack.api.response.AuthenticationCmdResponse)

Example 4 with ResponseObject

use of org.apache.cloudstack.api.ResponseObject in project cloudstack by apache.

the class ApiServer method buildAsyncListResponse.

@SuppressWarnings("unchecked")
private void buildAsyncListResponse(final BaseListCmd command, final Account account) {
    final List<ResponseObject> responses = ((ListResponse) command.getResponseObject()).getResponses();
    if (responses != null && responses.size() > 0) {
        List<? extends AsyncJob> jobs = null;
        // list all jobs for ROOT admin
        if (accountMgr.isRootAdmin(account.getId())) {
            jobs = asyncMgr.findInstancePendingAsyncJobs(command.getInstanceType().toString(), null);
        } else {
            jobs = asyncMgr.findInstancePendingAsyncJobs(command.getInstanceType().toString(), account.getId());
        }
        if (jobs.size() == 0) {
            return;
        }
        final Map<String, AsyncJob> objectJobMap = new HashMap<String, AsyncJob>();
        for (final AsyncJob job : jobs) {
            if (job.getInstanceId() == null) {
                continue;
            }
            final String instanceUuid = ApiDBUtils.findJobInstanceUuid(job);
            objectJobMap.put(instanceUuid, job);
        }
        for (final ResponseObject response : responses) {
            if (response.getObjectId() != null && objectJobMap.containsKey(response.getObjectId())) {
                final AsyncJob job = objectJobMap.get(response.getObjectId());
                response.setJobId(job.getUuid());
                response.setJobStatus(job.getStatus().ordinal());
            }
        }
    }
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) HashMap(java.util.HashMap) ResponseObject(org.apache.cloudstack.api.ResponseObject) AsyncJob(org.apache.cloudstack.framework.jobs.AsyncJob)

Example 5 with ResponseObject

use of org.apache.cloudstack.api.ResponseObject in project cloudstack by apache.

the class ApiResponseSerializer method serializeResponseObjFieldsXML.

private static void serializeResponseObjFieldsXML(StringBuilder sb, StringBuilder log, ResponseObject obj) {
    boolean isAsync = false;
    if (obj instanceof AsyncJobResponse)
        isAsync = true;
    Field[] fields = getFlattenFields(obj.getClass());
    for (Field field : fields) {
        if ((field.getModifiers() & Modifier.TRANSIENT) != 0) {
            // skip transient fields
            continue;
        }
        SerializedName serializedName = field.getAnnotation(SerializedName.class);
        if (serializedName == null) {
            // skip fields w/o serialized name
            continue;
        }
        boolean logField = true;
        Param param = field.getAnnotation(Param.class);
        if (param != null) {
            RoleType[] allowedRoles = param.authorized();
            if (allowedRoles.length > 0) {
                boolean permittedParameter = false;
                Account caller = CallContext.current().getCallingAccount();
                for (RoleType allowedRole : allowedRoles) {
                    if (allowedRole.getAccountType() == caller.getType()) {
                        permittedParameter = true;
                        break;
                    }
                }
                if (!permittedParameter) {
                    s_logger.trace("Ignoring parameter " + param.name() + " as the caller is not authorized to see it");
                    continue;
                }
            }
            if (param.isSensitive()) {
                logField = false;
            }
        }
        field.setAccessible(true);
        Object fieldValue = null;
        try {
            fieldValue = field.get(obj);
        } catch (IllegalArgumentException e) {
            throw new CloudRuntimeException("how illegal is it?", e);
        } catch (IllegalAccessException e) {
            throw new CloudRuntimeException("come on...we set accessible already", e);
        }
        if (fieldValue != null) {
            if (fieldValue instanceof ResponseObject) {
                ResponseObject subObj = (ResponseObject) fieldValue;
                if (isAsync) {
                    sb.append("<jobresult>");
                    log.append("<jobresult>");
                }
                serializeResponseObjXML(sb, log, subObj);
                if (isAsync) {
                    sb.append("</jobresult>");
                    log.append("</jobresult>");
                }
            } else if (fieldValue instanceof Collection<?>) {
                Collection<?> subResponseList = (Collection<?>) fieldValue;
                boolean usedUuidList = false;
                for (Object value : subResponseList) {
                    if (value instanceof ResponseObject) {
                        ResponseObject subObj = (ResponseObject) value;
                        if (serializedName != null) {
                            subObj.setObjectName(serializedName.value());
                        }
                        serializeResponseObjXML(sb, log, subObj);
                    } else if (value instanceof ExceptionProxyObject) {
                        // Only exception reponses carry a list of
                        // ExceptionProxyObject objects.
                        ExceptionProxyObject idProxy = (ExceptionProxyObject) value;
                        // encountered, put in a uuidList tag.
                        if (!usedUuidList) {
                            sb.append("<" + serializedName.value() + ">");
                            log.append("<" + serializedName.value() + ">");
                            usedUuidList = true;
                        }
                        sb.append("<" + "uuid" + ">" + idProxy.getUuid() + "</" + "uuid" + ">");
                        log.append("<" + "uuid" + ">" + idProxy.getUuid() + "</" + "uuid" + ">");
                        // Append the new descriptive property also.
                        String idFieldName = idProxy.getDescription();
                        if (idFieldName != null) {
                            sb.append("<" + "uuidProperty" + ">" + idFieldName + "</" + "uuidProperty" + ">");
                            log.append("<" + "uuidProperty" + ">" + idFieldName + "</" + "uuidProperty" + ">");
                        }
                    } else if (value instanceof String) {
                        sb.append("<").append(serializedName.value()).append(">").append(value).append("</").append(serializedName.value()).append(">");
                        if (logField) {
                            log.append("<").append(serializedName.value()).append(">").append(value).append("</").append(serializedName.value()).append(">");
                        }
                    }
                }
                if (usedUuidList) {
                    // close the uuidList.
                    sb.append("</").append(serializedName.value()).append(">");
                    log.append("</").append(serializedName.value()).append(">");
                }
            } else if (fieldValue instanceof Date) {
                sb.append("<").append(serializedName.value()).append(">").append(BaseCmd.getDateString((Date) fieldValue)).append("</").append(serializedName.value()).append(">");
                log.append("<").append(serializedName.value()).append(">").append(BaseCmd.getDateString((Date) fieldValue)).append("</").append(serializedName.value()).append(">");
            } else {
                String resultString = escapeSpecialXmlChars(fieldValue.toString());
                if (!(obj instanceof ExceptionResponse)) {
                    resultString = encodeParam(resultString);
                }
                sb.append("<").append(serializedName.value()).append(">").append(resultString).append("</").append(serializedName.value()).append(">");
                if (logField) {
                    log.append("<").append(serializedName.value()).append(">").append(resultString).append("</").append(serializedName.value()).append(">");
                }
            }
        }
    }
}
Also used : Account(com.cloud.user.Account) ExceptionResponse(org.apache.cloudstack.api.response.ExceptionResponse) RoleType(org.apache.cloudstack.acl.RoleType) AsyncJobResponse(org.apache.cloudstack.api.response.AsyncJobResponse) SerializedName(com.google.gson.annotations.SerializedName) Date(java.util.Date) Field(java.lang.reflect.Field) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Param(com.cloud.serializer.Param) ResponseObject(org.apache.cloudstack.api.ResponseObject) Collection(java.util.Collection) ExceptionProxyObject(com.cloud.utils.exception.ExceptionProxyObject) ResponseObject(org.apache.cloudstack.api.ResponseObject) ExceptionProxyObject(com.cloud.utils.exception.ExceptionProxyObject)

Aggregations

ResponseObject (org.apache.cloudstack.api.ResponseObject)9 Gson (com.google.gson.Gson)3 AsyncJobResponse (org.apache.cloudstack.api.response.AsyncJobResponse)3 ListResponse (org.apache.cloudstack.api.response.ListResponse)3 ExceptionProxyObject (com.cloud.utils.exception.ExceptionProxyObject)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Param (com.cloud.serializer.Param)1 Account (com.cloud.user.Account)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializedName (com.google.gson.annotations.SerializedName)1 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1 Enumeration (java.util.Enumeration)1 RoleType (org.apache.cloudstack.acl.RoleType)1 AuthenticationCmdResponse (org.apache.cloudstack.api.response.AuthenticationCmdResponse)1 CreateCmdResponse (org.apache.cloudstack.api.response.CreateCmdResponse)1 ExceptionResponse (org.apache.cloudstack.api.response.ExceptionResponse)1