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.model.ElectionInfo; import com.teasoft.teavote.service.CandidateService; import com.teasoft.teavote.service.CategoryService; import com.teasoft.teavote.service.LoginService; import com.teasoft.teavote.util.AppProperties; import com.teasoft.teavote.util.ConfigLocation; import com.teasoft.teavote.util.Enums; import com.teasoft.teavote.util.JSONResponse; import io.jsonwebtoken.ExpiredJwtException; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.activation.MimetypesFileTypeMap; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import org.apache.commons.compress.utils.IOUtils; import org.springframework.beans.factory.annotation.Autowired; 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.util.DefaultPropertiesPersister; import org.springframework.web.bind.annotation.ExceptionHandler; 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 ElectionInfoController { final int MAX_IMAGE_DIM_DIFF = 70; final static int IMG_WIDTH = 250; final static int IMG_HEIGHT = 250; @Autowired CandidateService candidateService; @Autowired CategoryService categoryService; @Autowired JdbcTemplate jdbcTemplate; @Autowired LoginService loginService; @Autowired AppProperties appProp; private static BufferedImage resizeImage(BufferedImage originalImage, int type) { BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null); g.dispose(); return resizedImage; } @RequestMapping(value = "api/teavote/election-info", method = RequestMethod.POST) @ResponseBody public JSONResponse saveElectionInfo(@RequestParam("logo") MultipartFile file, @RequestParam("commissioner") String commissioner, @RequestParam("orgName") String orgName, @RequestParam("electionDate") String electionDate, @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime, @RequestParam("pollingStation") String pollingStation, @RequestParam("startTimeInMillis") String startTimeInMillis, @RequestParam("endTimeInMillis") String endTimeInMillis, HttpServletRequest request) throws Exception { JSONResponse jSONResponse = new JSONResponse(); //Candidate candidate = new Candidate(); Map<String, String> errorMessages = new HashMap<>(); if (!file.isEmpty()) { BufferedImage image = ImageIO.read(file.getInputStream()); Integer width = image.getWidth(); Integer height = image.getHeight(); if (Math.abs(width - height) > MAX_IMAGE_DIM_DIFF) { errorMessages.put("image", "Invalid Image Dimension - Max abs(Width - height) must be 50 or less"); } else { //Resize image BufferedImage originalImage = ImageIO.read(file.getInputStream()); int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType(); BufferedImage resizeImagePng = resizeImage(originalImage, type); ConfigLocation configLocation = new ConfigLocation(); //String rootPath = request.getSession().getServletContext().getRealPath("/"); File serverFile = new File(configLocation.getConfigPath() + File.separator + "teavote-logo.jpg"); switch (file.getContentType()) { case "image/png": ImageIO.write(resizeImagePng, "png", serverFile); break; case "image/jpeg": ImageIO.write(resizeImagePng, "jpg", serverFile); break; default: ImageIO.write(resizeImagePng, "png", serverFile); break; } } } else { // errorMessages.put("image", "File Empty"); } //Load properties Properties prop = appProp.getProperties(); ConfigLocation configLocation = new ConfigLocation(); prop.load(configLocation.getExternalProperties()); prop.setProperty("teavote.orgName", orgName); prop.setProperty("teavote.commissioner", commissioner); prop.setProperty("teavote.electionDate", electionDate); prop.setProperty("teavote.startTime", startTime); prop.setProperty("teavote.endTime", endTime); prop.setProperty("teavote.pollingStation", pollingStation); prop.setProperty("teavote.startTimeInMillis", startTimeInMillis); prop.setProperty("teavote.endTimeInMillis", endTimeInMillis); File f = new File(configLocation.getConfigPath() + File.separator + "application.properties"); OutputStream out = new FileOutputStream(f); DefaultPropertiesPersister p = new DefaultPropertiesPersister(); p.store(prop, out, "Header Comment"); if (errorMessages.isEmpty()) { return new JSONResponse(true, 0, null, Enums.JSONResponseMessage.SUCCESS.toString()); } return new JSONResponse(false, 0, errorMessages, Enums.JSONResponseMessage.SERVER_ERROR.toString()); } @RequestMapping(value = "api/teavote/election-info", method = RequestMethod.GET) @ResponseBody public JSONResponse getElectionInfo(HttpServletRequest request) throws Exception { JSONResponse jSONResponse = new JSONResponse(); String home = System.getProperty("user.home"); String rootLocation = home + File.separator + ".tvconfig"; //Get external properties and get values for comminisioner and orgName //Load properties Properties prop = appProp.getProperties(); ConfigLocation configLocation = new ConfigLocation(); prop.load(configLocation.getExternalProperties()); String commissioner = prop.getProperty("teavote.commissioner"); String orgName = prop.getProperty("teavote.orgName"); String electionDate = prop.getProperty("teavote.electionDate"); String startTime = prop.getProperty("teavote.startTime"); String endTime = prop.getProperty("teavote.endTime"); String pollingStation = prop.getProperty("teavote.pollingStation"); String startTimeInMillis = prop.getProperty("teavote.startTimeInMillis"); String endTimeInMillis = prop.getProperty("teavote.endTimeInMillis"); byte[] logo = null; //Get teavote-logo.jpg as byte[] if (Files.exists(Paths.get(configLocation.getConfigPath() + File.separator + "teavote-logo.jpg"))) { InputStream is = new FileInputStream( configLocation.getConfigPath() + File.separator + "teavote-logo.jpg"); logo = IOUtils.toByteArray(is); is.close(); } ElectionInfo electionInfo = new ElectionInfo(commissioner, orgName, logo, electionDate, startTime, endTime, pollingStation, startTimeInMillis, endTimeInMillis); return new JSONResponse(true, 1, electionInfo, "SUCCESS"); } @RequestMapping(value = "resources/election-info/logo-img", method = RequestMethod.GET) @ResponseBody public HttpEntity<byte[]> getElectionLogo(HttpServletRequest request) throws Exception { JSONResponse jSONResponse = new JSONResponse(); String home = System.getProperty("user.home"); String rootLocation = home + File.separator + ".tvconfig"; //Get external properties and get values for comminisioner and orgName //Load properties Properties prop = appProp.getProperties(); ConfigLocation configLocation = new ConfigLocation(); prop.load(configLocation.getExternalProperties()); byte[] logo = null; HttpHeaders httpHeaders = new HttpHeaders(); //Get teavote-logo.jpg as byte[] if (Files.exists(Paths.get(configLocation.getConfigPath() + File.separator + "teavote-logo.jpg"))) { InputStream is = new FileInputStream( configLocation.getConfigPath() + File.separator + "teavote-logo.jpg"); logo = IOUtils.toByteArray(is); httpHeaders.setContentType(MediaType.IMAGE_JPEG); is.close(); } return new ResponseEntity<>(logo, httpHeaders, HttpStatus.OK); } @ExceptionHandler(NullPointerException.class) @ResponseBody public JSONResponse nullPointerException(NullPointerException 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"); } }