package tachyon.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tachyon.master.MasterInfo;
import tachyon.thrift.ClientFileInfo;
import tachyon.thrift.InvalidPathException;

/**
 * Servlet that provides data for displaying which files are currently in memory.
 */
public class WebInterfaceMemoryServlet extends HttpServlet {
  private static final long serialVersionUID = 4293149962399443914L;
  private MasterInfo mMasterInfo;

  public WebInterfaceMemoryServlet(MasterInfo masterInfo) {
    mMasterInfo = masterInfo;
  }

  /**
   * Populates attributes before redirecting to a jsp.
   * 
   * @param request
   *          The HttpServletRequest object
   * @param response
   *          The HttpServletReponse object
   */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    request.setAttribute("masterNodeAddress", mMasterInfo.getMasterAddress().toString());

    List<String> inMemoryFiles = mMasterInfo.getInMemoryFiles();
    Collections.sort(inMemoryFiles);
    request.setAttribute("inMemoryFiles", inMemoryFiles);

    List<UiFileInfo> fileInfos = new ArrayList<UiFileInfo>(inMemoryFiles.size());
    for (String file : inMemoryFiles) {
      try {
        ClientFileInfo fileInfo = mMasterInfo.getClientFileInfo(file);
        if (fileInfo != null && fileInfo.getInMemoryPercentage() == 100) {
          fileInfos.add(new UiFileInfo(fileInfo));
        }
      } catch (InvalidPathException ipe) {
        request.setAttribute("invalidPathError",
            "Error: Invalid Path " + ipe.getLocalizedMessage());
        getServletContext().getRequestDispatcher("/memory.jsp").forward(request, response);
        return;
      }
    }
    request.setAttribute("fileInfos", fileInfos);

    getServletContext().getRequestDispatcher("/memory.jsp").forward(request, response);
  }
}
@@ -1190,16 +1190,60 @@
 ring());
+%0A    request.setAttribute(%22fatalError%22, %22%22);
 %0A%0A    Li
@@ -1341,66 +1341,8 @@
 es);
-%0A    request.setAttribute(%22inMemoryFiles%22, inMemoryFiles);
 %0A%0A  
@@ -1757,38 +1757,20 @@
 te(%22
-invalidPathError%22,%0A           
+fatalError%22,
  %22Er
@@ -1971,31 +1971,1448 @@
 te(%22
-fileInfos%22, fileInfos);
+inMemoryFileNum%22, new Integer(fileInfos.size()));%0A%0A    // URL is %22./memory%22, can not determine offset and limit, let javascript in jsp determine%0A    // and redirect to %22./memory?offset=xxx&limit=xxx%22%0A    if (request.getParameter(%22offset%22) == null && request.getParameter(%22limit%22) == null) %7B%0A      getServletContext().getRequestDispatcher(%22/memory.jsp%22).forward(request, response);%0A      return;%0A    %7D%0A%0A    try %7B%0A      int offset = Integer.parseInt(request.getParameter(%22offset%22));%0A      int limit = Integer.parseInt(request.getParameter(%22limit%22));%0A      List%3CUiFileInfo%3E sub = fileInfos.subList(offset, offset + limit);%0A      request.setAttribute(%22fileInfos%22, sub);%0A    %7D catch (NumberFormatException nfe) %7B%0A      request.setAttribute(%22fatalError%22,%0A          %22Error: offset or limit parse error, %22 + nfe.getLocalizedMessage());%0A      getServletContext().getRequestDispatcher(%22/memory.jsp%22).forward(request, response);%0A      return;%0A    %7D catch (IndexOutOfBoundsException iobe) %7B%0A      request.setAttribute(%22fatalError%22, %22Error: offset or offset + limit is out of bound, %22%0A          + iobe.getLocalizedMessage());%0A      getServletContext().getRequestDispatcher(%22/memory.jsp%22).forward(request, response);%0A      return;%0A    %7D catch (IllegalArgumentException iae) %7B%0A      request.setAttribute(%22fatalError%22, iae.getLocalizedMessage());%0A      getServletContext().getRequestDispatcher(%22/memory.jsp%22).forward(request, response);%0A      return;%0A    %7D
 %0A%0A  

update servlet to support pagination