WebBox Klassenbibliothek
ulrich
2017-03-11 1e534604266081f011786dfe9b27874bc3d030d4
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;
1e5346 27 import java.util.logging.Level;
61b659 28 import java.util.logging.Logger;
7bcebf 29 import javax.servlet.ServletException;
U 30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import org.apache.catalina.servlets.DefaultServlet;
33
34 /**
35  * Mit dem TNServlet kann f&uuml;r eine Bilddatei eine 
36  * Miniaturansicht erzeugt werden, wie sie im Dateimanager 
37  * der WebBox verwendet wird.
38  * 
39  */
40 public class TNServlet extends DefaultServlet {
61b659 41   
U 42   private static final Logger logger = Logger.getLogger(TNServlet.class.getName());
7bcebf 43   
U 44   /**
45    * Diese String-Konstanten noetigenfalls in eine 
46    * Konfigurationsdatei auslagern
47    */
48   public static final String TN = "_tn";
49   public static final String JPG = ".jpg";
50   public static final String JPEG = ".jpeg";
51   public static final String PNG = ".png";
52   
53   /**
54    * Handles the HTTP <code>GET</code> method.
55    *
56    * @param request servlet request
57    * @param response servlet response
58    * @throws ServletException if a servlet-specific error occurs
59    * @throws IOException if an I/O error occurs
60    */
61   @Override
62   protected void doGet(HttpServletRequest request, HttpServletResponse response)
63           throws ServletException, IOException {
64     
61b659 65     String uriStr = request.getRequestURI();
1e5346 66     String relname = uriStr.substring(request.getContextPath().length());
61b659 67     
1e5346 68     /*
U 69     File urifile = new File(uriStr);
70     uriStr = uriStr.substring(request.getContextPath().length());
71     File dir = new File(request.getServletContext().getRealPath("/"));
72     File imgfile = new File(dir, uriStr);
61b659 73     
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: ");
82     buf.append(urifile.getAbsolutePath());
83     buf.append("\n");
84     buf.append("imgfile: ");
85     buf.append(imgfile.getAbsolutePath());
86     logger.fine(buf.toString());
87     */
61b659 88     
U 89     if(uriStr.contains(TN) && (uriStr.endsWith(JPG) || uriStr.endsWith(JPEG) || uriStr.endsWith(PNG))) {
7bcebf 90       /*
U 91       TODO: hier mit Hilfe der Klasse Bild eine Miniaturansicht erzeugen  
92       und in die Antwort schreiben
1e5346 93       */ 
U 94       File dir = new File(request.getServletContext().getRealPath("/"));
95       relname = relname.replace(TN, "");
96       File imgfile = new File(dir, relname);
97       Image image = Toolkit.getDefaultToolkit().getImage(imgfile.getAbsolutePath());
98       MediaTracker mediaTracker = new MediaTracker(new Container());
99       mediaTracker.addImage(image, 0);
100       try {
101         mediaTracker.waitForID(0);
102
103         if (!mediaTracker.isErrorAny()) {
104           Bild bild = new Bild();
105           bild.writeImageStream(image, bild.getVariantenGroesse(Bild.WINZIG), bild.getMimeType(imgfile), response.getOutputStream());
106         }
107       } catch (InterruptedException ex) {
108         logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
109       }
110       
7bcebf 111     } else {
U 112       super.doGet(request, response);
113     }
114             
115     
116   }
117   
118 }