/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.core.Response;

/**
 * Utilities for handling REST calls.
 */
public final class RestUtils {
  private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);

  /**
   * Calls the given {@link RestUtils.RestCallable} and handles any exceptions thrown.
   *
   * @param <T> the return type of the callable
   * @param callable the callable to call
   * @return the response object
   */
  public static <T> Response call(RestUtils.RestCallable<T> callable) {
    try {
      return createResponse(callable.call());
    } catch (Exception e) {
      LOG.error("Unexpected error invoking rest endpoint", e);
      return createErrorResponse(e.getMessage());
    }
  }

  /**
   * An interface representing a callable.
   *
   * @param <T> the return type of the callable
   */
  public interface RestCallable<T> {
    /**
     * The REST endpoint implementation.
     *
     * @return the return value from the callable
     * @throws Exception if an exception occurs
     */
    T call() throws Exception;
  }

  /**
   * Creates a response using the given object.
   *
   * @param object the object to respond with
   * @return the response
   */
  private static Response createResponse(Object object) {
    if (object instanceof Void) {
      return Response.ok().build();
    }
    if (object instanceof String) {
      // Need to explicitly encode the string as JSON because Jackson will not do it automatically.
      ObjectMapper mapper = new ObjectMapper();
      try {
        return Response.ok(mapper.writeValueAsString(object)).build();
      } catch (JsonProcessingException e) {
        return createErrorResponse(e.getMessage());
      }
    }
    return Response.ok(object).build();
  }

  /**
   * Creates an error response using the given message.
   *
   * @param message the message to respond with
   * @return the response
   */
  private static Response createErrorResponse(String message) {
    return Response.serverError().entity(message).build();
  }

  private RestUtils() {} // prevent instantiation
}

@@ -525,16 +525,151 @@
 luxio;%0A%0A
+import alluxio.security.LoginUser;%0Aimport alluxio.security.authentication.AuthenticatedClientUser;%0Aimport alluxio.util.SecurityUtils;%0A%0A
 import c
@@ -719,16 +719,16 @@
 eption;%0A
-
 import c
@@ -829,16 +829,45 @@
 ctory;%0A%0A
+import java.io.IOException;%0A%0A
 import j
@@ -1278,24 +1278,24 @@
 bject%0A   */%0A
-
   public sta
@@ -1350,24 +1350,367 @@
 callable) %7B%0A
+    try %7B%0A      if (SecurityUtils.isSecurityEnabled() && AuthenticatedClientUser.get() == null) %7B%0A        AuthenticatedClientUser.set(LoginUser.get().getName());%0A      %7D%0A    %7D catch (IOException e) %7B%0A      LOG.error(%22Failed to set AuthenticatedClientUser in REST service handler.%22, e);%0A      return createErrorResponse(e.getMessage());%0A    %7D%0A%0A
     try %7B%0A  

Add default user in REST service