2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include <NGObjWeb/WORequestHandler.h>
24 @interface WOResourceRequestHandler : WORequestHandler
27 #include <NGObjWeb/WOApplication.h>
28 #include <NGObjWeb/WORequest.h>
29 #include <NGObjWeb/WOResponse.h>
30 #include <NGObjWeb/WOResourceManager.h>
33 @interface WOResourceManager(PrivateKeyedAccess)
35 - (id)_dataForKey:(NSString *)_key sessionID:(NSString *)_sid;
39 @implementation WOResourceRequestHandler
41 static BOOL debugOn = NO;
44 return [super version] + 0 /* v2 */;
47 NSAssert2([super version] == 2,
48 @"invalid superclass (%@) version %i !",
49 NSStringFromClass([self superclass]), [super version]);
52 - (WOResponse *)_handleWebServerResourcesRequest:(WORequest *)_request {
54 NSArray *handlerPath = nil;
55 WOResponse *response = nil;
57 NSString *resourceName;
58 NSString *resourcePath;
61 if (debugOn) [self logWithFormat:@"handle WS request: %@", _request];
63 if (_request == nil) return nil;
65 *(&app) = [WOApplication application];
66 handlerPath = [_request requestHandlerPathArray];
68 /* check for WebServerResources requests ... */
70 if ([handlerPath count] < 1) {
71 if (debugOn) [self logWithFormat:@"path to short: '%@'", handlerPath];
74 if (debugOn) [self logWithFormat:@" check path: '%@'", handlerPath];
76 /* ok, found a resource request */
78 if ([handlerPath count] > 1) {
81 lang = [handlerPath objectAtIndex:0];
82 lang = [lang stringByDeletingPathExtension];
83 languages = [NSArray arrayWithObject:lang];
84 languages = [languages arrayByAddingObjectsFromArray:languages];
86 resourceName = [handlerPath objectAtIndex:1];
89 languages = [_request browserLanguages];
90 resourceName = [handlerPath objectAtIndex:0];
93 resourcePath = [[app resourceManager]
94 pathForResourceNamed:resourceName
97 if (debugOn) [self logWithFormat:@" resource path: '%@'", resourcePath];
99 data = (resourcePath != nil)
100 ? [NSData dataWithContentsOfFile:resourcePath]
104 response = [WOResponse responseWithRequest:_request];
105 [response setStatus:404]; /* not found */
106 [response setHeader:@"text/html" forKey:@"content-type"];
107 [response appendContentString:@"<h3>Resource not found</h3><pre>"];
108 [response appendContentHTMLString:@"Name: "];
109 [response appendContentHTMLString:resourceName];
110 [response appendContentHTMLString:@"\nLanguages: "];
111 [response appendContentHTMLString:[languages description]];
112 [response appendContentHTMLString:@"\nResourceManager: "];
113 [response appendContentHTMLString:[[app resourceManager] description]];
114 [response appendContentString:@"</pre>"];
118 //NSLog(@"shall deliver %@", resourcePath);
120 response = [WOResponse responseWithRequest:_request];
122 /* determine content-type */
125 NSString *pathExtension;
127 pathExtension = [resourcePath pathExtension];
128 ctype = @"application/octet-stream";
130 if ([pathExtension isEqualToString:@"html"])
131 ctype = @"text/html";
132 else if ([pathExtension isEqualToString:@"gif"])
133 ctype = @"image/gif";
135 [response setHeader:ctype forKey:@"content-type"];
138 [response setContent:data];
143 - (WOResponse *)handleRequest:(WORequest *)_request {
144 NSArray *handlerPath = nil;
146 if (debugOn) [self logWithFormat:@"handle request: %@", _request];
148 if ([[_request requestHandlerKey] isEqualToString:@"WebServerResources"])
149 return [self _handleWebServerResourcesRequest:_request];
151 handlerPath = [_request requestHandlerPathArray];
153 if ([handlerPath count] > 0) {
156 rmkey = [handlerPath objectAtIndex:0];
157 if ([rmkey length] > 0) {
158 WOResourceManager *rm;
161 rm = [[WOApplication application] resourceManager];
163 if ((data = [rm _dataForKey:rmkey sessionID:nil])) {
164 WOResponse *response;
166 response = [WOResponse responseWithRequest:_request];
167 [response setHeader:[data objectForKey:@"mimeType"]
168 forKey:@"content-type"];
169 [response setContent:[data objectForKey:@"data"]];
173 [[WOApplication application]
174 logWithFormat:@"WOResourceRequestHandler: "
175 @"didn't find data for resource key '%@'",
181 /* if everything fails, try locating resource in WebServerResources */
182 return [self _handleWebServerResourcesRequest:_request];
187 - (NSString *)loggingPrefix {
188 return @"[resource-handler]";
191 @end /* WOResourceRequestHandler */