View Javadoc

1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //   this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //   this list of conditions and the following disclaimer in the documentation  
12  //   and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //   nor the names of its contributors may be used to endorse or promote products  
15  //   derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  
29  package org.objectledge.filesystem;
30  
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.net.MalformedURLException;
34  import java.net.URL;
35  
36  import javax.servlet.ServletContext;
37  
38  import org.objectledge.ComponentInitializationError;
39  import org.objectledge.filesystem.impl.ReadOnlyFileSystemProvider;
40  
41  /***
42   * An implementation of FileSystem provider that operates on the ServletContext.
43   * 
44   * <p>This is a read-only implementation. It is able to use WEB-INF/files for
45   * listing functionality. </p>
46   * 
47   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
48   * @version $Id: ServletFileSystemProvider.java,v 1.5 2005/08/03 10:22:23 pablo Exp $
49   */
50  public class ServletFileSystemProvider 
51  	extends ReadOnlyFileSystemProvider
52  {
53  	// instance variables /////////////////////////////////////////////////////////////////////////
54  
55      /*** the servlet context used for reading resources. */	
56  	private ServletContext context;
57  
58      // initialization /////////////////////////////////////////////////////////////////////////////
59      
60      /***
61       * Crates a new instance of the provider.
62       * 
63       * @param name the name of the provider.
64       * @param context the servlet context to read resources from.
65       */
66      public ServletFileSystemProvider(String name, ServletContext context)
67      {
68          super(name);
69          this.context = context;
70          try
71          {
72              processListings();
73          }
74          catch(IOException e)
75          {
76              throw new ComponentInitializationError("failed to parse listings", e);
77          }
78      }
79      
80      // public interface ///////////////////////////////////////////////////////////////////////////
81      
82      /***
83       * {@inheritDoc}
84       */
85      public InputStream getInputStream(String path) 
86      {
87  		return context.getResourceAsStream(FileSystem.normalizedPath(path));
88      }
89      
90      /***
91       * {@inheritDoc}
92       */
93      public URL getResource(String path)
94          throws MalformedURLException
95      {
96          if(path.charAt(0) != '/')
97          {
98              path = "/"+path;
99          }
100         if(context.getResourceAsStream(path) != null)
101         {
102             return context.getResource(path);
103         }
104         else
105         {
106             return null;
107         }
108     }
109 }