Exemplo n.º 1
0
  private void handleCreateProject(
      HttpServletRequest request, HttpServletResponse response, HttpSession session, String json)
      throws IOException {
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();

    int accountId = getAccountIdFromSessionAttributes(session);
    CreateProjectRequest createProjectRequest =
        new Gson().fromJson(json, CreateProjectRequest.class);
    String projectName = createProjectRequest.arguments.project;

    if (Security.isSafeProjectName(projectName)
        && Security.isUniqueProjectName(projectName, accountId)
        && Security.projectFits(DatabaseApi.getNumberOfProjects(accountId))) {
      session.setAttribute(Attribute.IS_SAFE.toString(), true);

      String icon = "/olive/images/Ponkan_folder_opened_64.png";
      Project project = new Project(projectName, accountId, icon, -1);
      Boolean added = DatabaseApi.addProject(project);
      if (!added) {
        session.setAttribute(Attribute.ADD_SUCCESSFULLY.toString(), false);
      } else {
        session.setAttribute(Attribute.ADD_SUCCESSFULLY.toString(), true);
        session.setAttribute(Attribute.IS_FIRST_SIGN_IN.toString(), false);
        out.println(createProjectRequest.arguments.project + " created successfully.");
      }
    } else {
      session.setAttribute(Attribute.IS_SAFE.toString(), false);
    }

    out.flush();
    out.close();
  }
Exemplo n.º 2
0
 // http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   log.info("The servlet is responding to an HTTP GET request");
   response.setContentType("text/html");
   HttpSession session = request.getSession();
   String projectName = request.getParameter("projectName");
   int accountId =
       DatabaseApi.getAccountId((String) session.getAttribute(Attribute.USERNAME.toString()));
   if (projectName != null
       && Security.isSafeProjectName(projectName)
       && DatabaseApi.projectExists(projectName, accountId)) { // Short-circuiting
     session.setAttribute(Attribute.PROJECT_NAME.toString(), projectName);
     response.sendRedirect("editor.jsp");
   } else {
     response.sendRedirect("projects.jsp");
   }
   PrintWriter out = response.getWriter();
   out.println("File uploaded. Please close this window and refresh the editor page.");
   out.flush();
   out.close();
 }
Exemplo n.º 3
0
  private void handleRenameProject(
      HttpServletRequest request, HttpServletResponse response, HttpSession session, String json)
      throws IOException {
    RenameProjectRequest renameProjectRequest =
        new Gson().fromJson(json, RenameProjectRequest.class);

    String newProjectName = renameProjectRequest.arguments.newProjectName;
    String oldProjectName = renameProjectRequest.arguments.oldProjectName;
    int projectId = getProjectIdFromSessionAttributes(session, oldProjectName);
    int accountId = getAccountIdFromSessionAttributes(session);
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();

    if (Security.isSafeProjectName(newProjectName)
        && Security.isUniqueProjectName(newProjectName, accountId)) {
      DatabaseApi.renameProject(projectId, newProjectName);
      out.println(newProjectName);
    } else {
      out.println(oldProjectName);
    }
    out.flush();
    out.close();
  }