/*
 * 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.master;

import alluxio.Configuration;
import alluxio.ConfigurationValueOptions;
import alluxio.RestUtils;
import alluxio.RuntimeConstants;
import alluxio.web.JobMasterWebServer;
import alluxio.wire.AlluxioJobMasterInfo;

import com.qmino.miredot.annotations.ReturnType;

import java.util.Map;

import javax.annotation.concurrent.NotThreadSafe;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * This class is a REST handler for requesting general job master information.
 */
@NotThreadSafe
@Path(AlluxioJobMasterRestServiceHandler.SERVICE_PREFIX)
@Produces(MediaType.APPLICATION_JSON)
public final class AlluxioJobMasterRestServiceHandler {
  public static final String SERVICE_PREFIX = "job_master";

  // endpoints
  public static final String GET_INFO = "info";

  // queries
  public static final String QUERY_RAW_CONFIGURATION = "raw_configuration";

  private final JobMasterProcess mJobMaster;

  /**
   * @param context context for the servlet
   */
  public AlluxioJobMasterRestServiceHandler(@Context ServletContext context) {
    mJobMaster = (JobMasterProcess) context
        .getAttribute(JobMasterWebServer.ALLUXIO_JOB_MASTER_SERVLET_RESOURCE_KEY);
  }

  /**
   * @summary get the Alluxio job master information
   * @param rawConfiguration if it's true, raw configuration values are returned,
   *    otherwise, they are looked up; if it's not provided in URL queries, then
   *    it is null, which means false.
   * @return the response object
   */
  @GET
  @Path(GET_INFO)
  @ReturnType("alluxio.wire.AlluxioJobMasterInfo")
  public Response getInfo(@QueryParam(QUERY_RAW_CONFIGURATION) final Boolean rawConfiguration) {
    // TODO(jiri): Add a mechanism for retrieving only a subset of the fields.
    return RestUtils.call(new RestUtils.RestCallable<AlluxioJobMasterInfo>() {
      @Override
      public AlluxioJobMasterInfo call() throws Exception {
        boolean rawConfig = false;
        if (rawConfiguration != null) {
          rawConfig = rawConfiguration;
        }
        AlluxioJobMasterInfo result =
            new AlluxioJobMasterInfo()
                .setConfiguration(getConfigurationInternal(rawConfig))
                .setStartTimeMs(mJobMaster.getStartTimeMs())
                .setUptimeMs(mJobMaster.getUptimeMs())
                .setVersion(RuntimeConstants.VERSION)
                .setWorkers(mJobMaster.getJobMaster().getWorkerInfoList());
        return result;
      }
    });
  }

  private Map<String, String> getConfigurationInternal(boolean raw) {
    if (raw) {
      return Configuration.toMap(ConfigurationValueOptions.defaults().useRawValue(raw));
    } else {
      return Configuration.toMap();
    }
  }
}

@@ -2480,139 +2480,16 @@
 all(
-new RestUtils.RestCallable%3CAlluxioJobMasterInfo%3E() %7B%0A      @Override%0A      public AlluxioJobMasterInfo call() throws Exception
+() -%3E
  %7B%0A
-  
     
@@ -2523,18 +2523,16 @@
 ;%0A      
- 
- 
 if (rawC
@@ -2563,18 +2563,16 @@
         
-  
 rawConfi
@@ -2593,18 +2593,16 @@
 ration;%0A
-  
       %7D%0A
@@ -2607,18 +2607,16 @@
 %7D%0A      
-  
 AlluxioJ
@@ -2647,18 +2647,16 @@
         
-  
 new Allu
@@ -2684,26 +2684,24 @@
             
- 
- 
 .setConfigur
@@ -2753,26 +2753,24 @@
             
-  
 .setStartTim
@@ -2794,34 +2794,32 @@
 tStartTimeMs())%0A
-  
               .s
@@ -2865,26 +2865,24 @@
             
-  
 .setVersion(
@@ -2907,18 +2907,16 @@
 ERSION)%0A
-  
         
@@ -2983,26 +2983,24 @@
 t());%0A      
- 
- 
 return resul
@@ -3002,24 +3002,16 @@
 result;%0A
-      %7D%0A
     %7D);%0A

replace anonymous type with lambda (#7943)