Search in sources :

Example 1 with AsyncJobResponse

use of org.apache.cloudstack.api.response.AsyncJobResponse 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 AsyncJobResponse

use of org.apache.cloudstack.api.response.AsyncJobResponse 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 3 with AsyncJobResponse

use of org.apache.cloudstack.api.response.AsyncJobResponse 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;
}
Also used : SuccessResponse(org.apache.cloudstack.api.response.SuccessResponse) ExceptionResponse(org.apache.cloudstack.api.response.ExceptionResponse) ListResponse(org.apache.cloudstack.api.response.ListResponse) AsyncJobResponse(org.apache.cloudstack.api.response.AsyncJobResponse) Gson(com.google.gson.Gson) CreateCmdResponse(org.apache.cloudstack.api.response.CreateCmdResponse) AuthenticationCmdResponse(org.apache.cloudstack.api.response.AuthenticationCmdResponse)

Example 4 with AsyncJobResponse

use of org.apache.cloudstack.api.response.AsyncJobResponse in project cloudstack by apache.

the class ApiServer method getBaseAsyncResponse.

private String getBaseAsyncResponse(final long jobId, final BaseAsyncCmd cmd) {
    final AsyncJobResponse response = new AsyncJobResponse();
    final AsyncJob job = entityMgr.findById(AsyncJob.class, jobId);
    response.setJobId(job.getUuid());
    response.setResponseName(cmd.getCommandName());
    return ApiResponseSerializer.toSerializedString(response, cmd.getResponseType());
}
Also used : AsyncJobResponse(org.apache.cloudstack.api.response.AsyncJobResponse) AsyncJob(org.apache.cloudstack.framework.jobs.AsyncJob)

Example 5 with AsyncJobResponse

use of org.apache.cloudstack.api.response.AsyncJobResponse in project cloudstack by apache.

the class AsyncJobJoinDaoTest method testNewAsyncJobResponseNullValues.

@Test
public void testNewAsyncJobResponseNullValues() {
    final AsyncJobJoinVO job = new AsyncJobJoinVO();
    final AsyncJobResponse response = dao.newAsyncJobResponse(job);
    Assert.assertEquals(job.getUuid(), response.getJobId());
    Assert.assertEquals(job.getAccountUuid(), ReflectionTestUtils.getField(response, "accountId"));
    Assert.assertEquals(job.getUserUuid(), ReflectionTestUtils.getField(response, "userId"));
    Assert.assertEquals(job.getCmd(), ReflectionTestUtils.getField(response, "cmd"));
    Assert.assertEquals(job.getStatus(), ReflectionTestUtils.getField(response, "jobStatus"));
    Assert.assertEquals(job.getStatus(), ReflectionTestUtils.getField(response, "jobProcStatus"));
    Assert.assertEquals(job.getResultCode(), ReflectionTestUtils.getField(response, "jobResultCode"));
    Assert.assertEquals(null, ReflectionTestUtils.getField(response, "jobResultType"));
    Assert.assertEquals(job.getResult(), ReflectionTestUtils.getField(response, "jobResult"));
    Assert.assertEquals(job.getInstanceType(), ReflectionTestUtils.getField(response, "jobInstanceType"));
    Assert.assertEquals(job.getInstanceUuid(), ReflectionTestUtils.getField(response, "jobInstanceId"));
    Assert.assertEquals(job.getCreated(), ReflectionTestUtils.getField(response, "created"));
    Assert.assertEquals(job.getRemoved(), ReflectionTestUtils.getField(response, "removed"));
}
Also used : AsyncJobJoinVO(com.cloud.api.query.vo.AsyncJobJoinVO) AsyncJobResponse(org.apache.cloudstack.api.response.AsyncJobResponse) Test(org.junit.Test)

Aggregations

AsyncJobResponse (org.apache.cloudstack.api.response.AsyncJobResponse)8 Date (java.util.Date)3 ResponseObject (org.apache.cloudstack.api.ResponseObject)3 AsyncJobJoinVO (com.cloud.api.query.vo.AsyncJobJoinVO)2 AuthenticationCmdResponse (org.apache.cloudstack.api.response.AuthenticationCmdResponse)2 CreateCmdResponse (org.apache.cloudstack.api.response.CreateCmdResponse)2 ExceptionResponse (org.apache.cloudstack.api.response.ExceptionResponse)2 ListResponse (org.apache.cloudstack.api.response.ListResponse)2 Test (org.junit.Test)2 Param (com.cloud.serializer.Param)1 Account (com.cloud.user.Account)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 ExceptionProxyObject (com.cloud.utils.exception.ExceptionProxyObject)1 Gson (com.google.gson.Gson)1 SerializedName (com.google.gson.annotations.SerializedName)1 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1 RoleType (org.apache.cloudstack.acl.RoleType)1 SuccessResponse (org.apache.cloudstack.api.response.SuccessResponse)1 AsyncJob (org.apache.cloudstack.framework.jobs.AsyncJob)1