1 2 package de.jwi.jgallery.servlets; 3 4 22 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.net.URL ; 29 import java.net.URLConnection ; 30 import java.util.Enumeration ; 31 import java.util.HashSet ; 32 import java.util.Hashtable ; 33 import java.util.Properties ; 34 import java.util.StringTokenizer ; 35 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 38 import javax.naming.NamingException ; 39 import javax.servlet.RequestDispatcher ; 40 import javax.servlet.ServletContext ; 41 import javax.servlet.ServletException ; 42 import javax.servlet.http.HttpServlet ; 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 import javax.servlet.http.HttpSession ; 46 import javax.sql.DataSource ; 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 63 public class Controller extends HttpServlet 64 { 65 66 private static String version = "unknown"; 67 68 private static String urlExtention; 69 70 private static final String FOLDERS = "folders"; 71 72 private static final String CONFIGFILE = "jGallery.properties"; 73 74 static final String VERSIONCONFIGFILE = "version.properties"; 75 76 private static final String WEBDIRSFILE = "web.properties"; 77 78 private Configuration configuration; 79 80 private Properties webDirectories = null; 81 82 private HashSet webKeys = new HashSet (); 83 84 private Properties dirmapping = null; 85 86 private String dataSource = null; 87 88 private boolean useDataBase = false; 89 90 private DBManager dBManager; 91 92 private void initDBConnection() throws ServletException 93 { 94 if (useDataBase) 95 { 96 Context context; 97 try 98 { 99 context = new InitialContext (); 100 if (context == null) 101 { 102 throw new ServletException ("Boom - No Context"); 103 } 104 DataSource ds = (DataSource ) context.lookup(dataSource); 105 106 dBManager = new DBManager(ds); 107 } 108 catch (NamingException e) 109 { 110 throw new ServletException (e.getMessage(), e); 111 } 112 } 113 } 114 115 116 public void init() throws ServletException 117 { 118 ServletContext context = getServletContext(); 119 120 dataSource = context.getInitParameter("dataSource"); 121 122 String s = context.getInitParameter("dirmappings"); 123 124 dirmapping = new Properties (); 125 126 if (null != s) 127 { 128 StringTokenizer st = new StringTokenizer (s, ","); 129 while (st.hasMoreTokens()) 130 { 131 String s1 = st.nextToken(); 132 int p = s1.indexOf('='); 133 if (p > -1) 134 { 135 String key = s1.substring(0, p); 136 String 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 is = context.getResourceAsStream("/WEB-INF/" + CONFIGFILE); 151 152 try 153 { 154 configuration = new Configuration(is); 155 156 s = context.getInitParameter("thumbnailWriter"); 157 158 Object o = Class.forName(s).newInstance(); 159 160 configuration.setThumbnailWriter((IThumbnailWriter) o); 161 } 162 catch (Exception e) 163 { 164 throw new ServletException (e.getMessage()); 165 } 166 167 if (is != null) 168 { 169 try 170 { 171 is.close(); 172 } 173 catch (IOException e) 174 { 175 throw new ServletException (e.getMessage()); 176 } 177 } 178 179 180 183 Configuration configurationFromContext = null; 184 Enumeration en = context.getInitParameterNames(); 185 while (en.hasMoreElements()) 186 { 187 if (configurationFromContext == null) 188 { 189 configuration = configurationFromContext = new Configuration(configuration); 190 } 191 s = (String )en.nextElement(); 192 String 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 (); 205 webDirectories.load(is); 206 207 for (en = webDirectories.keys(); en.hasMoreElements();) 208 { 209 webKeys.add(en.nextElement()); 210 } 211 212 } 213 catch (IOException e) 214 { 215 throw new ServletException (e.getMessage()); 216 } 217 } 218 219 if (is != null) 220 { 221 try 222 { 223 is.close(); 224 } 225 catch (IOException e) 226 { 227 throw new ServletException (e.getMessage()); 228 } 229 } 230 231 232 try 233 { 234 is = context.getResourceAsStream("/WEB-INF/" + VERSIONCONFIGFILE); 235 236 Properties versionProperties = new Properties (); 237 versionProperties.load(is); 238 239 s = versionProperties.getProperty("version"); 240 if (null != s) 241 { 242 version = s; 243 } 244 } 245 catch (Exception e) 246 { 247 e.printStackTrace(System.err); } 249 250 if (is != null) 251 { 252 try 253 { 254 is.close(); 255 } 256 catch (IOException e) 257 { 258 throw new ServletException (e.getMessage()); 259 } 260 } 261 262 } 263 264 public static String getVersion() 265 { 266 return version; 267 } 268 269 private Folder getFolder(HttpSession session, String folderPath) 270 { 271 Hashtable folders = getFolders(session); 272 273 Folder folder = (Folder) folders.get(folderPath); 274 275 return folder; 276 } 277 278 private Hashtable getFolders(HttpSession session) 279 { 280 Hashtable folders = (Hashtable ) session.getAttribute(FOLDERS); 281 282 if (null == folders) 283 { 284 folders = new Hashtable (); 285 session.setAttribute(FOLDERS, folders); 286 } 287 return folders; 288 } 289 290 private Folder createFolder(HttpSession session, String folderPath, 291 String imagePath, String folderRealPath, 292 String jgalleryContextPath, boolean doCount) 293 throws GalleryException 294 { 295 301 Folder folder; 302 Configuration configuration = this.configuration; 303 304 File directory = new File (folderRealPath); 305 306 if (!directory.exists()) 307 { 308 throw new GalleryNotFoundException("directory does not exist: " 309 + directory.toString()); 310 } 311 312 File config = new File (directory, CONFIGFILE); 313 if (config.exists()) 314 { 315 InputStream is; 316 try 317 { 318 is = new FileInputStream (config); 319 } 320 catch (FileNotFoundException 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 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 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 folders = getFolders(session); 360 361 folders.put(folderPath, folder); 362 363 return folder; 364 } 365 366 private Folder createWebFolder(HttpSession session, String remoteKey, 367 String folderPath, String baseURL, String jgalleryContextPath) 368 throws GalleryException 369 { 370 376 Configuration configuration = this.configuration; 377 Folder folder; 378 InputStream wis = null; 379 URL url; 380 URLConnection connection = null; 381 try 382 { 383 url = new URL (baseURL + folderPath + "images.txt"); 384 connection = url.openConnection(); 385 connection.connect(); 386 } 387 catch (Exception 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 e) 398 { 399 throw new GalleryNotFoundException(e.getMessage()); 400 } 401 402 try 403 { 404 URL url1 = new URL (baseURL + folderPath + CONFIGFILE); 405 URLConnection connection1 = url1.openConnection(); 406 connection1.connect(); 407 InputStream is = connection1.getInputStream(); 408 409 if (null != is) 410 { 411 Configuration conf = new Configuration(is, configuration); 412 configuration = conf; 413 } 414 } 415 catch (Exception e1) 416 { 417 } 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 folders = getFolders(session); 430 431 folders.put(folderPath, folder); 432 433 return folder; 434 } 435 436 437 public void doGet(HttpServletRequest request, HttpServletResponse response) 438 throws IOException , ServletException 439 { 440 String pathInfo = request.getPathInfo(); 441 String queryString = request.getQueryString(); 442 String pathTranslated = request.getPathTranslated(); 443 String requestURI = request.getRequestURI(); 444 String requestURL = request.getRequestURL().toString(); 445 446 String contextPath = request.getContextPath(); 448 449 String servletPath = request.getServletPath(); 450 451 String nocount = request.getParameter("nocount"); 452 boolean doCount = (!"true".equals(nocount)); 453 454 456 458 463 String folderPath = servletPath.substring(0, servletPath 464 .lastIndexOf('/') + 1); 465 String imageName = servletPath 466 .substring(servletPath.lastIndexOf('/') + 1); 467 468 urlExtention = imageName.substring(imageName.lastIndexOf('.') + 1); 469 470 472 String folderRealPath = null; 473 474 String 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 imagePath = folderPath; 495 496 if (!"".equals(contextPath) && contextPath.substring(1).equals(theRealPath.getContext())) 497 { 498 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 (e.getMessage()); 533 } 536 537 RequestDispatcher requestDispatcher = getServletContext() 538 .getRequestDispatcher(forward); 539 540 requestDispatcher.forward(request, response); 541 } 542 } | Popular Tags |