hu.bme.iit.quiz.controller.FileController.java Source code

Java tutorial

Introduction

Here is the source code for hu.bme.iit.quiz.controller.FileController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.bme.iit.quiz.controller;

import hu.bme.iit.quiz.service.QuizService;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/file")
public class FileController {

    private static final Logger logger = Logger.getLogger(FileController.class);

    @Autowired
    private QuizService quizService;

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    /**
     * *************************************************
     * URL: /file/upload upload(): Upload a single file
     * **************************************************
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody String uploadFileHandler(@RequestParam("name") String name,
            @RequestParam("file") MultipartFile file) {

        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return quizService.validateAndCreateQuizForUserFromFile(file, name, user.getUsername());
    }
}