private Response launch(String yaml, EntitySpec<? extends Application> spec) {
    try {
      Application app = EntityManagementUtils.createUnstarted(mgmt(), spec);
      CreationResult<Application, Void> result = EntityManagementUtils.start(app);

      boolean isEntitled =
          Entitlements.isEntitled(
              mgmt().getEntitlementManager(),
              Entitlements.INVOKE_EFFECTOR,
              EntityAndItem.of(app, StringAndArgument.of(Startable.START.getName(), null)));

      if (!isEntitled) {
        throw WebResourceUtils.unauthorized(
            "User '%s' is not authorized to start application %s",
            Entitlements.getEntitlementContext().user(), spec.getType());
      }

      log.info("Launched from YAML: " + yaml + " -> " + app + " (" + result.task() + ")");

      URI ref = URI.create(app.getApplicationId());
      ResponseBuilder response = created(ref);
      if (result.task() != null) response.entity(TaskTransformer.FROM_TASK.apply(result.task()));
      return response.build();
    } catch (ConstraintViolationException e) {
      throw new UserFacingException(e);
    } catch (Exception e) {
      throw Exceptions.propagate(e);
    }
  }
 private EntitySpec<? extends Application> createEntitySpecForApplication(String potentialYaml) {
   try {
     return EntityManagementUtils.createEntitySpecForApplication(mgmt(), potentialYaml);
   } catch (Exception e) {
     // An IllegalArgumentException for creating the entity spec gets wrapped in a ISE, and
     // possibly a Compound.
     // But we want to return a 400 rather than 500, so ensure we throw IAE.
     IllegalArgumentException iae =
         (IllegalArgumentException)
             Exceptions.getFirstThrowableOfType(e, IllegalArgumentException.class);
     if (iae != null) {
       throw new IllegalArgumentException("Cannot create spec for app: " + iae.getMessage(), e);
     } else {
       throw Exceptions.propagate(e);
     }
   }
 }
 private EntitySpec<? extends Application> createAppSpec(String... lines) {
   return EntityManagementUtils.createEntitySpecForApplication(mgmt(), joinLines(lines));
 }