Java tutorial
/* * Cerberus Copyright (C) 2013 vertigo17 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This file is part of Cerberus. * * Cerberus is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Cerberus is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Cerberus. If not, see <http://www.gnu.org/licenses/>. */ package org.cerberus.servlet.crud.countryenvironment; import com.google.gson.Gson; import com.mortennobel.imagescaling.DimensionConstrain; import com.mortennobel.imagescaling.ResampleOp; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateUtils; import org.cerberus.crud.entity.ApplicationObject; import org.cerberus.crud.service.IApplicationObjectService; import org.cerberus.enums.MessageEventEnum; import org.cerberus.exception.CerberusException; import org.cerberus.servlet.crud.test.PictureConnector; import org.cerberus.util.ParameterParserUtil; import org.cerberus.util.answer.AnswerItem; import org.cerberus.util.answer.AnswerList; import org.cerberus.util.answer.AnswerUtil; import org.cerberus.util.servlet.ServletUtil; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.owasp.html.PolicyFactory; import org.owasp.html.Sanitizers; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import sun.misc.BASE64Decoder; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.*; import java.util.*; import java.util.logging.Logger; /** * * @author vertigo */ @WebServlet(name = "ReadApplicationObjectImage", urlPatterns = { "/ReadApplicationObjectImage" }) public class ReadApplicationObjectImage extends HttpServlet { private IApplicationObjectService applicationObjectService; String data = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAFABQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigAooooA//9k="; String base64Image = data.split(",")[1]; byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image); /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs * @throws CerberusException */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException { String charset = request.getCharacterEncoding(); String application = ParameterParserUtil .parseStringParamAndDecodeAndSanitize(request.getParameter("application"), "", charset); String object = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("object"), "", charset); int width = (!StringUtils.isEmpty(request.getParameter("w"))) ? Integer.valueOf(request.getParameter("w")) : -1; int height = (!StringUtils.isEmpty(request.getParameter("h"))) ? Integer.valueOf(request.getParameter("h")) : -1; ApplicationContext appContext = WebApplicationContextUtils .getWebApplicationContext(this.getServletContext()); IApplicationObjectService applicationObjectService = appContext.getBean(IApplicationObjectService.class); BufferedImage image = applicationObjectService.readImageByKey(application, object); BufferedImage b; if (image != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); /** * If width and height not defined, get image in real size */ if (width == -1 && height == -1) { b = image; } else { ResampleOp rop = new ResampleOp(DimensionConstrain.createMaxDimension(width, height, true)); rop.setNumberOfThreads(4); b = rop.filter(image, null); } ImageIO.write(b, "png", baos); // byte[] bytesOut = baos.toByteArray(); } else { // create a buffered image ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes); b = ImageIO.read(bis); bis.close(); } response.setHeader("Last-Modified", DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360).toGMTString()); response.setHeader("Expires", DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360).toGMTString()); ImageIO.write(b, "png", response.getOutputStream()); } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { processRequest(request, response); } catch (CerberusException ex) { Logger.getLogger(ReadApplicationObjectImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { processRequest(request, response); } catch (CerberusException ex) { Logger.getLogger(ReadApplicationObjectImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }