@Override
  public Response createPoly(byte[] inputToAutodetectType) {
    log.debug("Creating app from autodetecting input");

    boolean looksLikeLegacy = false;
    Exception legacyFormatException = null;
    // attempt legacy format
    try {
      ApplicationSpec appSpec = mapper().readValue(inputToAutodetectType, ApplicationSpec.class);
      if (appSpec.getType() != null || appSpec.getEntities() != null) {
        looksLikeLegacy = true;
      }
      return createFromAppSpec(appSpec);
    } catch (Exception e) {
      Exceptions.propagateIfFatal(e);
      legacyFormatException = e;
      log.debug("Input is not legacy ApplicationSpec JSON (will try others): " + e, e);
    }

    // TODO infer encoding from request
    String potentialYaml = new String(inputToAutodetectType);
    EntitySpec<? extends Application> spec = createEntitySpecForApplication(potentialYaml);

    // TODO not json - try ZIP, etc

    if (spec != null) {
      return launch(potentialYaml, spec);
    } else if (looksLikeLegacy) {
      throw Throwables.propagate(legacyFormatException);
    } else {
      return Response.serverError().entity("Unsupported format; not able to autodetect.").build();
    }
  }
  private void checkApplicationTypesAreValid(ApplicationSpec applicationSpec) {
    String appType = applicationSpec.getType();
    if (appType != null) {
      checkEntityTypeIsValid(appType);

      if (applicationSpec.getEntities() != null) {
        throw WebResourceUtils.preconditionFailed(
            "Application given explicit type '%s' must not define entities", appType);
      }
      return;
    }

    for (org.apache.brooklyn.rest.domain.EntitySpec entitySpec : applicationSpec.getEntities()) {
      String entityType = entitySpec.getType();
      checkEntityTypeIsValid(checkNotNull(entityType, "entityType"));
    }
  }