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