class SpaceController {

  @RequestMapping(
      value = "",
      method = RequestMethod.POST,
      consumes = {"application/json;charset=UTF-8"},
      produces = {"application/json;charset=UTF-8"})
  public ResponseEntity<Space> createSpace(
      @RequestBody Space space,
      @ApiIgnore @RequestHeader(value = Constants.HTTP_TENANT_HEADER, required = false)
          String tenant) {
    final StopWatch sw =
        new StopWatch(
                "Create space ["
                    + (space == null ? null : space.getSpaceName())
                    + "] "
                    + "for tenant token ["
                    + tenant
                    + "] ")
            .start();

    try {
      // Check arguments
      if (space == null) {
        log.error("No space submitted");
        return new ResponseEntity<Space>(HttpStatus.BAD_REQUEST);
      } else if (!space.hasNameAndDescription()) {
        log.error("Work spaces require a name and description");
        return new ResponseEntity<Space>(HttpStatus.BAD_REQUEST);
      } else if (space.isReadOnly()) {
        log.error("Work spaces cannot be created with read-only set to true");
        return new ResponseEntity<Space>(HttpStatus.BAD_REQUEST);
      }

      // Check whether tenant exists or retrieve default
      Tenant t = null;
      try {
        t = this.tenantRepository.getTenant(tenant);
      } catch (EntityNotFoundException enfe) {
        log.error("Tenant [" + tenant + "] not found");
        return new ResponseEntity<Space>(HttpStatus.NOT_FOUND);
      } catch (Exception e) {
        log.error("Error retrieving tenant [" + tenant + "]: " + e);
        return new ResponseEntity<Space>(HttpStatus.NOT_FOUND);
      }

      // check that only 1 default space per tenant is created
      if (space.isDefault() && this.spaceRepository.findDefault(t.getTenantToken()) != null) {
        log.error(
            "A default space for the given tenant already exists, adjust the configuration"
                + " accordingly");
        return new ResponseEntity<Space>(HttpStatus.BAD_REQUEST);
      }

      // Always create token, whatever has been submitted
      space.setTenant(t);
      space.setSpaceToken(TokenUtil.generateToken());
      this.spaceRepository.customSave(space);

      sw.stop();
      return new ResponseEntity<Space>(space, HttpStatus.CREATED);
    } catch (Exception enfe) {
      sw.stop(enfe);
      return new ResponseEntity<Space>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  @Autowired  SpaceController(
      TenantRepository tenantRepository,
      SpaceRepository spaceRepository,
      ApplicationRepository appRepository,
      ApplicationExporter appExporter,
      Filter cacheFilter);

}