com.walmart.gatling.endpoint.v1.FileUploadController.java Source code

Java tutorial

Introduction

Here is the source code for com.walmart.gatling.endpoint.v1.FileUploadController.java

Source

/*
 *
 *   Copyright 2016 Walmart Technology
 *  
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.walmart.gatling.endpoint.v1;

import com.walmart.gatling.repository.ServerRepository;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Spring mvc controller which exposes the http end point that accepts user file uploads
 * User could upload a lib files, conf files, simulation scripts and simulation data files
 * Files are staged to a temporary directory specified using file.repository configuration property
 */
@Controller
public class FileUploadController {
    private ServerRepository serverRepository;

    @Value("${file.repository}")
    private String tempFileDir;

    @Autowired
    public FileUploadController(ServerRepository serverRepository) {
        this.serverRepository = serverRepository;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/upload")
    public String provideUploadInfo(Model model) throws IOException {
        File rootFolder = new File(tempFileDir);
        if (!rootFolder.exists()) {
            rootFolder.mkdir();
        }
        rootFolder = new File(tempFileDir);

        model.addAttribute("files",
                Arrays.stream(rootFolder.listFiles()).sorted(Comparator.comparingLong(f -> -1 * f.lastModified()))
                        .map(f -> f.getName()).collect(Collectors.toList()));

        return "uploadForm";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/upload")
    public String handleFileUpload(@RequestParam("name") String name, @RequestParam("role") String role,
            @RequestParam("type") String type, @RequestParam("file") MultipartFile file,
            RedirectAttributes redirectAttributes) {

        if (!file.isEmpty()) {
            try {
                String path = tempFileDir + "/" + name;
                System.out.println(path);
                FileUtils.touch(new File(path));
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(path)));
                FileCopyUtils.copy(file.getInputStream(), stream);
                stream.close();
                Optional<String> trackingId = serverRepository.uploadFile(path, name, role, type);
                redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + name + "! ");

                redirectAttributes.addFlashAttribute("link", "/#/file/" + trackingId.get());
            } catch (Exception e) {
                redirectAttributes.addFlashAttribute("message",
                        "You failed to upload " + name + " => " + e.getMessage());
            }
        } else {
            redirectAttributes.addFlashAttribute("message",
                    "You failed to upload " + name + " because the file was empty");
        }

        return "redirect:upload";
    }

}