View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Blacksmith, Inc.
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library 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 GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.web;
20  
21  import java.util.Enumeration;
22  
23  import net.wotonomy.foundation.NSData;
24  import net.wotonomy.foundation.NSMutableDictionary;
25  
26  /***
27  * An implementation of WORequestHandler that  
28  * retrieves resources from the deployed application.
29  *
30  * @author michael@mpowers.net
31  * @author $Author: cgruber $
32  * @version $Revision: 905 $
33  */
34  public class WOResourceRequestHandler
35  	extends WORequestHandler
36  {
37      private NSMutableDictionary resourceCache;
38      private WOResourceManager resourceManager;
39      
40      public WOResourceRequestHandler()
41      {
42          //TODO: should probably have some kind of limit on the cache
43          resourceCache = new NSMutableDictionary();
44          resourceManager = WOApplication.application().resourceManager();
45      }
46      
47      public WOResponse handleRequest( WORequest aRequest )
48      {
49          WOResponse response = new WOResponse();
50          
51          StringBuffer buf = new StringBuffer();
52          
53          //TODO: this is just to get things working...
54          String framework = null;
55          Enumeration e = aRequest.requestHandlerPathArray().objectEnumerator();
56          if ( e.hasMoreElements() )
57          {
58              framework = e.nextElement().toString();
59              if ( framework.equals( "application" ) )
60              {
61                  buf.append('/').append( framework );
62                  framework = null;
63              }
64          }
65          if ( e.hasMoreElements() )
66          {
67              buf.append( e.nextElement() );
68          }
69          while ( e.hasMoreElements() )
70          {
71              buf.append('/').append( e.nextElement() );
72          }
73          
74          String resource;
75          if ( buf.length() > 0 )
76          {
77              resource = buf.toString();
78              byte[] data = resourceManager.bytesForResourceNamed(
79                  resource, framework, aRequest.browserLanguages() );
80              if ( data != null )
81              {
82                  response.setHeader(
83                      resourceManager.contentTypeForResourceNamed( resource ),
84                      "Content-Type" );
85                  response.setContent( new NSData( data ) );
86                  return response;
87              }
88          }
89          response.setStatus( 404 ); // not found
90          return response;
91      }
92  }
93  
94  /*
95   * $Log$
96   * Revision 1.2  2006/02/19 01:44:02  cgruber
97   * Add xmlrpc files
98   * Remove jclark and replace with dom4j and javax.xml.sax stuff
99   * Re-work dependencies and imports so it all compiles.
100  *
101  * Revision 1.1  2006/02/16 13:22:22  cgruber
102  * Check in all sources in eclipse-friendly maven-enabled packages.
103  *
104  * Revision 1.6  2003/08/07 00:15:15  chochos
105  * general cleanup (mostly removing unused imports)
106  *
107  * Revision 1.5  2003/01/27 15:08:00  mpowers
108  * Implemented WOResourceManager, using java resources for now.
109  *
110  * Revision 1.4  2003/01/22 23:01:36  mpowers
111  * Better handling for request handler path.
112  * Better support for resources with absolute path.
113  *
114  * Revision 1.3  2003/01/20 16:18:22  mpowers
115  * Fixed class loading issues with resource retrieval.
116  *
117  * Revision 1.2  2003/01/17 20:58:20  mpowers
118  * Fixed up WOHyperlink.
119  *
120  *
121  */
122