KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jgallery > servlets > Controller


1
2 package de.jwi.jgallery.servlets;
3
4 /*
5  * jGallery - Java web application to display image galleries
6  *
7  * Copyright (C) 2004 Juergen Weber
8  *
9  * This file is part of jGallery.
10  *
11  * jGallery is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * jGallery is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with jGallery; if not, write to the Free
20  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston
21  */

22
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.URLConnection JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39 import javax.servlet.RequestDispatcher JavaDoc;
40 import javax.servlet.ServletContext JavaDoc;
41 import javax.servlet.ServletException JavaDoc;
42 import javax.servlet.http.HttpServlet JavaDoc;
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import javax.servlet.http.HttpSession JavaDoc;
46 import javax.sql.DataSource JavaDoc;
47
48 import de.jwi.jgallery.ConfigData;
49 import de.jwi.jgallery.Configuration;
50 import de.jwi.jgallery.Folder;
51 import de.jwi.jgallery.GalleryException;
52 import de.jwi.jgallery.GalleryNotFoundException;
53 import de.jwi.jgallery.IThumbnailWriter;
54 import de.jwi.jgallery.WebFolder;
55 import de.jwi.jgallery.db.DBManager;
56 import de.jwi.servletutil.PathHelper;
57 import de.jwi.servletutil.RealPath;
58
59 /**
60  * @author Jürgen Weber Source file created on 17.02.2004
61  *
62  */

63 public class Controller extends HttpServlet JavaDoc
64 {
65
66     private static String JavaDoc version = "unknown";
67
68     private static String JavaDoc urlExtention;
69
70     private static final String JavaDoc FOLDERS = "folders";
71
72     private static final String JavaDoc CONFIGFILE = "jGallery.properties";
73
74     static final String JavaDoc VERSIONCONFIGFILE = "version.properties";
75
76     private static final String JavaDoc WEBDIRSFILE = "web.properties";
77
78     private Configuration configuration;
79
80     private Properties JavaDoc webDirectories = null;
81
82     private HashSet JavaDoc webKeys = new HashSet JavaDoc();
83
84     private Properties JavaDoc dirmapping = null;
85
86     private String JavaDoc dataSource = null;
87
88     private boolean useDataBase = false;
89
90     private DBManager dBManager;
91
92     private void initDBConnection() throws ServletException JavaDoc
93     {
94         if (useDataBase)
95         {
96             Context JavaDoc context;
97             try
98             {
99                 context = new InitialContext JavaDoc();
100                 if (context == null)
101                 {
102                     throw new ServletException JavaDoc("Boom - No Context");
103                 }
104                 DataSource JavaDoc ds = (DataSource JavaDoc) context.lookup(dataSource);
105
106                 dBManager = new DBManager(ds);
107             }
108             catch (NamingException JavaDoc e)
109             {
110                 throw new ServletException JavaDoc(e.getMessage(), e);
111             }
112         }
113     }
114
115
116     public void init() throws ServletException JavaDoc
117     {
118         ServletContext JavaDoc context = getServletContext();
119
120         dataSource = context.getInitParameter("dataSource");
121
122         String JavaDoc s = context.getInitParameter("dirmappings");
123
124         dirmapping = new Properties JavaDoc();
125
126         if (null != s)
127         {
128             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, ",");
129             while (st.hasMoreTokens())
130             {
131                 String JavaDoc s1 = st.nextToken();
132                 int p = s1.indexOf('=');
133                 if (p > -1)
134                 {
135                     String JavaDoc key = s1.substring(0, p);
136                     String JavaDoc val = s1.substring(p + 1);
137                     dirmapping.setProperty(key, val);
138                 }
139             }
140         }
141
142         s = context.getInitParameter("useDataBase");
143
144         if (null != s)
145         {
146             useDataBase = Boolean.valueOf(s).booleanValue();
147             initDBConnection();
148         }
149
150         InputStream JavaDoc is = context.getResourceAsStream("/WEB-INF/" + CONFIGFILE);
151
152         try
153         {
154             configuration = new Configuration(is);
155
156             s = context.getInitParameter("thumbnailWriter");
157
158             Object JavaDoc o = Class.forName(s).newInstance();
159
160             configuration.setThumbnailWriter((IThumbnailWriter) o);
161         }
162         catch (Exception JavaDoc e)
163         {
164             throw new ServletException JavaDoc(e.getMessage());
165         }
166
167         if (is != null)
168         {
169             try
170             {
171                 is.close();
172             }
173             catch (IOException JavaDoc e)
174             {
175                 throw new ServletException JavaDoc(e.getMessage());
176             }
177         }
178
179         
180         // Parameters from the config file in WEB-INF can be overridden from parameters
181
// in the context configuration
182

183         Configuration configurationFromContext = null;
184         Enumeration JavaDoc en = context.getInitParameterNames();
185         while (en.hasMoreElements())
186         {
187             if (configurationFromContext == null)
188             {
189                 configuration = configurationFromContext = new Configuration(configuration);
190             }
191             s = (String JavaDoc)en.nextElement();
192             String JavaDoc s1 = context.getInitParameter(s);
193             configurationFromContext.addProperty(s,s1);
194         }
195         
196         
197         
198         is = context.getResourceAsStream("/WEB-INF/" + WEBDIRSFILE);
199
200         if (null != is)
201         {
202             try
203             {
204                 webDirectories = new Properties JavaDoc();
205                 webDirectories.load(is);
206
207                 for (en = webDirectories.keys(); en.hasMoreElements();)
208                 {
209                     webKeys.add(en.nextElement());
210                 }
211
212             }
213             catch (IOException JavaDoc e)
214             {
215                 throw new ServletException JavaDoc(e.getMessage());
216             }
217         }
218
219         if (is != null)
220         {
221             try
222             {
223                 is.close();
224             }
225             catch (IOException JavaDoc e)
226             {
227                 throw new ServletException JavaDoc(e.getMessage());
228             }
229         }
230
231
232         try
233         {
234             is = context.getResourceAsStream("/WEB-INF/" + VERSIONCONFIGFILE);
235
236             Properties JavaDoc versionProperties = new Properties JavaDoc();
237             versionProperties.load(is);
238
239             s = versionProperties.getProperty("version");
240             if (null != s)
241             {
242                 version = s;
243             }
244         }
245         catch (Exception JavaDoc e)
246         {
247             e.printStackTrace(System.err); // TODO
248
}
249
250         if (is != null)
251         {
252             try
253             {
254                 is.close();
255             }
256             catch (IOException JavaDoc e)
257             {
258                 throw new ServletException JavaDoc(e.getMessage());
259             }
260         }
261
262     }
263
264     public static String JavaDoc getVersion()
265     {
266         return version;
267     }
268
269     private Folder getFolder(HttpSession JavaDoc session, String JavaDoc folderPath)
270     {
271         Hashtable JavaDoc folders = getFolders(session);
272
273         Folder folder = (Folder) folders.get(folderPath);
274
275         return folder;
276     }
277
278     private Hashtable JavaDoc getFolders(HttpSession JavaDoc session)
279     {
280         Hashtable JavaDoc folders = (Hashtable JavaDoc) session.getAttribute(FOLDERS);
281
282         if (null == folders)
283         {
284             folders = new Hashtable JavaDoc();
285             session.setAttribute(FOLDERS, folders);
286         }
287         return folders;
288     }
289
290     private Folder createFolder(HttpSession JavaDoc session, String JavaDoc folderPath,
291             String JavaDoc imagePath, String JavaDoc folderRealPath,
292             String JavaDoc jgalleryContextPath, boolean doCount)
293             throws GalleryException
294     {
295         // No need to synchronize access to the Folders because they are per
296
// session. In the worst case if a user has several browser windows
297
// (that
298
// share the same session), there might be unnecessary creations of
299
// Folders.
300

301         Folder folder;
302         Configuration configuration = this.configuration;
303
304         File JavaDoc directory = new File JavaDoc(folderRealPath);
305
306         if (!directory.exists())
307         {
308             throw new GalleryNotFoundException("directory does not exist: "
309                     + directory.toString());
310         }
311
312         File JavaDoc config = new File JavaDoc(directory, CONFIGFILE);
313         if (config.exists())
314         {
315             InputStream JavaDoc is;
316             try
317             {
318                 is = new FileInputStream JavaDoc(config);
319             }
320             catch (FileNotFoundException JavaDoc e)
321             {
322                 throw new GalleryException(e.getMessage());
323             }
324
325             try
326             {
327                 Configuration conf = new Configuration(is, configuration);
328                 configuration = conf;
329             }
330             catch (IOException JavaDoc e)
331             {
332                 throw new GalleryException(e.getMessage());
333             }
334             finally
335             {
336                 if (is != null)
337                 {
338                     try
339                     {
340                         is.close();
341                     }
342                     catch (IOException JavaDoc e)
343                     {
344                         throw new GalleryException(e.getMessage());
345                     }
346                 }
347             }
348         }
349
350         ConfigData configData = new ConfigData();
351         configData.version = version;
352         configData.urlExtention = urlExtention;
353         configData.doCount = doCount;
354
355         folder = new Folder(directory, getServletContext(), configuration,
356                 configData, jgalleryContextPath, folderPath, imagePath,
357                 dBManager);
358
359         Hashtable JavaDoc folders = getFolders(session);
360
361         folders.put(folderPath, folder);
362
363         return folder;
364     }
365
366     private Folder createWebFolder(HttpSession JavaDoc session, String JavaDoc remoteKey,
367             String JavaDoc folderPath, String JavaDoc baseURL, String JavaDoc jgalleryContextPath)
368             throws GalleryException
369     {
370         // No need to synchronize access to the Folders because they are per
371
// session. In the worst case if a user has several browser windows
372
// (that
373
// share the same session), there might be unnecessary creations of
374
// Folders.
375

376         Configuration configuration = this.configuration;
377         Folder folder;
378         InputStream JavaDoc wis = null;
379         URL JavaDoc url;
380         URLConnection JavaDoc connection = null;
381         try
382         {
383             url = new URL JavaDoc(baseURL + folderPath + "images.txt");
384             connection = url.openConnection();
385             connection.connect();
386         }
387         catch (Exception JavaDoc e)
388         {
389             throw new GalleryException(e.getMessage());
390         }
391
392         try
393         {
394             wis = connection.getInputStream();
395             long l = connection.getLastModified();
396         }
397         catch (IOException JavaDoc e)
398         {
399             throw new GalleryNotFoundException(e.getMessage());
400         }
401
402         try
403         {
404             URL JavaDoc url1 = new URL JavaDoc(baseURL + folderPath + CONFIGFILE);
405             URLConnection JavaDoc connection1 = url1.openConnection();
406             connection1.connect();
407             InputStream JavaDoc is = connection1.getInputStream();
408
409             if (null != is)
410             {
411                 Configuration conf = new Configuration(is, configuration);
412                 configuration = conf;
413             }
414         }
415         catch (Exception JavaDoc e1)
416         {
417             // nothing
418
}
419         configuration = new Configuration(configuration);
420         configuration.addProperty("thumbnails.create", "false");
421
422         ConfigData configData = new ConfigData();
423         configData.version = version;
424         configData.urlExtention = urlExtention;
425
426         folder = new WebFolder(baseURL, getServletContext(), configuration,
427                 configData, remoteKey, jgalleryContextPath, folderPath, wis);
428
429         Hashtable JavaDoc folders = getFolders(session);
430
431         folders.put(folderPath, folder);
432
433         return folder;
434     }
435
436     
437     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
438             throws IOException JavaDoc, ServletException JavaDoc
439     {
440         String JavaDoc pathInfo = request.getPathInfo();
441         String JavaDoc queryString = request.getQueryString();
442         String JavaDoc pathTranslated = request.getPathTranslated();
443         String JavaDoc requestURI = request.getRequestURI();
444         String JavaDoc requestURL = request.getRequestURL().toString();
445
446         // contextPath := "/jGallery"
447
String JavaDoc contextPath = request.getContextPath();
448
449         String JavaDoc servletPath = request.getServletPath();
450
451         String JavaDoc nocount = request.getParameter("nocount");
452         boolean doCount = (!"true".equals(nocount));
453
454         // servletPath := "/galleries" with path mapping
455

456         // servletPath= "/testalbum/CRW_9753.html" with ext mapping
457

458         // /myfolder/subfolder/myimage.html
459
// gets split into /myfolder/subfolder/ (which is used as key for the
460
// Folder object)
461
// and imageName myimage.html
462

463         String JavaDoc folderPath = servletPath.substring(0, servletPath
464                 .lastIndexOf('/') + 1);
465         String JavaDoc imageName = servletPath
466                 .substring(servletPath.lastIndexOf('/') + 1);
467
468         urlExtention = imageName.substring(imageName.lastIndexOf('.') + 1);
469
470         // getServletContext().log("folderPath: "+folderPath);
471

472         String JavaDoc folderRealPath = null;
473
474         String JavaDoc forward = null;
475
476         Folder folder = getFolder(request.getSession(), folderPath);
477
478         try
479         {
480             if (null == folder)
481             {
482                 RealPath theRealPath = PathHelper.getHttpRealPath(
483                         getServletContext(), folderPath, dirmapping);
484
485                 if (null == theRealPath)
486                 {
487                     response.sendError(HttpServletResponse.SC_NOT_FOUND,
488                             request.getRequestURI());
489                     return;
490                 }
491
492                 folderRealPath = theRealPath.getRealPath();
493
494                 String JavaDoc imagePath = folderPath;
495
496                 if (!"".equals(contextPath) && contextPath.substring(1).equals(theRealPath.getContext()))
497                 {
498                     // special case for image folders below jGallery's context
499

500                     imagePath = contextPath + folderPath;
501                 }
502
503                 folder = createFolder(request.getSession(), folderPath,
504                         imagePath, folderRealPath, contextPath, doCount);
505
506                 folder.loadFolder();
507             }
508             int n = folder.setFileName(imageName);
509
510             if (Folder.INDEX == n)
511             {
512                 forward = folder.getIndexJsp();
513             }
514             else
515             {
516                 forward = folder.getSlideJsp();
517             }
518
519             request.setAttribute("folder", folder);
520             request.setAttribute("image", folder.getImage());
521
522         }
523         catch (GalleryNotFoundException e)
524         {
525             response.sendError(HttpServletResponse.SC_NOT_FOUND, request
526                     .getRequestURI());
527             return;
528         }
529         catch (GalleryException e)
530         {
531             e.printStackTrace(System.err);
532             throw new ServletException JavaDoc(e.getMessage());
533             // request.setAttribute("javax.servlet.error.exception",e);
534
// forward = "/errorpage.jsp";
535
}
536
537         RequestDispatcher JavaDoc requestDispatcher = getServletContext()
538                 .getRequestDispatcher(forward);
539
540         requestDispatcher.forward(request, response);
541     }
542 }
Popular Tags