package com.hubspot.singularity.resources;

import static com.hubspot.singularity.WebExceptions.checkBadRequest;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.google.common.base.Charsets;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.io.BaseEncoding;
import com.google.inject.Inject;
import com.hubspot.singularity.SingularityService;
import com.hubspot.singularity.SingularityUserSettings;
import com.hubspot.singularity.data.UserManager;
import com.wordnik.swagger.annotations.ApiParam;

@Path(UserResource.PATH)
@Produces({ MediaType.APPLICATION_JSON })
public class UserResource {
  public static final String PATH = SingularityService.API_BASE_PATH + "/users";

  private final UserManager userManager;

  @Inject
  public UserResource(UserManager userManager) {
    this.userManager = userManager;
  }

  private static String checkAndEncodeUserId(String name) {
    checkBadRequest(!Strings.isNullOrEmpty(name), "Name must be present and non-null");
    final String encodedName = BaseEncoding.base64Url().encode(name.getBytes(Charsets.UTF_8));
    checkBadRequest(!encodedName.equals("zookeeper"), "Name must not encode to reserved zookeeper word");
    return encodedName;
  }

  @GET
  @Path("/settings")
  public Optional<SingularityUserSettings> getUserSettings(
      @ApiParam("The user id to use") @QueryParam("userId") String userId) {
    return userManager.getUserSettings(checkAndEncodeUserId(userId));
  }

  @POST
  @Path("/settings")
  public void setUserSettings(
      @ApiParam("The user id to use") @QueryParam("userId") String userId,
      @ApiParam("The new settings") SingularityUserSettings settings) {
    userManager.updateUserSettings(checkAndEncodeUserId(userId), settings);
  }
}

@@ -41,78 +41,8 @@
 s;%0A%0A
-import static com.hubspot.singularity.WebExceptions.checkBadRequest;%0A%0A
 impo
@@ -241,137 +241,16 @@
 ase.
-Charsets;%0Aimport com.google.common.base.Optional;%0Aimport com.google.common.base.Strings;%0Aimport com.google.common.io.BaseEncoding
+Optional
 ;%0Aim
@@ -809,386 +809,8 @@
  %7D%0A%0A
-  private static String checkAndEncodeUserId(String name) %7B%0A    checkBadRequest(!Strings.isNullOrEmpty(name), %22Name must be present and non-null%22);%0A    final String encodedName = BaseEncoding.base64Url().encode(name.getBytes(Charsets.UTF_8));%0A    checkBadRequest(!encodedName.equals(%22zookeeper%22), %22Name must not encode to reserved zookeeper word%22);%0A    return encodedName;%0A  %7D%0A%0A
   @G
@@ -1013,37 +1013,15 @@
 ngs(
-checkAndEncodeUserId(
 userId)
-)
 ;%0A  
@@ -1269,36 +1269,14 @@
 ngs(
-checkAndEncodeUserId(
 userId
-)
 , se

Move validation/encoding work out of UserResource class