WebBox Klassenbibliothek
ulrich
2017-04-08 9934756c7859bf60f217623cf8cf9b67d5467d9a
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
1e5346 20 import de.uhilger.wbx.Bild;
U 21 import java.awt.Container;
61b659 22 import java.awt.Image;
1e5346 23 import java.awt.MediaTracker;
U 24 import java.awt.Toolkit;
61b659 25 import java.io.File;
7bcebf 26 import java.io.IOException;
0622f3 27 import java.net.URLDecoder;
1e5346 28 import java.util.logging.Level;
61b659 29 import java.util.logging.Logger;
7bcebf 30 import javax.servlet.ServletException;
U 31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import org.apache.catalina.servlets.DefaultServlet;
34
35 /**
36  * Mit dem TNServlet kann f&uuml;r eine Bilddatei eine 
37  * Miniaturansicht erzeugt werden, wie sie im Dateimanager 
38  * der WebBox verwendet wird.
39  * 
40  */
41 public class TNServlet extends DefaultServlet {
61b659 42   
U 43   private static final Logger logger = Logger.getLogger(TNServlet.class.getName());
7bcebf 44   
U 45   /**
46    * Diese String-Konstanten noetigenfalls in eine 
47    * Konfigurationsdatei auslagern
48    */
49   public static final String TN = "_tn";
50   public static final String JPG = ".jpg";
51   public static final String JPEG = ".jpeg";
52   public static final String PNG = ".png";
53   
54   /**
55    * Handles the HTTP <code>GET</code> method.
56    *
57    * @param request servlet request
58    * @param response servlet response
59    * @throws ServletException if a servlet-specific error occurs
60    * @throws IOException if an I/O error occurs
61    */
62   @Override
63   protected void doGet(HttpServletRequest request, HttpServletResponse response)
64           throws ServletException, IOException {
65     
61b659 66     String uriStr = request.getRequestURI();
1e5346 67     String relname = uriStr.substring(request.getContextPath().length());
61b659 68     
0622f3 69     // --- Logausgabe Start
U 70     File logurifile = new File(uriStr);
71     String loguriStr = uriStr.substring(request.getContextPath().length());
72     File logdir = new File(request.getServletContext().getRealPath("/"));
73     File logimgfile = new File(logdir, loguriStr);    
1e5346 74     StringBuffer buf = new StringBuffer();
U 75     buf.append("contextPath: ");
76     buf.append(request.getContextPath());
77     buf.append("\n");
78     buf.append("realpath of /: ");
79     buf.append(request.getServletContext().getRealPath("/"));
80     buf.append("\n");
81     buf.append("urifile: ");
0622f3 82     buf.append(logurifile.getAbsolutePath());
1e5346 83     buf.append("\n");
U 84     buf.append("imgfile: ");
0622f3 85     buf.append(logimgfile.getAbsolutePath());
1e5346 86     logger.fine(buf.toString());
0622f3 87     // --- Logausgabe Ende
U 88         
61b659 89     if(uriStr.contains(TN) && (uriStr.endsWith(JPG) || uriStr.endsWith(JPEG) || uriStr.endsWith(PNG))) {
1e5346 90       File dir = new File(request.getServletContext().getRealPath("/"));
U 91       relname = relname.replace(TN, "");
0622f3 92       File imgfile = new File(dir, URLDecoder.decode(relname, "utf-8"));
1e5346 93       Image image = Toolkit.getDefaultToolkit().getImage(imgfile.getAbsolutePath());
U 94       MediaTracker mediaTracker = new MediaTracker(new Container());
95       mediaTracker.addImage(image, 0);
96       try {
97         mediaTracker.waitForID(0);
98
99         if (!mediaTracker.isErrorAny()) {
100           Bild bild = new Bild();
101           bild.writeImageStream(image, bild.getVariantenGroesse(Bild.WINZIG), bild.getMimeType(imgfile), response.getOutputStream());
102         }
103       } catch (InterruptedException ex) {
104         logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
105       }
7bcebf 106     } else {
U 107       super.doGet(request, response);
108     }
109   }
110 }