Example usage for org.springframework.web.multipart.support DefaultMultipartHttpServletRequest getFileNames

List of usage examples for org.springframework.web.multipart.support DefaultMultipartHttpServletRequest getFileNames

Introduction

In this page you can find the example usage for org.springframework.web.multipart.support DefaultMultipartHttpServletRequest getFileNames.

Prototype

@Override
    public Iterator<String> getFileNames() 

Source Link

Usage

From source file:org.wise.portal.presentation.web.controllers.author.project.AuthorProjectController.java

@RequestMapping("/author/authorproject.html")
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    User user = ControllerUtil.getSignedInUser();

    String projectIdStr = request.getParameter(PROJECT_ID_PARAM_NAME);
    String forward = request.getParameter(FORWARD);

    Project project;//ww  w. jav  a  2s. c o m
    if (projectIdStr != null && !projectIdStr.equals("") && !projectIdStr.equals("none")) {
        //project = projectService.getProjectWithoutMetadata(Long.parseLong(projectIdStr));
        project = projectService.getById(Long.parseLong(projectIdStr));
    } else {
        project = null;
    }

    /* catch forwarding requests, authenticate and forward request upon successful authentication */
    if (forward != null && !forward.equals("")) {
        //get the command
        String command = request.getParameter("command");

        if (forward.equals("filemanager") || forward.equals("assetmanager")) {

            if ((this.isProjectlessRequest(request, forward)
                    || this.projectService.canAuthorProject(project, user))
                    || ("copyProject".equals(command) && project.getFamilytag().equals(FamilyTag.TELS))
                    || ("retrieveFile".equals(command) && project.getFamilytag().equals(FamilyTag.TELS))) {

                if ("createProject".equals(command) && !this.hasAuthorPermissions(user)) {
                    return new ModelAndView(new RedirectView("accessdenied.html"));
                }

                if ("copyProject".equals(command)
                        && (project == null || (!project.getFamilytag().equals(FamilyTag.TELS)
                                && !this.projectService.canAuthorProject(project, user)))) {
                    return new ModelAndView(new RedirectView("accessdenied.html"));
                }

                CredentialManager.setRequestCredentials(request, user);

                if (forward.equals("filemanager")) {
                    if (command != null) {
                        String pathAllowedToAccess = CredentialManager.getAllowedPathAccess(request);

                        if (command.equals("createProject")) {
                            String projectName = request.getParameter("projectName");
                            String curriculumBaseDir = wiseProperties.getProperty("curriculum_base_dir");

                            String result = "";

                            if (SecurityUtils.isAllowedAccess(pathAllowedToAccess, curriculumBaseDir)) {
                                result = FileManager.createProject(curriculumBaseDir, projectName);
                            } else {
                                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                                result = "unauthorized";
                            }

                            response.getWriter().write(result);
                        } else if (command.equals("projectList")) {
                            String projectPaths = request.getParameter("projectPaths");
                            String projectExt = ".project.json";

                            String result = FileManager.getProjectList(projectPaths, projectExt);

                            response.getWriter().write(result);
                        } else if (command.equals("retrieveFile")) {
                            //get the file name
                            String fileName = request.getParameter("fileName");

                            //get the full file path
                            String filePath = FileManager.getFilePath(project, fileName);

                            String result = "";

                            if (SecurityUtils.isAllowedAccess(pathAllowedToAccess, filePath)) {
                                result = FileManager.retrieveFile(filePath);
                            } else {
                                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                                result = "unauthorized";
                            }

                            response.getWriter().write(result);
                        } else if (command.equals("updateFile")) {
                            /*
                             * get the project folder path
                             * e.g.
                             * /Users/geoffreykwan/dev/apache-tomcat-5.5.27/webapps/curriculum/667
                             */
                            String projectFolderPath = FileManager.getProjectFolderPath(project);

                            //get the file name
                            String fileName = request.getParameter("fileName");

                            //get the content to save to the file
                            String data = request.getParameter("data");

                            String result = "";

                            if (SecurityUtils.isAllowedAccess(pathAllowedToAccess, projectFolderPath)) {
                                result = FileManager.updateFile(projectFolderPath, fileName, data);
                            } else {
                                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                                result = "unauthorized";
                            }

                            response.getWriter().write(result);
                        } else if (command.equals("createNode")) {
                            /*
                             * get the project file path
                             * e.g.
                             * /Users/geoffreykwan/dev/apache-tomcat-5.5.27/webapps/curriculum/667/wise4.project.json
                             */
                            String projectPath = FileManager.getProjectFilePath(project);
                            String nodeClass = request.getParameter("nodeClass");
                            String title = request.getParameter("title");
                            String type = request.getParameter("type");

                            //get the string that contains an array of node template params
                            String nodeTemplateParams = request.getParameter("nodeTemplateParams");

                            String result = "";

                            if (SecurityUtils.isAllowedAccess(pathAllowedToAccess, projectPath)) {
                                result = FileManager.createNode(projectPath, nodeClass, title, type,
                                        nodeTemplateParams);
                            } else {
                                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                                result = "not authorized";
                            }

                            response.getWriter().write(result);
                        } else if (command.equals("createSequence")) {
                            /*
                             * get the project file name
                             * e.g.
                             * /wise4.project.json
                             */
                            String projectFileName = request.getParameter("projectFileName");
                            String name = request.getParameter("name");
                            String id = request.getParameter("id");

                            /*
                             * get the project folder path
                             * e.g.
                             * /Users/geoffreykwan/dev/apache-tomcat-5.5.27/webapps/curriculum/667
                             */
                            String projectFolderPath = FileManager.getProjectFolderPath(project);

                            String result = "";

                            if (SecurityUtils.isAllowedAccess(pathAllowedToAccess, projectFolderPath)) {
                                result = FileManager.createSequence(projectFileName, name, id,
                                        projectFolderPath);
                            } else {
                                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                                result = "not authorized";
                            }

                            response.getWriter().write(result);
                        } else if (command.equals("removeFile")) {
                            /*
                             * get the project folder path
                             * e.g.
                             * /Users/geoffreykwan/dev/apache-tomcat-5.5.27/webapps/curriculum/667
                             */
                            String projectFolderPath = FileManager.getProjectFolderPath(project);

                            /*
                             * get the file name
                             * node_1.or
                             */
                            String fileName = request.getParameter("fileName");

                            String result = "";

                            if (SecurityUtils.isAllowedAccess(pathAllowedToAccess, projectFolderPath)) {
                                result = FileManager.removeFile(projectFolderPath, fileName);
                            } else {
                                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                                result = "unauthorized";
                            }

                            response.getWriter().write(result);
                        } else if (command.equals("copyNode")) {
                            //get the parameters for the node
                            String data = request.getParameter("data");
                            String type = request.getParameter("type");
                            String title = request.getParameter("title");
                            String nodeClass = request.getParameter("nodeClass");
                            String contentFile = request.getParameter("contentFile");

                            /*
                             * get the file name
                             * e.g.
                             * /node_1.or
                             */
                            String projectFileName = request.getParameter("projectFileName");

                            /*
                             * get the project folder path
                             * e.g.
                             * /Users/geoffreykwan/dev/apache-tomcat-5.5.27/webapps/curriculum/667
                             */
                            String projectFolderPath = FileManager.getProjectFolderPath(project);

                            String result = "";

                            if (SecurityUtils.isAllowedAccess(pathAllowedToAccess, projectFolderPath)) {
                                result = FileManager.copyNode(projectFolderPath, projectFileName, data, type,
                                        title, nodeClass, contentFile);
                            } else {
                                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                            }

                            response.getWriter().write(result);
                        } else if (command.equals("createSequenceFromJSON")) {
                            /*
                             * get the project file name
                             * e.g.
                             * /wise4.project.json
                             */
                            String projectFileName = request.getParameter("projectFileName");

                            //get the json for the new sequence we are going to add to the project
                            String data = request.getParameter("data");

                            /*
                             * get the project folder path
                             * e.g.
                             * /Users/geoffreykwan/dev/apache-tomcat-5.5.27/webapps/curriculum/667
                             */
                            String projectFolderPath = FileManager.getProjectFolderPath(project);

                            String result = "";

                            if (SecurityUtils.isAllowedAccess(pathAllowedToAccess, projectFolderPath)) {
                                result = FileManager.createSequenceFromJSON(projectFolderPath, projectFileName,
                                        data);
                            } else {
                                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                            }

                            response.getWriter().write(result);
                        } else if (command.equals("getScripts")) {
                            String data = request.getParameter("param1");

                            String result = FileManager.getScripts(servletContext, data);

                            response.getWriter().write(result);
                        } else if (command.equals("copyProject")) {
                            /*
                             * get the project folder path
                             * e.g.
                             * /Users/geoffreykwan/dev/apache-tomcat-5.5.27/webapps/curriculum/667
                             */
                            String projectFolderPath = FileManager.getProjectFolderPath(project);

                            /*
                             * get the curriculum base
                             * e.g.
                             * /Users/geoffreykwan/dev/apache-tomcat-5.5.27/webapps/curriculum
                             */
                            String curriculumBaseDir = wiseProperties.getProperty("curriculum_base_dir");

                            String result = "";

                            if (SecurityUtils.isAllowedAccess(pathAllowedToAccess, projectFolderPath)) {
                                result = FileManager.copyProject(curriculumBaseDir, projectFolderPath);
                            } else {
                                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                                result = "unauthorized";
                            }

                            response.getWriter().write(result);
                        } else if (command.equals("createFile")) {
                            /*
                             * get the file name
                             * e.g.
                             * /node_1.or
                             */
                            String fileName = request.getParameter("path");
                            String data = request.getParameter("data");

                            /*
                             * get the project folder path
                             * e.g.
                             * /Users/geoffreykwan/dev/apache-tomcat-5.5.27/webapps/curriculum/667
                             */
                            String projectFolderPath = FileManager.getProjectFolderPath(project);

                            String result = "";

                            if (SecurityUtils.isAllowedAccess(pathAllowedToAccess, projectFolderPath)) {
                                result = FileManager.createFile(projectFolderPath, fileName, data);
                            } else {
                                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                            }

                            response.getWriter().write(result);
                        } else if (command.equals("reviewUpdateProject")) {
                            //get the curriculum base directory e.g. /Users/geoffreykwan/dev/apache-tomcat-5.5.27/webapps/curriculum
                            String curriculumBaseDir = wiseProperties.getProperty("curriculum_base_dir");

                            //get the relative child project url e.g. /236/wise4.project.json
                            String projectUrl = (String) project.getCurnit()
                                    .accept(new CurnitGetCurnitUrlVisitor());

                            //get the parent project id
                            Long parentProjectId = project.getParentProjectId();

                            //get the parent project
                            Project parentProject = projectService.getById(parentProjectId);

                            //get the relative parent project url e.g. /235/wise4.project.json
                            String parentProjectUrl = (String) parentProject.getCurnit()
                                    .accept(new CurnitGetCurnitUrlVisitor());

                            String result = FileManager.reviewUpdateProject(curriculumBaseDir, parentProjectUrl,
                                    projectUrl);

                            response.getWriter().write(result);
                        } else if (command.equals("updateProject")) {
                            //get the curriculum base directory e.g. /Users/geoffreykwan/dev/apache-tomcat-5.5.27/webapps/curriculum
                            String curriculumBaseDir = wiseProperties.getProperty("curriculum_base_dir");

                            //get the relative child project url e.g. /236/wise4.project.json
                            String childProjectUrl = (String) project.getCurnit()
                                    .accept(new CurnitGetCurnitUrlVisitor());

                            //get the child project folder path
                            String childProjectFolderPath = FileManager.getProjectFolderPath(project);

                            //get the parent project id
                            Long parentProjectId = project.getParentProjectId();

                            //get the parent project
                            Project parentProject = projectService.getById(parentProjectId);

                            //get the relative parent project url e.g. /235/wise4.project.json
                            String parentProjectUrl = (String) parentProject.getCurnit()
                                    .accept(new CurnitGetCurnitUrlVisitor());

                            String result = "";

                            if (SecurityUtils.isAllowedAccess(pathAllowedToAccess, childProjectFolderPath)) {
                                result = FileManager.updateProject(curriculumBaseDir, parentProjectUrl,
                                        childProjectUrl);
                            } else {
                                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                                result = "unauthorized";
                            }

                            response.getWriter().write(result);
                        } else if (command.equals("importSteps")) {
                            //get the curriculum base directory e.g. /Users/geoffreykwan/dev/apache-tomcat-5.5.27/webapps/curriculum
                            String curriculumBaseDir = wiseProperties.getProperty("curriculum_base_dir");

                            //get the relative child project url e.g. /172/wise4.project.json
                            String fromProjectUrl = "";

                            //get the from project id string
                            String fromProjectIdStr = request.getParameter("fromProjectId");

                            if (fromProjectIdStr != null) {
                                try {
                                    //get the from project id
                                    long fromProjectId = Long.parseLong(fromProjectIdStr);

                                    //get the from project
                                    Project fromProject = projectService.getById(fromProjectId);

                                    //get the from project url e.g. /172/wise4.project.json
                                    fromProjectUrl = (String) fromProject.getCurnit()
                                            .accept(new CurnitGetCurnitUrlVisitor());
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }

                            //get the relative child project url e.g. /236/wise4.project.json
                            String toProjectUrl = (String) project.getCurnit()
                                    .accept(new CurnitGetCurnitUrlVisitor());

                            //get the child project folder path
                            String toProjectFolderPath = FileManager.getProjectFolderPath(project);

                            //get all the files we need to import
                            String nodeIds = (String) request.getParameter("nodeIds");

                            String result = "";

                            if (SecurityUtils.isAllowedAccess(pathAllowedToAccess, toProjectFolderPath)) {
                                result = FileManager.importSteps(curriculumBaseDir, fromProjectUrl,
                                        toProjectUrl, nodeIds);
                            } else {
                                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                                result = "unauthorized";
                            }

                            response.getWriter().write(result);
                        } else if (command.equals("getProjectUsageAndMax")) {
                            //get the path to the folder
                            String path = FileManager.getProjectFolderPath(project);

                            //get the max project size for this project if it was separately specified for this project
                            Long projectMaxTotalAssetsSizeLong = project.getMaxTotalAssetsSize();

                            String result = FileManager.getProjectUsageAndMax(path,
                                    projectMaxTotalAssetsSizeLong);

                            response.getWriter().write(result);
                        } else {
                            /* we don't understand this command */
                            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                        }
                    } else {
                        /* no command was provided */
                        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                    }
                } else if (forward.equals("assetmanager")) {

                    if (command == null && ServletFileUpload.isMultipartContent(request)) {
                        //user is uploading a file

                        ServletFileUpload uploader = new ServletFileUpload(new DiskFileItemFactory());
                        List<?> fileList = null;
                        try {
                            //get a list of the files the user is uploading
                            fileList = uploader.parseRequest(request);
                        } catch (FileUploadException e) {
                            e.printStackTrace();
                        }

                        //get the project folder path
                        String projectFolderPath = FileManager.getProjectFolderPath(project);

                        //get the folder name that will contain the assets
                        String dirName = "assets";
                        String pathToCheckSize = projectFolderPath;

                        //get the max disk space size this project can use
                        Long projectMaxTotalAssetsSize = project.getMaxTotalAssetsSize();

                        if (projectMaxTotalAssetsSize == null) {
                            //get the default max project size
                            projectMaxTotalAssetsSize = new Long(
                                    wiseProperties.getProperty("project_max_total_assets_size", "15728640"));
                        }

                        String allowedProjectAssetContentTypesStr = wiseProperties
                                .getProperty("normalAuthorAllowedProjectAssetContentTypes");
                        if (user.isTrustedAuthor()) {
                            allowedProjectAssetContentTypesStr += "," + wiseProperties
                                    .getProperty("trustedAuthorAllowedProjectAssetContentTypes");
                        }

                        DefaultMultipartHttpServletRequest multiRequest = (DefaultMultipartHttpServletRequest) request;
                        List<String> fileNames = new ArrayList<String>();
                        Map<String, MultipartFile> fileMap = new TreeMap<String, MultipartFile>();

                        //get all the file names and files to be uploaded
                        Iterator<String> iter = multiRequest.getFileNames();
                        while (iter.hasNext()) {
                            String filename = (String) iter.next();
                            fileNames.add(filename);
                            MultipartFile oneFile = multiRequest.getFile(filename);
                            String contentType = oneFile.getContentType();
                            if (!allowedProjectAssetContentTypesStr.contains(contentType)) {
                                if (contentType.equals("application/octet-stream") && (filename.endsWith(".mml")
                                        || filename.endsWith(".cml") || filename.endsWith(".json"))) {
                                    // .mml and .cml files are acceptable. Their content-type is not well-known, so it will show up at application/octet-stream
                                } else {
                                    response.getWriter().write(
                                            "Uploading this file type is not allowed. Operation aborted.");
                                    return null;
                                }
                            }
                            fileMap.put(filename, oneFile);
                        }

                        //tell the asset manager to handle the file upload
                        String result = AssetManager.uploadAsset(fileList, fileNames, fileMap,
                                projectFolderPath, dirName, pathToCheckSize, projectMaxTotalAssetsSize);
                        response.getWriter().write(result);
                    } else if (command.equals("remove")) {
                        //get the project folder path
                        String path = FileManager.getProjectFolderPath(project);

                        //get the assets folder name
                        String dirName = "assets";

                        //get the file name that we are going to remove
                        String assetFileName = request.getParameter("asset");

                        //tell the asset manager to remove the file
                        String result = AssetManager.removeAsset(path, dirName, assetFileName);
                        response.getWriter().write(result);
                    } else if (command.equals("getSize")) {
                        //get the project folder path
                        String path = FileManager.getProjectFolderPath(project);

                        //get the assets folder name
                        String dirName = "assets";

                        //tell the asset manager to get the size of the assets folder
                        String result = AssetManager.getSize(path, dirName);
                        response.getWriter().write(result);
                    } else if (command.equals("assetList")) {
                        //get the project folder path
                        String path = FileManager.getProjectFolderPath(project);

                        //get the assets folder name
                        String dirName = "assets";

                        //get the file names for all the assets in the assets folder
                        String assetList = AssetManager.getAssetList(path, dirName);
                        response.getWriter().write(assetList);
                    } else if (command.equals("studentAssetCopyForReference")) {
                        //AssetManager.copyAssetForReference();
                    } else {
                        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
                    }
                }

                if ("updateFile".equals(command)) {
                    //we have updated a file in a project so we will update the project edited timestamp

                    /*
                     * set the project into the request so the handleProjectEdited 
                     * function doesn't have to retrieve it again
                     */
                    request.setAttribute("project", project);

                    //update the project edited timestamp
                    handleProjectEdited(request, response);
                }

                return null;
            } else {
                return new ModelAndView(new RedirectView("accessdenied.html"));
            }
        } else if (command.equals("getTimestamp")) {
            //get the current timestamp on the server and write it to the response
            response.getWriter().write(String.valueOf(new Date().getTime()));
            return null;
        } else if (forward.equals("minifier")) {
            if (this.isProjectlessRequest(request, forward)
                    || this.projectService.canAuthorProject(project, user)) {
                CredentialManager.setRequestCredentials(request, user);
                servletContext.getRequestDispatcher("/util/" + forward + ".html").forward(request, response);
                return null;
            }
        }
    }

    String command = request.getParameter(COMMAND);
    if (command != null && command != "") {
        if (command.equals("launchAuthoring")) {
            return handleLaunchAuthoring(request, response);
        } else if (command.equals("createProject")) {
            return handleCreateProject(request, response);
        } else if (command.equals("projectList")) {
            return handleProjectList(request, response);
        } else if (command.equals("notifyProjectOpen")) {
            return handleNotifyProjectOpen(request, response);
        } else if (command.equals("notifyProjectClose")) {
            return handleNotifyProjectClose(request, response);
        } else if (command.equals("publishMetadata")) {
            return this.handlePublishMetadata(request, response);
        } else if (command.equals("getUsername")) {
            return this.handleGetUsername(request, response);
        } else if (command.equals("getCurriculumBaseUrl")) {
            return this.handleGetCurriculumBaseUrl(request, response);
        } else if (command.equals("getConfig")) {
            return this.handleGetConfig(request, response);
        } else if (command.equals("getEditors")) {
            if (this.projectService.canAuthorProject(project, user)) {
                return this.handleGetEditors(request, response);
            } else {
                return new ModelAndView(new RedirectView("accessdenied.html"));
            }
        } else if (command.equals("preview")) {
            PreviewProjectParameters previewParams = new PreviewProjectParameters();
            previewParams.setProject(project);
            previewParams.setHttpServletRequest(request);

            return (ModelAndView) this.projectService.previewProject(previewParams);
        } else if (command.equals("createTag") || command.equals("updateTag") || command.equals("removeTag")
                || command.equals("retrieveProjectTags")) {
            return this.taggerController.handleRequestInternal(request, response);
        } else if (command.equals("getMetadata")) {
            request.setAttribute("project", project);
            return handleGetMetadata(request, response);
        } else if (command.equals("postMetadata")) {
            request.setAttribute("project", project);
            return handlePostMetadata(request, response);
        } else if (command.equals("reviewUpdateProject")) {
            return handleReviewUpdateProject(request, response);
        } else if (command.equals("updateProject")) {
            return handleUpdateProject(request, response);
        } else if (command.equals("importSteps")) {
            return handleReviewOrUpdateProject(request, response);
        }
    }

    return handleLaunchAuthoring(request, response);
}