Java tutorial
/* * 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 com.teasoft.teavote.controller; import com.teasoft.teavote.exceptions.MissingParameterException; import com.teasoft.teavote.util.ConfigLocation; import com.teasoft.teavote.util.Enums; import com.teasoft.teavote.util.JSONResponse; import com.teasoft.teavote.util.Signature; import com.teasoft.teavote.util.Utilities; import io.jsonwebtoken.ExpiredJwtException; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.security.SignatureException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.compress.utils.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestBody; 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.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; /** * * @author Elikem */ @RestController public class BackupController { @Autowired Utilities utilities; @Autowired Environment env; @Autowired Signature sig; @RequestMapping(value = "/api/teavote/back-up", method = RequestMethod.GET) @ResponseBody public HttpEntity<byte[]> backup(HttpServletRequest request, HttpServletResponse response) throws Exception { HttpHeaders httpHeaders = new HttpHeaders(); String dbName = env.getProperty("teavote.db"); String dbUserName = env.getProperty("teavote.user"); String dbPassword = utilities.getPassword(); DateFormat df = new SimpleDateFormat("dd_MM_yyyy_HH_mm_ss"); Date dateobj = new Date(); String fileName = "teavote_backup_" + df.format(dateobj) + ".sql"; String path = new ConfigLocation().getConfigPath() + File.separator + fileName; if (!utilities.backupDB(dbName, dbUserName, dbPassword, path)) { return null; } //Sign file sig.signData(path); File backupFile = new File(path); byte[] bytes; try (InputStream backupInputStream = new FileInputStream(backupFile)) { httpHeaders.setContentType(MediaType.TEXT_PLAIN); httpHeaders.setContentDispositionFormData("Backup", fileName); bytes = IOUtils.toByteArray(backupInputStream); backupInputStream.close(); } backupFile.delete(); return new ResponseEntity<>(bytes, httpHeaders, HttpStatus.OK); } @RequestMapping(value = "/api/teavote/verify-back-up", method = RequestMethod.POST) @ResponseBody public JSONResponse verify(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception { if (!file.isEmpty()) { byte[] fileBytes = file.getBytes(); String pathToFileToVerify = new ConfigLocation().getConfigPath() + File.separator + "fileToVerify.sql"; FileOutputStream fos = new FileOutputStream(pathToFileToVerify); fos.write(fileBytes); fos.close(); if (sig.verifyFile(pathToFileToVerify)) { //Go ahead and restore database. String dbUserName = env.getProperty("teavote.user"); String dbPassword = utilities.getPassword(); if (!utilities.restoreDB(dbUserName, dbPassword, pathToFileToVerify)) { return new JSONResponse(false, 0, null, Enums.JSONResponseMessage.SERVER_ERROR.toString() + ": Could not restore database"); } //Delete the file new File(pathToFileToVerify).delete(); return new JSONResponse(true, 0, null, Enums.JSONResponseMessage.SUCCESS.toString()); } else { return new JSONResponse(false, 0, null, Enums.JSONResponseMessage.ACCESS_DENIED.toString() + ": Digital Signature could not be verified"); } } return new JSONResponse(false, 0, null, "Empty file"); } @ExceptionHandler(NullPointerException.class) @ResponseBody public JSONResponse nullPointerException(NullPointerException e) { return new JSONResponse(false, 0, null, e.getMessage()); } @ExceptionHandler(SignatureException.class) @ResponseBody public JSONResponse signatureException(SignatureException e) { return new JSONResponse(false, 0, null, e.getMessage()); } // @ExceptionHandler(Base64DecodingException.class) // @ResponseBody // public JSONResponse base64DecodingException(Base64DecodingException e) { // return new JSONResponse(false, 0, null, e.getMessage()); // } @ExceptionHandler(Exception.class) @ResponseBody public JSONResponse exception(Exception e) { return new JSONResponse(false, 0, null, e.getMessage()); } @ExceptionHandler(EmptyResultDataAccessException.class) @ResponseBody public JSONResponse exception(EmptyResultDataAccessException e) { return new JSONResponse(false, 0, null, e.getMessage()); } @ExceptionHandler(MissingParameterException.class) @ResponseBody public JSONResponse exception(MissingParameterException e) { return new JSONResponse(false, 0, null, e.getMessage()); } @ExceptionHandler(ExpiredJwtException.class) @ResponseBody public JSONResponse expiredJwtException(Exception e) { return new JSONResponse(false, 0, e.getMessage(), "ExpiredJwt"); } }