summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOResourceRequestHandler.java
blob: f6111496267597c6e822d168b56d5d49d5670b1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.web;

import java.util.Enumeration;

import net.wotonomy.foundation.NSData;
import net.wotonomy.foundation.NSMutableDictionary;

/**
 * An implementation of WORequestHandler that retrieves resources from the
 * deployed application.
 *
 * @author michael@mpowers.net
 * @author $Author: cgruber $
 * @version $Revision: 905 $
 */
public class WOResourceRequestHandler extends WORequestHandler {
	private NSMutableDictionary resourceCache;
	private WOResourceManager resourceManager;

	public WOResourceRequestHandler() {
		// TODO: should probably have some kind of limit on the cache
		resourceCache = new NSMutableDictionary();
		resourceManager = WOApplication.application().resourceManager();
	}

	public WOResponse handleRequest(WORequest aRequest) {
		WOResponse response = new WOResponse();

		StringBuffer buf = new StringBuffer();

		// TODO: this is just to get things working...
		String framework = null;
		Enumeration e = aRequest.requestHandlerPathArray().objectEnumerator();
		if (e.hasMoreElements()) {
			framework = e.nextElement().toString();
			if (framework.equals("application")) {
				buf.append('/').append(framework);
				framework = null;
			}
		}
		if (e.hasMoreElements()) {
			buf.append(e.nextElement());
		}
		while (e.hasMoreElements()) {
			buf.append('/').append(e.nextElement());
		}

		String resource;
		if (buf.length() > 0) {
			resource = buf.toString();
			byte[] data = resourceManager.bytesForResourceNamed(resource, framework, aRequest.browserLanguages());
			if (data != null) {
				response.setHeader(resourceManager.contentTypeForResourceNamed(resource), "Content-Type");
				response.setContent(new NSData(data));
				return response;
			}
		}
		response.setStatus(404); // not found
		return response;
	}
}

/*
 * $Log$ Revision 1.2 2006/02/19 01:44:02 cgruber Add xmlrpc files Remove jclark
 * and replace with dom4j and javax.xml.sax stuff Re-work dependencies and
 * imports so it all compiles.
 *
 * Revision 1.1 2006/02/16 13:22:22 cgruber Check in all sources in
 * eclipse-friendly maven-enabled packages.
 *
 * Revision 1.6 2003/08/07 00:15:15 chochos general cleanup (mostly removing
 * unused imports)
 *
 * Revision 1.5 2003/01/27 15:08:00 mpowers Implemented WOResourceManager, using
 * java resources for now.
 *
 * Revision 1.4 2003/01/22 23:01:36 mpowers Better handling for request handler
 * path. Better support for resources with absolute path.
 *
 * Revision 1.3 2003/01/20 16:18:22 mpowers Fixed class loading issues with
 * resource retrieval.
 *
 * Revision 1.2 2003/01/17 20:58:20 mpowers Fixed up WOHyperlink.
 *
 *
 */