WebBox Klassenbibliothek
ulrich
2018-04-03 5ebac825d99fb9b6bed2edeeb4c15ba34e8b6350
commit | author | age
7bcebf 1 /*
U 2     WebBox - Dein Server.
3     Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU Affero General Public License as
7     published by the Free Software Foundation, either version 3 of the
8     License, or (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU Affero General Public License for more details.
14
15     You should have received a copy of the GNU Affero General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package de.uhilger.wbx.web;
19
61b659 20 import java.io.File;
7bcebf 21 import java.io.IOException;
91d228 22 import java.io.UnsupportedEncodingException;
0622f3 23 import java.net.URLDecoder;
61b659 24 import java.util.logging.Logger;
7bcebf 25 import javax.servlet.ServletException;
U 26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
35827a 28 import net.coobird.thumbnailator.Thumbnails;
7bcebf 29 import org.apache.catalina.servlets.DefaultServlet;
U 30
31 /**
32  * Mit dem TNServlet kann f&uuml;r eine Bilddatei eine 
33  * Miniaturansicht erzeugt werden, wie sie im Dateimanager 
34  * der WebBox verwendet wird.
35  * 
91d228 36  * Unterstuetzt werden die Varianten
U 37  * 120 (_tn), 240 (_kl), 500 (_dd), 700 (_mt), 1200 (_gr)
7bcebf 38  */
U 39 public class TNServlet extends DefaultServlet {
61b659 40   
U 41   private static final Logger logger = Logger.getLogger(TNServlet.class.getName());
7bcebf 42   
U 43   /**
44    * Diese String-Konstanten noetigenfalls in eine 
45    * Konfigurationsdatei auslagern
46    */
47   public static final String TN = "_tn";
91d228 48   public static final String KL = "_kl";
U 49   public static final String SM = "_sm";
50   public static final String MT = "_mt";
51   public static final String GR = "_gr";
7bcebf 52   public static final String JPG = ".jpg";
U 53   public static final String JPEG = ".jpeg";
54   public static final String PNG = ".png";
55   
56   /**
57    * Handles the HTTP <code>GET</code> method.
58    *
59    * @param request servlet request
60    * @param response servlet response
61    * @throws ServletException if a servlet-specific error occurs
62    * @throws IOException if an I/O error occurs
63    */
64   @Override
65   protected void doGet(HttpServletRequest request, HttpServletResponse response)
66           throws ServletException, IOException {
67     
61b659 68     String uriStr = request.getRequestURI();
1e5346 69     String relname = uriStr.substring(request.getContextPath().length());
61b659 70     
0622f3 71     // --- Logausgabe Start
U 72     File logurifile = new File(uriStr);
73     String loguriStr = uriStr.substring(request.getContextPath().length());
74     File logdir = new File(request.getServletContext().getRealPath("/"));
75     File logimgfile = new File(logdir, loguriStr);    
1e5346 76     StringBuffer buf = new StringBuffer();
U 77     buf.append("contextPath: ");
78     buf.append(request.getContextPath());
79     buf.append("\n");
80     buf.append("realpath of /: ");
81     buf.append(request.getServletContext().getRealPath("/"));
82     buf.append("\n");
83     buf.append("urifile: ");
0622f3 84     buf.append(logurifile.getAbsolutePath());
1e5346 85     buf.append("\n");
U 86     buf.append("imgfile: ");
0622f3 87     buf.append(logimgfile.getAbsolutePath());
1e5346 88     logger.fine(buf.toString());
0622f3 89     // --- Logausgabe Ende
U 90         
91d228 91     if(uriStr.endsWith(JPG) || uriStr.endsWith(JPEG) || uriStr.endsWith(PNG)) {
U 92       if(uriStr.contains(TN)) {
35827a 93         bildAusgeben(request, response, relname, TN, 120);
91d228 94       } else if(uriStr.contains(KL)) {
35827a 95         bildAusgeben(request, response, relname, KL, 240);
91d228 96       } else if(uriStr.contains(SM)) {
35827a 97         bildAusgeben(request, response, relname, SM, 500);
91d228 98       } else if(uriStr.contains(MT)) {
35827a 99         bildAusgeben(request, response, relname, MT, 700);
91d228 100       } else if(uriStr.contains(GR)) {
35827a 101         bildAusgeben(request, response, relname, GR, 1200);
91d228 102       } else {
U 103         super.doGet(request, response);
104       }      
7bcebf 105     } else {
U 106       super.doGet(request, response);
107     }
108   }
91d228 109   
35827a 110   private void bildAusgeben(HttpServletRequest request, HttpServletResponse response, String relname, String indicator, int gr) throws UnsupportedEncodingException, IOException {
91d228 111     File dir = new File(request.getServletContext().getRealPath("/"));
U 112     relname = relname.replace(indicator, "");
113     File imgfile = new File(dir, URLDecoder.decode(relname, "utf-8"));
35827a 114     
U 115     // 120, 240, 500, 700, 1200
91d228 116
35827a 117     Thumbnails.of(imgfile)
U 118             .size(gr, gr)
119             .keepAspectRatio(true)
1475ef 120             .outputQuality(0.7)
35827a 121             .toOutputStream(response.getOutputStream());
U 122   }  
7bcebf 123 }