/**
   * Create a new client
   *
   * @param json
   * @param m
   * @param principal
   * @return
   */
  @PreAuthorize("hasRole('ROLE_ADMIN')")
  @RequestMapping(
      method = RequestMethod.POST,
      consumes = "application/json",
      produces = "application/json")
  public String apiAddClient(@RequestBody String jsonString, Model m, Authentication auth) {

    JsonObject json = null;
    ClientDetailsEntity client = null;

    try {
      json = parser.parse(jsonString).getAsJsonObject();
      client = gson.fromJson(json, ClientDetailsEntity.class);
    } catch (JsonSyntaxException e) {
      logger.error("apiAddClient failed due to JsonSyntaxException", e);
      m.addAttribute("code", HttpStatus.BAD_REQUEST);
      m.addAttribute(
          "errorMessage",
          "Could not save new client. The server encountered a JSON syntax exception. Contact a system administrator for assistance.");
      return "jsonErrorView";
    } catch (IllegalStateException e) {
      logger.error("apiAddClient failed due to IllegalStateException", e);
      m.addAttribute("code", HttpStatus.BAD_REQUEST);
      m.addAttribute(
          "errorMessage",
          "Could not save new client. The server encountered an IllegalStateException. Refresh and try again - if the problem persists, contact a system administrator for assistance.");
      return "jsonErrorView";
    }

    // if they leave the client identifier empty, force it to be generated
    if (Strings.isNullOrEmpty(client.getClientId())) {
      client = clientService.generateClientId(client);
    }

    // if they've asked for us to generate a client secret, do so here
    if (json.has("generateClientSecret") && json.get("generateClientSecret").getAsBoolean()) {
      client = clientService.generateClientSecret(client);
    }

    // set owners as current logged in user
    // try to look up a user based on the principal's name
    if (client.getContacts() == null || client.getContacts().isEmpty()) {
      UserInfo user = userInfoService.getByUsername(auth.getName());
      if (user != null && user.getEmail() != null) {
        client.setContacts(Sets.newHashSet(user.getEmail()));
      }
    }

    client.setDynamicallyRegistered(false);

    ClientDetailsEntity newClient = clientService.saveNewClient(client);
    m.addAttribute("entity", newClient);

    if (isAdmin(auth)) {
      return "clientEntityViewAdmins";
    } else {
      return "clientEntityViewUsers";
    }
  }