示例#1
0
 private void writeObject(ObjectOutputStream stream) throws IOException {
   stream.defaultWriteObject();
   Type<E> type = SerializationContext.getType(entityClass);
   EntityProxy<E> proxy = type.proxyProvider().apply(entity);
   for (Property<E, ?> property : proxy.filterProperties(getPropertyFilter())) {
     Object value = property.get();
     stream.writeObject(value);
   }
 }
示例#2
0
  @Override
  public String getSerializedApiError(
      ServerApiException ex, Map<String, Object[]> apiCommandParams, String responseType) {
    String responseName = null;
    Class<?> cmdClass = null;
    String responseText = null;

    if (ex == null) {
      // this call should not be invoked with null exception
      return getSerializedApiError(
          HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
          "Some internal error happened",
          apiCommandParams,
          responseType);
    }
    try {
      if (ex.getErrorCode() == ApiErrorCode.UNSUPPORTED_ACTION_ERROR
          || apiCommandParams == null
          || apiCommandParams.isEmpty()) {
        responseName = "errorresponse";
      } else {
        Object cmdObj = apiCommandParams.get("command");
        // cmd name can be null when "command" parameter is missing in
        // the request
        if (cmdObj != null) {
          String cmdName = ((String[]) cmdObj)[0];
          cmdClass = getCmdClass(cmdName);
          if (cmdClass != null) {
            responseName = ((BaseCmd) cmdClass.newInstance()).getCommandName();
          } else {
            responseName = "errorresponse";
          }
        }
      }
      ExceptionResponse apiResponse = new ExceptionResponse();
      apiResponse.setErrorCode(ex.getErrorCode().getHttpCode());
      apiResponse.setErrorText(ex.getDescription());
      apiResponse.setResponseName(responseName);
      ArrayList<ExceptionProxyObject> idList = ex.getIdProxyList();
      if (idList != null) {
        for (int i = 0; i < idList.size(); i++) {
          apiResponse.addProxyObject(idList.get(i));
        }
      }
      // Also copy over the cserror code and the function/layer in which
      // it was thrown.
      apiResponse.setCSErrorCode(ex.getCSErrorCode());

      SerializationContext.current().setUuidTranslation(true);
      responseText = ApiResponseSerializer.toSerializedString(apiResponse, responseType);

    } catch (Exception e) {
      s_logger.error("Exception responding to http request", e);
    }
    return responseText;
  }
示例#3
0
 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
   stream.defaultReadObject();
   Type<E> type = SerializationContext.getType(entityClass);
   entity = type.factory().get();
   EntityProxy<E> proxy = type.proxyProvider().apply(entity);
   for (Property<E, ?> property : proxy.filterProperties(getPropertyFilter())) {
     Object value = stream.readObject();
     property.setObject(value, PropertyState.LOADED);
   }
 }
示例#4
0
  @Override
  public String getSerializedApiError(
      int errorCode,
      String errorText,
      Map<String, Object[]> apiCommandParams,
      String responseType) {
    String responseName = null;
    Class<?> cmdClass = null;
    String responseText = null;

    try {
      if (apiCommandParams == null || apiCommandParams.isEmpty()) {
        responseName = "errorresponse";
      } else {
        Object cmdObj = apiCommandParams.get("command");
        // cmd name can be null when "command" parameter is missing in the request
        if (cmdObj != null) {
          String cmdName = ((String[]) cmdObj)[0];
          cmdClass = getCmdClass(cmdName);
          if (cmdClass != null) {
            responseName = ((BaseCmd) cmdClass.newInstance()).getCommandName();
          } else {
            responseName = "errorresponse";
          }
        }
      }
      ExceptionResponse apiResponse = new ExceptionResponse();
      apiResponse.setErrorCode(errorCode);
      apiResponse.setErrorText(errorText);
      apiResponse.setResponseName(responseName);
      SerializationContext.current().setUuidTranslation(true);
      responseText = ApiResponseSerializer.toSerializedString(apiResponse, responseType);

    } catch (Exception e) {
      s_logger.error("Exception responding to http request", e);
    }
    return responseText;
  }
示例#5
0
  private String queueCommand(BaseCmd cmdObj, Map<String, String> params) throws Exception {
    CallContext ctx = CallContext.current();
    Long callerUserId = ctx.getCallingUserId();
    Account caller = ctx.getCallingAccount();

    // Queue command based on Cmd super class:
    // BaseCmd: cmd is dispatched to ApiDispatcher, executed, serialized and returned.
    // BaseAsyncCreateCmd: cmd params are processed and create() is called, then same workflow as
    // BaseAsyncCmd.
    // BaseAsyncCmd: cmd is processed and submitted as an AsyncJob, job related info is serialized
    // and returned.
    if (cmdObj instanceof BaseAsyncCmd) {
      Long objectId = null;
      String objectUuid = null;
      if (cmdObj instanceof BaseAsyncCreateCmd) {
        BaseAsyncCreateCmd createCmd = (BaseAsyncCreateCmd) cmdObj;
        _dispatcher.dispatchCreateCmd(createCmd, params);
        objectId = createCmd.getEntityId();
        objectUuid = createCmd.getEntityUuid();
        params.put("id", objectId.toString());
      } else {
        ApiDispatcher.processParameters(cmdObj, params);
      }

      BaseAsyncCmd asyncCmd = (BaseAsyncCmd) cmdObj;

      if (callerUserId != null) {
        params.put("ctxUserId", callerUserId.toString());
      }
      if (caller != null) {
        params.put("ctxAccountId", String.valueOf(caller.getId()));
      }

      long startEventId = ctx.getStartEventId();
      asyncCmd.setStartEventId(startEventId);

      // save the scheduled event
      Long eventId =
          ActionEventUtils.onScheduledActionEvent(
              (callerUserId == null) ? User.UID_SYSTEM : callerUserId,
              asyncCmd.getEntityOwnerId(),
              asyncCmd.getEventType(),
              asyncCmd.getEventDescription(),
              startEventId);
      if (startEventId == 0) {
        // There was no create event before, set current event id as start eventId
        startEventId = eventId;
      }

      params.put("ctxStartEventId", String.valueOf(startEventId));
      params.put("cmdEventType", asyncCmd.getEventType().toString());

      Long instanceId = (objectId == null) ? asyncCmd.getInstanceId() : objectId;
      AsyncJobVO job =
          new AsyncJobVO(
              ctx.getContextId(),
              callerUserId,
              caller.getId(),
              cmdObj.getClass().getName(),
              ApiGsonHelper.getBuilder().create().toJson(params),
              instanceId,
              asyncCmd.getInstanceType() != null ? asyncCmd.getInstanceType().toString() : null);
      job.setDispatcher(_asyncDispatcher.getName());

      long jobId = _asyncMgr.submitAsyncJob(job);

      if (jobId == 0L) {
        String errorMsg = "Unable to schedule async job for command " + job.getCmd();
        s_logger.warn(errorMsg);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, errorMsg);
      }

      if (objectId != null) {
        String objUuid = (objectUuid == null) ? objectId.toString() : objectUuid;
        return getBaseAsyncCreateResponse(jobId, (BaseAsyncCreateCmd) asyncCmd, objUuid);
      } else {
        SerializationContext.current().setUuidTranslation(true);
        return getBaseAsyncResponse(jobId, asyncCmd);
      }
    } else {
      _dispatcher.dispatch(cmdObj, params, false);

      // if the command is of the listXXXCommand, we will need to also return the
      // the job id and status if possible
      // For those listXXXCommand which we have already created DB views, this step is not needed
      // since async job is joined in their db views.
      if (cmdObj instanceof BaseListCmd
          && !(cmdObj instanceof ListVMsCmd)
          && !(cmdObj instanceof ListRoutersCmd)
          && !(cmdObj instanceof ListSecurityGroupsCmd)
          && !(cmdObj instanceof ListTagsCmd)
          && !(cmdObj instanceof ListEventsCmd)
          && !(cmdObj instanceof ListVMGroupsCmd)
          && !(cmdObj instanceof ListProjectsCmd)
          && !(cmdObj instanceof ListProjectAccountsCmd)
          && !(cmdObj instanceof ListProjectInvitationsCmd)
          && !(cmdObj instanceof ListHostsCmd)
          && !(cmdObj instanceof ListVolumesCmd)
          && !(cmdObj instanceof ListUsersCmd)
          && !(cmdObj instanceof ListAccountsCmd)
          && !(cmdObj instanceof ListStoragePoolsCmd)
          && !(cmdObj instanceof ListDiskOfferingsCmd)
          && !(cmdObj instanceof ListServiceOfferingsCmd)
          && !(cmdObj instanceof ListZonesByCmd)) {
        buildAsyncListResponse((BaseListCmd) cmdObj, caller);
      }

      SerializationContext.current().setUuidTranslation(true);
      return ApiResponseSerializer.toSerializedString(
          (ResponseObject) cmdObj.getResponseObject(), cmdObj.getResponseType());
    }
  }
 public ClassDefinition toReal(SerializationContext context) throws IOException {
   final ClassDefinition cd = context.lookup(factoryId, classId, version);
   return cd != null ? cd : context.createClassDefinition(factoryId, getBinary());
 }
 @Override
 public void write(AbstractLocation obj, SerializationContext context) throws IOException {
   doWrite(context.out(), obj.getX(), obj.getY(), obj.getZ(), obj.getPitch(), obj.getYaw());
 }
示例#8
0
 @Override
 public void write(Integer obj, SerializationContext context) throws IOException {
   context.writeInt(obj);
 }
示例#9
0
 @Override
 public void write(Short obj, SerializationContext context) throws IOException {
   context.writeShort(obj);
 }
示例#10
0
 @Override
 public void write(Character obj, SerializationContext context) throws IOException {
   context.writeChar(obj);
 }
示例#11
0
 @Override
 public void write(Byte obj, SerializationContext context) throws IOException {
   context.writeByte(obj);
 }
示例#12
0
 @Override
 public void write(UUID obj, SerializationContext context) throws IOException {
   context.writeLong(obj.getMostSignificantBits());
   context.writeLong(obj.getLeastSignificantBits());
 }
示例#13
0
 @Override
 public void write(Set obj, SerializationContext context) throws IOException {
   context.writeCollection(obj);
 }
示例#14
0
 @Override
 public void write(Boolean obj, SerializationContext context) throws IOException {
   context.writeByte((obj) ? 1 : 0);
 }
示例#15
0
 @Override
 public void write(Long obj, SerializationContext context) throws IOException {
   context.writeLong(obj);
 }